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 * Delegating is tedious but the default manager class is hidden in an
156 * anonymous namespace in LLVM, so we cannot just derive from it to change
159 class DelegatingJITMemoryManager
: public llvm::JITMemoryManager
{
162 virtual llvm::JITMemoryManager
*mgr() const = 0;
166 * From JITMemoryManager
168 virtual void setMemoryWritable() {
169 mgr()->setMemoryWritable();
171 virtual void setMemoryExecutable() {
172 mgr()->setMemoryExecutable();
174 virtual void setPoisonMemory(bool poison
) {
175 mgr()->setPoisonMemory(poison
);
177 virtual void AllocateGOT() {
178 mgr()->AllocateGOT();
180 * isManagingGOT() is not virtual in base class so we can't delegate.
181 * Instead we mirror the value of HasGOT in our instance.
183 HasGOT
= mgr()->isManagingGOT();
185 virtual uint8_t *getGOTBase() const {
186 return mgr()->getGOTBase();
188 virtual uint8_t *startFunctionBody(const llvm::Function
*F
,
189 uintptr_t &ActualSize
) {
190 return mgr()->startFunctionBody(F
, ActualSize
);
192 virtual uint8_t *allocateStub(const llvm::GlobalValue
*F
,
194 unsigned Alignment
) {
195 return mgr()->allocateStub(F
, StubSize
, Alignment
);
197 virtual void endFunctionBody(const llvm::Function
*F
,
198 uint8_t *FunctionStart
,
199 uint8_t *FunctionEnd
) {
200 mgr()->endFunctionBody(F
, FunctionStart
, FunctionEnd
);
202 virtual uint8_t *allocateSpace(intptr_t Size
, unsigned Alignment
) {
203 return mgr()->allocateSpace(Size
, Alignment
);
205 virtual uint8_t *allocateGlobal(uintptr_t Size
, unsigned Alignment
) {
206 return mgr()->allocateGlobal(Size
, Alignment
);
208 virtual void deallocateFunctionBody(void *Body
) {
209 mgr()->deallocateFunctionBody(Body
);
211 #if HAVE_LLVM < 0x0304
212 virtual uint8_t *startExceptionTable(const llvm::Function
*F
,
213 uintptr_t &ActualSize
) {
214 return mgr()->startExceptionTable(F
, ActualSize
);
216 virtual void endExceptionTable(const llvm::Function
*F
,
219 uint8_t *FrameRegister
) {
220 mgr()->endExceptionTable(F
, TableStart
, TableEnd
,
223 virtual void deallocateExceptionTable(void *ET
) {
224 mgr()->deallocateExceptionTable(ET
);
227 virtual bool CheckInvariants(std::string
&s
) {
228 return mgr()->CheckInvariants(s
);
230 virtual size_t GetDefaultCodeSlabSize() {
231 return mgr()->GetDefaultCodeSlabSize();
233 virtual size_t GetDefaultDataSlabSize() {
234 return mgr()->GetDefaultDataSlabSize();
236 virtual size_t GetDefaultStubSlabSize() {
237 return mgr()->GetDefaultStubSlabSize();
239 virtual unsigned GetNumCodeSlabs() {
240 return mgr()->GetNumCodeSlabs();
242 virtual unsigned GetNumDataSlabs() {
243 return mgr()->GetNumDataSlabs();
245 virtual unsigned GetNumStubSlabs() {
246 return mgr()->GetNumStubSlabs();
250 * From RTDyldMemoryManager
252 #if HAVE_LLVM >= 0x0304
253 virtual uint8_t *allocateCodeSection(uintptr_t Size
,
256 llvm::StringRef SectionName
) {
257 return mgr()->allocateCodeSection(Size
, Alignment
, SectionID
,
261 virtual uint8_t *allocateCodeSection(uintptr_t Size
,
263 unsigned SectionID
) {
264 return mgr()->allocateCodeSection(Size
, Alignment
, SectionID
);
267 #if HAVE_LLVM >= 0x0303
268 virtual uint8_t *allocateDataSection(uintptr_t Size
,
271 #if HAVE_LLVM >= 0x0304
272 llvm::StringRef SectionName
,
275 return mgr()->allocateDataSection(Size
, Alignment
, SectionID
,
276 #if HAVE_LLVM >= 0x0304
281 #if HAVE_LLVM >= 0x0304
282 virtual void registerEHFrames(uint8_t *Addr
, uint64_t LoadAddr
, size_t Size
) {
283 mgr()->registerEHFrames(Addr
, LoadAddr
, Size
);
285 virtual void deregisterEHFrames(uint8_t *Addr
, uint64_t LoadAddr
, size_t Size
) {
286 mgr()->deregisterEHFrames(Addr
, LoadAddr
, Size
);
289 virtual void registerEHFrames(llvm::StringRef SectionData
) {
290 mgr()->registerEHFrames(SectionData
);
294 virtual uint8_t *allocateDataSection(uintptr_t Size
,
296 unsigned SectionID
) {
297 return mgr()->allocateDataSection(Size
, Alignment
, SectionID
);
300 virtual void *getPointerToNamedFunction(const std::string
&Name
,
301 bool AbortOnFailure
=true) {
302 return mgr()->getPointerToNamedFunction(Name
, AbortOnFailure
);
304 #if HAVE_LLVM == 0x0303
305 virtual bool applyPermissions(std::string
*ErrMsg
= 0) {
306 return mgr()->applyPermissions(ErrMsg
);
308 #elif HAVE_LLVM > 0x0303
309 virtual bool finalizeMemory(std::string
*ErrMsg
= 0) {
310 return mgr()->finalizeMemory(ErrMsg
);
317 * Delegate memory management to one shared manager for more efficient use
318 * of memory than creating a separate pool for each LLVM engine.
319 * Keep generated code until freeGeneratedCode() is called, instead of when
320 * memory manager is destroyed, which happens during engine destruction.
321 * This allows additional memory savings as we don't have to keep the engine
322 * around in order to use the code.
323 * All methods are delegated to the shared manager except destruction and
324 * deallocating code. For the latter we just remember what needs to be
325 * deallocated later. The shared manager is deleted once it is empty.
327 class ShaderMemoryManager
: public DelegatingJITMemoryManager
{
329 static llvm::JITMemoryManager
*TheMM
;
330 static unsigned NumUsers
;
332 struct GeneratedCode
{
333 typedef std::vector
<void *> Vec
;
334 Vec FunctionBody
, ExceptionTable
;
342 * Deallocate things as previously requested and
343 * free shared manager when no longer used.
348 for ( i
= FunctionBody
.begin(); i
!= FunctionBody
.end(); ++i
)
349 TheMM
->deallocateFunctionBody(*i
);
350 #if HAVE_LLVM < 0x0304
351 for ( i
= ExceptionTable
.begin(); i
!= ExceptionTable
.end(); ++i
)
352 TheMM
->deallocateExceptionTable(*i
);
364 llvm::JITMemoryManager
*mgr() const {
366 TheMM
= CreateDefaultMemManager();
373 ShaderMemoryManager() {
374 code
= new GeneratedCode
;
377 virtual ~ShaderMemoryManager() {
379 * 'code' is purposely not deleted. It is the user's responsibility
380 * to call getGeneratedCode() and freeGeneratedCode().
384 struct lp_generated_code
*getGeneratedCode() {
385 return (struct lp_generated_code
*) code
;
388 static void freeGeneratedCode(struct lp_generated_code
*code
) {
389 delete (GeneratedCode
*) code
;
392 #if HAVE_LLVM < 0x0304
393 virtual void deallocateExceptionTable(void *ET
) {
394 // remember for later deallocation
395 code
->ExceptionTable
.push_back(ET
);
399 virtual void deallocateFunctionBody(void *Body
) {
400 // remember for later deallocation
401 code
->FunctionBody
.push_back(Body
);
405 llvm::JITMemoryManager
*ShaderMemoryManager::TheMM
= 0;
406 unsigned ShaderMemoryManager::NumUsers
= 0;
410 * Same as LLVMCreateJITCompilerForModule, but:
411 * - allows using MCJIT and enabling AVX feature where available.
412 * - set target options
415 * - llvm/lib/ExecutionEngine/ExecutionEngineBindings.cpp
416 * - llvm/tools/lli/lli.cpp
417 * - http://markmail.org/message/ttkuhvgj4cxxy2on#query:+page:1+mid:aju2dggerju3ivd3+state:results
421 lp_build_create_jit_compiler_for_module(LLVMExecutionEngineRef
*OutJIT
,
422 lp_generated_code
**OutCode
,
428 using namespace llvm
;
431 EngineBuilder
builder(unwrap(M
));
434 * LLVM 3.1+ haven't more "extern unsigned llvm::StackAlignmentOverride" and
435 * friends for configuring code generation options, like stack alignment.
437 TargetOptions options
;
438 #if defined(PIPE_ARCH_X86)
439 options
.StackAlignmentOverride
= 4;
440 #if HAVE_LLVM < 0x0304
441 options
.RealignStack
= true;
446 options
.JITEmitDebugInfo
= true;
449 #if defined(DEBUG) || defined(PROFILE)
450 #if HAVE_LLVM < 0x0304
451 options
.NoFramePointerElimNonLeaf
= true;
453 options
.NoFramePointerElim
= true;
456 builder
.setEngineKind(EngineKind::JIT
)
458 .setTargetOptions(options
)
459 .setOptLevel((CodeGenOpt::Level
)OptLevel
);
462 builder
.setUseMCJIT(true);
465 llvm::SmallVector
<std::string
, 1> MAttrs
;
466 if (util_cpu_caps
.has_avx
) {
468 * AVX feature is not automatically detected from CPUID by the X86 target
469 * yet, because the old (yet default) JIT engine is not capable of
470 * emitting the opcodes. But as we're using MCJIT here, it is safe to
471 * add set this attribute.
473 MAttrs
.push_back("+avx");
474 if (util_cpu_caps
.has_f16c
) {
475 MAttrs
.push_back("+f16c");
477 builder
.setMAttrs(MAttrs
);
480 ShaderMemoryManager
*MM
= new ShaderMemoryManager();
481 *OutCode
= MM
->getGeneratedCode();
483 builder
.setJITMemoryManager(MM
);
485 ExecutionEngine
*JIT
;
487 JIT
= builder
.create();
490 * Workaround http://llvm.org/bugs/show_bug.cgi?id=12833
492 StringRef MArch
= "";
494 Triple
TT(unwrap(M
)->getTargetTriple());
495 JIT
= builder
.create(builder
.selectTarget(TT
, MArch
, MCPU
, MAttrs
));
501 lp_free_generated_code(*OutCode
);
504 *OutError
= strdup(Error
.c_str());
511 lp_free_generated_code(struct lp_generated_code
*code
)
513 ShaderMemoryManager::freeGeneratedCode(code
);