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 34#include <clang/CodeGen/CodeGenAction.h> 35#include <clang/Lex/PreprocessorOptions.h> 36#include <clang/Frontend/TextDiagnosticBuffer.h> 37#include <clang/Frontend/TextDiagnosticPrinter.h> 38#include <clang/Basic/TargetInfo.h> 39 40// We need to include internal headers last, because the internal headers 41// include CL headers which have #define's like: 42// 43//#define cl_khr_gl_sharing 1 44//#define cl_khr_icd 1 45// 46// Which will break the compilation of clang/Basic/OpenCLOptions.h 47 48#include "core/error.hpp" 49#include "llvm/codegen.hpp" 50#include "llvm/compat.hpp" 51#include "llvm/invocation.hpp" 52#include "llvm/metadata.hpp" 53#include "llvm/util.hpp" 54#include "util/algorithm.hpp" 55 56 57using namespace clover; 58using namespace clover::llvm; 59 60using ::llvm::Function; 61using ::llvm::LLVMContext; 62using ::llvm::Module; 63using ::llvm::raw_string_ostream; 64 65namespace { 66 67 struct cl_version { 68 std::string version_str; // CL Version 69 unsigned version_number; // Numeric CL Version 70 }; 71 72 static const unsigned ANY_VERSION = 999; 73 const cl_version cl_versions[] = { 74 { "1.0", 100}, 75 { "1.1", 110}, 76 { "1.2", 120}, 77 { "2.0", 200}, 78 { "2.1", 210}, 79 { "2.2", 220}, 80 }; 81 82 struct clc_version_lang_std { 83 unsigned version_number; // CLC Version 84 clang::LangStandard::Kind clc_lang_standard; 85 }; 86 87 const clc_version_lang_std cl_version_lang_stds[] = { 88 { 100, compat::lang_opencl10}, 89 { 110, clang::LangStandard::lang_opencl11}, 90 { 120, clang::LangStandard::lang_opencl12}, 91 { 200, clang::LangStandard::lang_opencl20}, 92 }; 93 94 void 95 init_targets() { 96 static bool targets_initialized = false; 97 if (!targets_initialized) { 98 LLVMInitializeAllTargets(); 99 LLVMInitializeAllTargetInfos(); 100 LLVMInitializeAllTargetMCs(); 101 LLVMInitializeAllAsmPrinters(); 102 targets_initialized = true; 103 } 104 } 105 106 void 107 diagnostic_handler(const ::llvm::DiagnosticInfo &di, void *data) { 108 if (di.getSeverity() == ::llvm::DS_Error) { 109 raw_string_ostream os { *reinterpret_cast<std::string *>(data) }; 110 ::llvm::DiagnosticPrinterRawOStream printer { os }; 111 di.print(printer); 112 throw build_error(); 113 } 114 } 115 116 std::unique_ptr<LLVMContext> 117 create_context(std::string &r_log) { 118 init_targets(); 119 std::unique_ptr<LLVMContext> ctx { new LLVMContext }; 120 compat::set_diagnostic_handler(*ctx, diagnostic_handler, &r_log); 121 return ctx; 122 } 123 124 const struct clc_version_lang_std& 125 get_cl_lang_standard(unsigned requested, unsigned max = ANY_VERSION) { 126 for (const struct clc_version_lang_std &version : cl_version_lang_stds) { 127 if (version.version_number == max || 128 version.version_number == requested) { 129 return version; 130 } 131 } 132 throw build_error("Unknown/Unsupported language version"); 133 } 134 135 const struct cl_version& 136 get_cl_version(const std::string &version_str, 137 unsigned max = ANY_VERSION) { 138 for (const struct cl_version &version : cl_versions) { 139 if (version.version_number == max || version.version_str == version_str) { 140 return version; 141 } 142 } 143 throw build_error("Unknown/Unsupported language version"); 144 } 145 146 clang::LangStandard::Kind 147 get_lang_standard_from_version_str(const std::string &version_str, 148 bool is_build_opt = false) { 149 150 //Per CL 2.0 spec, section 5.8.4.5: 151 // If it's an option, use the value directly. 152 // If it's a device version, clamp to max 1.x version, a.k.a. 1.2 153 const cl_version version = 154 get_cl_version(version_str, is_build_opt ? ANY_VERSION : 120); 155 156 const struct clc_version_lang_std standard = 157 get_cl_lang_standard(version.version_number); 158 159 return standard.clc_lang_standard; 160 } 161 162 clang::LangStandard::Kind 163 get_language_version(const std::vector<std::string> &opts, 164 const std::string &device_version) { 165 166 const std::string search = "-cl-std=CL"; 167 168 for (auto &opt: opts) { 169 auto pos = opt.find(search); 170 if (pos == 0){ 171 const auto ver = opt.substr(pos + search.size()); 172 const auto device_ver = get_cl_version(device_version); 173 const auto requested = get_cl_version(ver); 174 if (requested.version_number > device_ver.version_number) { 175 throw build_error(); 176 } 177 return get_lang_standard_from_version_str(ver, true); 178 } 179 } 180 181 return get_lang_standard_from_version_str(device_version); 182 } 183 184 std::unique_ptr<clang::CompilerInstance> 185 create_compiler_instance(const device &dev, 186 const std::vector<std::string> &opts, 187 std::string &r_log) { 188 std::unique_ptr<clang::CompilerInstance> c { new clang::CompilerInstance }; 189 clang::TextDiagnosticBuffer *diag_buffer = new clang::TextDiagnosticBuffer; 190 clang::DiagnosticsEngine diag { new clang::DiagnosticIDs, 191 new clang::DiagnosticOptions, diag_buffer }; 192 193 // Parse the compiler options. A file name should be present at the end 194 // and must have the .cl extension in order for the CompilerInvocation 195 // class to recognize it as an OpenCL source file. 196 const std::vector<const char *> copts = 197 map(std::mem_fn(&std::string::c_str), opts); 198 199 const target &target = dev.ir_target(); 200 const std::string &device_clc_version = dev.device_clc_version(); 201 202 if (!clang::CompilerInvocation::CreateFromArgs( 203 c->getInvocation(), copts.data(), copts.data() + copts.size(), diag)) 204 throw invalid_build_options_error(); 205 206 diag_buffer->FlushDiagnostics(diag); 207 if (diag.hasErrorOccurred()) 208 throw invalid_build_options_error(); 209 210 c->getTargetOpts().CPU = target.cpu; 211 c->getTargetOpts().Triple = target.triple; 212 c->getLangOpts().NoBuiltin = true; 213 214 // This is a workaround for a Clang bug which causes the number 215 // of warnings and errors to be printed to stderr. 216 // http://www.llvm.org/bugs/show_bug.cgi?id=19735 217 c->getDiagnosticOpts().ShowCarets = false; 218 219 c->getInvocation().setLangDefaults(c->getLangOpts(), 220 compat::ik_opencl, ::llvm::Triple(target.triple), 221 c->getPreprocessorOpts(), 222 get_language_version(opts, device_clc_version)); 223 224 c->createDiagnostics(new clang::TextDiagnosticPrinter( 225 *new raw_string_ostream(r_log), 226 &c->getDiagnosticOpts(), true)); 227 228 c->setTarget(clang::TargetInfo::CreateTargetInfo( 229 c->getDiagnostics(), c->getInvocation().TargetOpts)); 230 231 return c; 232 } 233 234 std::unique_ptr<Module> 235 compile(LLVMContext &ctx, clang::CompilerInstance &c, 236 const std::string &name, const std::string &source, 237 const header_map &headers, const device &dev, 238 const std::string &opts, std::string &r_log) { 239 c.getFrontendOpts().ProgramAction = clang::frontend::EmitLLVMOnly; 240 c.getHeaderSearchOpts().UseBuiltinIncludes = true; 241 c.getHeaderSearchOpts().UseStandardSystemIncludes = true; 242 c.getHeaderSearchOpts().ResourceDir = CLANG_RESOURCE_DIR; 243 244 // Add libclc generic search path 245 c.getHeaderSearchOpts().AddPath(LIBCLC_INCLUDEDIR, 246 clang::frontend::Angled, 247 false, false); 248 249 // Add libclc include 250 c.getPreprocessorOpts().Includes.push_back("clc/clc.h"); 251 252 // Add definition for the OpenCL version 253 c.getPreprocessorOpts().addMacroDef("__OPENCL_VERSION__=" + 254 std::to_string(get_cl_version( 255 dev.device_version()).version_number)); 256 257 // clc.h requires that this macro be defined: 258 c.getPreprocessorOpts().addMacroDef("cl_clang_storage_class_specifiers"); 259 c.getPreprocessorOpts().addRemappedFile( 260 name, ::llvm::MemoryBuffer::getMemBuffer(source).release()); 261 262 if (headers.size()) { 263 const std::string tmp_header_path = "/tmp/clover/"; 264 265 c.getHeaderSearchOpts().AddPath(tmp_header_path, 266 clang::frontend::Angled, 267 false, false); 268 269 for (const auto &header : headers) 270 c.getPreprocessorOpts().addRemappedFile( 271 tmp_header_path + header.first, 272 ::llvm::MemoryBuffer::getMemBuffer(header.second).release()); 273 } 274 275 // Tell clang to link this file before performing any 276 // optimizations. This is required so that we can replace calls 277 // to the OpenCL C barrier() builtin with calls to target 278 // intrinsics that have the noduplicate attribute. This 279 // attribute will prevent Clang from creating illegal uses of 280 // barrier() (e.g. Moving barrier() inside a conditional that is 281 // no executed by all threads) during its optimizaton passes. 282 compat::add_link_bitcode_file(c.getCodeGenOpts(), 283 LIBCLC_LIBEXECDIR + dev.ir_target() + ".bc"); 284 285 // Compile the code 286 clang::EmitLLVMOnlyAction act(&ctx); 287 if (!c.ExecuteAction(act)) 288 throw build_error(); 289 290 return act.takeModule(); 291 } 292} 293 294module 295clover::llvm::compile_program(const std::string &source, 296 const header_map &headers, 297 const device &dev, 298 const std::string &opts, 299 std::string &r_log) { 300 if (has_flag(debug::clc)) 301 debug::log(".cl", "// Options: " + opts + '\n' + source); 302 303 auto ctx = create_context(r_log); 304 auto c = create_compiler_instance(dev, tokenize(opts + " input.cl"), r_log); 305 auto mod = compile(*ctx, *c, "input.cl", source, headers, dev, opts, r_log); 306 307 if (has_flag(debug::llvm)) 308 debug::log(".ll", print_module_bitcode(*mod)); 309 310 return build_module_library(*mod, module::section::text_intermediate); 311} 312 313namespace { 314 void 315 optimize(Module &mod, unsigned optimization_level, 316 bool internalize_symbols) { 317 ::llvm::legacy::PassManager pm; 318 319 // By default, the function internalizer pass will look for a function 320 // called "main" and then mark all other functions as internal. Marking 321 // functions as internal enables the optimizer to perform optimizations 322 // like function inlining and global dead-code elimination. 323 // 324 // When there is no "main" function in a module, the internalize pass will 325 // treat the module like a library, and it won't internalize any functions. 326 // Since there is no "main" function in our kernels, we need to tell 327 // the internalizer pass that this module is not a library by passing a 328 // list of kernel functions to the internalizer. The internalizer will 329 // treat the functions in the list as "main" functions and internalize 330 // all of the other functions. 331 if (internalize_symbols) { 332 std::vector<std::string> names = 333 map(std::mem_fn(&Function::getName), get_kernels(mod)); 334 pm.add(::llvm::createInternalizePass( 335 [=](const ::llvm::GlobalValue &gv) { 336 return std::find(names.begin(), names.end(), 337 gv.getName()) != names.end(); 338 })); 339 } 340 341 ::llvm::PassManagerBuilder pmb; 342 pmb.OptLevel = optimization_level; 343 pmb.LibraryInfo = new ::llvm::TargetLibraryInfoImpl( 344 ::llvm::Triple(mod.getTargetTriple())); 345 pmb.populateModulePassManager(pm); 346 pm.run(mod); 347 } 348 349 std::unique_ptr<Module> 350 link(LLVMContext &ctx, const clang::CompilerInstance &c, 351 const std::vector<module> &modules, std::string &r_log) { 352 std::unique_ptr<Module> mod { new Module("link", ctx) }; 353 std::unique_ptr< ::llvm::Linker> linker { new ::llvm::Linker(*mod) }; 354 355 for (auto &m : modules) { 356 if (linker->linkInModule(parse_module_library(m, ctx, r_log))) 357 throw build_error(); 358 } 359 360 return std::move(mod); 361 } 362} 363 364module 365clover::llvm::link_program(const std::vector<module> &modules, 366 const device &dev, 367 const std::string &opts, std::string &r_log) { 368 std::vector<std::string> options = tokenize(opts + " input.cl"); 369 const bool create_library = count("-create-library", options); 370 erase_if(equals("-create-library"), options); 371 372 auto ctx = create_context(r_log); 373 auto c = create_compiler_instance(dev, options, r_log); 374 auto mod = link(*ctx, *c, modules, r_log); 375 376 optimize(*mod, c->getCodeGenOpts().OptimizationLevel, !create_library); 377 378 static std::atomic_uint seq(0); 379 const std::string id = "." + mod->getModuleIdentifier() + "-" + 380 std::to_string(seq++); 381 382 if (has_flag(debug::llvm)) 383 debug::log(id + ".ll", print_module_bitcode(*mod)); 384 385 if (create_library) { 386 return build_module_library(*mod, module::section::text_library); 387 388 } else if (dev.ir_format() == PIPE_SHADER_IR_NATIVE) { 389 if (has_flag(debug::native)) 390 debug::log(id + ".asm", print_module_native(*mod, dev.ir_target())); 391 392 return build_module_native(*mod, dev.ir_target(), *c, r_log); 393 394 } else { 395 unreachable("Unsupported IR."); 396 } 397} 398