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();
122 lp_build_load_volatile(LLVMBuilderRef B
, LLVMValueRef PointerVal
,
125 return llvm::wrap(llvm::unwrap(B
)->CreateLoad(llvm::unwrap(PointerVal
), true, Name
));
131 lp_set_load_alignment(LLVMValueRef Inst
,
134 llvm::unwrap
<llvm::LoadInst
>(Inst
)->setAlignment(Align
);
139 lp_set_store_alignment(LLVMValueRef Inst
,
142 llvm::unwrap
<llvm::StoreInst
>(Inst
)->setAlignment(Align
);
147 * Delegating is tedious but the default manager class is hidden in an
148 * anonymous namespace in LLVM, so we cannot just derive from it to change
151 class DelegatingJITMemoryManager
: public llvm::JITMemoryManager
{
154 virtual llvm::JITMemoryManager
*mgr() const = 0;
158 * From JITMemoryManager
160 virtual void setMemoryWritable() {
161 mgr()->setMemoryWritable();
163 virtual void setMemoryExecutable() {
164 mgr()->setMemoryExecutable();
166 virtual void setPoisonMemory(bool poison
) {
167 mgr()->setPoisonMemory(poison
);
169 virtual void AllocateGOT() {
170 mgr()->AllocateGOT();
172 * isManagingGOT() is not virtual in base class so we can't delegate.
173 * Instead we mirror the value of HasGOT in our instance.
175 HasGOT
= mgr()->isManagingGOT();
177 virtual uint8_t *getGOTBase() const {
178 return mgr()->getGOTBase();
180 virtual uint8_t *startFunctionBody(const llvm::Function
*F
,
181 uintptr_t &ActualSize
) {
182 return mgr()->startFunctionBody(F
, ActualSize
);
184 virtual uint8_t *allocateStub(const llvm::GlobalValue
*F
,
186 unsigned Alignment
) {
187 return mgr()->allocateStub(F
, StubSize
, Alignment
);
189 virtual void endFunctionBody(const llvm::Function
*F
,
190 uint8_t *FunctionStart
,
191 uint8_t *FunctionEnd
) {
192 mgr()->endFunctionBody(F
, FunctionStart
, FunctionEnd
);
194 virtual uint8_t *allocateSpace(intptr_t Size
, unsigned Alignment
) {
195 return mgr()->allocateSpace(Size
, Alignment
);
197 virtual uint8_t *allocateGlobal(uintptr_t Size
, unsigned Alignment
) {
198 return mgr()->allocateGlobal(Size
, Alignment
);
200 virtual void deallocateFunctionBody(void *Body
) {
201 mgr()->deallocateFunctionBody(Body
);
203 #if HAVE_LLVM < 0x0304
204 virtual uint8_t *startExceptionTable(const llvm::Function
*F
,
205 uintptr_t &ActualSize
) {
206 return mgr()->startExceptionTable(F
, ActualSize
);
208 virtual void endExceptionTable(const llvm::Function
*F
,
211 uint8_t *FrameRegister
) {
212 mgr()->endExceptionTable(F
, TableStart
, TableEnd
,
215 virtual void deallocateExceptionTable(void *ET
) {
216 mgr()->deallocateExceptionTable(ET
);
219 virtual bool CheckInvariants(std::string
&s
) {
220 return mgr()->CheckInvariants(s
);
222 virtual size_t GetDefaultCodeSlabSize() {
223 return mgr()->GetDefaultCodeSlabSize();
225 virtual size_t GetDefaultDataSlabSize() {
226 return mgr()->GetDefaultDataSlabSize();
228 virtual size_t GetDefaultStubSlabSize() {
229 return mgr()->GetDefaultStubSlabSize();
231 virtual unsigned GetNumCodeSlabs() {
232 return mgr()->GetNumCodeSlabs();
234 virtual unsigned GetNumDataSlabs() {
235 return mgr()->GetNumDataSlabs();
237 virtual unsigned GetNumStubSlabs() {
238 return mgr()->GetNumStubSlabs();
242 * From RTDyldMemoryManager
244 #if HAVE_LLVM >= 0x0304
245 virtual uint8_t *allocateCodeSection(uintptr_t Size
,
248 llvm::StringRef SectionName
) {
249 return mgr()->allocateCodeSection(Size
, Alignment
, SectionID
,
253 virtual uint8_t *allocateCodeSection(uintptr_t Size
,
255 unsigned SectionID
) {
256 return mgr()->allocateCodeSection(Size
, Alignment
, SectionID
);
259 #if HAVE_LLVM >= 0x0303
260 virtual uint8_t *allocateDataSection(uintptr_t Size
,
263 #if HAVE_LLVM >= 0x0304
264 llvm::StringRef SectionName
,
267 return mgr()->allocateDataSection(Size
, Alignment
, SectionID
,
268 #if HAVE_LLVM >= 0x0304
273 #if HAVE_LLVM >= 0x0304
274 virtual void registerEHFrames(uint8_t *Addr
, uint64_t LoadAddr
, size_t Size
) {
275 mgr()->registerEHFrames(Addr
, LoadAddr
, Size
);
277 virtual void deregisterEHFrames(uint8_t *Addr
, uint64_t LoadAddr
, size_t Size
) {
278 mgr()->deregisterEHFrames(Addr
, LoadAddr
, Size
);
281 virtual void registerEHFrames(llvm::StringRef SectionData
) {
282 mgr()->registerEHFrames(SectionData
);
286 virtual uint8_t *allocateDataSection(uintptr_t Size
,
288 unsigned SectionID
) {
289 return mgr()->allocateDataSection(Size
, Alignment
, SectionID
);
292 virtual void *getPointerToNamedFunction(const std::string
&Name
,
293 bool AbortOnFailure
=true) {
294 return mgr()->getPointerToNamedFunction(Name
, AbortOnFailure
);
296 #if HAVE_LLVM == 0x0303
297 virtual bool applyPermissions(std::string
*ErrMsg
= 0) {
298 return mgr()->applyPermissions(ErrMsg
);
300 #elif HAVE_LLVM > 0x0303
301 virtual bool finalizeMemory(std::string
*ErrMsg
= 0) {
302 return mgr()->finalizeMemory(ErrMsg
);
309 * Delegate memory management to one shared manager for more efficient use
310 * of memory than creating a separate pool for each LLVM engine.
311 * Keep generated code until freeGeneratedCode() is called, instead of when
312 * memory manager is destroyed, which happens during engine destruction.
313 * This allows additional memory savings as we don't have to keep the engine
314 * around in order to use the code.
315 * All methods are delegated to the shared manager except destruction and
316 * deallocating code. For the latter we just remember what needs to be
317 * deallocated later. The shared manager is deleted once it is empty.
319 class ShaderMemoryManager
: public DelegatingJITMemoryManager
{
321 static llvm::JITMemoryManager
*TheMM
;
322 static unsigned NumUsers
;
324 struct GeneratedCode
{
325 typedef std::vector
<void *> Vec
;
326 Vec FunctionBody
, ExceptionTable
;
334 * Deallocate things as previously requested and
335 * free shared manager when no longer used.
340 for ( i
= FunctionBody
.begin(); i
!= FunctionBody
.end(); ++i
)
341 TheMM
->deallocateFunctionBody(*i
);
342 #if HAVE_LLVM < 0x0304
343 for ( i
= ExceptionTable
.begin(); i
!= ExceptionTable
.end(); ++i
)
344 TheMM
->deallocateExceptionTable(*i
);
356 llvm::JITMemoryManager
*mgr() const {
358 TheMM
= CreateDefaultMemManager();
365 ShaderMemoryManager() {
366 code
= new GeneratedCode
;
369 virtual ~ShaderMemoryManager() {
371 * 'code' is purposely not deleted. It is the user's responsibility
372 * to call getGeneratedCode() and freeGeneratedCode().
376 struct lp_generated_code
*getGeneratedCode() {
377 return (struct lp_generated_code
*) code
;
380 static void freeGeneratedCode(struct lp_generated_code
*code
) {
381 delete (GeneratedCode
*) code
;
384 #if HAVE_LLVM < 0x0304
385 virtual void deallocateExceptionTable(void *ET
) {
386 // remember for later deallocation
387 code
->ExceptionTable
.push_back(ET
);
391 virtual void deallocateFunctionBody(void *Body
) {
392 // remember for later deallocation
393 code
->FunctionBody
.push_back(Body
);
397 llvm::JITMemoryManager
*ShaderMemoryManager::TheMM
= 0;
398 unsigned ShaderMemoryManager::NumUsers
= 0;
402 * Same as LLVMCreateJITCompilerForModule, but:
403 * - allows using MCJIT and enabling AVX feature where available.
404 * - set target options
407 * - llvm/lib/ExecutionEngine/ExecutionEngineBindings.cpp
408 * - llvm/tools/lli/lli.cpp
409 * - http://markmail.org/message/ttkuhvgj4cxxy2on#query:+page:1+mid:aju2dggerju3ivd3+state:results
413 lp_build_create_jit_compiler_for_module(LLVMExecutionEngineRef
*OutJIT
,
414 lp_generated_code
**OutCode
,
420 using namespace llvm
;
423 EngineBuilder
builder(unwrap(M
));
426 * LLVM 3.1+ haven't more "extern unsigned llvm::StackAlignmentOverride" and
427 * friends for configuring code generation options, like stack alignment.
429 TargetOptions options
;
430 #if defined(PIPE_ARCH_X86)
431 options
.StackAlignmentOverride
= 4;
432 #if HAVE_LLVM < 0x0304
433 options
.RealignStack
= true;
438 options
.JITEmitDebugInfo
= true;
441 #if defined(DEBUG) || defined(PROFILE)
442 #if HAVE_LLVM < 0x0304
443 options
.NoFramePointerElimNonLeaf
= true;
445 options
.NoFramePointerElim
= true;
448 builder
.setEngineKind(EngineKind::JIT
)
450 .setTargetOptions(options
)
451 .setOptLevel((CodeGenOpt::Level
)OptLevel
);
454 builder
.setUseMCJIT(true);
457 llvm::SmallVector
<std::string
, 1> MAttrs
;
458 if (util_cpu_caps
.has_avx
) {
460 * AVX feature is not automatically detected from CPUID by the X86 target
461 * yet, because the old (yet default) JIT engine is not capable of
462 * emitting the opcodes. But as we're using MCJIT here, it is safe to
463 * add set this attribute.
465 MAttrs
.push_back("+avx");
466 if (util_cpu_caps
.has_f16c
) {
467 MAttrs
.push_back("+f16c");
469 builder
.setMAttrs(MAttrs
);
472 ShaderMemoryManager
*MM
= new ShaderMemoryManager();
473 *OutCode
= MM
->getGeneratedCode();
475 builder
.setJITMemoryManager(MM
);
477 ExecutionEngine
*JIT
;
479 JIT
= builder
.create();
482 * Workaround http://llvm.org/bugs/show_bug.cgi?id=12833
484 StringRef MArch
= "";
486 Triple
TT(unwrap(M
)->getTargetTriple());
487 JIT
= builder
.create(builder
.selectTarget(TT
, MArch
, MCPU
, MAttrs
));
493 lp_free_generated_code(*OutCode
);
496 *OutError
= strdup(Error
.c_str());
503 lp_free_generated_code(struct lp_generated_code
*code
)
505 ShaderMemoryManager::freeGeneratedCode(code
);