1 //
2 // Copyright 2012-2016 Francisco Jerez
3 // Copyright 2012-2016 Advanced Micro Devices, Inc.
4 // Copyright 2014-2016 Jan Vesely
5 // Copyright 2014-2015 Serge Martin
6 // Copyright 2015 Zoltan Gilian
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 // OTHER DEALINGS IN THE SOFTWARE.
25 //
26
27 #include <llvm/IR/DiagnosticPrinter.h>
28 #include <llvm/IR/DiagnosticInfo.h>
29 #include <llvm/IR/LLVMContext.h>
30 #include <llvm/Support/raw_ostream.h>
31 #include <llvm/Transforms/IPO/PassManagerBuilder.h>
32 #include <llvm-c/Target.h>
33 #ifdef HAVE_CLOVER_SPIRV
34 #include <LLVMSPIRVLib/LLVMSPIRVLib.h>
35 #endif
36
37 #include <clang/CodeGen/CodeGenAction.h>
38 #include <clang/Lex/PreprocessorOptions.h>
39 #include <clang/Frontend/TextDiagnosticBuffer.h>
40 #include <clang/Frontend/TextDiagnosticPrinter.h>
41 #include <clang/Basic/TargetInfo.h>
42
43 // We need to include internal headers last, because the internal headers
44 // include CL headers which have #define's like:
45 //
46 //#define cl_khr_gl_sharing 1
47 //#define cl_khr_icd 1
48 //
49 // Which will break the compilation of clang/Basic/OpenCLOptions.h
50
51 #include "core/error.hpp"
52 #include "llvm/codegen.hpp"
53 #include "llvm/compat.hpp"
54 #include "llvm/invocation.hpp"
55 #include "llvm/metadata.hpp"
56 #include "llvm/util.hpp"
57 #ifdef HAVE_CLOVER_SPIRV
58 #include "spirv/invocation.hpp"
59 #endif
60 #include "util/algorithm.hpp"
61
62
63 using clover::binary;
64 using clover::device;
65 using clover::build_error;
66 using clover::invalid_build_options_error;
67 using clover::map;
68 using clover::header_map;
69 using namespace clover::llvm;
70
71 using ::llvm::Function;
72 using ::llvm::LLVMContext;
73 using ::llvm::Module;
74 using ::llvm::raw_string_ostream;
75
76 namespace {
77
78 static const cl_version ANY_VERSION = CL_MAKE_VERSION(9, 9, 9);
79 const cl_version cl_versions[] = {
80 CL_MAKE_VERSION(1, 1, 0),
81 CL_MAKE_VERSION(1, 2, 0),
82 CL_MAKE_VERSION(2, 0, 0),
83 CL_MAKE_VERSION(2, 1, 0),
84 CL_MAKE_VERSION(2, 2, 0),
85 CL_MAKE_VERSION(3, 0, 0),
86 };
87
88 struct clc_version_lang_std {
89 cl_version version_number; // CLC Version
90 clang::LangStandard::Kind clc_lang_standard;
91 };
92
93 const clc_version_lang_std cl_version_lang_stds[] = {
94 { CL_MAKE_VERSION(1, 0, 0), clang::LangStandard::lang_opencl10},
95 { CL_MAKE_VERSION(1, 1, 0), clang::LangStandard::lang_opencl11},
96 { CL_MAKE_VERSION(1, 2, 0), clang::LangStandard::lang_opencl12},
97 { CL_MAKE_VERSION(2, 0, 0), clang::LangStandard::lang_opencl20},
98 #if LLVM_VERSION_MAJOR >= 12
99 { CL_MAKE_VERSION(3, 0, 0), clang::LangStandard::lang_opencl30},
100 #endif
101 };
102
103 bool
are_equal(cl_version_khr version1,cl_version_khr version2,bool ignore_patch_version=false)104 are_equal(cl_version_khr version1, cl_version_khr version2,
105 bool ignore_patch_version = false) {
106 if (ignore_patch_version) {
107 version1 &= ~CL_VERSION_PATCH_MASK_KHR;
108 version2 &= ~CL_VERSION_PATCH_MASK_KHR;
109 }
110 return version1 == version2;
111 }
112
113 void
init_targets()114 init_targets() {
115 static bool targets_initialized = false;
116 if (!targets_initialized) {
117 LLVMInitializeAllTargets();
118 LLVMInitializeAllTargetInfos();
119 LLVMInitializeAllTargetMCs();
120 LLVMInitializeAllAsmParsers();
121 LLVMInitializeAllAsmPrinters();
122 targets_initialized = true;
123 }
124 }
125
126 void
diagnostic_handler(const::llvm::DiagnosticInfo & di,void * data)127 diagnostic_handler(const ::llvm::DiagnosticInfo &di, void *data) {
128 if (di.getSeverity() == ::llvm::DS_Error) {
129 raw_string_ostream os { *reinterpret_cast<std::string *>(data) };
130 ::llvm::DiagnosticPrinterRawOStream printer { os };
131 di.print(printer);
132 throw build_error();
133 }
134 }
135
136 std::unique_ptr<LLVMContext>
create_context(std::string & r_log)137 create_context(std::string &r_log) {
138 init_targets();
139 std::unique_ptr<LLVMContext> ctx { new LLVMContext };
140
141 ctx->setDiagnosticHandlerCallBack(diagnostic_handler, &r_log);
142 return ctx;
143 }
144
145 const struct clc_version_lang_std&
get_cl_lang_standard(unsigned requested,unsigned max=ANY_VERSION)146 get_cl_lang_standard(unsigned requested, unsigned max = ANY_VERSION) {
147 for (const struct clc_version_lang_std &version : cl_version_lang_stds) {
148 if (version.version_number == max ||
149 version.version_number == requested) {
150 return version;
151 }
152 }
153 throw build_error("Unknown/Unsupported language version");
154 }
155
156 const cl_version
get_cl_version(cl_version requested,cl_version max=ANY_VERSION)157 get_cl_version(cl_version requested,
158 cl_version max = ANY_VERSION) {
159 for (const auto &version : cl_versions) {
160 if (are_equal(version, max, true) ||
161 are_equal(version, requested, true)) {
162 return version;
163 }
164 }
165 throw build_error("Unknown/Unsupported language version");
166 }
167
168 clang::LangStandard::Kind
get_lang_standard_from_version(const cl_version input_version,bool is_build_opt=false)169 get_lang_standard_from_version(const cl_version input_version,
170 bool is_build_opt = false) {
171
172 //Per CL 2.0 spec, section 5.8.4.5:
173 // If it's an option, use the value directly.
174 // If it's a device version, clamp to max 1.x version, a.k.a. 1.2
175 const cl_version version =
176 get_cl_version(input_version, is_build_opt ? ANY_VERSION : 120);
177
178 const struct clc_version_lang_std standard =
179 get_cl_lang_standard(version);
180
181 return standard.clc_lang_standard;
182 }
183
184 clang::LangStandard::Kind
get_language_version(const std::vector<std::string> & opts,const cl_version device_version)185 get_language_version(const std::vector<std::string> &opts,
186 const cl_version device_version) {
187
188 const std::string search = "-cl-std=CL";
189
190 for (auto &opt: opts) {
191 auto pos = opt.find(search);
192 if (pos == 0){
193 std::stringstream ver_str(opt.substr(pos + search.size()));
194 unsigned int ver_major = 0;
195 char separator = '\0';
196 unsigned int ver_minor = 0;
197 ver_str >> ver_major >> separator >> ver_minor;
198 if (ver_str.fail() || ver_str.bad() || !ver_str.eof() ||
199 separator != '.') {
200 throw build_error();
201 }
202 const auto ver = CL_MAKE_VERSION_KHR(ver_major, ver_minor, 0);
203 const auto device_ver = get_cl_version(device_version);
204 const auto requested = get_cl_version(ver);
205 if (requested > device_ver) {
206 throw build_error();
207 }
208 return get_lang_standard_from_version(ver, true);
209 }
210 }
211
212 return get_lang_standard_from_version(device_version);
213 }
214
215 std::unique_ptr<clang::CompilerInstance>
create_compiler_instance(const device & dev,const std::string & ir_target,const std::vector<std::string> & opts,std::string & r_log)216 create_compiler_instance(const device &dev, const std::string& ir_target,
217 const std::vector<std::string> &opts,
218 std::string &r_log) {
219 std::unique_ptr<clang::CompilerInstance> c { new clang::CompilerInstance };
220 clang::TextDiagnosticBuffer *diag_buffer = new clang::TextDiagnosticBuffer;
221 clang::DiagnosticsEngine diag { new clang::DiagnosticIDs,
222 new clang::DiagnosticOptions, diag_buffer };
223
224 // Parse the compiler options. A file name should be present at the end
225 // and must have the .cl extension in order for the CompilerInvocation
226 // class to recognize it as an OpenCL source file.
227 #if LLVM_VERSION_MAJOR >= 12
228 std::vector<const char *> copts;
229 for (auto &opt : opts) {
230 if (opt == "-cl-denorms-are-zero")
231 copts.push_back("-fdenormal-fp-math=positive-zero");
232 else
233 copts.push_back(opt.c_str());
234 }
235 #else
236 const std::vector<const char *> copts =
237 map(std::mem_fn(&std::string::c_str), opts);
238 #endif
239
240 const target &target = ir_target;
241 const cl_version device_clc_version = dev.device_clc_version();
242
243 if (!compat::create_compiler_invocation_from_args(
244 c->getInvocation(), copts, diag))
245 throw invalid_build_options_error();
246
247 diag_buffer->FlushDiagnostics(diag);
248 if (diag.hasErrorOccurred())
249 throw invalid_build_options_error();
250
251 c->getTargetOpts().CPU = target.cpu;
252 c->getTargetOpts().Triple = target.triple;
253 c->getLangOpts().NoBuiltin = true;
254
255 #if LLVM_VERSION_MAJOR >= 13
256 c->getTargetOpts().OpenCLExtensionsAsWritten.push_back("-__opencl_c_generic_address_space");
257 c->getTargetOpts().OpenCLExtensionsAsWritten.push_back("-__opencl_c_pipes");
258 c->getTargetOpts().OpenCLExtensionsAsWritten.push_back("-__opencl_c_device_enqueue");
259 c->getTargetOpts().OpenCLExtensionsAsWritten.push_back("-__opencl_c_program_scope_global_variables");
260 c->getTargetOpts().OpenCLExtensionsAsWritten.push_back("-__opencl_c_subgroups");
261 c->getTargetOpts().OpenCLExtensionsAsWritten.push_back("-__opencl_c_work_group_collective_functions");
262 c->getTargetOpts().OpenCLExtensionsAsWritten.push_back("-__opencl_c_atomic_scope_device");
263 c->getTargetOpts().OpenCLExtensionsAsWritten.push_back("-__opencl_c_atomic_order_seq_cst");
264 #endif
265
266 // This is a workaround for a Clang bug which causes the number
267 // of warnings and errors to be printed to stderr.
268 // http://www.llvm.org/bugs/show_bug.cgi?id=19735
269 c->getDiagnosticOpts().ShowCarets = false;
270
271 compat::compiler_set_lang_defaults(c, compat::ik_opencl,
272 ::llvm::Triple(target.triple),
273 get_language_version(opts, device_clc_version));
274
275 c->createDiagnostics(new clang::TextDiagnosticPrinter(
276 *new raw_string_ostream(r_log),
277 &c->getDiagnosticOpts(), true));
278
279 c->setTarget(clang::TargetInfo::CreateTargetInfo(
280 c->getDiagnostics(), c->getInvocation().TargetOpts));
281
282 return c;
283 }
284
285 std::unique_ptr<Module>
compile(LLVMContext & ctx,clang::CompilerInstance & c,const std::string & name,const std::string & source,const header_map & headers,const device & dev,const std::string & opts,bool use_libclc,std::string & r_log)286 compile(LLVMContext &ctx, clang::CompilerInstance &c,
287 const std::string &name, const std::string &source,
288 const header_map &headers, const device &dev,
289 const std::string &opts, bool use_libclc, std::string &r_log) {
290 c.getFrontendOpts().ProgramAction = clang::frontend::EmitLLVMOnly;
291 c.getHeaderSearchOpts().UseBuiltinIncludes = true;
292 c.getHeaderSearchOpts().UseStandardSystemIncludes = true;
293 c.getHeaderSearchOpts().ResourceDir = CLANG_RESOURCE_DIR;
294
295 if (use_libclc) {
296 // Add libclc generic search path
297 c.getHeaderSearchOpts().AddPath(LIBCLC_INCLUDEDIR,
298 clang::frontend::Angled,
299 false, false);
300
301 // Add libclc include
302 c.getPreprocessorOpts().Includes.push_back("clc/clc.h");
303 } else {
304 // Add opencl-c generic search path
305 c.getHeaderSearchOpts().AddPath(CLANG_RESOURCE_DIR,
306 clang::frontend::Angled,
307 false, false);
308
309 // Add opencl include
310 c.getPreprocessorOpts().Includes.push_back("opencl-c.h");
311 }
312
313 // Add definition for the OpenCL version
314 const auto dev_version = dev.device_version();
315 c.getPreprocessorOpts().addMacroDef("__OPENCL_VERSION__=" +
316 std::to_string(CL_VERSION_MAJOR_KHR(dev_version)) +
317 std::to_string(CL_VERSION_MINOR_KHR(dev_version)) + "0");
318
319 if (CL_VERSION_MAJOR(dev.version) >= 3) {
320 const auto features = dev.opencl_c_features();
321 for (const auto &feature : features)
322 c.getPreprocessorOpts().addMacroDef(feature.name);
323 }
324
325 // clc.h requires that this macro be defined:
326 c.getPreprocessorOpts().addMacroDef("cl_clang_storage_class_specifiers");
327 c.getPreprocessorOpts().addRemappedFile(
328 name, ::llvm::MemoryBuffer::getMemBuffer(source).release());
329
330 if (headers.size()) {
331 const std::string tmp_header_path = "/tmp/clover/";
332
333 c.getHeaderSearchOpts().AddPath(tmp_header_path,
334 clang::frontend::Angled,
335 false, false);
336
337 for (const auto &header : headers)
338 c.getPreprocessorOpts().addRemappedFile(
339 tmp_header_path + header.first,
340 ::llvm::MemoryBuffer::getMemBuffer(header.second).release());
341 }
342
343 // Tell clang to link this file before performing any
344 // optimizations. This is required so that we can replace calls
345 // to the OpenCL C barrier() builtin with calls to target
346 // intrinsics that have the noduplicate attribute. This
347 // attribute will prevent Clang from creating illegal uses of
348 // barrier() (e.g. Moving barrier() inside a conditional that is
349 // no executed by all threads) during its optimizaton passes.
350 if (use_libclc) {
351 clang::CodeGenOptions::BitcodeFileToLink F;
352
353 F.Filename = LIBCLC_LIBEXECDIR + dev.ir_target() + ".bc";
354 F.PropagateAttrs = true;
355 F.LinkFlags = ::llvm::Linker::Flags::None;
356 c.getCodeGenOpts().LinkBitcodeFiles.emplace_back(F);
357 }
358
359 // undefine __IMAGE_SUPPORT__ for device without image support
360 if (!dev.image_support())
361 c.getPreprocessorOpts().addMacroUndef("__IMAGE_SUPPORT__");
362
363 // Compile the code
364 clang::EmitLLVMOnlyAction act(&ctx);
365 if (!c.ExecuteAction(act))
366 throw build_error();
367
368 return act.takeModule();
369 }
370
371 #ifdef HAVE_CLOVER_SPIRV
372 SPIRV::TranslatorOpts
get_spirv_translator_options(const device & dev)373 get_spirv_translator_options(const device &dev) {
374 const auto supported_versions = clover::spirv::supported_versions();
375 const auto max_supported = clover::spirv::to_spirv_version_encoding(supported_versions.back().version);
376 const auto maximum_spirv_version =
377 std::min(static_cast<SPIRV::VersionNumber>(max_supported),
378 SPIRV::VersionNumber::MaximumVersion);
379
380 SPIRV::TranslatorOpts::ExtensionsStatusMap spirv_extensions;
381 for (auto &ext : clover::spirv::supported_extensions()) {
382 #define EXT(X) if (ext == #X) spirv_extensions.insert({ SPIRV::ExtensionID::X, true });
383 #include <LLVMSPIRVLib/LLVMSPIRVExtensions.inc>
384 #undef EXT
385 }
386
387 return SPIRV::TranslatorOpts(maximum_spirv_version, spirv_extensions);
388 }
389 #endif
390 }
391
392 binary
compile_program(const std::string & source,const header_map & headers,const device & dev,const std::string & opts,std::string & r_log)393 clover::llvm::compile_program(const std::string &source,
394 const header_map &headers,
395 const device &dev,
396 const std::string &opts,
397 std::string &r_log) {
398 if (has_flag(debug::clc))
399 debug::log(".cl", "// Options: " + opts + '\n' + source);
400
401 auto ctx = create_context(r_log);
402 auto c = create_compiler_instance(dev, dev.ir_target(),
403 tokenize(opts + " input.cl"), r_log);
404 auto mod = compile(*ctx, *c, "input.cl", source, headers, dev, opts, true,
405 r_log);
406
407 if (has_flag(debug::llvm))
408 debug::log(".ll", print_module_bitcode(*mod));
409
410 return build_module_library(*mod, binary::section::text_intermediate);
411 }
412
413 namespace {
414 void
optimize(Module & mod,unsigned optimization_level,bool internalize_symbols)415 optimize(Module &mod, unsigned optimization_level,
416 bool internalize_symbols) {
417 ::llvm::legacy::PassManager pm;
418
419 // By default, the function internalizer pass will look for a function
420 // called "main" and then mark all other functions as internal. Marking
421 // functions as internal enables the optimizer to perform optimizations
422 // like function inlining and global dead-code elimination.
423 //
424 // When there is no "main" function in a binary, the internalize pass will
425 // treat the binary like a library, and it won't internalize any functions.
426 // Since there is no "main" function in our kernels, we need to tell
427 // the internalizer pass that this binary is not a library by passing a
428 // list of kernel functions to the internalizer. The internalizer will
429 // treat the functions in the list as "main" functions and internalize
430 // all of the other functions.
431 if (internalize_symbols) {
432 std::vector<std::string> names =
433 map(std::mem_fn(&Function::getName), get_kernels(mod));
434 pm.add(::llvm::createInternalizePass(
435 [=](const ::llvm::GlobalValue &gv) {
436 return std::find(names.begin(), names.end(),
437 gv.getName()) != names.end();
438 }));
439 }
440
441 ::llvm::PassManagerBuilder pmb;
442 pmb.OptLevel = optimization_level;
443 pmb.LibraryInfo = new ::llvm::TargetLibraryInfoImpl(
444 ::llvm::Triple(mod.getTargetTriple()));
445 pmb.populateModulePassManager(pm);
446 pm.run(mod);
447 }
448
449 std::unique_ptr<Module>
link(LLVMContext & ctx,const clang::CompilerInstance & c,const std::vector<binary> & binaries,std::string & r_log)450 link(LLVMContext &ctx, const clang::CompilerInstance &c,
451 const std::vector<binary> &binaries, std::string &r_log) {
452 std::unique_ptr<Module> mod { new Module("link", ctx) };
453 std::unique_ptr< ::llvm::Linker> linker { new ::llvm::Linker(*mod) };
454
455 for (auto &b : binaries) {
456 if (linker->linkInModule(parse_module_library(b, ctx, r_log)))
457 throw build_error();
458 }
459
460 return mod;
461 }
462 }
463
464 binary
link_program(const std::vector<binary> & binaries,const device & dev,const std::string & opts,std::string & r_log)465 clover::llvm::link_program(const std::vector<binary> &binaries,
466 const device &dev, const std::string &opts,
467 std::string &r_log) {
468 std::vector<std::string> options = tokenize(opts + " input.cl");
469 const bool create_library = count("-create-library", options);
470 erase_if(equals("-create-library"), options);
471
472 auto ctx = create_context(r_log);
473 auto c = create_compiler_instance(dev, dev.ir_target(), options, r_log);
474 auto mod = link(*ctx, *c, binaries, r_log);
475
476 optimize(*mod, c->getCodeGenOpts().OptimizationLevel, !create_library);
477
478 static std::atomic_uint seq(0);
479 const std::string id = "." + mod->getModuleIdentifier() + "-" +
480 std::to_string(seq++);
481
482 if (has_flag(debug::llvm))
483 debug::log(id + ".ll", print_module_bitcode(*mod));
484
485 if (create_library) {
486 return build_module_library(*mod, binary::section::text_library);
487
488 } else if (dev.ir_format() == PIPE_SHADER_IR_NATIVE) {
489 if (has_flag(debug::native))
490 debug::log(id + ".asm", print_module_native(*mod, dev.ir_target()));
491
492 return build_module_native(*mod, dev.ir_target(), *c, r_log);
493
494 } else {
495 unreachable("Unsupported IR.");
496 }
497 }
498
499 #ifdef HAVE_CLOVER_SPIRV
500 binary
compile_to_spirv(const std::string & source,const header_map & headers,const device & dev,const std::string & opts,std::string & r_log)501 clover::llvm::compile_to_spirv(const std::string &source,
502 const header_map &headers,
503 const device &dev,
504 const std::string &opts,
505 std::string &r_log) {
506 if (has_flag(debug::clc))
507 debug::log(".cl", "// Options: " + opts + '\n' + source);
508
509 auto ctx = create_context(r_log);
510 const std::string target = dev.address_bits() == 32u ?
511 "-spir-unknown-unknown" :
512 "-spir64-unknown-unknown";
513 auto c = create_compiler_instance(dev, target,
514 tokenize(opts + " -O0 -fgnu89-inline input.cl"), r_log);
515 auto mod = compile(*ctx, *c, "input.cl", source, headers, dev, opts, false,
516 r_log);
517
518 if (has_flag(debug::llvm))
519 debug::log(".ll", print_module_bitcode(*mod));
520
521 const auto spirv_options = get_spirv_translator_options(dev);
522
523 std::string error_msg;
524 std::ostringstream os;
525 if (!::llvm::writeSpirv(mod.get(), spirv_options, os, error_msg)) {
526 r_log += "Translation from LLVM IR to SPIR-V failed: " + error_msg + ".\n";
527 throw error(CL_INVALID_VALUE);
528 }
529
530 const std::string osContent = os.str();
531 std::string binary(osContent.begin(), osContent.end());
532 if (binary.empty()) {
533 r_log += "Failed to retrieve SPIR-V binary.\n";
534 throw error(CL_INVALID_VALUE);
535 }
536
537 if (has_flag(debug::spirv))
538 debug::log(".spvasm", spirv::print_module(binary, dev.device_version()));
539
540 return spirv::compile_program(binary, dev, r_log);
541 }
542 #endif
543