Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/frontends/clover/core/device.cpp
4572 views
1
//
2
// Copyright 2012 Francisco Jerez
3
//
4
// Permission is hereby granted, free of charge, to any person obtaining a
5
// copy of this software and associated documentation files (the "Software"),
6
// to deal in the Software without restriction, including without limitation
7
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
// and/or sell copies of the Software, and to permit persons to whom the
9
// Software is furnished to do so, subject to the following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included in
12
// all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20
// OTHER DEALINGS IN THE SOFTWARE.
21
//
22
23
#include <algorithm>
24
#include "core/device.hpp"
25
#include "core/platform.hpp"
26
#include "pipe/p_screen.h"
27
#include "pipe/p_state.h"
28
#include "spirv/invocation.hpp"
29
#include "util/bitscan.h"
30
#include "util/u_debug.h"
31
#include "spirv/invocation.hpp"
32
#include "nir/invocation.hpp"
33
#include <fstream>
34
35
using namespace clover;
36
37
namespace {
38
template<typename T>
39
std::vector<T>
40
get_compute_param(pipe_screen *pipe, pipe_shader_ir ir_format,
41
pipe_compute_cap cap) {
42
int sz = pipe->get_compute_param(pipe, ir_format, cap, NULL);
43
std::vector<T> v(sz / sizeof(T));
44
45
pipe->get_compute_param(pipe, ir_format, cap, &v.front());
46
return v;
47
}
48
}
49
50
device::device(clover::platform &platform, pipe_loader_device *ldev) :
51
platform(platform), clc_cache(NULL), ldev(ldev) {
52
unsigned major = 1, minor = 1;
53
debug_get_version_option("CLOVER_DEVICE_VERSION_OVERRIDE", &major, &minor);
54
version = CL_MAKE_VERSION(major, minor, 0);
55
56
major = 1, minor = 1;
57
debug_get_version_option("CLOVER_DEVICE_CLC_VERSION_OVERRIDE", &major, &minor);
58
clc_version = CL_MAKE_VERSION(major, minor, 0);
59
60
pipe = pipe_loader_create_screen(ldev);
61
if (pipe && pipe->get_param(pipe, PIPE_CAP_COMPUTE)) {
62
if (supports_ir(PIPE_SHADER_IR_NATIVE))
63
return;
64
#ifdef HAVE_CLOVER_SPIRV
65
if (supports_ir(PIPE_SHADER_IR_NIR_SERIALIZED)) {
66
nir::check_for_libclc(*this);
67
clc_cache = nir::create_clc_disk_cache();
68
clc_nir = lazy<std::shared_ptr<nir_shader>>([&] () { std::string log; return std::shared_ptr<nir_shader>(nir::load_libclc_nir(*this, log), ralloc_free); });
69
return;
70
}
71
#endif
72
}
73
if (pipe)
74
pipe->destroy(pipe);
75
throw error(CL_INVALID_DEVICE);
76
}
77
78
device::~device() {
79
if (clc_cache)
80
disk_cache_destroy(clc_cache);
81
if (pipe)
82
pipe->destroy(pipe);
83
if (ldev)
84
pipe_loader_release(&ldev, 1);
85
}
86
87
bool
88
device::operator==(const device &dev) const {
89
return this == &dev;
90
}
91
92
cl_device_type
93
device::type() const {
94
switch (ldev->type) {
95
case PIPE_LOADER_DEVICE_SOFTWARE:
96
return CL_DEVICE_TYPE_CPU;
97
case PIPE_LOADER_DEVICE_PCI:
98
case PIPE_LOADER_DEVICE_PLATFORM:
99
return CL_DEVICE_TYPE_GPU;
100
default:
101
unreachable("Unknown device type.");
102
}
103
}
104
105
cl_uint
106
device::vendor_id() const {
107
switch (ldev->type) {
108
case PIPE_LOADER_DEVICE_SOFTWARE:
109
case PIPE_LOADER_DEVICE_PLATFORM:
110
return 0;
111
case PIPE_LOADER_DEVICE_PCI:
112
return ldev->u.pci.vendor_id;
113
default:
114
unreachable("Unknown device type.");
115
}
116
}
117
118
size_t
119
device::max_images_read() const {
120
return PIPE_MAX_SHADER_SAMPLER_VIEWS;
121
}
122
123
size_t
124
device::max_images_write() const {
125
return PIPE_MAX_SHADER_IMAGES;
126
}
127
128
size_t
129
device::max_image_buffer_size() const {
130
return pipe->get_param(pipe, PIPE_CAP_MAX_TEXTURE_BUFFER_SIZE);
131
}
132
133
cl_uint
134
device::max_image_size() const {
135
return pipe->get_param(pipe, PIPE_CAP_MAX_TEXTURE_2D_SIZE);
136
}
137
138
cl_uint
139
device::max_image_size_3d() const {
140
return 1 << (pipe->get_param(pipe, PIPE_CAP_MAX_TEXTURE_3D_LEVELS) - 1);
141
}
142
143
size_t
144
device::max_image_array_number() const {
145
return pipe->get_param(pipe, PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS);
146
}
147
148
cl_uint
149
device::max_samplers() const {
150
return pipe->get_shader_param(pipe, PIPE_SHADER_COMPUTE,
151
PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS);
152
}
153
154
cl_ulong
155
device::max_mem_global() const {
156
return get_compute_param<uint64_t>(pipe, ir_format(),
157
PIPE_COMPUTE_CAP_MAX_GLOBAL_SIZE)[0];
158
}
159
160
cl_ulong
161
device::max_mem_local() const {
162
return get_compute_param<uint64_t>(pipe, ir_format(),
163
PIPE_COMPUTE_CAP_MAX_LOCAL_SIZE)[0];
164
}
165
166
cl_ulong
167
device::max_mem_input() const {
168
return get_compute_param<uint64_t>(pipe, ir_format(),
169
PIPE_COMPUTE_CAP_MAX_INPUT_SIZE)[0];
170
}
171
172
cl_ulong
173
device::max_const_buffer_size() const {
174
return pipe->get_shader_param(pipe, PIPE_SHADER_COMPUTE,
175
PIPE_SHADER_CAP_MAX_CONST_BUFFER_SIZE);
176
}
177
178
cl_uint
179
device::max_const_buffers() const {
180
return pipe->get_shader_param(pipe, PIPE_SHADER_COMPUTE,
181
PIPE_SHADER_CAP_MAX_CONST_BUFFERS);
182
}
183
184
size_t
185
device::max_threads_per_block() const {
186
return get_compute_param<uint64_t>(
187
pipe, ir_format(), PIPE_COMPUTE_CAP_MAX_THREADS_PER_BLOCK)[0];
188
}
189
190
cl_ulong
191
device::max_mem_alloc_size() const {
192
return get_compute_param<uint64_t>(pipe, ir_format(),
193
PIPE_COMPUTE_CAP_MAX_MEM_ALLOC_SIZE)[0];
194
}
195
196
cl_uint
197
device::max_clock_frequency() const {
198
return get_compute_param<uint32_t>(pipe, ir_format(),
199
PIPE_COMPUTE_CAP_MAX_CLOCK_FREQUENCY)[0];
200
}
201
202
cl_uint
203
device::max_compute_units() const {
204
return get_compute_param<uint32_t>(pipe, ir_format(),
205
PIPE_COMPUTE_CAP_MAX_COMPUTE_UNITS)[0];
206
}
207
208
cl_uint
209
device::max_printf_buffer_size() const {
210
return 1024 * 1024;
211
}
212
213
bool
214
device::image_support() const {
215
return get_compute_param<uint32_t>(pipe, ir_format(),
216
PIPE_COMPUTE_CAP_IMAGES_SUPPORTED)[0];
217
}
218
219
bool
220
device::has_doubles() const {
221
return pipe->get_param(pipe, PIPE_CAP_DOUBLES);
222
}
223
224
bool
225
device::has_halves() const {
226
return pipe->get_shader_param(pipe, PIPE_SHADER_COMPUTE,
227
PIPE_SHADER_CAP_FP16);
228
}
229
230
bool
231
device::has_int64_atomics() const {
232
return pipe->get_shader_param(pipe, PIPE_SHADER_COMPUTE,
233
PIPE_SHADER_CAP_INT64_ATOMICS);
234
}
235
236
bool
237
device::has_unified_memory() const {
238
return pipe->get_param(pipe, PIPE_CAP_UMA);
239
}
240
241
size_t
242
device::mem_base_addr_align() const {
243
uint64_t page_size = 0;
244
os_get_page_size(&page_size);
245
return std::max((size_t)page_size, sizeof(cl_long) * 16);
246
}
247
248
cl_device_svm_capabilities
249
device::svm_support() const {
250
// Without CAP_RESOURCE_FROM_USER_MEMORY SVM and CL_MEM_USE_HOST_PTR
251
// interactions won't work according to spec as clover manages a GPU side
252
// copy of the host data.
253
//
254
// The biggest problem are memory buffers created with CL_MEM_USE_HOST_PTR,
255
// but the application and/or the kernel updates the memory via SVM and not
256
// the cl_mem buffer.
257
// We can't even do proper tracking on what memory might have been accessed
258
// as the host ptr to the buffer could be within a SVM region, where through
259
// the CL API there is no reliable way of knowing if a certain cl_mem buffer
260
// was accessed by a kernel or not and the runtime can't reliably know from
261
// which side the GPU buffer content needs to be updated.
262
//
263
// Another unsolvable scenario is a cl_mem object passed by cl_mem reference
264
// and SVM pointer into the same kernel at the same time.
265
if (allows_user_pointers() && pipe->get_param(pipe, PIPE_CAP_SYSTEM_SVM))
266
// we can emulate all lower levels if we support fine grain system
267
return CL_DEVICE_SVM_FINE_GRAIN_SYSTEM |
268
CL_DEVICE_SVM_COARSE_GRAIN_BUFFER |
269
CL_DEVICE_SVM_FINE_GRAIN_BUFFER;
270
return 0;
271
}
272
273
bool
274
device::allows_user_pointers() const {
275
return pipe->get_param(pipe, PIPE_CAP_RESOURCE_FROM_USER_MEMORY) ||
276
pipe->get_param(pipe, PIPE_CAP_RESOURCE_FROM_USER_MEMORY_COMPUTE_ONLY);
277
}
278
279
std::vector<size_t>
280
device::max_block_size() const {
281
auto v = get_compute_param<uint64_t>(pipe, ir_format(),
282
PIPE_COMPUTE_CAP_MAX_BLOCK_SIZE);
283
return { v.begin(), v.end() };
284
}
285
286
cl_uint
287
device::subgroup_size() const {
288
return get_compute_param<uint32_t>(pipe, ir_format(),
289
PIPE_COMPUTE_CAP_SUBGROUP_SIZE)[0];
290
}
291
292
cl_uint
293
device::address_bits() const {
294
return get_compute_param<uint32_t>(pipe, ir_format(),
295
PIPE_COMPUTE_CAP_ADDRESS_BITS)[0];
296
}
297
298
std::string
299
device::device_name() const {
300
return pipe->get_name(pipe);
301
}
302
303
std::string
304
device::vendor_name() const {
305
return pipe->get_device_vendor(pipe);
306
}
307
308
enum pipe_shader_ir
309
device::ir_format() const {
310
if (supports_ir(PIPE_SHADER_IR_NATIVE))
311
return PIPE_SHADER_IR_NATIVE;
312
313
assert(supports_ir(PIPE_SHADER_IR_NIR_SERIALIZED));
314
return PIPE_SHADER_IR_NIR_SERIALIZED;
315
}
316
317
std::string
318
device::ir_target() const {
319
std::vector<char> target = get_compute_param<char>(
320
pipe, ir_format(), PIPE_COMPUTE_CAP_IR_TARGET);
321
return { target.data() };
322
}
323
324
enum pipe_endian
325
device::endianness() const {
326
return (enum pipe_endian)pipe->get_param(pipe, PIPE_CAP_ENDIANNESS);
327
}
328
329
std::string
330
device::device_version_as_string() const {
331
static const std::string version_string =
332
std::to_string(CL_VERSION_MAJOR(version)) + "." +
333
std::to_string(CL_VERSION_MINOR(version));
334
return version_string;
335
}
336
337
std::string
338
device::device_clc_version_as_string() const {
339
static const std::string version_string =
340
std::to_string(CL_VERSION_MAJOR(clc_version)) + "." +
341
std::to_string(CL_VERSION_MINOR(clc_version));
342
return version_string;
343
}
344
345
bool
346
device::supports_ir(enum pipe_shader_ir ir) const {
347
return pipe->get_shader_param(pipe, PIPE_SHADER_COMPUTE,
348
PIPE_SHADER_CAP_SUPPORTED_IRS) & (1 << ir);
349
}
350
351
std::vector<cl_name_version>
352
device::supported_extensions() const {
353
std::vector<cl_name_version> vec;
354
355
vec.push_back( cl_name_version{ CL_MAKE_VERSION(1, 0, 0), "cl_khr_byte_addressable_store" } );
356
vec.push_back( cl_name_version{ CL_MAKE_VERSION(1, 0, 0), "cl_khr_global_int32_base_atomics" } );
357
vec.push_back( cl_name_version{ CL_MAKE_VERSION(1, 0, 0), "cl_khr_global_int32_extended_atomics" } );
358
vec.push_back( cl_name_version{ CL_MAKE_VERSION(1, 0, 0), "cl_khr_local_int32_base_atomics" } );
359
vec.push_back( cl_name_version{ CL_MAKE_VERSION(1, 0, 0), "cl_khr_local_int32_extended_atomics" } );
360
if (has_int64_atomics()) {
361
vec.push_back( cl_name_version{ CL_MAKE_VERSION(1, 0, 0), "cl_khr_int64_base_atomics" } );
362
vec.push_back( cl_name_version{ CL_MAKE_VERSION(1, 0, 0), "cl_khr_int64_extended_atomics" } );
363
}
364
if (has_doubles())
365
vec.push_back( cl_name_version{ CL_MAKE_VERSION(1, 0, 0), "cl_khr_fp64" } );
366
if (has_halves())
367
vec.push_back( cl_name_version{ CL_MAKE_VERSION(1, 0, 0), "cl_khr_fp16" } );
368
if (svm_support())
369
vec.push_back( cl_name_version{ CL_MAKE_VERSION(1, 0, 0), "cl_arm_shared_virtual_memory" } );
370
if (!clover::spirv::supported_versions().empty() &&
371
supports_ir(PIPE_SHADER_IR_NIR_SERIALIZED))
372
vec.push_back( cl_name_version{ CL_MAKE_VERSION(1, 0, 0), "cl_khr_il_program" } );
373
vec.push_back( cl_name_version{ CL_MAKE_VERSION(1, 0, 0), "cl_khr_extended_versioning" } );
374
return vec;
375
}
376
377
std::string
378
device::supported_extensions_as_string() const {
379
static std::string extensions_string;
380
381
if (!extensions_string.empty())
382
return extensions_string;
383
384
const auto extension_list = supported_extensions();
385
for (const auto &extension : extension_list) {
386
if (!extensions_string.empty())
387
extensions_string += " ";
388
extensions_string += extension.name;
389
}
390
return extensions_string;
391
}
392
393
std::vector<cl_name_version>
394
device::supported_il_versions() const {
395
return clover::spirv::supported_versions();
396
}
397
398
const void *
399
device::get_compiler_options(enum pipe_shader_ir ir) const {
400
return pipe->get_compiler_options(pipe, ir, PIPE_SHADER_COMPUTE);
401
}
402
403
cl_version
404
device::device_version() const {
405
return version;
406
}
407
408
cl_version
409
device::device_clc_version() const {
410
return clc_version;
411
}
412
413
std::vector<cl_name_version>
414
device::opencl_c_all_versions() const {
415
std::vector<cl_name_version> vec;
416
vec.push_back( cl_name_version{ CL_MAKE_VERSION(1, 0, 0), "OpenCL C" } );
417
vec.push_back( cl_name_version{ CL_MAKE_VERSION(1, 1, 0), "OpenCL C" } );
418
419
if (CL_VERSION_MAJOR(clc_version) == 1 &&
420
CL_VERSION_MINOR(clc_version) == 2)
421
vec.push_back( cl_name_version{ CL_MAKE_VERSION(1, 2, 0), "OpenCL C" } );
422
if (CL_VERSION_MAJOR(clc_version) == 3) {
423
vec.push_back( cl_name_version{ CL_MAKE_VERSION(1, 2, 0), "OpenCL C" } );
424
vec.push_back( cl_name_version{ CL_MAKE_VERSION(3, 0, 0), "OpenCL C" } );
425
}
426
return vec;
427
}
428
429
std::vector<cl_name_version>
430
device::opencl_c_features() const {
431
std::vector<cl_name_version> vec;
432
433
vec.push_back( cl_name_version {CL_MAKE_VERSION(3, 0, 0), "__opencl_c_int64" });
434
if (has_doubles())
435
vec.push_back( cl_name_version {CL_MAKE_VERSION(3, 0, 0), "__opencl_c_fp64" });
436
437
return vec;
438
}
439
440