clover/llvm: Split native codegen into separate file.
[mesa.git] / src / gallium / state_trackers / clover / llvm / invocation.cpp
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/codegen.hpp"
28 #include "llvm/compat.hpp"
29 #include "llvm/metadata.hpp"
30 #include "llvm/util.hpp"
31 #include "core/compiler.hpp"
32 #include "util/algorithm.hpp"
33
34 #include <clang/Frontend/CompilerInstance.h>
35 #include <clang/Frontend/TextDiagnosticBuffer.h>
36 #include <clang/Frontend/TextDiagnosticPrinter.h>
37 #include <clang/CodeGen/CodeGenAction.h>
38 #include <clang/Basic/TargetInfo.h>
39 #include <llvm/Bitcode/BitstreamWriter.h>
40 #include <llvm/Bitcode/ReaderWriter.h>
41 #include <llvm/Linker/Linker.h>
42 #include <llvm/IR/DiagnosticInfo.h>
43 #include <llvm/IR/DiagnosticPrinter.h>
44 #include <llvm/IR/DerivedTypes.h>
45 #include <llvm/IR/LLVMContext.h>
46 #include <llvm/IR/Module.h>
47 #include <llvm/Support/SourceMgr.h>
48 #include <llvm/IRReader/IRReader.h>
49 #include <llvm/Support/CodeGen.h>
50 #include <llvm/Support/TargetSelect.h>
51 #include <llvm/Support/MemoryBuffer.h>
52 #include <llvm/Support/FormattedStream.h>
53 #include <llvm/Support/TargetRegistry.h>
54 #include <llvm/Transforms/IPO.h>
55 #include <llvm/Transforms/IPO/PassManagerBuilder.h>
56 #include <llvm/Transforms/Utils/Cloning.h>
57
58
59 #include <llvm/IR/DataLayout.h>
60 #include <llvm/Target/TargetMachine.h>
61 #include <llvm/Target/TargetOptions.h>
62
63 #include <llvm-c/Target.h>
64 #include <llvm-c/TargetMachine.h>
65 #include <llvm-c/Core.h>
66
67 #include "pipe/p_state.h"
68 #include "util/u_memory.h"
69 #include "util/u_math.h"
70
71 #include <iostream>
72 #include <iomanip>
73 #include <fstream>
74 #include <cstdio>
75 #include <sstream>
76 #include <libelf.h>
77 #include <gelf.h>
78
79 using namespace clover;
80 using namespace clover::llvm;
81
82 using ::llvm::cast;
83 using ::llvm::dyn_cast;
84 using ::llvm::Function;
85 using ::llvm::isa;
86 using ::llvm::LLVMContext;
87 using ::llvm::Module;
88 using ::llvm::raw_string_ostream;
89 using ::llvm::TargetMachine;
90
91 namespace {
92 // XXX - Temporary hack to avoid breaking the build for the moment, will
93 // get rid of this later.
94 namespace llvm {
95 using namespace ::llvm;
96 }
97
98 void
99 init_targets() {
100 static bool targets_initialized = false;
101 if (!targets_initialized) {
102 LLVMInitializeAllTargets();
103 LLVMInitializeAllTargetInfos();
104 LLVMInitializeAllTargetMCs();
105 LLVMInitializeAllAsmPrinters();
106 targets_initialized = true;
107 }
108 }
109
110 void
111 diagnostic_handler(const ::llvm::DiagnosticInfo &di, void *data) {
112 if (di.getSeverity() == ::llvm::DS_Error) {
113 raw_string_ostream os { *reinterpret_cast<std::string *>(data) };
114 ::llvm::DiagnosticPrinterRawOStream printer { os };
115 di.print(printer);
116 throw compile_error();
117 }
118 }
119
120 std::unique_ptr<LLVMContext>
121 create_context(std::string &r_log) {
122 init_targets();
123 std::unique_ptr<LLVMContext> ctx { new LLVMContext };
124 ctx->setDiagnosticHandler(diagnostic_handler, &r_log);
125 return ctx;
126 }
127
128 std::unique_ptr<clang::CompilerInstance>
129 create_compiler_instance(const target &target,
130 const std::vector<std::string> &opts,
131 std::string &r_log) {
132 std::unique_ptr<clang::CompilerInstance> c { new clang::CompilerInstance };
133 clang::DiagnosticsEngine diag { new clang::DiagnosticIDs,
134 new clang::DiagnosticOptions, new clang::TextDiagnosticBuffer };
135
136 // Parse the compiler options. A file name should be present at the end
137 // and must have the .cl extension in order for the CompilerInvocation
138 // class to recognize it as an OpenCL source file.
139 const std::vector<const char *> copts =
140 map(std::mem_fn(&std::string::c_str), opts);
141
142 if (!clang::CompilerInvocation::CreateFromArgs(
143 c->getInvocation(), copts.data(), copts.data() + copts.size(), diag))
144 throw error(CL_INVALID_COMPILER_OPTIONS);
145
146 c->getTargetOpts().CPU = target.cpu;
147 c->getTargetOpts().Triple = target.triple;
148 c->getLangOpts().NoBuiltin = true;
149
150 // This is a workaround for a Clang bug which causes the number
151 // of warnings and errors to be printed to stderr.
152 // http://www.llvm.org/bugs/show_bug.cgi?id=19735
153 c->getDiagnosticOpts().ShowCarets = false;
154
155 compat::set_lang_defaults(c->getInvocation(), c->getLangOpts(),
156 clang::IK_OpenCL, ::llvm::Triple(target.triple),
157 c->getPreprocessorOpts(),
158 clang::LangStandard::lang_opencl11);
159
160 c->createDiagnostics(new clang::TextDiagnosticPrinter(
161 *new raw_string_ostream(r_log),
162 &c->getDiagnosticOpts(), true));
163
164 c->setTarget(clang::TargetInfo::CreateTargetInfo(
165 c->getDiagnostics(), c->getInvocation().TargetOpts));
166
167 return c;
168 }
169
170 std::unique_ptr<Module>
171 compile(LLVMContext &ctx, clang::CompilerInstance &c,
172 const std::string &name, const std::string &source,
173 const header_map &headers, const std::string &target,
174 const std::string &opts, std::string &r_log) {
175 c.getFrontendOpts().ProgramAction = clang::frontend::EmitLLVMOnly;
176 c.getHeaderSearchOpts().UseBuiltinIncludes = true;
177 c.getHeaderSearchOpts().UseStandardSystemIncludes = true;
178 c.getHeaderSearchOpts().ResourceDir = CLANG_RESOURCE_DIR;
179
180 // Add libclc generic search path
181 c.getHeaderSearchOpts().AddPath(LIBCLC_INCLUDEDIR,
182 clang::frontend::Angled,
183 false, false);
184
185 // Add libclc include
186 c.getPreprocessorOpts().Includes.push_back("clc/clc.h");
187
188 // clc.h requires that this macro be defined:
189 c.getPreprocessorOpts().addMacroDef("cl_clang_storage_class_specifiers");
190 c.getPreprocessorOpts().addRemappedFile(
191 name, ::llvm::MemoryBuffer::getMemBuffer(source).release());
192
193 if (headers.size()) {
194 const std::string tmp_header_path = "/tmp/clover/";
195
196 c.getHeaderSearchOpts().AddPath(tmp_header_path,
197 clang::frontend::Angled,
198 false, false);
199
200 for (const auto &header : headers)
201 c.getPreprocessorOpts().addRemappedFile(
202 tmp_header_path + header.first,
203 ::llvm::MemoryBuffer::getMemBuffer(header.second).release());
204 }
205
206 // Tell clang to link this file before performing any
207 // optimizations. This is required so that we can replace calls
208 // to the OpenCL C barrier() builtin with calls to target
209 // intrinsics that have the noduplicate attribute. This
210 // attribute will prevent Clang from creating illegal uses of
211 // barrier() (e.g. Moving barrier() inside a conditional that is
212 // no executed by all threads) during its optimizaton passes.
213 compat::add_link_bitcode_file(c.getCodeGenOpts(),
214 LIBCLC_LIBEXECDIR + target + ".bc");
215
216 // Compile the code
217 clang::EmitLLVMOnlyAction act(&ctx);
218 if (!c.ExecuteAction(act))
219 throw compile_error();
220
221 return act.takeModule();
222 }
223
224 void
225 optimize(Module &mod, unsigned optimization_level) {
226 compat::pass_manager pm;
227
228 compat::add_data_layout_pass(pm);
229
230 // By default, the function internalizer pass will look for a function
231 // called "main" and then mark all other functions as internal. Marking
232 // functions as internal enables the optimizer to perform optimizations
233 // like function inlining and global dead-code elimination.
234 //
235 // When there is no "main" function in a module, the internalize pass will
236 // treat the module like a library, and it won't internalize any functions.
237 // Since there is no "main" function in our kernels, we need to tell
238 // the internalizer pass that this module is not a library by passing a
239 // list of kernel functions to the internalizer. The internalizer will
240 // treat the functions in the list as "main" functions and internalize
241 // all of the other functions.
242 compat::add_internalize_pass(pm, map(std::mem_fn(&Function::getName),
243 get_kernels(mod)));
244
245 ::llvm::PassManagerBuilder pmb;
246 pmb.OptLevel = optimization_level;
247 pmb.LibraryInfo = new compat::target_library_info(
248 ::llvm::Triple(mod.getTargetTriple()));
249 pmb.populateModulePassManager(pm);
250 pm.run(mod);
251 }
252 } // End anonymous namespace
253
254 module
255 clover::compile_program_llvm(const std::string &source,
256 const header_map &headers,
257 enum pipe_shader_ir ir,
258 const std::string &target,
259 const std::string &opts,
260 std::string &r_log) {
261 if (has_flag(debug::clc))
262 debug::log(".cl", "// Build options: " + opts + '\n' + source);
263
264 auto ctx = create_context(r_log);
265 // The input file name must have the .cl extension in order for the
266 // CompilerInvocation class to recognize it as an OpenCL source file.
267 const auto c = create_compiler_instance(target, tokenize(opts + " input.cl"),
268 r_log);
269 auto mod = compile(*ctx, *c, "input.cl", source, headers, target, opts, r_log);
270
271 optimize(*mod, c->getCodeGenOpts().OptimizationLevel);
272
273 if (has_flag(debug::llvm))
274 debug::log(".ll", print_module_bitcode(*mod));
275
276 module m;
277 // Build the clover::module
278 switch (ir) {
279 case PIPE_SHADER_IR_NIR:
280 case PIPE_SHADER_IR_TGSI:
281 //XXX: Handle TGSI, NIR
282 assert(0);
283 m = module();
284 break;
285 case PIPE_SHADER_IR_LLVM:
286 m = build_module_bitcode(*mod, *c);
287 break;
288 case PIPE_SHADER_IR_NATIVE:
289 if (has_flag(debug::native))
290 debug::log(".asm", print_module_native(*mod, target));
291
292 m = build_module_native(*mod, target, *c, r_log);
293 break;
294 }
295
296 return m;
297 }