clover: Handle PIPE_SHADER_IR_NIR in switch
[mesa.git] / src / gallium / state_trackers / clover / llvm / invocation.cpp
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 "core/compiler.hpp"
24
25 #include <clang/Frontend/CompilerInstance.h>
26 #include <clang/Frontend/TextDiagnosticBuffer.h>
27 #include <clang/Frontend/TextDiagnosticPrinter.h>
28 #include <clang/CodeGen/CodeGenAction.h>
29 #include <clang/Basic/TargetInfo.h>
30 #include <llvm/Bitcode/BitstreamWriter.h>
31 #include <llvm/Bitcode/ReaderWriter.h>
32 #include <llvm/Linker/Linker.h>
33 #include <llvm/IR/DiagnosticInfo.h>
34 #include <llvm/IR/DiagnosticPrinter.h>
35 #include <llvm/IR/DerivedTypes.h>
36 #include <llvm/IR/LLVMContext.h>
37 #include <llvm/IR/Module.h>
38 #include <llvm/Support/SourceMgr.h>
39 #include <llvm/IRReader/IRReader.h>
40 #if HAVE_LLVM >= 0x0307
41 #include <llvm/IR/LegacyPassManager.h>
42 #else
43 #include <llvm/PassManager.h>
44 #endif
45 #include <llvm/Support/CodeGen.h>
46 #include <llvm/Support/TargetSelect.h>
47 #include <llvm/Support/MemoryBuffer.h>
48 #include <llvm/Support/FormattedStream.h>
49 #include <llvm/Support/TargetRegistry.h>
50 #include <llvm/Transforms/IPO.h>
51 #include <llvm/Transforms/IPO/PassManagerBuilder.h>
52 #include <llvm/Transforms/Utils/Cloning.h>
53
54
55 #include <llvm/IR/DataLayout.h>
56 #if HAVE_LLVM >= 0x0307
57 #include <llvm/Analysis/TargetLibraryInfo.h>
58 #else
59 #include <llvm/Target/TargetLibraryInfo.h>
60 #endif
61 #include <llvm/Target/TargetMachine.h>
62 #include <llvm/Target/TargetOptions.h>
63
64 #include <llvm-c/Target.h>
65 #include <llvm-c/TargetMachine.h>
66 #include <llvm-c/Core.h>
67
68 #include "pipe/p_state.h"
69 #include "util/u_memory.h"
70 #include "util/u_math.h"
71
72 #include <iostream>
73 #include <iomanip>
74 #include <fstream>
75 #include <cstdio>
76 #include <sstream>
77 #include <libelf.h>
78 #include <gelf.h>
79
80 using namespace clover;
81
82 namespace {
83 #if 0
84 void
85 build_binary(const std::string &source, const std::string &target,
86 const std::string &name) {
87 clang::CompilerInstance c;
88 clang::EmitObjAction act(&llvm::getGlobalContext());
89 std::string log;
90 llvm::raw_string_ostream s_log(log);
91
92 LLVMInitializeTGSITarget();
93 LLVMInitializeTGSITargetInfo();
94 LLVMInitializeTGSITargetMC();
95 LLVMInitializeTGSIAsmPrinter();
96
97 c.getFrontendOpts().Inputs.push_back(
98 std::make_pair(clang::IK_OpenCL, name));
99 c.getHeaderSearchOpts().UseBuiltinIncludes = false;
100 c.getHeaderSearchOpts().UseStandardIncludes = false;
101 c.getLangOpts().NoBuiltin = true;
102 c.getTargetOpts().Triple = target;
103 c.getInvocation().setLangDefaults(clang::IK_OpenCL);
104 c.createDiagnostics(0, NULL, new clang::TextDiagnosticPrinter(
105 s_log, c.getDiagnosticOpts()));
106
107 c.getPreprocessorOpts().addRemappedFile(
108 name, llvm::MemoryBuffer::getMemBuffer(source));
109
110 if (!c.ExecuteAction(act))
111 throw compile_error(log);
112 }
113
114 module
115 load_binary(const char *name) {
116 std::ifstream fs((name));
117 std::vector<unsigned char> str((std::istreambuf_iterator<char>(fs)),
118 (std::istreambuf_iterator<char>()));
119 compat::istream cs(str);
120 return module::deserialize(cs);
121 }
122 #endif
123 void debug_log(const std::string &msg, const std::string &suffix) {
124 const char *dbg_file = debug_get_option("CLOVER_DEBUG_FILE", "stderr");
125 if (!strcmp("stderr", dbg_file)) {
126 std::cerr << msg;
127 } else {
128 std::ofstream file(dbg_file + suffix, std::ios::app);
129 file << msg;
130 }
131 }
132
133 llvm::Module *
134 compile_llvm(llvm::LLVMContext &llvm_ctx, const std::string &source,
135 const header_map &headers,
136 const std::string &name, const std::string &triple,
137 const std::string &processor, const std::string &opts,
138 clang::LangAS::Map& address_spaces, unsigned &optimization_level,
139 std::string &r_log) {
140
141 clang::CompilerInstance c;
142 clang::EmitLLVMOnlyAction act(&llvm_ctx);
143 std::string log;
144 llvm::raw_string_ostream s_log(log);
145 std::string libclc_path = LIBCLC_LIBEXECDIR + processor + "-"
146 + triple + ".bc";
147
148 // Parse the compiler options:
149 std::vector<std::string> opts_array;
150 std::istringstream ss(opts);
151
152 while (!ss.eof()) {
153 std::string opt;
154 getline(ss, opt, ' ');
155 opts_array.push_back(opt);
156 }
157
158 opts_array.push_back(name);
159
160 std::vector<const char *> opts_carray;
161 for (unsigned i = 0; i < opts_array.size(); i++) {
162 opts_carray.push_back(opts_array.at(i).c_str());
163 }
164
165 llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> DiagID;
166 llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> DiagOpts;
167 clang::TextDiagnosticBuffer *DiagsBuffer;
168
169 DiagID = new clang::DiagnosticIDs();
170 DiagOpts = new clang::DiagnosticOptions();
171 DiagsBuffer = new clang::TextDiagnosticBuffer();
172
173 clang::DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer);
174 bool Success;
175
176 Success = clang::CompilerInvocation::CreateFromArgs(c.getInvocation(),
177 opts_carray.data(),
178 opts_carray.data() + opts_carray.size(),
179 Diags);
180 if (!Success) {
181 throw error(CL_INVALID_COMPILER_OPTIONS);
182 }
183 c.getFrontendOpts().ProgramAction = clang::frontend::EmitLLVMOnly;
184 c.getHeaderSearchOpts().UseBuiltinIncludes = true;
185 c.getHeaderSearchOpts().UseStandardSystemIncludes = true;
186 c.getHeaderSearchOpts().ResourceDir = CLANG_RESOURCE_DIR;
187
188 // Add libclc generic search path
189 c.getHeaderSearchOpts().AddPath(LIBCLC_INCLUDEDIR,
190 clang::frontend::Angled,
191 false, false
192 );
193
194 // Add libclc include
195 c.getPreprocessorOpts().Includes.push_back("clc/clc.h");
196
197 // clc.h requires that this macro be defined:
198 c.getPreprocessorOpts().addMacroDef("cl_clang_storage_class_specifiers");
199
200 c.getLangOpts().NoBuiltin = true;
201 c.getTargetOpts().Triple = triple;
202 c.getTargetOpts().CPU = processor;
203
204 // This is a workaround for a Clang bug which causes the number
205 // of warnings and errors to be printed to stderr.
206 // http://www.llvm.org/bugs/show_bug.cgi?id=19735
207 c.getDiagnosticOpts().ShowCarets = false;
208 c.getInvocation().setLangDefaults(c.getLangOpts(), clang::IK_OpenCL,
209 #if HAVE_LLVM >= 0x0309
210 llvm::Triple(triple),
211 #endif
212 clang::LangStandard::lang_opencl11);
213 c.createDiagnostics(
214 new clang::TextDiagnosticPrinter(
215 s_log,
216 &c.getDiagnosticOpts()));
217
218 #if HAVE_LLVM >= 0x0306
219 c.getPreprocessorOpts().addRemappedFile(name,
220 llvm::MemoryBuffer::getMemBuffer(source).release());
221 #else
222 c.getPreprocessorOpts().addRemappedFile(name,
223 llvm::MemoryBuffer::getMemBuffer(source));
224 #endif
225
226 if (headers.size()) {
227 const std::string tmp_header_path = "/tmp/clover/";
228
229 c.getHeaderSearchOpts().AddPath(tmp_header_path,
230 clang::frontend::Angled,
231 false, false
232 );
233
234 for (header_map::const_iterator it = headers.begin();
235 it != headers.end(); ++it) {
236 const std::string path = tmp_header_path + std::string(it->first);
237 c.getPreprocessorOpts().addRemappedFile(path,
238 #if HAVE_LLVM >= 0x0306
239 llvm::MemoryBuffer::getMemBuffer(it->second.c_str()).release());
240 #else
241 llvm::MemoryBuffer::getMemBuffer(it->second.c_str()));
242 #endif
243 }
244 }
245
246 // Setting this attribute tells clang to link this file before
247 // performing any optimizations. This is required so that
248 // we can replace calls to the OpenCL C barrier() builtin
249 // with calls to target intrinsics that have the noduplicate
250 // attribute. This attribute will prevent Clang from creating
251 // illegal uses of barrier() (e.g. Moving barrier() inside a conditional
252 // that is no executed by all threads) during its optimizaton passes.
253 #if HAVE_LLVM >= 0x0308
254 c.getCodeGenOpts().LinkBitcodeFiles.emplace_back(llvm::Linker::Flags::None,
255 libclc_path);
256 #else
257 c.getCodeGenOpts().LinkBitcodeFile = libclc_path;
258 #endif
259 optimization_level = c.getCodeGenOpts().OptimizationLevel;
260
261 // Compile the code
262 bool ExecSuccess = c.ExecuteAction(act);
263 r_log = log;
264
265 if (!ExecSuccess)
266 throw compile_error();
267
268 // Get address spaces map to be able to find kernel argument address space
269 memcpy(address_spaces, c.getTarget().getAddressSpaceMap(),
270 sizeof(address_spaces));
271
272 #if HAVE_LLVM >= 0x0306
273 return act.takeModule().release();
274 #else
275 return act.takeModule();
276 #endif
277 }
278
279 std::vector<llvm::Function *>
280 find_kernels(const llvm::Module *mod) {
281 const llvm::NamedMDNode *kernel_node =
282 mod->getNamedMetadata("opencl.kernels");
283 // This means there are no kernels in the program. The spec does not
284 // require that we return an error here, but there will be an error if
285 // the user tries to pass this program to a clCreateKernel() call.
286 if (!kernel_node) {
287 return std::vector<llvm::Function *>();
288 }
289
290 std::vector<llvm::Function *> kernels;
291 kernels.reserve(kernel_node->getNumOperands());
292 for (unsigned i = 0; i < kernel_node->getNumOperands(); ++i) {
293 #if HAVE_LLVM >= 0x0306
294 kernels.push_back(llvm::mdconst::dyn_extract<llvm::Function>(
295 #else
296 kernels.push_back(llvm::dyn_cast<llvm::Function>(
297 #endif
298 kernel_node->getOperand(i)->getOperand(0)));
299 }
300 return kernels;
301 }
302
303 void
304 optimize(llvm::Module *mod, unsigned optimization_level) {
305
306 #if HAVE_LLVM >= 0x0307
307 llvm::legacy::PassManager PM;
308 #else
309 llvm::PassManager PM;
310 #endif
311
312 const std::vector<llvm::Function *> kernels = find_kernels(mod);
313
314 // Add a function internalizer pass.
315 //
316 // By default, the function internalizer pass will look for a function
317 // called "main" and then mark all other functions as internal. Marking
318 // functions as internal enables the optimizer to perform optimizations
319 // like function inlining and global dead-code elimination.
320 //
321 // When there is no "main" function in a module, the internalize pass will
322 // treat the module like a library, and it won't internalize any functions.
323 // Since there is no "main" function in our kernels, we need to tell
324 // the internalizer pass that this module is not a library by passing a
325 // list of kernel functions to the internalizer. The internalizer will
326 // treat the functions in the list as "main" functions and internalize
327 // all of the other functions.
328 #if HAVE_LLVM >= 0x0309
329 auto preserve_kernels = [=](const llvm::GlobalValue &GV) {
330 for (const auto &kernel : kernels) {
331 if (GV.getName() == kernel->getName())
332 return true;
333 }
334 return false;
335 };
336 #else
337 std::vector<const char*> export_list;
338 for (std::vector<llvm::Function *>::const_iterator I = kernels.begin(),
339 E = kernels.end();
340 I != E; ++I) {
341 llvm::Function *kernel = *I;
342 export_list.push_back(kernel->getName().data());
343 }
344 #endif
345 #if HAVE_LLVM < 0x0306
346 PM.add(new llvm::DataLayoutPass(mod));
347 #elif HAVE_LLVM < 0x0307
348 PM.add(new llvm::DataLayoutPass());
349 #endif
350 #if HAVE_LLVM >= 0x0309
351 PM.add(llvm::createInternalizePass(preserve_kernels));
352 #else
353 PM.add(llvm::createInternalizePass(export_list));
354 #endif
355
356 llvm::PassManagerBuilder PMB;
357 PMB.OptLevel = optimization_level;
358 #if HAVE_LLVM < 0x0307
359 PMB.LibraryInfo = new llvm::TargetLibraryInfo(
360 #else
361 PMB.LibraryInfo = new llvm::TargetLibraryInfoImpl(
362 #endif
363 llvm::Triple(mod->getTargetTriple()));
364 PMB.populateModulePassManager(PM);
365 PM.run(*mod);
366 }
367
368 // Kernel metadata
369
370 struct kernel_arg_md {
371 llvm::StringRef type_name;
372 llvm::StringRef access_qual;
373 kernel_arg_md(llvm::StringRef type_name_, llvm::StringRef access_qual_):
374 type_name(type_name_), access_qual(access_qual_) {}
375 };
376
377 #if HAVE_LLVM >= 0x0306
378
379 const llvm::MDNode *
380 get_kernel_metadata(const llvm::Function *kernel_func) {
381 auto mod = kernel_func->getParent();
382 auto kernels_node = mod->getNamedMetadata("opencl.kernels");
383 if (!kernels_node) {
384 return nullptr;
385 }
386
387 const llvm::MDNode *kernel_node = nullptr;
388 for (unsigned i = 0; i < kernels_node->getNumOperands(); ++i) {
389 auto func = llvm::mdconst::dyn_extract<llvm::Function>(
390 kernels_node->getOperand(i)->getOperand(0));
391 if (func == kernel_func) {
392 kernel_node = kernels_node->getOperand(i);
393 break;
394 }
395 }
396
397 return kernel_node;
398 }
399
400 llvm::MDNode*
401 node_from_op_checked(const llvm::MDOperand &md_operand,
402 llvm::StringRef expect_name,
403 unsigned expect_num_args)
404 {
405 auto node = llvm::cast<llvm::MDNode>(md_operand);
406 assert(node->getNumOperands() == expect_num_args &&
407 "Wrong number of operands.");
408
409 auto str_node = llvm::cast<llvm::MDString>(node->getOperand(0));
410 assert(str_node->getString() == expect_name &&
411 "Wrong metadata node name.");
412
413 return node;
414 }
415
416 std::vector<kernel_arg_md>
417 get_kernel_arg_md(const llvm::Function *kernel_func) {
418 auto num_args = kernel_func->getArgumentList().size();
419
420 auto kernel_node = get_kernel_metadata(kernel_func);
421 auto aq = node_from_op_checked(kernel_node->getOperand(2),
422 "kernel_arg_access_qual", num_args + 1);
423 auto ty = node_from_op_checked(kernel_node->getOperand(3),
424 "kernel_arg_type", num_args + 1);
425
426 std::vector<kernel_arg_md> res;
427 res.reserve(num_args);
428 for (unsigned i = 0; i < num_args; ++i) {
429 res.push_back(kernel_arg_md(
430 llvm::cast<llvm::MDString>(ty->getOperand(i+1))->getString(),
431 llvm::cast<llvm::MDString>(aq->getOperand(i+1))->getString()));
432 }
433
434 return res;
435 }
436
437 #else
438
439 std::vector<kernel_arg_md>
440 get_kernel_arg_md(const llvm::Function *kernel_func) {
441 return std::vector<kernel_arg_md>(
442 kernel_func->getArgumentList().size(),
443 kernel_arg_md("", ""));
444 }
445
446 #endif // HAVE_LLVM >= 0x0306
447
448 std::vector<module::argument>
449 get_kernel_args(const llvm::Module *mod, const std::string &kernel_name,
450 const clang::LangAS::Map &address_spaces) {
451
452 std::vector<module::argument> args;
453 llvm::Function *kernel_func = mod->getFunction(kernel_name);
454 assert(kernel_func && "Kernel name not found in module.");
455 auto arg_md = get_kernel_arg_md(kernel_func);
456
457 llvm::DataLayout TD(mod);
458 llvm::Type *size_type =
459 TD.getSmallestLegalIntType(mod->getContext(), sizeof(cl_uint) * 8);
460
461 for (const auto &arg: kernel_func->args()) {
462
463 llvm::Type *arg_type = arg.getType();
464 const unsigned arg_store_size = TD.getTypeStoreSize(arg_type);
465
466 // OpenCL 1.2 specification, Ch. 6.1.5: "A built-in data
467 // type that is not a power of two bytes in size must be
468 // aligned to the next larger power of two". We need this
469 // alignment for three element vectors, which have
470 // non-power-of-2 store size.
471 const unsigned arg_api_size = util_next_power_of_two(arg_store_size);
472
473 llvm::Type *target_type = arg_type->isIntegerTy() ?
474 TD.getSmallestLegalIntType(mod->getContext(), arg_store_size * 8)
475 : arg_type;
476 unsigned target_size = TD.getTypeStoreSize(target_type);
477 unsigned target_align = TD.getABITypeAlignment(target_type);
478
479 llvm::StringRef type_name = arg_md[arg.getArgNo()].type_name;
480 llvm::StringRef access_qual = arg_md[arg.getArgNo()].access_qual;
481
482 // Image
483 const bool is_image2d = type_name == "image2d_t";
484 const bool is_image3d = type_name == "image3d_t";
485 if (is_image2d || is_image3d) {
486 const bool is_write_only = access_qual == "write_only";
487 const bool is_read_only = access_qual == "read_only";
488
489 enum module::argument::type marg_type;
490 if (is_image2d && is_read_only) {
491 marg_type = module::argument::image2d_rd;
492 } else if (is_image2d && is_write_only) {
493 marg_type = module::argument::image2d_wr;
494 } else if (is_image3d && is_read_only) {
495 marg_type = module::argument::image3d_rd;
496 } else if (is_image3d && is_write_only) {
497 marg_type = module::argument::image3d_wr;
498 } else {
499 assert(0 && "Wrong image access qualifier");
500 }
501
502 args.push_back(module::argument(marg_type,
503 arg_store_size, target_size,
504 target_align,
505 module::argument::zero_ext));
506 continue;
507 }
508
509 // Image size implicit argument
510 if (type_name == "__llvm_image_size") {
511 args.push_back(module::argument(module::argument::scalar,
512 sizeof(cl_uint),
513 TD.getTypeStoreSize(size_type),
514 TD.getABITypeAlignment(size_type),
515 module::argument::zero_ext,
516 module::argument::image_size));
517 continue;
518 }
519
520 // Image format implicit argument
521 if (type_name == "__llvm_image_format") {
522 args.push_back(module::argument(module::argument::scalar,
523 sizeof(cl_uint),
524 TD.getTypeStoreSize(size_type),
525 TD.getABITypeAlignment(size_type),
526 module::argument::zero_ext,
527 module::argument::image_format));
528 continue;
529 }
530
531 // Other types
532 if (llvm::isa<llvm::PointerType>(arg_type) && arg.hasByValAttr()) {
533 arg_type =
534 llvm::dyn_cast<llvm::PointerType>(arg_type)->getElementType();
535 }
536
537 if (arg_type->isPointerTy()) {
538 unsigned address_space = llvm::cast<llvm::PointerType>(arg_type)->getAddressSpace();
539 if (address_space == address_spaces[clang::LangAS::opencl_local
540 - clang::LangAS::Offset]) {
541 args.push_back(module::argument(module::argument::local,
542 arg_api_size, target_size,
543 target_align,
544 module::argument::zero_ext));
545 } else {
546 // XXX: Correctly handle constant address space. There is no
547 // way for r600g to pass a handle for constant buffers back
548 // to clover like it can for global buffers, so
549 // creating constant arguments will break r600g. For now,
550 // continue treating constant buffers as global buffers
551 // until we can come up with a way to create handles for
552 // constant buffers.
553 args.push_back(module::argument(module::argument::global,
554 arg_api_size, target_size,
555 target_align,
556 module::argument::zero_ext));
557 }
558
559 } else {
560 llvm::AttributeSet attrs = kernel_func->getAttributes();
561 enum module::argument::ext_type ext_type =
562 (attrs.hasAttribute(arg.getArgNo() + 1,
563 llvm::Attribute::SExt) ?
564 module::argument::sign_ext :
565 module::argument::zero_ext);
566
567 args.push_back(
568 module::argument(module::argument::scalar, arg_api_size,
569 target_size, target_align, ext_type));
570 }
571 }
572
573 // Append implicit arguments. XXX - The types, ordering and
574 // vector size of the implicit arguments should depend on the
575 // target according to the selected calling convention.
576 args.push_back(
577 module::argument(module::argument::scalar, sizeof(cl_uint),
578 TD.getTypeStoreSize(size_type),
579 TD.getABITypeAlignment(size_type),
580 module::argument::zero_ext,
581 module::argument::grid_dimension));
582
583 args.push_back(
584 module::argument(module::argument::scalar, sizeof(cl_uint),
585 TD.getTypeStoreSize(size_type),
586 TD.getABITypeAlignment(size_type),
587 module::argument::zero_ext,
588 module::argument::grid_offset));
589
590 return args;
591 }
592
593 module
594 build_module_llvm(llvm::Module *mod,
595 clang::LangAS::Map& address_spaces) {
596
597 module m;
598 struct pipe_llvm_program_header header;
599
600 llvm::SmallVector<char, 1024> llvm_bitcode;
601 llvm::raw_svector_ostream bitcode_ostream(llvm_bitcode);
602 llvm::BitstreamWriter writer(llvm_bitcode);
603 llvm::WriteBitcodeToFile(mod, bitcode_ostream);
604 #if HAVE_LLVM < 0x0308
605 bitcode_ostream.flush();
606 #endif
607
608 const std::vector<llvm::Function *> kernels = find_kernels(mod);
609 for (unsigned i = 0; i < kernels.size(); ++i) {
610 std::string kernel_name = kernels[i]->getName();
611 std::vector<module::argument> args =
612 get_kernel_args(mod, kernel_name, address_spaces);
613
614 m.syms.push_back(module::symbol(kernel_name, 0, i, args ));
615 }
616
617 header.num_bytes = llvm_bitcode.size();
618 std::vector<char> data;
619 data.insert(data.end(), (char*)(&header),
620 (char*)(&header) + sizeof(header));
621 data.insert(data.end(), llvm_bitcode.begin(),
622 llvm_bitcode.end());
623 m.secs.push_back(module::section(0, module::section::text,
624 header.num_bytes, data));
625
626 return m;
627 }
628
629 void
630 emit_code(LLVMTargetMachineRef tm, LLVMModuleRef mod,
631 LLVMCodeGenFileType file_type,
632 LLVMMemoryBufferRef *out_buffer,
633 std::string &r_log) {
634 LLVMBool err;
635 char *err_message = NULL;
636
637 err = LLVMTargetMachineEmitToMemoryBuffer(tm, mod, file_type,
638 &err_message, out_buffer);
639
640 if (err) {
641 r_log = std::string(err_message);
642 }
643
644 LLVMDisposeMessage(err_message);
645
646 if (err) {
647 throw compile_error();
648 }
649 }
650
651 std::vector<char>
652 compile_native(const llvm::Module *mod, const std::string &triple,
653 const std::string &processor, unsigned dump_asm,
654 std::string &r_log) {
655
656 std::string log;
657 LLVMTargetRef target;
658 char *error_message;
659 LLVMMemoryBufferRef out_buffer;
660 unsigned buffer_size;
661 const char *buffer_data;
662 LLVMModuleRef mod_ref = wrap(mod);
663
664 if (LLVMGetTargetFromTriple(triple.c_str(), &target, &error_message)) {
665 r_log = std::string(error_message);
666 LLVMDisposeMessage(error_message);
667 throw compile_error();
668 }
669
670 LLVMTargetMachineRef tm = LLVMCreateTargetMachine(
671 target, triple.c_str(), processor.c_str(), "",
672 LLVMCodeGenLevelDefault, LLVMRelocDefault, LLVMCodeModelDefault);
673
674 if (!tm) {
675 r_log = "Could not create TargetMachine: " + triple;
676 throw compile_error();
677 }
678
679 if (dump_asm) {
680 LLVMSetTargetMachineAsmVerbosity(tm, true);
681 #if HAVE_LLVM >= 0x0308
682 LLVMModuleRef debug_mod = wrap(llvm::CloneModule(mod).release());
683 #else
684 LLVMModuleRef debug_mod = wrap(llvm::CloneModule(mod));
685 #endif
686 emit_code(tm, debug_mod, LLVMAssemblyFile, &out_buffer, r_log);
687 buffer_size = LLVMGetBufferSize(out_buffer);
688 buffer_data = LLVMGetBufferStart(out_buffer);
689 debug_log(std::string(buffer_data, buffer_size), ".asm");
690
691 LLVMSetTargetMachineAsmVerbosity(tm, false);
692 LLVMDisposeMemoryBuffer(out_buffer);
693 LLVMDisposeModule(debug_mod);
694 }
695
696 emit_code(tm, mod_ref, LLVMObjectFile, &out_buffer, r_log);
697
698 buffer_size = LLVMGetBufferSize(out_buffer);
699 buffer_data = LLVMGetBufferStart(out_buffer);
700
701 std::vector<char> code(buffer_data, buffer_data + buffer_size);
702
703 LLVMDisposeMemoryBuffer(out_buffer);
704 LLVMDisposeTargetMachine(tm);
705
706 return code;
707 }
708
709 std::map<std::string, unsigned>
710 get_kernel_offsets(std::vector<char> &code,
711 const std::vector<llvm::Function *> &kernels,
712 std::string &r_log) {
713
714 // One of the libelf implementations
715 // (http://www.mr511.de/software/english.htm) requires calling
716 // elf_version() before elf_memory().
717 //
718 elf_version(EV_CURRENT);
719
720 Elf *elf = elf_memory(&code[0], code.size());
721 size_t section_str_index;
722 elf_getshdrstrndx(elf, &section_str_index);
723 Elf_Scn *section = NULL;
724 Elf_Scn *symtab = NULL;
725 GElf_Shdr symtab_header;
726
727 // Find the symbol table
728 try {
729 while ((section = elf_nextscn(elf, section))) {
730 const char *name;
731 if (gelf_getshdr(section, &symtab_header) != &symtab_header) {
732 r_log = "Failed to read ELF section header.";
733 throw compile_error();
734 }
735 name = elf_strptr(elf, section_str_index, symtab_header.sh_name);
736 if (!strcmp(name, ".symtab")) {
737 symtab = section;
738 break;
739 }
740 }
741 if (!symtab) {
742 r_log = "Unable to find symbol table.";
743 throw compile_error();
744 }
745 } catch (compile_error &e) {
746 elf_end(elf);
747 throw e;
748 }
749
750
751 // Extract symbol information from the table
752 Elf_Data *symtab_data = NULL;
753 GElf_Sym *symbol;
754 GElf_Sym s;
755
756 std::map<std::string, unsigned> kernel_offsets;
757 symtab_data = elf_getdata(symtab, symtab_data);
758
759 // Determine the offsets for each kernel
760 for (int i = 0; (symbol = gelf_getsym(symtab_data, i, &s)); i++) {
761 char *name = elf_strptr(elf, symtab_header.sh_link, symbol->st_name);
762 for (std::vector<llvm::Function*>::const_iterator it = kernels.begin(),
763 e = kernels.end(); it != e; ++it) {
764 llvm::Function *f = *it;
765 if (f->getName() == std::string(name))
766 kernel_offsets[f->getName()] = symbol->st_value;
767 }
768 }
769 elf_end(elf);
770 return kernel_offsets;
771 }
772
773 module
774 build_module_native(std::vector<char> &code,
775 const llvm::Module *mod,
776 const clang::LangAS::Map &address_spaces,
777 std::string &r_log) {
778
779 const std::vector<llvm::Function *> kernels = find_kernels(mod);
780
781 std::map<std::string, unsigned> kernel_offsets =
782 get_kernel_offsets(code, kernels, r_log);
783
784 // Begin building the clover module
785 module m;
786 struct pipe_llvm_program_header header;
787
788 // Store the generated ELF binary in the module's text section.
789 header.num_bytes = code.size();
790 std::vector<char> data;
791 data.insert(data.end(), (char*)(&header),
792 (char*)(&header) + sizeof(header));
793 data.insert(data.end(), code.begin(), code.end());
794 m.secs.push_back(module::section(0, module::section::text,
795 header.num_bytes, data));
796
797 for (std::map<std::string, unsigned>::iterator i = kernel_offsets.begin(),
798 e = kernel_offsets.end(); i != e; ++i) {
799 std::vector<module::argument> args =
800 get_kernel_args(mod, i->first, address_spaces);
801 m.syms.push_back(module::symbol(i->first, 0, i->second, args ));
802 }
803
804 return m;
805 }
806
807 void
808 diagnostic_handler(const llvm::DiagnosticInfo &di, void *data) {
809 if (di.getSeverity() == llvm::DS_Error) {
810 std::string message = *(std::string*)data;
811 llvm::raw_string_ostream stream(message);
812 llvm::DiagnosticPrinterRawOStream dp(stream);
813 di.print(dp);
814 stream.flush();
815 *(std::string*)data = message;
816
817 throw compile_error();
818 }
819 }
820
821 void
822 init_targets() {
823 static bool targets_initialized = false;
824 if (!targets_initialized) {
825 LLVMInitializeAllTargets();
826 LLVMInitializeAllTargetInfos();
827 LLVMInitializeAllTargetMCs();
828 LLVMInitializeAllAsmPrinters();
829 targets_initialized = true;
830 }
831 }
832
833 #define DBG_CLC (1 << 0)
834 #define DBG_LLVM (1 << 1)
835 #define DBG_ASM (1 << 2)
836
837 unsigned
838 get_debug_flags() {
839 static const struct debug_named_value debug_options[] = {
840 {"clc", DBG_CLC, "Dump the OpenCL C code for all kernels."},
841 {"llvm", DBG_LLVM, "Dump the generated LLVM IR for all kernels."},
842 {"asm", DBG_ASM, "Dump kernel assembly code for targets specifying "
843 "PIPE_SHADER_IR_NATIVE"},
844 DEBUG_NAMED_VALUE_END // must be last
845 };
846 static const unsigned debug_flags =
847 debug_get_flags_option("CLOVER_DEBUG", debug_options, 0);
848
849 return debug_flags;
850 }
851
852 } // End anonymous namespace
853
854 module
855 clover::compile_program_llvm(const std::string &source,
856 const header_map &headers,
857 enum pipe_shader_ir ir,
858 const std::string &target,
859 const std::string &opts,
860 std::string &r_log) {
861
862 init_targets();
863
864 size_t processor_str_len = std::string(target).find_first_of("-");
865 std::string processor(target, 0, processor_str_len);
866 std::string triple(target, processor_str_len + 1,
867 target.size() - processor_str_len - 1);
868 clang::LangAS::Map address_spaces;
869 llvm::LLVMContext llvm_ctx;
870 unsigned optimization_level;
871
872 llvm_ctx.setDiagnosticHandler(diagnostic_handler, &r_log);
873
874 if (get_debug_flags() & DBG_CLC)
875 debug_log("// Build options: " + opts + '\n' + source, ".cl");
876
877 // The input file name must have the .cl extension in order for the
878 // CompilerInvocation class to recognize it as an OpenCL source file.
879 llvm::Module *mod = compile_llvm(llvm_ctx, source, headers, "input.cl",
880 triple, processor, opts, address_spaces,
881 optimization_level, r_log);
882
883 optimize(mod, optimization_level);
884
885 if (get_debug_flags() & DBG_LLVM) {
886 std::string log;
887 llvm::raw_string_ostream s_log(log);
888 mod->print(s_log, NULL);
889 s_log.flush();
890 debug_log(log, ".ll");
891 }
892
893 module m;
894 // Build the clover::module
895 switch (ir) {
896 case PIPE_SHADER_IR_NIR:
897 case PIPE_SHADER_IR_TGSI:
898 //XXX: Handle TGSI, NIR
899 assert(0);
900 m = module();
901 break;
902 case PIPE_SHADER_IR_LLVM:
903 m = build_module_llvm(mod, address_spaces);
904 break;
905 case PIPE_SHADER_IR_NATIVE: {
906 std::vector<char> code = compile_native(mod, triple, processor,
907 get_debug_flags() & DBG_ASM,
908 r_log);
909 m = build_module_native(code, mod, address_spaces, r_log);
910 break;
911 }
912 }
913 #if HAVE_LLVM >= 0x0306
914 // LLVM 3.6 and newer, the user takes ownership of the module.
915 delete mod;
916 #endif
917
918 return m;
919 }