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 namespace clover; 64 using namespace clover::llvm; 65 66 using ::llvm::Function; 67 using ::llvm::LLVMContext; 68 using ::llvm::Module; 69 using ::llvm::raw_string_ostream; 70 71 namespace { 72 73 struct cl_version { 74 std::string version_str; // CL Version 75 unsigned version_number; // Numeric CL Version 76 }; 77 78 static const unsigned ANY_VERSION = 999; 79 const cl_version cl_versions[] = { 80 { "1.0", 100}, 81 { "1.1", 110}, 82 { "1.2", 120}, 83 { "2.0", 200}, 84 { "2.1", 210}, 85 { "2.2", 220}, 86 { "3.0", 300}, 87 }; 88 89 struct clc_version_lang_std { 90 unsigned version_number; // CLC Version 91 clang::LangStandard::Kind clc_lang_standard; 92 }; 93 94 const clc_version_lang_std cl_version_lang_stds[] = { 95 { 100, clang::LangStandard::lang_opencl10}, 96 { 110, clang::LangStandard::lang_opencl11}, 97 { 120, clang::LangStandard::lang_opencl12}, 98 { 200, clang::LangStandard::lang_opencl20}, 99 #if LLVM_VERSION_MAJOR >= 12 100 { 300, clang::LangStandard::lang_opencl30}, 101 #endif 102 }; 103 104 void init_targets()105 init_targets() { 106 static bool targets_initialized = false; 107 if (!targets_initialized) { 108 LLVMInitializeAllTargets(); 109 LLVMInitializeAllTargetInfos(); 110 LLVMInitializeAllTargetMCs(); 111 LLVMInitializeAllAsmParsers(); 112 LLVMInitializeAllAsmPrinters(); 113 targets_initialized = true; 114 } 115 } 116 117 void diagnostic_handler(const::llvm::DiagnosticInfo & di,void * data)118 diagnostic_handler(const ::llvm::DiagnosticInfo &di, void *data) { 119 if (di.getSeverity() == ::llvm::DS_Error) { 120 raw_string_ostream os { *reinterpret_cast<std::string *>(data) }; 121 ::llvm::DiagnosticPrinterRawOStream printer { os }; 122 di.print(printer); 123 throw build_error(); 124 } 125 } 126 127 std::unique_ptr<LLVMContext> create_context(std::string & r_log)128 create_context(std::string &r_log) { 129 init_targets(); 130 std::unique_ptr<LLVMContext> ctx { new LLVMContext }; 131 132 ctx->setDiagnosticHandlerCallBack(diagnostic_handler, &r_log); 133 return ctx; 134 } 135 136 const struct clc_version_lang_std& get_cl_lang_standard(unsigned requested,unsigned max=ANY_VERSION)137 get_cl_lang_standard(unsigned requested, unsigned max = ANY_VERSION) { 138 for (const struct clc_version_lang_std &version : cl_version_lang_stds) { 139 if (version.version_number == max || 140 version.version_number == requested) { 141 return version; 142 } 143 } 144 throw build_error("Unknown/Unsupported language version"); 145 } 146 147 const struct cl_version& get_cl_version(const std::string & version_str,unsigned max=ANY_VERSION)148 get_cl_version(const std::string &version_str, 149 unsigned max = ANY_VERSION) { 150 for (const struct cl_version &version : cl_versions) { 151 if (version.version_number == max || version.version_str == version_str) { 152 return version; 153 } 154 } 155 throw build_error("Unknown/Unsupported language version"); 156 } 157 158 clang::LangStandard::Kind get_lang_standard_from_version_str(const std::string & version_str,bool is_build_opt=false)159 get_lang_standard_from_version_str(const std::string &version_str, 160 bool is_build_opt = false) { 161 162 //Per CL 2.0 spec, section 5.8.4.5: 163 // If it's an option, use the value directly. 164 // If it's a device version, clamp to max 1.x version, a.k.a. 1.2 165 const cl_version version = 166 get_cl_version(version_str, is_build_opt ? ANY_VERSION : 120); 167 168 const struct clc_version_lang_std standard = 169 get_cl_lang_standard(version.version_number); 170 171 return standard.clc_lang_standard; 172 } 173 174 clang::LangStandard::Kind get_language_version(const std::vector<std::string> & opts,const std::string & device_version)175 get_language_version(const std::vector<std::string> &opts, 176 const std::string &device_version) { 177 178 const std::string search = "-cl-std=CL"; 179 180 for (auto &opt: opts) { 181 auto pos = opt.find(search); 182 if (pos == 0){ 183 const auto ver = opt.substr(pos + search.size()); 184 const auto device_ver = get_cl_version(device_version); 185 const auto requested = get_cl_version(ver); 186 if (requested.version_number > device_ver.version_number) { 187 throw build_error(); 188 } 189 return get_lang_standard_from_version_str(ver, true); 190 } 191 } 192 193 return get_lang_standard_from_version_str(device_version); 194 } 195 196 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)197 create_compiler_instance(const device &dev, const std::string& ir_target, 198 const std::vector<std::string> &opts, 199 std::string &r_log) { 200 std::unique_ptr<clang::CompilerInstance> c { new clang::CompilerInstance }; 201 clang::TextDiagnosticBuffer *diag_buffer = new clang::TextDiagnosticBuffer; 202 clang::DiagnosticsEngine diag { new clang::DiagnosticIDs, 203 new clang::DiagnosticOptions, diag_buffer }; 204 205 // Parse the compiler options. A file name should be present at the end 206 // and must have the .cl extension in order for the CompilerInvocation 207 // class to recognize it as an OpenCL source file. 208 const std::vector<const char *> copts = 209 map(std::mem_fn(&std::string::c_str), opts); 210 211 const target &target = ir_target; 212 const std::string &device_clc_version = dev.device_clc_version(); 213 214 if (!compat::create_compiler_invocation_from_args( 215 c->getInvocation(), copts, diag)) 216 throw invalid_build_options_error(); 217 218 diag_buffer->FlushDiagnostics(diag); 219 if (diag.hasErrorOccurred()) 220 throw invalid_build_options_error(); 221 222 c->getTargetOpts().CPU = target.cpu; 223 c->getTargetOpts().Triple = target.triple; 224 c->getLangOpts().NoBuiltin = true; 225 226 // This is a workaround for a Clang bug which causes the number 227 // of warnings and errors to be printed to stderr. 228 // http://www.llvm.org/bugs/show_bug.cgi?id=19735 229 c->getDiagnosticOpts().ShowCarets = false; 230 231 c->getInvocation().setLangDefaults(c->getLangOpts(), 232 compat::ik_opencl, ::llvm::Triple(target.triple), 233 c->getPreprocessorOpts(), 234 get_language_version(opts, device_clc_version)); 235 236 c->createDiagnostics(new clang::TextDiagnosticPrinter( 237 *new raw_string_ostream(r_log), 238 &c->getDiagnosticOpts(), true)); 239 240 c->setTarget(clang::TargetInfo::CreateTargetInfo( 241 c->getDiagnostics(), c->getInvocation().TargetOpts)); 242 243 return c; 244 } 245 246 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)247 compile(LLVMContext &ctx, clang::CompilerInstance &c, 248 const std::string &name, const std::string &source, 249 const header_map &headers, const device &dev, 250 const std::string &opts, bool use_libclc, std::string &r_log) { 251 c.getFrontendOpts().ProgramAction = clang::frontend::EmitLLVMOnly; 252 c.getHeaderSearchOpts().UseBuiltinIncludes = true; 253 c.getHeaderSearchOpts().UseStandardSystemIncludes = true; 254 c.getHeaderSearchOpts().ResourceDir = CLANG_RESOURCE_DIR; 255 256 if (use_libclc) { 257 // Add libclc generic search path 258 c.getHeaderSearchOpts().AddPath(LIBCLC_INCLUDEDIR, 259 clang::frontend::Angled, 260 false, false); 261 262 // Add libclc include 263 c.getPreprocessorOpts().Includes.push_back("clc/clc.h"); 264 } else { 265 // Add opencl-c generic search path 266 c.getHeaderSearchOpts().AddPath(CLANG_RESOURCE_DIR, 267 clang::frontend::Angled, 268 false, false); 269 270 // Add opencl include 271 c.getPreprocessorOpts().Includes.push_back("opencl-c.h"); 272 } 273 274 // Add definition for the OpenCL version 275 c.getPreprocessorOpts().addMacroDef("__OPENCL_VERSION__=" + 276 std::to_string(get_cl_version( 277 dev.device_version()).version_number)); 278 279 // clc.h requires that this macro be defined: 280 c.getPreprocessorOpts().addMacroDef("cl_clang_storage_class_specifiers"); 281 c.getPreprocessorOpts().addRemappedFile( 282 name, ::llvm::MemoryBuffer::getMemBuffer(source).release()); 283 284 if (headers.size()) { 285 const std::string tmp_header_path = "/tmp/clover/"; 286 287 c.getHeaderSearchOpts().AddPath(tmp_header_path, 288 clang::frontend::Angled, 289 false, false); 290 291 for (const auto &header : headers) 292 c.getPreprocessorOpts().addRemappedFile( 293 tmp_header_path + header.first, 294 ::llvm::MemoryBuffer::getMemBuffer(header.second).release()); 295 } 296 297 // Tell clang to link this file before performing any 298 // optimizations. This is required so that we can replace calls 299 // to the OpenCL C barrier() builtin with calls to target 300 // intrinsics that have the noduplicate attribute. This 301 // attribute will prevent Clang from creating illegal uses of 302 // barrier() (e.g. Moving barrier() inside a conditional that is 303 // no executed by all threads) during its optimizaton passes. 304 if (use_libclc) { 305 clang::CodeGenOptions::BitcodeFileToLink F; 306 307 F.Filename = LIBCLC_LIBEXECDIR + dev.ir_target() + ".bc"; 308 F.PropagateAttrs = true; 309 F.LinkFlags = ::llvm::Linker::Flags::None; 310 c.getCodeGenOpts().LinkBitcodeFiles.emplace_back(F); 311 } 312 313 // undefine __IMAGE_SUPPORT__ for device without image support 314 if (!dev.image_support()) 315 c.getPreprocessorOpts().addMacroUndef("__IMAGE_SUPPORT__"); 316 317 // Compile the code 318 clang::EmitLLVMOnlyAction act(&ctx); 319 if (!c.ExecuteAction(act)) 320 throw build_error(); 321 322 return act.takeModule(); 323 } 324 325 #ifdef HAVE_CLOVER_SPIRV 326 SPIRV::TranslatorOpts get_spirv_translator_options(const device & dev)327 get_spirv_translator_options(const device &dev) { 328 const auto supported_versions = spirv::supported_versions(); 329 const auto maximum_spirv_version = 330 std::min(static_cast<SPIRV::VersionNumber>(supported_versions.back()), 331 SPIRV::VersionNumber::MaximumVersion); 332 333 SPIRV::TranslatorOpts::ExtensionsStatusMap spirv_extensions; 334 for (auto &ext : spirv::supported_extensions()) { 335 #define EXT(X) if (ext == #X) spirv_extensions.insert({ SPIRV::ExtensionID::X, true }); 336 #include <LLVMSPIRVLib/LLVMSPIRVExtensions.inc> 337 #undef EXT 338 } 339 340 return SPIRV::TranslatorOpts(maximum_spirv_version, spirv_extensions); 341 } 342 #endif 343 } 344 345 module 346 clover::llvm::compile_program(const std::string &source, 347 const header_map &headers, 348 const device &dev, 349 const std::string &opts, 350 std::string &r_log) { 351 if (has_flag(debug::clc)) 352 debug::log(".cl", "// Options: " + opts + '\n' + source); 353 354 auto ctx = create_context(r_log); 355 auto c = create_compiler_instance(dev, dev.ir_target(), 356 tokenize(opts + " input.cl"), r_log); 357 auto mod = compile(*ctx, *c, "input.cl", source, headers, dev, opts, true, 358 r_log); 359 360 if (has_flag(debug::llvm)) 361 debug::log(".ll", print_module_bitcode(*mod)); 362 363 return build_module_library(*mod, module::section::text_intermediate); 364 } 365 366 namespace { 367 void 368 optimize(Module &mod, unsigned optimization_level, 369 bool internalize_symbols) { 370 ::llvm::legacy::PassManager pm; 371 372 // By default, the function internalizer pass will look for a function 373 // called "main" and then mark all other functions as internal. Marking 374 // functions as internal enables the optimizer to perform optimizations 375 // like function inlining and global dead-code elimination. 376 // 377 // When there is no "main" function in a module, the internalize pass will 378 // treat the module like a library, and it won't internalize any functions. 379 // Since there is no "main" function in our kernels, we need to tell 380 // the internalizer pass that this module is not a library by passing a 381 // list of kernel functions to the internalizer. The internalizer will 382 // treat the functions in the list as "main" functions and internalize 383 // all of the other functions. 384 if (internalize_symbols) { 385 std::vector<std::string> names = 386 map(std::mem_fn(&Function::getName), get_kernels(mod)); 387 pm.add(::llvm::createInternalizePass( __anon258a73aa0202(const ::llvm::GlobalValue &gv) 388 [=](const ::llvm::GlobalValue &gv) { 389 return std::find(names.begin(), names.end(), 390 gv.getName()) != names.end(); 391 })); 392 } 393 394 ::llvm::PassManagerBuilder pmb; 395 pmb.OptLevel = optimization_level; 396 pmb.LibraryInfo = new ::llvm::TargetLibraryInfoImpl( 397 ::llvm::Triple(mod.getTargetTriple())); 398 pmb.populateModulePassManager(pm); 399 pm.run(mod); 400 } 401 402 std::unique_ptr<Module> 403 link(LLVMContext &ctx, const clang::CompilerInstance &c, 404 const std::vector<module> &modules, std::string &r_log) { 405 std::unique_ptr<Module> mod { new Module("link", ctx) }; 406 std::unique_ptr< ::llvm::Linker> linker { new ::llvm::Linker(*mod) }; 407 408 for (auto &m : modules) { 409 if (linker->linkInModule(parse_module_library(m, ctx, r_log))) 410 throw build_error(); 411 } 412 413 return mod; 414 } 415 } 416 417 module 418 clover::llvm::link_program(const std::vector<module> &modules, 419 const device &dev, const std::string &opts, 420 std::string &r_log) { 421 std::vector<std::string> options = tokenize(opts + " input.cl"); 422 const bool create_library = count("-create-library", options); 423 erase_if(equals("-create-library"), options); 424 425 auto ctx = create_context(r_log); 426 auto c = create_compiler_instance(dev, dev.ir_target(), options, r_log); 427 auto mod = link(*ctx, *c, modules, r_log); 428 429 optimize(*mod, c->getCodeGenOpts().OptimizationLevel, !create_library); 430 431 static std::atomic_uint seq(0); 432 const std::string id = "." + mod->getModuleIdentifier() + "-" + 433 std::to_string(seq++); 434 435 if (has_flag(debug::llvm)) 436 debug::log(id + ".ll", print_module_bitcode(*mod)); 437 438 if (create_library) { 439 return build_module_library(*mod, module::section::text_library); 440 441 } else if (dev.ir_format() == PIPE_SHADER_IR_NATIVE) { 442 if (has_flag(debug::native)) 443 debug::log(id + ".asm", print_module_native(*mod, dev.ir_target())); 444 445 return build_module_native(*mod, dev.ir_target(), *c, r_log); 446 447 } else { 448 unreachable("Unsupported IR."); 449 } 450 } 451 452 #ifdef HAVE_CLOVER_SPIRV 453 module 454 clover::llvm::compile_to_spirv(const std::string &source, 455 const header_map &headers, 456 const device &dev, 457 const std::string &opts, 458 std::string &r_log) { 459 if (has_flag(debug::clc)) 460 debug::log(".cl", "// Options: " + opts + '\n' + source); 461 462 auto ctx = create_context(r_log); 463 const std::string target = dev.address_bits() == 32u ? 464 "-spir-unknown-unknown" : 465 "-spir64-unknown-unknown"; 466 auto c = create_compiler_instance(dev, target, 467 tokenize(opts + " -O0 input.cl"), r_log); 468 auto mod = compile(*ctx, *c, "input.cl", source, headers, dev, opts, false, 469 r_log); 470 471 if (has_flag(debug::llvm)) 472 debug::log(".ll", print_module_bitcode(*mod)); 473 474 const auto spirv_options = get_spirv_translator_options(dev); 475 476 std::string error_msg; 477 std::ostringstream os; 478 if (!::llvm::writeSpirv(mod.get(), spirv_options, os, error_msg)) { 479 r_log += "Translation from LLVM IR to SPIR-V failed: " + error_msg + ".\n"; 480 throw error(CL_INVALID_VALUE); 481 } 482 483 const std::string osContent = os.str(); 484 std::vector<char> binary(osContent.begin(), osContent.end()); 485 if (binary.empty()) { 486 r_log += "Failed to retrieve SPIR-V binary.\n"; 487 throw error(CL_INVALID_VALUE); 488 } 489 490 if (has_flag(debug::spirv)) 491 debug::log(".spvasm", spirv::print_module(binary, dev.device_version())); 492 493 return spirv::compile_program(binary, dev, r_log); 494 } 495 #endif 496