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
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>
54 #include <llvm/Support/CommandLine.h>
55 #include <llvm/Support/PrettyStackTrace.h>
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 */
63 #include "pipe/p_config.h"
64 #include "util/u_debug.h"
65 #include "util/u_cpu_detect.h"
67 #include "lp_bld_misc.h"
71 * Register the engine with oprofile.
73 * This allows to see the LLVM IR function names in oprofile output.
75 * To actually work LLVM needs to be built with the --with-oprofile configure
78 * Also a oprofile:oprofile user:group is necessary. Which is not created by
79 * default on some distributions.
82 lp_register_oprofile_jit_event_listener(LLVMExecutionEngineRef EE
)
84 #if HAVE_LLVM >= 0x0301
85 llvm::unwrap(EE
)->RegisterJITEventListener(llvm::JITEventListener::createOProfileJITEventListener());
87 llvm::unwrap(EE
)->RegisterJITEventListener(llvm::createOProfileJITEventListener());
93 lp_set_target_options(void)
95 #if HAVE_LLVM <= 0x0300
97 #if HAVE_LLVM >= 0x0207
98 llvm::JITEmitDebugInfo
= true;
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
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.
111 #if defined(PIPE_ARCH_X86)
112 #if HAVE_LLVM == 0x0300
113 llvm::StackAlignmentOverride
= 4;
115 llvm::StackAlignment
= 4;
119 #if defined(DEBUG) || defined(PROFILE)
120 llvm::NoFramePointerElim
= true;
121 #if HAVE_LLVM >= 0x0208
122 llvm::NoFramePointerElimNonLeaf
= true;
126 llvm::NoExcessFPPrecision
= false;
128 /* XXX: Investigate this */
130 llvm::UnsafeFPMath
= true;
132 #endif /* HAVE_LLVM <= 0x0300 */
134 #if HAVE_LLVM < 0x0209
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.
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/
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.
151 * Thankfully this ugly hack is not necessary on LLVM 2.9 onwards.
153 if (!llvm::DisablePrettyStackTrace
) {
154 static boolean first
= TRUE
;
155 static const char* options
[] = {
160 llvm::cl::ParseCommandLineOptions(2, const_cast<char**>(options
));
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.
170 llvm::DisablePrettyStackTrace
= true;
172 // If we have a native target, initialize it to ensure it is linked in and
173 // usable by the JIT.
174 llvm::InitializeNativeTarget();
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();
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();
199 lp_func_delete_body(LLVMValueRef FF
)
201 llvm::Function
*func
= llvm::unwrap
<llvm::Function
>(FF
);
208 lp_build_load_volatile(LLVMBuilderRef B
, LLVMValueRef PointerVal
,
211 return llvm::wrap(llvm::unwrap(B
)->CreateLoad(llvm::unwrap(PointerVal
), true, Name
));
217 lp_set_load_alignment(LLVMValueRef Inst
,
220 llvm::unwrap
<llvm::LoadInst
>(Inst
)->setAlignment(Align
);
225 lp_set_store_alignment(LLVMValueRef Inst
,
228 llvm::unwrap
<llvm::StoreInst
>(Inst
)->setAlignment(Align
);
232 #if HAVE_LLVM >= 0x301
235 * Same as LLVMCreateJITCompilerForModule, but:
236 * - allows using MCJIT and enabling AVX feature where available.
237 * - set target options
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
246 lp_build_create_jit_compiler_for_module(LLVMExecutionEngineRef
*OutJIT
,
252 using namespace llvm
;
255 EngineBuilder
builder(unwrap(M
));
258 * LLVM 3.1+ haven't more "extern unsigned llvm::StackAlignmentOverride" and
259 * friends for configuring code generation options, like stack alignment.
261 TargetOptions options
;
262 #if defined(PIPE_ARCH_X86)
263 options
.StackAlignmentOverride
= 4;
264 options
.RealignStack
= true;
268 options
.JITEmitDebugInfo
= true;
271 #if defined(DEBUG) || defined(PROFILE)
272 options
.NoFramePointerElimNonLeaf
= true;
273 options
.NoFramePointerElim
= true;
276 builder
.setEngineKind(EngineKind::JIT
)
278 .setTargetOptions(options
)
279 .setOptLevel((CodeGenOpt::Level
)OptLevel
);
282 builder
.setUseMCJIT(true);
285 llvm::SmallVector
<std::string
, 1> MAttrs
;
286 if (util_cpu_caps
.has_avx
) {
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.
293 MAttrs
.push_back("+avx");
294 builder
.setMAttrs(MAttrs
);
296 builder
.setJITMemoryManager(JITMemoryManager::CreateDefaultMemManager());
298 ExecutionEngine
*JIT
;
300 JIT
= builder
.create();
303 * Workaround http://llvm.org/bugs/show_bug.cgi?id=12833
305 StringRef MArch
= "";
307 Triple
TT(unwrap(M
)->getTargetTriple());
308 JIT
= builder
.create(builder
.selectTarget(TT
, MArch
, MCPU
, MAttrs
));
314 *OutError
= strdup(Error
.c_str());
318 #endif /* HAVE_LLVM >= 0x301 */