1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
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:
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.
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
26 **************************************************************************/
30 * The purpose of this module is to expose LLVM functionality not available
31 * through the C++ bindings.
35 #ifndef __STDC_LIMIT_MACROS
36 #define __STDC_LIMIT_MACROS
39 #ifndef __STDC_CONSTANT_MACROS
40 #define __STDC_CONSTANT_MACROS
43 // Undef these vars just to silence warnings
44 #undef PACKAGE_BUGREPORT
47 #undef PACKAGE_TARNAME
48 #undef PACKAGE_VERSION
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>
61 #include <llvm/Support/CommandLine.h>
62 #include <llvm/Support/PrettyStackTrace.h>
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 */
70 #include "pipe/p_config.h"
71 #include "util/u_debug.h"
72 #include "util/u_cpu_detect.h"
74 #include "lp_bld_misc.h"
78 lp_set_target_options(void)
80 #if HAVE_LLVM <= 0x0300
82 #if HAVE_LLVM >= 0x0207
83 llvm::JITEmitDebugInfo
= true;
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
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.
96 #if defined(PIPE_ARCH_X86)
97 #if HAVE_LLVM == 0x0300
98 llvm::StackAlignmentOverride
= 4;
100 llvm::StackAlignment
= 4;
104 #if defined(DEBUG) || defined(PROFILE)
105 llvm::NoFramePointerElim
= true;
106 #if HAVE_LLVM >= 0x0208
107 llvm::NoFramePointerElimNonLeaf
= true;
111 llvm::NoExcessFPPrecision
= false;
113 /* XXX: Investigate this */
115 llvm::UnsafeFPMath
= true;
117 #endif /* HAVE_LLVM <= 0x0300 */
119 #if HAVE_LLVM < 0x0209
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.
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/
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.
136 * Thankfully this ugly hack is not necessary on LLVM 2.9 onwards.
138 if (!llvm::DisablePrettyStackTrace
) {
139 static boolean first
= TRUE
;
140 static const char* options
[] = {
145 llvm::cl::ParseCommandLineOptions(2, const_cast<char**>(options
));
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.
155 llvm::DisablePrettyStackTrace
= true;
157 // If we have a native target, initialize it to ensure it is linked in and
158 // usable by the JIT.
159 llvm::InitializeNativeTarget();
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();
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();
184 lp_func_delete_body(LLVMValueRef FF
)
186 llvm::Function
*func
= llvm::unwrap
<llvm::Function
>(FF
);
193 lp_build_load_volatile(LLVMBuilderRef B
, LLVMValueRef PointerVal
,
196 return llvm::wrap(llvm::unwrap(B
)->CreateLoad(llvm::unwrap(PointerVal
), true, Name
));
202 lp_set_load_alignment(LLVMValueRef Inst
,
205 llvm::unwrap
<llvm::LoadInst
>(Inst
)->setAlignment(Align
);
210 lp_set_store_alignment(LLVMValueRef Inst
,
213 llvm::unwrap
<llvm::StoreInst
>(Inst
)->setAlignment(Align
);
217 #if HAVE_LLVM >= 0x301
220 * Same as LLVMCreateJITCompilerForModule, but:
221 * - allows using MCJIT and enabling AVX feature where available.
222 * - set target options
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
231 lp_build_create_jit_compiler_for_module(LLVMExecutionEngineRef
*OutJIT
,
237 using namespace llvm
;
240 EngineBuilder
builder(unwrap(M
));
243 * LLVM 3.1+ haven't more "extern unsigned llvm::StackAlignmentOverride" and
244 * friends for configuring code generation options, like stack alignment.
246 TargetOptions options
;
247 #if defined(PIPE_ARCH_X86)
248 options
.StackAlignmentOverride
= 4;
249 options
.RealignStack
= true;
253 options
.JITEmitDebugInfo
= true;
256 #if defined(DEBUG) || defined(PROFILE)
257 options
.NoFramePointerElimNonLeaf
= true;
258 options
.NoFramePointerElim
= true;
261 builder
.setEngineKind(EngineKind::JIT
)
263 .setTargetOptions(options
)
264 .setOptLevel((CodeGenOpt::Level
)OptLevel
);
267 builder
.setUseMCJIT(true);
270 llvm::SmallVector
<std::string
, 1> MAttrs
;
271 if (util_cpu_caps
.has_avx
) {
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.
278 MAttrs
.push_back("+avx");
279 if (util_cpu_caps
.has_f16c
) {
280 MAttrs
.push_back("+f16c");
282 builder
.setMAttrs(MAttrs
);
284 builder
.setJITMemoryManager(JITMemoryManager::CreateDefaultMemManager());
286 ExecutionEngine
*JIT
;
288 JIT
= builder
.create();
291 * Workaround http://llvm.org/bugs/show_bug.cgi?id=12833
293 StringRef MArch
= "";
295 Triple
TT(unwrap(M
)->getTargetTriple());
296 JIT
= builder
.create(builder
.selectTarget(TT
, MArch
, MCPU
, MAttrs
));
302 *OutError
= strdup(Error
.c_str());
306 #endif /* HAVE_LLVM >= 0x301 */