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 #include <llvm/ADT/Triple.h>
58 #include <llvm/ExecutionEngine/JITMemoryManager.h>
59 #include <llvm/Support/CommandLine.h>
60 #include <llvm/Support/PrettyStackTrace.h>
62 #include <llvm/Support/TargetSelect.h>
64 #if HAVE_LLVM >= 0x0303
65 #include <llvm/IR/IRBuilder.h>
66 #include <llvm/IR/Module.h>
67 #include <llvm/Support/CBindingWrapping.h>
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 class LLVMEnsureMultithreaded
{
80 LLVMEnsureMultithreaded()
82 #if HAVE_LLVM < 0x0303
83 if (!llvm::llvm_is_multithreaded()) {
84 llvm::llvm_start_multithreaded();
87 if (!LLVMIsMultithreaded()) {
88 LLVMStartMultithreaded();
94 static LLVMEnsureMultithreaded lLVMEnsureMultithreaded
;
99 lp_set_target_options(void)
101 #if HAVE_LLVM < 0x0304
103 * By default LLVM adds a signal handler to output a pretty stack trace.
104 * This signal handler is never removed, causing problems when unloading the
105 * shared object where the gallium driver resides.
107 llvm::DisablePrettyStackTrace
= true;
110 // If we have a native target, initialize it to ensure it is linked in and
111 // usable by the JIT.
112 llvm::InitializeNativeTarget();
114 llvm::InitializeNativeTargetAsmPrinter();
116 llvm::InitializeNativeTargetDisassembler();
121 lp_func_delete_body(LLVMValueRef FF
)
123 llvm::Function
*func
= llvm::unwrap
<llvm::Function
>(FF
);
130 lp_build_load_volatile(LLVMBuilderRef B
, LLVMValueRef PointerVal
,
133 return llvm::wrap(llvm::unwrap(B
)->CreateLoad(llvm::unwrap(PointerVal
), true, Name
));
139 lp_set_load_alignment(LLVMValueRef Inst
,
142 llvm::unwrap
<llvm::LoadInst
>(Inst
)->setAlignment(Align
);
147 lp_set_store_alignment(LLVMValueRef Inst
,
150 llvm::unwrap
<llvm::StoreInst
>(Inst
)->setAlignment(Align
);
155 * Same as LLVMCreateJITCompilerForModule, but:
156 * - allows using MCJIT and enabling AVX feature where available.
157 * - set target options
160 * - llvm/lib/ExecutionEngine/ExecutionEngineBindings.cpp
161 * - llvm/tools/lli/lli.cpp
162 * - http://markmail.org/message/ttkuhvgj4cxxy2on#query:+page:1+mid:aju2dggerju3ivd3+state:results
166 lp_build_create_jit_compiler_for_module(LLVMExecutionEngineRef
*OutJIT
,
172 using namespace llvm
;
175 EngineBuilder
builder(unwrap(M
));
178 * LLVM 3.1+ haven't more "extern unsigned llvm::StackAlignmentOverride" and
179 * friends for configuring code generation options, like stack alignment.
181 TargetOptions options
;
182 #if defined(PIPE_ARCH_X86)
183 options
.StackAlignmentOverride
= 4;
184 #if HAVE_LLVM < 0x0304
185 options
.RealignStack
= true;
190 options
.JITEmitDebugInfo
= true;
193 #if defined(DEBUG) || defined(PROFILE)
194 #if HAVE_LLVM < 0x0304
195 options
.NoFramePointerElimNonLeaf
= true;
197 options
.NoFramePointerElim
= true;
200 builder
.setEngineKind(EngineKind::JIT
)
202 .setTargetOptions(options
)
203 .setOptLevel((CodeGenOpt::Level
)OptLevel
);
206 builder
.setUseMCJIT(true);
209 llvm::SmallVector
<std::string
, 1> MAttrs
;
210 if (util_cpu_caps
.has_avx
) {
212 * AVX feature is not automatically detected from CPUID by the X86 target
213 * yet, because the old (yet default) JIT engine is not capable of
214 * emitting the opcodes. But as we're using MCJIT here, it is safe to
215 * add set this attribute.
217 MAttrs
.push_back("+avx");
218 if (util_cpu_caps
.has_f16c
) {
219 MAttrs
.push_back("+f16c");
221 builder
.setMAttrs(MAttrs
);
223 builder
.setJITMemoryManager(JITMemoryManager::CreateDefaultMemManager());
225 ExecutionEngine
*JIT
;
227 JIT
= builder
.create();
230 * Workaround http://llvm.org/bugs/show_bug.cgi?id=12833
232 StringRef MArch
= "";
234 Triple
TT(unwrap(M
)->getTargetTriple());
235 JIT
= builder
.create(builder
.selectTarget(TT
, MArch
, MCPU
, MAttrs
));
241 *OutError
= strdup(Error
.c_str());