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 63using clover::binary; 64using clover::device; 65using clover::build_error; 66using clover::invalid_build_options_error; 67using clover::map; 68using clover::header_map; 69using namespace clover::llvm; 70 71using ::llvm::Function; 72using ::llvm::LLVMContext; 73using ::llvm::Module; 74using ::llvm::raw_string_ostream; 75 76namespace { 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 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 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 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> 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& 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 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 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 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> 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#if LLVM_VERSION_MAJOR >= 15 230 // Since LLVM commit 702d5de4 opaque pointers are enabled by default: 231 // https://gitlab.freedesktop.org/mesa/mesa/-/issues/6342 232 // A better implementation may be doable following suggestions from there: 233 // https://github.com/llvm/llvm-project/issues/54970#issuecomment-1102254254 234 copts.push_back("-no-opaque-pointers"); 235#endif 236 for (auto &opt : opts) { 237 if (opt == "-cl-denorms-are-zero") 238 copts.push_back("-fdenormal-fp-math=positive-zero"); 239 else 240 copts.push_back(opt.c_str()); 241 } 242#else 243 const std::vector<const char *> copts = 244 map(std::mem_fn(&std::string::c_str), opts); 245#endif 246 247 const target &target = ir_target; 248 const cl_version device_clc_version = dev.device_clc_version(); 249 250 if (!compat::create_compiler_invocation_from_args( 251 c->getInvocation(), copts, diag)) 252 throw invalid_build_options_error(); 253 254 diag_buffer->FlushDiagnostics(diag); 255 if (diag.hasErrorOccurred()) 256 throw invalid_build_options_error(); 257 258 c->getTargetOpts().CPU = target.cpu; 259 c->getTargetOpts().Triple = target.triple; 260 c->getLangOpts().NoBuiltin = true; 261 262#if LLVM_VERSION_MAJOR >= 13 263 c->getTargetOpts().OpenCLExtensionsAsWritten.push_back("-__opencl_c_generic_address_space"); 264 c->getTargetOpts().OpenCLExtensionsAsWritten.push_back("-__opencl_c_pipes"); 265 c->getTargetOpts().OpenCLExtensionsAsWritten.push_back("-__opencl_c_device_enqueue"); 266 c->getTargetOpts().OpenCLExtensionsAsWritten.push_back("-__opencl_c_program_scope_global_variables"); 267 c->getTargetOpts().OpenCLExtensionsAsWritten.push_back("-__opencl_c_subgroups"); 268 c->getTargetOpts().OpenCLExtensionsAsWritten.push_back("-__opencl_c_work_group_collective_functions"); 269 c->getTargetOpts().OpenCLExtensionsAsWritten.push_back("-__opencl_c_atomic_scope_device"); 270 c->getTargetOpts().OpenCLExtensionsAsWritten.push_back("-__opencl_c_atomic_order_seq_cst"); 271#endif 272 273 // This is a workaround for a Clang bug which causes the number 274 // of warnings and errors to be printed to stderr. 275 // http://www.llvm.org/bugs/show_bug.cgi?id=19735 276 c->getDiagnosticOpts().ShowCarets = false; 277 278 compat::compiler_set_lang_defaults(c, compat::ik_opencl, 279 ::llvm::Triple(target.triple), 280 get_language_version(opts, device_clc_version)); 281 282 c->createDiagnostics(new clang::TextDiagnosticPrinter( 283 *new raw_string_ostream(r_log), 284 &c->getDiagnosticOpts(), true)); 285 286 c->setTarget(clang::TargetInfo::CreateTargetInfo( 287 c->getDiagnostics(), c->getInvocation().TargetOpts)); 288 289 return c; 290 } 291 292 std::unique_ptr<Module> 293 compile(LLVMContext &ctx, clang::CompilerInstance &c, 294 const std::string &name, const std::string &source, 295 const header_map &headers, const device &dev, 296 const std::string &opts, bool use_libclc, std::string &r_log) { 297 c.getFrontendOpts().ProgramAction = clang::frontend::EmitLLVMOnly; 298 c.getHeaderSearchOpts().UseBuiltinIncludes = true; 299 c.getHeaderSearchOpts().UseStandardSystemIncludes = true; 300 c.getHeaderSearchOpts().ResourceDir = CLANG_RESOURCE_DIR; 301 302 if (use_libclc) { 303 // Add libclc generic search path 304 c.getHeaderSearchOpts().AddPath(LIBCLC_INCLUDEDIR, 305 clang::frontend::Angled, 306 false, false); 307 308 // Add libclc include 309 c.getPreprocessorOpts().Includes.push_back("clc/clc.h"); 310 } else { 311 // Add opencl-c generic search path 312 c.getHeaderSearchOpts().AddPath(CLANG_RESOURCE_DIR, 313 clang::frontend::Angled, 314 false, false); 315 316 // Add opencl include 317 c.getPreprocessorOpts().Includes.push_back("opencl-c.h"); 318 } 319 320 // Add definition for the OpenCL version 321 const auto dev_version = dev.device_version(); 322 c.getPreprocessorOpts().addMacroDef("__OPENCL_VERSION__=" + 323 std::to_string(CL_VERSION_MAJOR_KHR(dev_version)) + 324 std::to_string(CL_VERSION_MINOR_KHR(dev_version)) + "0"); 325 326 if (CL_VERSION_MAJOR(dev.version) >= 3) { 327 const auto features = dev.opencl_c_features(); 328 for (const auto &feature : features) 329 c.getPreprocessorOpts().addMacroDef(feature.name); 330 } 331 332 // clc.h requires that this macro be defined: 333 c.getPreprocessorOpts().addMacroDef("cl_clang_storage_class_specifiers"); 334 c.getPreprocessorOpts().addRemappedFile( 335 name, ::llvm::MemoryBuffer::getMemBuffer(source).release()); 336 337 if (headers.size()) { 338 const std::string tmp_header_path = "/tmp/clover/"; 339 340 c.getHeaderSearchOpts().AddPath(tmp_header_path, 341 clang::frontend::Angled, 342 false, false); 343 344 for (const auto &header : headers) 345 c.getPreprocessorOpts().addRemappedFile( 346 tmp_header_path + header.first, 347 ::llvm::MemoryBuffer::getMemBuffer(header.second).release()); 348 } 349 350 // Tell clang to link this file before performing any 351 // optimizations. This is required so that we can replace calls 352 // to the OpenCL C barrier() builtin with calls to target 353 // intrinsics that have the noduplicate attribute. This 354 // attribute will prevent Clang from creating illegal uses of 355 // barrier() (e.g. Moving barrier() inside a conditional that is 356 // no executed by all threads) during its optimizaton passes. 357 if (use_libclc) { 358 clang::CodeGenOptions::BitcodeFileToLink F; 359 360 F.Filename = LIBCLC_LIBEXECDIR + dev.ir_target() + ".bc"; 361 F.PropagateAttrs = true; 362 F.LinkFlags = ::llvm::Linker::Flags::None; 363 c.getCodeGenOpts().LinkBitcodeFiles.emplace_back(F); 364 } 365 366 // undefine __IMAGE_SUPPORT__ for device without image support 367 if (!dev.image_support()) 368 c.getPreprocessorOpts().addMacroUndef("__IMAGE_SUPPORT__"); 369 370 // Compile the code 371 clang::EmitLLVMOnlyAction act(&ctx); 372 if (!c.ExecuteAction(act)) 373 throw build_error(); 374 375 return act.takeModule(); 376 } 377 378#ifdef HAVE_CLOVER_SPIRV 379 SPIRV::TranslatorOpts 380 get_spirv_translator_options(const device &dev) { 381 const auto supported_versions = clover::spirv::supported_versions(); 382 const auto max_supported = clover::spirv::to_spirv_version_encoding(supported_versions.back().version); 383 const auto maximum_spirv_version = 384 std::min(static_cast<SPIRV::VersionNumber>(max_supported), 385 SPIRV::VersionNumber::MaximumVersion); 386 387 SPIRV::TranslatorOpts::ExtensionsStatusMap spirv_extensions; 388 for (auto &ext : clover::spirv::supported_extensions()) { 389 #define EXT(X) if (ext == #X) spirv_extensions.insert({ SPIRV::ExtensionID::X, true }); 390 #include <LLVMSPIRVLib/LLVMSPIRVExtensions.inc> 391 #undef EXT 392 } 393 394 return SPIRV::TranslatorOpts(maximum_spirv_version, spirv_extensions); 395 } 396#endif 397} 398 399binary 400clover::llvm::compile_program(const std::string &source, 401 const header_map &headers, 402 const device &dev, 403 const std::string &opts, 404 std::string &r_log) { 405 if (has_flag(debug::clc)) 406 debug::log(".cl", "// Options: " + opts + '\n' + source); 407 408 auto ctx = create_context(r_log); 409 auto c = create_compiler_instance(dev, dev.ir_target(), 410 tokenize(opts + " input.cl"), r_log); 411 auto mod = compile(*ctx, *c, "input.cl", source, headers, dev, opts, true, 412 r_log); 413 414 if (has_flag(debug::llvm)) 415 debug::log(".ll", print_module_bitcode(*mod)); 416 417 return build_module_library(*mod, binary::section::text_intermediate); 418} 419 420namespace { 421 void 422 optimize(Module &mod, unsigned optimization_level, 423 bool internalize_symbols) { 424 ::llvm::legacy::PassManager pm; 425 426 // By default, the function internalizer pass will look for a function 427 // called "main" and then mark all other functions as internal. Marking 428 // functions as internal enables the optimizer to perform optimizations 429 // like function inlining and global dead-code elimination. 430 // 431 // When there is no "main" function in a binary, the internalize pass will 432 // treat the binary like a library, and it won't internalize any functions. 433 // Since there is no "main" function in our kernels, we need to tell 434 // the internalizer pass that this binary is not a library by passing a 435 // list of kernel functions to the internalizer. The internalizer will 436 // treat the functions in the list as "main" functions and internalize 437 // all of the other functions. 438 if (internalize_symbols) { 439 std::vector<std::string> names = 440 map(std::mem_fn(&Function::getName), get_kernels(mod)); 441 pm.add(::llvm::createInternalizePass( 442 [=](const ::llvm::GlobalValue &gv) { 443 return std::find(names.begin(), names.end(), 444 gv.getName()) != names.end(); 445 })); 446 } 447 448 ::llvm::PassManagerBuilder pmb; 449 pmb.OptLevel = optimization_level; 450 pmb.LibraryInfo = new ::llvm::TargetLibraryInfoImpl( 451 ::llvm::Triple(mod.getTargetTriple())); 452 pmb.populateModulePassManager(pm); 453 pm.run(mod); 454 } 455 456 std::unique_ptr<Module> 457 link(LLVMContext &ctx, const clang::CompilerInstance &c, 458 const std::vector<binary> &binaries, std::string &r_log) { 459 std::unique_ptr<Module> mod { new Module("link", ctx) }; 460 std::unique_ptr< ::llvm::Linker> linker { new ::llvm::Linker(*mod) }; 461 462 for (auto &b : binaries) { 463 if (linker->linkInModule(parse_module_library(b, ctx, r_log))) 464 throw build_error(); 465 } 466 467 return mod; 468 } 469} 470 471binary 472clover::llvm::link_program(const std::vector<binary> &binaries, 473 const device &dev, const std::string &opts, 474 std::string &r_log) { 475 std::vector<std::string> options = tokenize(opts + " input.cl"); 476 const bool create_library = count("-create-library", options); 477 erase_if(equals("-create-library"), options); 478 479 auto ctx = create_context(r_log); 480 auto c = create_compiler_instance(dev, dev.ir_target(), options, r_log); 481 auto mod = link(*ctx, *c, binaries, r_log); 482 483 optimize(*mod, c->getCodeGenOpts().OptimizationLevel, !create_library); 484 485 static std::atomic_uint seq(0); 486 const std::string id = "." + mod->getModuleIdentifier() + "-" + 487 std::to_string(seq++); 488 489 if (has_flag(debug::llvm)) 490 debug::log(id + ".ll", print_module_bitcode(*mod)); 491 492 if (create_library) { 493 return build_module_library(*mod, binary::section::text_library); 494 495 } else if (dev.ir_format() == PIPE_SHADER_IR_NATIVE) { 496 if (has_flag(debug::native)) 497 debug::log(id + ".asm", print_module_native(*mod, dev.ir_target())); 498 499 return build_module_native(*mod, dev.ir_target(), *c, r_log); 500 501 } else { 502 unreachable("Unsupported IR."); 503 } 504} 505 506#ifdef HAVE_CLOVER_SPIRV 507binary 508clover::llvm::compile_to_spirv(const std::string &source, 509 const header_map &headers, 510 const device &dev, 511 const std::string &opts, 512 std::string &r_log) { 513 if (has_flag(debug::clc)) 514 debug::log(".cl", "// Options: " + opts + '\n' + source); 515 516 auto ctx = create_context(r_log); 517 const std::string target = dev.address_bits() == 32u ? 518 "-spir-unknown-unknown" : 519 "-spir64-unknown-unknown"; 520 auto c = create_compiler_instance(dev, target, 521 tokenize(opts + " -O0 -fgnu89-inline input.cl"), r_log); 522 auto mod = compile(*ctx, *c, "input.cl", source, headers, dev, opts, false, 523 r_log); 524 525 if (has_flag(debug::llvm)) 526 debug::log(".ll", print_module_bitcode(*mod)); 527 528 const auto spirv_options = get_spirv_translator_options(dev); 529 530 std::string error_msg; 531 std::ostringstream os; 532 if (!::llvm::writeSpirv(mod.get(), spirv_options, os, error_msg)) { 533 r_log += "Translation from LLVM IR to SPIR-V failed: " + error_msg + ".\n"; 534 throw error(CL_INVALID_VALUE); 535 } 536 537 const std::string osContent = os.str(); 538 std::string binary(osContent.begin(), osContent.end()); 539 if (binary.empty()) { 540 r_log += "Failed to retrieve SPIR-V binary.\n"; 541 throw error(CL_INVALID_VALUE); 542 } 543 544 if (has_flag(debug::spirv)) 545 debug::log(".spvasm", spirv::print_module(binary, dev.device_version())); 546 547 return spirv::compile_program(binary, dev, r_log); 548} 549#endif 550