65c02d886d44264a03fe92b09709ef3e422a5f50
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_misc.cpp
1 /**************************************************************************
2 *
3 * Copyright 2010 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
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 NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 *
26 **************************************************************************/
27
28
29 /**
30 * The purpose of this module is to expose LLVM functionality not available
31 * through the C++ bindings.
32 */
33
34
35 #ifndef __STDC_LIMIT_MACROS
36 #define __STDC_LIMIT_MACROS
37 #endif
38
39 #ifndef __STDC_CONSTANT_MACROS
40 #define __STDC_CONSTANT_MACROS
41 #endif
42
43 // Undef these vars just to silence warnings
44 #undef PACKAGE_BUGREPORT
45 #undef PACKAGE_NAME
46 #undef PACKAGE_STRING
47 #undef PACKAGE_TARNAME
48 #undef PACKAGE_VERSION
49
50
51 #include <stddef.h>
52
53 #include <llvm-c/Core.h>
54 #include <llvm-c/ExecutionEngine.h>
55 #include <llvm/Target/TargetOptions.h>
56 #include <llvm/ExecutionEngine/ExecutionEngine.h>
57 #if HAVE_LLVM >= 0x0301
58 #include <llvm/ADT/Triple.h>
59 #include <llvm/ExecutionEngine/JITMemoryManager.h>
60 #endif
61 #include <llvm/Support/CommandLine.h>
62 #include <llvm/Support/PrettyStackTrace.h>
63
64 #if HAVE_LLVM >= 0x0300
65 #include <llvm/Support/TargetSelect.h>
66 #else /* HAVE_LLVM < 0x0300 */
67 #include <llvm/Target/TargetSelect.h>
68 #endif /* HAVE_LLVM < 0x0300 */
69
70 #if HAVE_LLVM >= 0x0303
71 #include <llvm/IR/IRBuilder.h>
72 #include <llvm/IR/Module.h>
73 #include <llvm/Support/CBindingWrapping.h>
74 #endif
75
76 #include "pipe/p_config.h"
77 #include "util/u_debug.h"
78 #include "util/u_cpu_detect.h"
79
80 #include "lp_bld_misc.h"
81
82 namespace {
83
84 class LLVMEnsureMultithreaded {
85 public:
86 LLVMEnsureMultithreaded()
87 {
88 #if HAVE_LLVM < 0x0303
89 if (!llvm::llvm_is_multithreaded()) {
90 llvm::llvm_start_multithreaded();
91 }
92 #else
93 if (!LLVMIsMultithreaded()) {
94 LLVMStartMultithreaded();
95 }
96 #endif
97 }
98 };
99
100 static LLVMEnsureMultithreaded lLVMEnsureMultithreaded;
101
102 }
103
104 extern "C" void
105 lp_set_target_options(void)
106 {
107 #if HAVE_LLVM <= 0x0300
108 #if defined(DEBUG)
109 #if HAVE_LLVM >= 0x0207
110 llvm::JITEmitDebugInfo = true;
111 #endif
112 #endif
113
114 /*
115 * LLVM revision 123367 switched the default stack alignment to 16 bytes on
116 * Linux (and several other Unices in later revisions), to match recent gcc
117 * versions.
118 *
119 * However our drivers can be loaded by old binary applications, still
120 * maintaining a 4 bytes stack alignment. Therefore we must tell LLVM here
121 * to only assume a 4 bytes alignment for backwards compatibility.
122 */
123 #if defined(PIPE_ARCH_X86)
124 #if HAVE_LLVM == 0x0300
125 llvm::StackAlignmentOverride = 4;
126 #else
127 llvm::StackAlignment = 4;
128 #endif
129 #endif
130
131 #if defined(DEBUG) || defined(PROFILE)
132 llvm::NoFramePointerElim = true;
133 #if HAVE_LLVM >= 0x0208
134 llvm::NoFramePointerElimNonLeaf = true;
135 #endif
136 #endif
137
138 llvm::NoExcessFPPrecision = false;
139
140 /* XXX: Investigate this */
141 #if 0
142 llvm::UnsafeFPMath = true;
143 #endif
144 #endif /* HAVE_LLVM <= 0x0300 */
145
146 #if HAVE_LLVM < 0x0209
147 /*
148 * LLVM will generate MMX instructions for vectors <= 64 bits, leading to
149 * innefficient code, and in 32bit systems, to the corruption of the FPU
150 * stack given that it expects the user to generate the EMMS instructions.
151 *
152 * See also:
153 * - http://llvm.org/bugs/show_bug.cgi?id=3287
154 * - http://l4.me.uk/post/2009/06/07/llvm-wrinkle-3-configuration-what-configuration/
155 *
156 * The -disable-mmx global option can be specified only once since we
157 * dynamically link against LLVM it will reside in a separate shared object,
158 * which may or not be delete when this shared object is, so we use the
159 * llvm::DisablePrettyStackTrace variable (which we set below and should
160 * reside in the same shared library) to determine whether the -disable-mmx
161 * option has been set or not.
162 *
163 * Thankfully this ugly hack is not necessary on LLVM 2.9 onwards.
164 */
165 if (!llvm::DisablePrettyStackTrace) {
166 static boolean first = TRUE;
167 static const char* options[] = {
168 "prog",
169 "-disable-mmx"
170 };
171 assert(first);
172 llvm::cl::ParseCommandLineOptions(2, const_cast<char**>(options));
173 first = FALSE;
174 }
175 #endif
176
177 #if HAVE_LLVM < 0x0304
178 /*
179 * By default LLVM adds a signal handler to output a pretty stack trace.
180 * This signal handler is never removed, causing problems when unloading the
181 * shared object where the gallium driver resides.
182 */
183 llvm::DisablePrettyStackTrace = true;
184 #endif
185
186 // If we have a native target, initialize it to ensure it is linked in and
187 // usable by the JIT.
188 llvm::InitializeNativeTarget();
189
190 #if HAVE_LLVM >= 0x0208
191 llvm::InitializeNativeTargetAsmPrinter();
192 #elif defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
193 LLVMInitializeX86AsmPrinter();
194 #elif defined(PIPE_ARCH_ARM)
195 LLVMInitializeARMAsmPrinter();
196 #elif defined(PIPE_ARCH_PPC)
197 LLVMInitializePowerPCAsmPrinter();
198 #endif
199
200 #if HAVE_LLVM >= 0x0207
201 # if HAVE_LLVM >= 0x0301
202 llvm::InitializeNativeTargetDisassembler();
203 # elif defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
204 LLVMInitializeX86Disassembler();
205 # elif defined(PIPE_ARCH_ARM)
206 LLVMInitializeARMDisassembler();
207 # endif
208 #endif
209 }
210
211
212 extern "C" void
213 lp_func_delete_body(LLVMValueRef FF)
214 {
215 llvm::Function *func = llvm::unwrap<llvm::Function>(FF);
216 func->deleteBody();
217 }
218
219
220 extern "C"
221 LLVMValueRef
222 lp_build_load_volatile(LLVMBuilderRef B, LLVMValueRef PointerVal,
223 const char *Name)
224 {
225 return llvm::wrap(llvm::unwrap(B)->CreateLoad(llvm::unwrap(PointerVal), true, Name));
226 }
227
228
229 extern "C"
230 void
231 lp_set_load_alignment(LLVMValueRef Inst,
232 unsigned Align)
233 {
234 llvm::unwrap<llvm::LoadInst>(Inst)->setAlignment(Align);
235 }
236
237 extern "C"
238 void
239 lp_set_store_alignment(LLVMValueRef Inst,
240 unsigned Align)
241 {
242 llvm::unwrap<llvm::StoreInst>(Inst)->setAlignment(Align);
243 }
244
245
246 #if HAVE_LLVM >= 0x301
247
248 /**
249 * Same as LLVMCreateJITCompilerForModule, but:
250 * - allows using MCJIT and enabling AVX feature where available.
251 * - set target options
252 *
253 * See also:
254 * - llvm/lib/ExecutionEngine/ExecutionEngineBindings.cpp
255 * - llvm/tools/lli/lli.cpp
256 * - http://markmail.org/message/ttkuhvgj4cxxy2on#query:+page:1+mid:aju2dggerju3ivd3+state:results
257 */
258 extern "C"
259 LLVMBool
260 lp_build_create_jit_compiler_for_module(LLVMExecutionEngineRef *OutJIT,
261 LLVMModuleRef M,
262 unsigned OptLevel,
263 int useMCJIT,
264 char **OutError)
265 {
266 using namespace llvm;
267
268 std::string Error;
269 EngineBuilder builder(unwrap(M));
270
271 /**
272 * LLVM 3.1+ haven't more "extern unsigned llvm::StackAlignmentOverride" and
273 * friends for configuring code generation options, like stack alignment.
274 */
275 TargetOptions options;
276 #if defined(PIPE_ARCH_X86)
277 options.StackAlignmentOverride = 4;
278 #if HAVE_LLVM < 0x0304
279 options.RealignStack = true;
280 #endif
281 #endif
282
283 #if defined(DEBUG)
284 options.JITEmitDebugInfo = true;
285 #endif
286
287 #if defined(DEBUG) || defined(PROFILE)
288 #if HAVE_LLVM < 0x0304
289 options.NoFramePointerElimNonLeaf = true;
290 #endif
291 options.NoFramePointerElim = true;
292 #endif
293
294 builder.setEngineKind(EngineKind::JIT)
295 .setErrorStr(&Error)
296 .setTargetOptions(options)
297 .setOptLevel((CodeGenOpt::Level)OptLevel);
298
299 if (useMCJIT) {
300 builder.setUseMCJIT(true);
301 }
302
303 llvm::SmallVector<std::string, 1> MAttrs;
304 if (util_cpu_caps.has_avx) {
305 /*
306 * AVX feature is not automatically detected from CPUID by the X86 target
307 * yet, because the old (yet default) JIT engine is not capable of
308 * emitting the opcodes. But as we're using MCJIT here, it is safe to
309 * add set this attribute.
310 */
311 MAttrs.push_back("+avx");
312 if (util_cpu_caps.has_f16c) {
313 MAttrs.push_back("+f16c");
314 }
315 builder.setMAttrs(MAttrs);
316 }
317 builder.setJITMemoryManager(JITMemoryManager::CreateDefaultMemManager());
318
319 ExecutionEngine *JIT;
320 #if 0
321 JIT = builder.create();
322 #else
323 /*
324 * Workaround http://llvm.org/bugs/show_bug.cgi?id=12833
325 */
326 StringRef MArch = "";
327 StringRef MCPU = "";
328 Triple TT(unwrap(M)->getTargetTriple());
329 JIT = builder.create(builder.selectTarget(TT, MArch, MCPU, MAttrs));
330 #endif
331 if (JIT) {
332 *OutJIT = wrap(JIT);
333 return 0;
334 }
335 *OutError = strdup(Error.c_str());
336 return 1;
337 }
338
339 #endif /* HAVE_LLVM >= 0x301 */