gallium/gallivm: code generation options for LLVM 3.1+
[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 #include <stddef.h>
44
45 #include <llvm-c/Core.h>
46 #include <llvm-c/ExecutionEngine.h>
47 #include <llvm/Target/TargetOptions.h>
48 #include <llvm/ExecutionEngine/ExecutionEngine.h>
49 #include <llvm/ExecutionEngine/JITEventListener.h>
50 #if HAVE_LLVM >= 0x0301
51 #include <llvm/ADT/Triple.h>
52 #include <llvm/ExecutionEngine/JITMemoryManager.h>
53 #endif
54 #include <llvm/Support/CommandLine.h>
55 #include <llvm/Support/PrettyStackTrace.h>
56
57 #if HAVE_LLVM >= 0x0300
58 #include <llvm/Support/TargetSelect.h>
59 #else /* HAVE_LLVM < 0x0300 */
60 #include <llvm/Target/TargetSelect.h>
61 #endif /* HAVE_LLVM < 0x0300 */
62
63 #include "pipe/p_config.h"
64 #include "util/u_debug.h"
65 #include "util/u_cpu_detect.h"
66
67 #include "lp_bld_misc.h"
68
69
70 /**
71 * Register the engine with oprofile.
72 *
73 * This allows to see the LLVM IR function names in oprofile output.
74 *
75 * To actually work LLVM needs to be built with the --with-oprofile configure
76 * option.
77 *
78 * Also a oprofile:oprofile user:group is necessary. Which is not created by
79 * default on some distributions.
80 */
81 extern "C" void
82 lp_register_oprofile_jit_event_listener(LLVMExecutionEngineRef EE)
83 {
84 #if HAVE_LLVM >= 0x0301
85 llvm::unwrap(EE)->RegisterJITEventListener(llvm::JITEventListener::createOProfileJITEventListener());
86 #else
87 llvm::unwrap(EE)->RegisterJITEventListener(llvm::createOProfileJITEventListener());
88 #endif
89 }
90
91
92 extern "C" void
93 lp_set_target_options(void)
94 {
95 #if HAVE_LLVM <= 0x0300
96 #if defined(DEBUG)
97 #if HAVE_LLVM >= 0x0207
98 llvm::JITEmitDebugInfo = true;
99 #endif
100 #endif
101
102 /*
103 * LLVM revision 123367 switched the default stack alignment to 16 bytes on
104 * Linux (and several other Unices in later revisions), to match recent gcc
105 * versions.
106 *
107 * However our drivers can be loaded by old binary applications, still
108 * maintaining a 4 bytes stack alignment. Therefore we must tell LLVM here
109 * to only assume a 4 bytes alignment for backwards compatibility.
110 */
111 #if defined(PIPE_ARCH_X86)
112 #if HAVE_LLVM == 0x0300
113 llvm::StackAlignmentOverride = 4;
114 #else
115 llvm::StackAlignment = 4;
116 #endif
117 #endif
118
119 #if defined(DEBUG) || defined(PROFILE)
120 llvm::NoFramePointerElim = true;
121 #if HAVE_LLVM >= 0x0208
122 llvm::NoFramePointerElimNonLeaf = true;
123 #endif
124 #endif
125
126 llvm::NoExcessFPPrecision = false;
127
128 /* XXX: Investigate this */
129 #if 0
130 llvm::UnsafeFPMath = true;
131 #endif
132 #endif /* HAVE_LLVM <= 0x0300 */
133
134 #if HAVE_LLVM < 0x0209
135 /*
136 * LLVM will generate MMX instructions for vectors <= 64 bits, leading to
137 * innefficient code, and in 32bit systems, to the corruption of the FPU
138 * stack given that it expects the user to generate the EMMS instructions.
139 *
140 * See also:
141 * - http://llvm.org/bugs/show_bug.cgi?id=3287
142 * - http://l4.me.uk/post/2009/06/07/llvm-wrinkle-3-configuration-what-configuration/
143 *
144 * The -disable-mmx global option can be specified only once since we
145 * dynamically link against LLVM it will reside in a separate shared object,
146 * which may or not be delete when this shared object is, so we use the
147 * llvm::DisablePrettyStackTrace variable (which we set below and should
148 * reside in the same shared library) to determine whether the -disable-mmx
149 * option has been set or not.
150 *
151 * Thankfully this ugly hack is not necessary on LLVM 2.9 onwards.
152 */
153 if (!llvm::DisablePrettyStackTrace) {
154 static boolean first = TRUE;
155 static const char* options[] = {
156 "prog",
157 "-disable-mmx"
158 };
159 assert(first);
160 llvm::cl::ParseCommandLineOptions(2, const_cast<char**>(options));
161 first = FALSE;
162 }
163 #endif
164
165 /*
166 * By default LLVM adds a signal handler to output a pretty stack trace.
167 * This signal handler is never removed, causing problems when unloading the
168 * shared object where the gallium driver resides.
169 */
170 llvm::DisablePrettyStackTrace = true;
171
172 // If we have a native target, initialize it to ensure it is linked in and
173 // usable by the JIT.
174 llvm::InitializeNativeTarget();
175
176 #if HAVE_LLVM >= 0x0208
177 llvm::InitializeNativeTargetAsmPrinter();
178 #elif defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
179 LLVMInitializeX86AsmPrinter();
180 #elif defined(PIPE_ARCH_ARM)
181 LLVMInitializeARMAsmPrinter();
182 #elif defined(PIPE_ARCH_PPC)
183 LLVMInitializePowerPCAsmPrinter();
184 #endif
185
186 #if HAVE_LLVM >= 0x0207
187 # if HAVE_LLVM >= 0x0301
188 llvm::InitializeNativeTargetDisassembler();
189 # elif defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
190 LLVMInitializeX86Disassembler();
191 # elif defined(PIPE_ARCH_ARM)
192 LLVMInitializeARMDisassembler();
193 # endif
194 #endif
195 }
196
197
198 extern "C" void
199 lp_func_delete_body(LLVMValueRef FF)
200 {
201 llvm::Function *func = llvm::unwrap<llvm::Function>(FF);
202 func->deleteBody();
203 }
204
205
206 extern "C"
207 LLVMValueRef
208 lp_build_load_volatile(LLVMBuilderRef B, LLVMValueRef PointerVal,
209 const char *Name)
210 {
211 return llvm::wrap(llvm::unwrap(B)->CreateLoad(llvm::unwrap(PointerVal), true, Name));
212 }
213
214
215 extern "C"
216 void
217 lp_set_load_alignment(LLVMValueRef Inst,
218 unsigned Align)
219 {
220 llvm::unwrap<llvm::LoadInst>(Inst)->setAlignment(Align);
221 }
222
223 extern "C"
224 void
225 lp_set_store_alignment(LLVMValueRef Inst,
226 unsigned Align)
227 {
228 llvm::unwrap<llvm::StoreInst>(Inst)->setAlignment(Align);
229 }
230
231
232 #if HAVE_LLVM >= 0x301
233
234 /**
235 * Same as LLVMCreateJITCompilerForModule, but:
236 * - allows using MCJIT and enabling AVX feature where available.
237 * - set target options
238 *
239 * See also:
240 * - llvm/lib/ExecutionEngine/ExecutionEngineBindings.cpp
241 * - llvm/tools/lli/lli.cpp
242 * - http://markmail.org/message/ttkuhvgj4cxxy2on#query:+page:1+mid:aju2dggerju3ivd3+state:results
243 */
244 extern "C"
245 LLVMBool
246 lp_build_create_jit_compiler_for_module(LLVMExecutionEngineRef *OutJIT,
247 LLVMModuleRef M,
248 unsigned OptLevel,
249 int useMCJIT,
250 char **OutError)
251 {
252 using namespace llvm;
253
254 std::string Error;
255 EngineBuilder builder(unwrap(M));
256
257 /**
258 * LLVM 3.1+ haven't more "extern unsigned llvm::StackAlignmentOverride" and
259 * friends for configuring code generation options, like stack alignment.
260 */
261 TargetOptions options;
262 #if defined(PIPE_ARCH_X86)
263 options.StackAlignmentOverride = 4;
264 options.RealignStack = true;
265 #endif
266
267 #if defined(DEBUG)
268 options.JITEmitDebugInfo = true;
269 #endif
270
271 #if defined(DEBUG) || defined(PROFILE)
272 options.NoFramePointerElimNonLeaf = true;
273 options.NoFramePointerElim = true;
274 #endif
275
276 builder.setEngineKind(EngineKind::JIT)
277 .setErrorStr(&Error)
278 .setTargetOptions(options)
279 .setOptLevel((CodeGenOpt::Level)OptLevel);
280
281 if (useMCJIT) {
282 builder.setUseMCJIT(true);
283 }
284
285 llvm::SmallVector<std::string, 1> MAttrs;
286 if (util_cpu_caps.has_avx) {
287 /*
288 * AVX feature is not automatically detected from CPUID by the X86 target
289 * yet, because the old (yet default) JIT engine is not capable of
290 * emitting the opcodes. But as we're using MCJIT here, it is safe to
291 * add set this attribute.
292 */
293 MAttrs.push_back("+avx");
294 builder.setMAttrs(MAttrs);
295 }
296 builder.setJITMemoryManager(JITMemoryManager::CreateDefaultMemManager());
297
298 ExecutionEngine *JIT;
299 #if 0
300 JIT = builder.create();
301 #else
302 /*
303 * Workaround http://llvm.org/bugs/show_bug.cgi?id=12833
304 */
305 StringRef MArch = "";
306 StringRef MCPU = "";
307 Triple TT(unwrap(M)->getTargetTriple());
308 JIT = builder.create(builder.selectTarget(TT, MArch, MCPU, MAttrs));
309 #endif
310 if (JIT) {
311 *OutJIT = wrap(JIT);
312 return 0;
313 }
314 *OutError = strdup(Error.c_str());
315 return 1;
316 }
317
318 #endif /* HAVE_LLVM >= 0x301 */