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 #if HAVE_LLVM >= 0x0303
71 #include <llvm/IR/IRBuilder.h>
72 #include <llvm/IR/Module.h>
73 #include <llvm/Support/CBindingWrapping.h>
76 #include "pipe/p_config.h"
77 #include "util/u_debug.h"
78 #include "util/u_cpu_detect.h"
80 #include "lp_bld_misc.h"
84 class LLVMEnsureMultithreaded
{
86 LLVMEnsureMultithreaded()
88 #if HAVE_LLVM < 0x0303
89 if (!llvm::llvm_is_multithreaded()) {
90 llvm::llvm_start_multithreaded();
93 if (!LLVMIsMultithreaded()) {
94 LLVMStartMultithreaded();
100 static LLVMEnsureMultithreaded lLVMEnsureMultithreaded
;
105 lp_set_target_options(void)
107 #if HAVE_LLVM <= 0x0300
109 #if HAVE_LLVM >= 0x0207
110 llvm::JITEmitDebugInfo
= true;
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
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.
123 #if defined(PIPE_ARCH_X86)
124 #if HAVE_LLVM == 0x0300
125 llvm::StackAlignmentOverride
= 4;
127 llvm::StackAlignment
= 4;
131 #if defined(DEBUG) || defined(PROFILE)
132 llvm::NoFramePointerElim
= true;
133 #if HAVE_LLVM >= 0x0208
134 llvm::NoFramePointerElimNonLeaf
= true;
138 llvm::NoExcessFPPrecision
= false;
140 /* XXX: Investigate this */
142 llvm::UnsafeFPMath
= true;
144 #endif /* HAVE_LLVM <= 0x0300 */
146 #if HAVE_LLVM < 0x0209
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.
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/
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.
163 * Thankfully this ugly hack is not necessary on LLVM 2.9 onwards.
165 if (!llvm::DisablePrettyStackTrace
) {
166 static boolean first
= TRUE
;
167 static const char* options
[] = {
172 llvm::cl::ParseCommandLineOptions(2, const_cast<char**>(options
));
177 #if HAVE_LLVM < 0x0304
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.
183 llvm::DisablePrettyStackTrace
= true;
186 // If we have a native target, initialize it to ensure it is linked in and
187 // usable by the JIT.
188 llvm::InitializeNativeTarget();
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();
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();
213 lp_func_delete_body(LLVMValueRef FF
)
215 llvm::Function
*func
= llvm::unwrap
<llvm::Function
>(FF
);
222 lp_build_load_volatile(LLVMBuilderRef B
, LLVMValueRef PointerVal
,
225 return llvm::wrap(llvm::unwrap(B
)->CreateLoad(llvm::unwrap(PointerVal
), true, Name
));
231 lp_set_load_alignment(LLVMValueRef Inst
,
234 llvm::unwrap
<llvm::LoadInst
>(Inst
)->setAlignment(Align
);
239 lp_set_store_alignment(LLVMValueRef Inst
,
242 llvm::unwrap
<llvm::StoreInst
>(Inst
)->setAlignment(Align
);
246 #if HAVE_LLVM >= 0x301
249 * Same as LLVMCreateJITCompilerForModule, but:
250 * - allows using MCJIT and enabling AVX feature where available.
251 * - set target options
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
260 lp_build_create_jit_compiler_for_module(LLVMExecutionEngineRef
*OutJIT
,
266 using namespace llvm
;
269 EngineBuilder
builder(unwrap(M
));
272 * LLVM 3.1+ haven't more "extern unsigned llvm::StackAlignmentOverride" and
273 * friends for configuring code generation options, like stack alignment.
275 TargetOptions options
;
276 #if defined(PIPE_ARCH_X86)
277 options
.StackAlignmentOverride
= 4;
278 #if HAVE_LLVM < 0x0304
279 options
.RealignStack
= true;
284 options
.JITEmitDebugInfo
= true;
287 #if defined(DEBUG) || defined(PROFILE)
288 #if HAVE_LLVM < 0x0304
289 options
.NoFramePointerElimNonLeaf
= true;
291 options
.NoFramePointerElim
= true;
294 builder
.setEngineKind(EngineKind::JIT
)
296 .setTargetOptions(options
)
297 .setOptLevel((CodeGenOpt::Level
)OptLevel
);
300 builder
.setUseMCJIT(true);
303 llvm::SmallVector
<std::string
, 1> MAttrs
;
304 if (util_cpu_caps
.has_avx
) {
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.
311 MAttrs
.push_back("+avx");
312 if (util_cpu_caps
.has_f16c
) {
313 MAttrs
.push_back("+f16c");
315 builder
.setMAttrs(MAttrs
);
317 builder
.setJITMemoryManager(JITMemoryManager::CreateDefaultMemManager());
319 ExecutionEngine
*JIT
;
321 JIT
= builder
.create();
324 * Workaround http://llvm.org/bugs/show_bug.cgi?id=12833
326 StringRef MArch
= "";
328 Triple
TT(unwrap(M
)->getTargetTriple());
329 JIT
= builder
.create(builder
.selectTarget(TT
, MArch
, MCPU
, MAttrs
));
335 *OutError
= strdup(Error
.c_str());
339 #endif /* HAVE_LLVM >= 0x301 */