Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/frontends/clover/api/program.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 "api/util.hpp"
24
#include "core/program.hpp"
25
#include "spirv/invocation.hpp"
26
#include "util/u_debug.h"
27
28
#include <limits>
29
#include <sstream>
30
31
using namespace clover;
32
33
namespace {
34
35
std::string
36
build_options(const char *p_opts, const char *p_debug) {
37
auto opts = std::string(p_opts ? p_opts : "");
38
std::string extra_opts = debug_get_option(p_debug, "");
39
40
return detokenize(std::vector<std::string>{opts, extra_opts}, " ");
41
}
42
43
class build_notifier {
44
public:
45
build_notifier(cl_program prog,
46
void (*notifer)(cl_program, void *), void *data) :
47
prog_(prog), notifer(notifer), data_(data) { }
48
49
~build_notifier() {
50
if (notifer)
51
notifer(prog_, data_);
52
}
53
54
private:
55
cl_program prog_;
56
void (*notifer)(cl_program, void *);
57
void *data_;
58
};
59
60
void
61
validate_build_common(const program &prog, cl_uint num_devs,
62
const cl_device_id *d_devs,
63
void (*pfn_notify)(cl_program, void *),
64
void *user_data) {
65
if (!pfn_notify && user_data)
66
throw error(CL_INVALID_VALUE);
67
68
if (prog.kernel_ref_count())
69
throw error(CL_INVALID_OPERATION);
70
71
if (any_of([&](const device &dev) {
72
return !count(dev, prog.devices());
73
}, objs<allow_empty_tag>(d_devs, num_devs)))
74
throw error(CL_INVALID_DEVICE);
75
}
76
77
enum program::il_type
78
identify_and_validate_il(const std::string &il,
79
const cl_version opencl_version,
80
const context::notify_action &notify) {
81
82
enum program::il_type il_type = program::il_type::none;
83
84
#ifdef HAVE_CLOVER_SPIRV
85
if (spirv::is_binary_spirv(il)) {
86
std::string log;
87
if (!spirv::is_valid_spirv(il, opencl_version, log)) {
88
if (notify) {
89
notify(log.c_str());
90
}
91
throw error(CL_INVALID_VALUE);
92
}
93
il_type = program::il_type::spirv;
94
}
95
#endif
96
97
return il_type;
98
}
99
}
100
101
CLOVER_API cl_program
102
clCreateProgramWithSource(cl_context d_ctx, cl_uint count,
103
const char **strings, const size_t *lengths,
104
cl_int *r_errcode) try {
105
auto &ctx = obj(d_ctx);
106
std::string source;
107
108
if (!count || !strings ||
109
any_of(is_zero(), range(strings, count)))
110
throw error(CL_INVALID_VALUE);
111
112
// Concatenate all the provided fragments together
113
for (unsigned i = 0; i < count; ++i)
114
source += (lengths && lengths[i] ?
115
std::string(strings[i], strings[i] + lengths[i]) :
116
std::string(strings[i]));
117
118
// ...and create a program object for them.
119
ret_error(r_errcode, CL_SUCCESS);
120
return new program(ctx, std::move(source), program::il_type::source);
121
122
} catch (error &e) {
123
ret_error(r_errcode, e);
124
return NULL;
125
}
126
127
CLOVER_API cl_program
128
clCreateProgramWithBinary(cl_context d_ctx, cl_uint n,
129
const cl_device_id *d_devs,
130
const size_t *lengths,
131
const unsigned char **binaries,
132
cl_int *r_status, cl_int *r_errcode) try {
133
auto &ctx = obj(d_ctx);
134
auto devs = objs(d_devs, n);
135
136
if (!lengths || !binaries)
137
throw error(CL_INVALID_VALUE);
138
139
if (any_of([&](const device &dev) {
140
return !count(dev, ctx.devices());
141
}, devs))
142
throw error(CL_INVALID_DEVICE);
143
144
// Deserialize the provided binaries,
145
std::vector<std::pair<cl_int, module>> result = map(
146
[](const unsigned char *p, size_t l) -> std::pair<cl_int, module> {
147
if (!p || !l)
148
return { CL_INVALID_VALUE, {} };
149
150
try {
151
std::stringbuf bin( std::string{ (char*)p, l } );
152
std::istream s(&bin);
153
154
return { CL_SUCCESS, module::deserialize(s) };
155
156
} catch (std::istream::failure &e) {
157
return { CL_INVALID_BINARY, {} };
158
}
159
},
160
range(binaries, n),
161
range(lengths, n));
162
163
// update the status array,
164
if (r_status)
165
copy(map(keys(), result), r_status);
166
167
if (any_of(key_equals(CL_INVALID_VALUE), result))
168
throw error(CL_INVALID_VALUE);
169
170
if (any_of(key_equals(CL_INVALID_BINARY), result))
171
throw error(CL_INVALID_BINARY);
172
173
// initialize a program object with them.
174
ret_error(r_errcode, CL_SUCCESS);
175
return new program(ctx, devs, map(values(), result));
176
177
} catch (error &e) {
178
ret_error(r_errcode, e);
179
return NULL;
180
}
181
182
cl_program
183
clover::CreateProgramWithILKHR(cl_context d_ctx, const void *il,
184
size_t length, cl_int *r_errcode) try {
185
auto &ctx = obj(d_ctx);
186
187
if (!il || !length)
188
throw error(CL_INVALID_VALUE);
189
190
// Compute the highest OpenCL version supported by all devices associated to
191
// the context. That is the version used for validating the SPIR-V binary.
192
cl_version min_opencl_version = std::numeric_limits<uint32_t>::max();
193
for (const device &dev : ctx.devices()) {
194
const cl_version opencl_version = dev.device_version();
195
min_opencl_version = std::min(opencl_version, min_opencl_version);
196
}
197
198
const char *stream = reinterpret_cast<const char *>(il);
199
std::string binary(stream, stream + length);
200
const enum program::il_type il_type = identify_and_validate_il(binary,
201
min_opencl_version,
202
ctx.notify);
203
204
if (il_type == program::il_type::none)
205
throw error(CL_INVALID_VALUE);
206
207
// Initialize a program object with it.
208
ret_error(r_errcode, CL_SUCCESS);
209
return new program(ctx, std::move(binary), il_type);
210
211
} catch (error &e) {
212
ret_error(r_errcode, e);
213
return NULL;
214
}
215
216
CLOVER_API cl_program
217
clCreateProgramWithIL(cl_context d_ctx,
218
const void *il,
219
size_t length,
220
cl_int *r_errcode) {
221
return CreateProgramWithILKHR(d_ctx, il, length, r_errcode);
222
}
223
224
CLOVER_API cl_program
225
clCreateProgramWithBuiltInKernels(cl_context d_ctx, cl_uint n,
226
const cl_device_id *d_devs,
227
const char *kernel_names,
228
cl_int *r_errcode) try {
229
auto &ctx = obj(d_ctx);
230
auto devs = objs(d_devs, n);
231
232
if (any_of([&](const device &dev) {
233
return !count(dev, ctx.devices());
234
}, devs))
235
throw error(CL_INVALID_DEVICE);
236
237
// No currently supported built-in kernels.
238
throw error(CL_INVALID_VALUE);
239
240
} catch (error &e) {
241
ret_error(r_errcode, e);
242
return NULL;
243
}
244
245
246
CLOVER_API cl_int
247
clRetainProgram(cl_program d_prog) try {
248
obj(d_prog).retain();
249
return CL_SUCCESS;
250
251
} catch (error &e) {
252
return e.get();
253
}
254
255
CLOVER_API cl_int
256
clReleaseProgram(cl_program d_prog) try {
257
if (obj(d_prog).release())
258
delete pobj(d_prog);
259
260
return CL_SUCCESS;
261
262
} catch (error &e) {
263
return e.get();
264
}
265
266
CLOVER_API cl_int
267
clBuildProgram(cl_program d_prog, cl_uint num_devs,
268
const cl_device_id *d_devs, const char *p_opts,
269
void (*pfn_notify)(cl_program, void *),
270
void *user_data) try {
271
auto &prog = obj(d_prog);
272
auto devs =
273
(d_devs ? objs(d_devs, num_devs) : ref_vector<device>(prog.devices()));
274
const auto opts = build_options(p_opts, "CLOVER_EXTRA_BUILD_OPTIONS");
275
276
validate_build_common(prog, num_devs, d_devs, pfn_notify, user_data);
277
278
auto notifier = build_notifier(d_prog, pfn_notify, user_data);
279
280
if (prog.il_type() != program::il_type::none) {
281
prog.compile(devs, opts);
282
prog.link(devs, opts, { prog });
283
} else if (any_of([&](const device &dev){
284
return prog.build(dev).binary_type() != CL_PROGRAM_BINARY_TYPE_EXECUTABLE;
285
}, devs)) {
286
// According to the OpenCL 1.2 specification, “if program is created
287
// with clCreateProgramWithBinary, then the program binary must be an
288
// executable binary (not a compiled binary or library).”
289
throw error(CL_INVALID_BINARY);
290
}
291
292
return CL_SUCCESS;
293
294
} catch (error &e) {
295
return e.get();
296
}
297
298
CLOVER_API cl_int
299
clCompileProgram(cl_program d_prog, cl_uint num_devs,
300
const cl_device_id *d_devs, const char *p_opts,
301
cl_uint num_headers, const cl_program *d_header_progs,
302
const char **header_names,
303
void (*pfn_notify)(cl_program, void *),
304
void *user_data) try {
305
auto &prog = obj(d_prog);
306
auto devs =
307
(d_devs ? objs(d_devs, num_devs) : ref_vector<device>(prog.devices()));
308
const auto opts = build_options(p_opts, "CLOVER_EXTRA_COMPILE_OPTIONS");
309
header_map headers;
310
311
validate_build_common(prog, num_devs, d_devs, pfn_notify, user_data);
312
313
auto notifier = build_notifier(d_prog, pfn_notify, user_data);
314
315
if (bool(num_headers) != bool(header_names))
316
throw error(CL_INVALID_VALUE);
317
318
if (prog.il_type() == program::il_type::none)
319
throw error(CL_INVALID_OPERATION);
320
321
for_each([&](const char *name, const program &header) {
322
if (header.il_type() == program::il_type::none)
323
throw error(CL_INVALID_OPERATION);
324
325
if (!any_of(key_equals(name), headers))
326
headers.push_back(std::pair<std::string, std::string>(
327
name, header.source()));
328
},
329
range(header_names, num_headers),
330
objs<allow_empty_tag>(d_header_progs, num_headers));
331
332
prog.compile(devs, opts, headers);
333
return CL_SUCCESS;
334
335
} catch (invalid_build_options_error &e) {
336
return CL_INVALID_COMPILER_OPTIONS;
337
338
} catch (build_error &e) {
339
return CL_COMPILE_PROGRAM_FAILURE;
340
341
} catch (error &e) {
342
return e.get();
343
}
344
345
namespace {
346
ref_vector<device>
347
validate_link_devices(const ref_vector<program> &progs,
348
const ref_vector<device> &all_devs,
349
const std::string &opts) {
350
std::vector<device *> devs;
351
const bool create_library =
352
opts.find("-create-library") != std::string::npos;
353
const bool enable_link_options =
354
opts.find("-enable-link-options") != std::string::npos;
355
const bool has_link_options =
356
opts.find("-cl-denorms-are-zero") != std::string::npos ||
357
opts.find("-cl-no-signed-zeroes") != std::string::npos ||
358
opts.find("-cl-unsafe-math-optimizations") != std::string::npos ||
359
opts.find("-cl-finite-math-only") != std::string::npos ||
360
opts.find("-cl-fast-relaxed-math") != std::string::npos ||
361
opts.find("-cl-no-subgroup-ifp") != std::string::npos;
362
363
// According to the OpenCL 1.2 specification, "[the
364
// -enable-link-options] option must be specified with the
365
// create-library option".
366
if (enable_link_options && !create_library)
367
throw error(CL_INVALID_LINKER_OPTIONS);
368
369
// According to the OpenCL 1.2 specification, "the
370
// [program linking options] can be specified when linking a program
371
// executable".
372
if (has_link_options && create_library)
373
throw error(CL_INVALID_LINKER_OPTIONS);
374
375
for (auto &dev : all_devs) {
376
const auto has_binary = [&](const program &prog) {
377
const auto t = prog.build(dev).binary_type();
378
return t == CL_PROGRAM_BINARY_TYPE_COMPILED_OBJECT ||
379
t == CL_PROGRAM_BINARY_TYPE_LIBRARY;
380
};
381
382
// According to the OpenCL 1.2 specification, a library is made of
383
// “compiled binaries specified in input_programs argument to
384
// clLinkProgram“; compiled binaries does not refer to libraries:
385
// “input_programs is an array of program objects that are compiled
386
// binaries or libraries that are to be linked to create the program
387
// executable”.
388
if (create_library && any_of([&](const program &prog) {
389
const auto t = prog.build(dev).binary_type();
390
return t != CL_PROGRAM_BINARY_TYPE_COMPILED_OBJECT;
391
}, progs))
392
throw error(CL_INVALID_OPERATION);
393
394
// According to the CL 1.2 spec, when "all programs specified [..]
395
// contain a compiled binary or library for the device [..] a link is
396
// performed",
397
else if (all_of(has_binary, progs))
398
devs.push_back(&dev);
399
400
// otherwise if "none of the programs contain a compiled binary or
401
// library for that device [..] no link is performed. All other
402
// cases will return a CL_INVALID_OPERATION error."
403
else if (any_of(has_binary, progs))
404
throw error(CL_INVALID_OPERATION);
405
406
// According to the OpenCL 1.2 specification, "[t]he linker may apply
407
// [program linking options] to all compiled program objects
408
// specified to clLinkProgram. The linker may apply these options
409
// only to libraries which were created with the
410
// -enable-link-option."
411
else if (has_link_options && any_of([&](const program &prog) {
412
const auto t = prog.build(dev).binary_type();
413
return !(t == CL_PROGRAM_BINARY_TYPE_COMPILED_OBJECT ||
414
(t == CL_PROGRAM_BINARY_TYPE_LIBRARY &&
415
prog.build(dev).opts.find("-enable-link-options") !=
416
std::string::npos));
417
}, progs))
418
throw error(CL_INVALID_LINKER_OPTIONS);
419
}
420
421
return map(derefs(), devs);
422
}
423
}
424
425
CLOVER_API cl_program
426
clLinkProgram(cl_context d_ctx, cl_uint num_devs, const cl_device_id *d_devs,
427
const char *p_opts, cl_uint num_progs, const cl_program *d_progs,
428
void (*pfn_notify) (cl_program, void *), void *user_data,
429
cl_int *r_errcode) try {
430
auto &ctx = obj(d_ctx);
431
const auto opts = build_options(p_opts, "CLOVER_EXTRA_LINK_OPTIONS");
432
auto progs = objs(d_progs, num_progs);
433
auto all_devs =
434
(d_devs ? objs(d_devs, num_devs) : ref_vector<device>(ctx.devices()));
435
auto prog = create<program>(ctx, all_devs);
436
auto r_prog = ret_object(prog);
437
438
auto notifier = build_notifier(r_prog, pfn_notify, user_data);
439
440
auto devs = validate_link_devices(progs, all_devs, opts);
441
442
validate_build_common(prog, num_devs, d_devs, pfn_notify, user_data);
443
444
try {
445
prog().link(devs, opts, progs);
446
ret_error(r_errcode, CL_SUCCESS);
447
448
} catch (build_error &e) {
449
ret_error(r_errcode, CL_LINK_PROGRAM_FAILURE);
450
}
451
452
return r_prog;
453
454
} catch (invalid_build_options_error &e) {
455
ret_error(r_errcode, CL_INVALID_LINKER_OPTIONS);
456
return NULL;
457
458
} catch (error &e) {
459
ret_error(r_errcode, e);
460
return NULL;
461
}
462
463
CLOVER_API cl_int
464
clUnloadCompiler() {
465
return CL_SUCCESS;
466
}
467
468
CLOVER_API cl_int
469
clUnloadPlatformCompiler(cl_platform_id d_platform) {
470
return CL_SUCCESS;
471
}
472
473
CLOVER_API cl_int
474
clGetProgramInfo(cl_program d_prog, cl_program_info param,
475
size_t size, void *r_buf, size_t *r_size) try {
476
property_buffer buf { r_buf, size, r_size };
477
auto &prog = obj(d_prog);
478
479
switch (param) {
480
case CL_PROGRAM_REFERENCE_COUNT:
481
buf.as_scalar<cl_uint>() = prog.ref_count();
482
break;
483
484
case CL_PROGRAM_CONTEXT:
485
buf.as_scalar<cl_context>() = desc(prog.context());
486
break;
487
488
case CL_PROGRAM_NUM_DEVICES:
489
buf.as_scalar<cl_uint>() = (prog.devices().size() ?
490
prog.devices().size() :
491
prog.context().devices().size());
492
break;
493
494
case CL_PROGRAM_DEVICES:
495
buf.as_vector<cl_device_id>() = (prog.devices().size() ?
496
descs(prog.devices()) :
497
descs(prog.context().devices()));
498
break;
499
500
case CL_PROGRAM_SOURCE:
501
buf.as_string() = prog.source();
502
break;
503
504
case CL_PROGRAM_BINARY_SIZES:
505
buf.as_vector<size_t>() = map([&](const device &dev) {
506
return prog.build(dev).binary.size();
507
},
508
prog.devices());
509
break;
510
511
case CL_PROGRAM_BINARIES:
512
buf.as_matrix<unsigned char>() = map([&](const device &dev) {
513
std::stringbuf bin;
514
std::ostream s(&bin);
515
prog.build(dev).binary.serialize(s);
516
return bin.str();
517
},
518
prog.devices());
519
break;
520
521
case CL_PROGRAM_NUM_KERNELS:
522
buf.as_scalar<cl_uint>() = prog.symbols().size();
523
break;
524
525
case CL_PROGRAM_KERNEL_NAMES:
526
buf.as_string() = fold([](const std::string &a, const module::symbol &s) {
527
return ((a.empty() ? "" : a + ";") + s.name);
528
}, std::string(), prog.symbols());
529
break;
530
531
case CL_PROGRAM_SCOPE_GLOBAL_CTORS_PRESENT:
532
case CL_PROGRAM_SCOPE_GLOBAL_DTORS_PRESENT:
533
buf.as_scalar<cl_bool>() = CL_FALSE;
534
break;
535
536
case CL_PROGRAM_IL:
537
if (prog.il_type() != program::il_type::none)
538
buf.as_string() = prog.source();
539
else if (r_size)
540
*r_size = 0u;
541
break;
542
default:
543
throw error(CL_INVALID_VALUE);
544
}
545
546
return CL_SUCCESS;
547
548
} catch (error &e) {
549
return e.get();
550
}
551
552
CLOVER_API cl_int
553
clGetProgramBuildInfo(cl_program d_prog, cl_device_id d_dev,
554
cl_program_build_info param,
555
size_t size, void *r_buf, size_t *r_size) try {
556
property_buffer buf { r_buf, size, r_size };
557
auto &prog = obj(d_prog);
558
auto &dev = obj(d_dev);
559
560
if (!count(dev, prog.context().devices()))
561
return CL_INVALID_DEVICE;
562
563
switch (param) {
564
case CL_PROGRAM_BUILD_STATUS:
565
buf.as_scalar<cl_build_status>() = prog.build(dev).status();
566
break;
567
568
case CL_PROGRAM_BUILD_OPTIONS:
569
buf.as_string() = prog.build(dev).opts;
570
break;
571
572
case CL_PROGRAM_BUILD_LOG:
573
buf.as_string() = prog.build(dev).log;
574
break;
575
576
case CL_PROGRAM_BINARY_TYPE:
577
buf.as_scalar<cl_program_binary_type>() = prog.build(dev).binary_type();
578
break;
579
580
case CL_PROGRAM_BUILD_GLOBAL_VARIABLE_TOTAL_SIZE:
581
buf.as_scalar<size_t>() = 0;
582
break;
583
584
default:
585
throw error(CL_INVALID_VALUE);
586
}
587
588
return CL_SUCCESS;
589
590
} catch (error &e) {
591
return e.get();
592
}
593
594