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