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