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 #if HAVE_LLVM < 0x0306
59 #include <llvm/ExecutionEngine/JITMemoryManager.h>
61 #include <llvm/Support/CommandLine.h>
62 #include <llvm/Support/Host.h>
63 #include <llvm/Support/PrettyStackTrace.h>
65 #include <llvm/Support/TargetSelect.h>
67 #if HAVE_LLVM >= 0x0303
68 #include <llvm/IR/IRBuilder.h>
69 #include <llvm/IR/Module.h>
70 #include <llvm/Support/CBindingWrapping.h>
73 #include "pipe/p_config.h"
74 #include "util/u_debug.h"
75 #include "util/u_cpu_detect.h"
77 #include "lp_bld_misc.h"
81 class LLVMEnsureMultithreaded
{
83 LLVMEnsureMultithreaded()
85 #if HAVE_LLVM < 0x0303
86 if (!llvm::llvm_is_multithreaded()) {
87 llvm::llvm_start_multithreaded();
90 if (!LLVMIsMultithreaded()) {
91 LLVMStartMultithreaded();
97 static LLVMEnsureMultithreaded lLVMEnsureMultithreaded
;
102 lp_set_target_options(void)
104 #if HAVE_LLVM < 0x0304
106 * By default LLVM adds a signal handler to output a pretty stack trace.
107 * This signal handler is never removed, causing problems when unloading the
108 * shared object where the gallium driver resides.
110 llvm::DisablePrettyStackTrace
= true;
113 // If we have a native target, initialize it to ensure it is linked in and
114 // usable by the JIT.
115 llvm::InitializeNativeTarget();
117 llvm::InitializeNativeTargetAsmPrinter();
119 llvm::InitializeNativeTargetDisassembler();
125 lp_build_load_volatile(LLVMBuilderRef B
, LLVMValueRef PointerVal
,
128 return llvm::wrap(llvm::unwrap(B
)->CreateLoad(llvm::unwrap(PointerVal
), true, Name
));
134 lp_set_load_alignment(LLVMValueRef Inst
,
137 llvm::unwrap
<llvm::LoadInst
>(Inst
)->setAlignment(Align
);
142 lp_set_store_alignment(LLVMValueRef Inst
,
145 llvm::unwrap
<llvm::StoreInst
>(Inst
)->setAlignment(Align
);
148 #if HAVE_LLVM < 0x0306
151 * Delegating is tedious but the default manager class is hidden in an
152 * anonymous namespace in LLVM, so we cannot just derive from it to change
155 class DelegatingJITMemoryManager
: public llvm::JITMemoryManager
{
158 virtual llvm::JITMemoryManager
*mgr() const = 0;
162 * From JITMemoryManager
164 virtual void setMemoryWritable() {
165 mgr()->setMemoryWritable();
167 virtual void setMemoryExecutable() {
168 mgr()->setMemoryExecutable();
170 virtual void setPoisonMemory(bool poison
) {
171 mgr()->setPoisonMemory(poison
);
173 virtual void AllocateGOT() {
174 mgr()->AllocateGOT();
176 * isManagingGOT() is not virtual in base class so we can't delegate.
177 * Instead we mirror the value of HasGOT in our instance.
179 HasGOT
= mgr()->isManagingGOT();
181 virtual uint8_t *getGOTBase() const {
182 return mgr()->getGOTBase();
184 virtual uint8_t *startFunctionBody(const llvm::Function
*F
,
185 uintptr_t &ActualSize
) {
186 return mgr()->startFunctionBody(F
, ActualSize
);
188 virtual uint8_t *allocateStub(const llvm::GlobalValue
*F
,
190 unsigned Alignment
) {
191 return mgr()->allocateStub(F
, StubSize
, Alignment
);
193 virtual void endFunctionBody(const llvm::Function
*F
,
194 uint8_t *FunctionStart
,
195 uint8_t *FunctionEnd
) {
196 mgr()->endFunctionBody(F
, FunctionStart
, FunctionEnd
);
198 virtual uint8_t *allocateSpace(intptr_t Size
, unsigned Alignment
) {
199 return mgr()->allocateSpace(Size
, Alignment
);
201 virtual uint8_t *allocateGlobal(uintptr_t Size
, unsigned Alignment
) {
202 return mgr()->allocateGlobal(Size
, Alignment
);
204 virtual void deallocateFunctionBody(void *Body
) {
205 mgr()->deallocateFunctionBody(Body
);
207 #if HAVE_LLVM < 0x0304
208 virtual uint8_t *startExceptionTable(const llvm::Function
*F
,
209 uintptr_t &ActualSize
) {
210 return mgr()->startExceptionTable(F
, ActualSize
);
212 virtual void endExceptionTable(const llvm::Function
*F
,
215 uint8_t *FrameRegister
) {
216 mgr()->endExceptionTable(F
, TableStart
, TableEnd
,
219 virtual void deallocateExceptionTable(void *ET
) {
220 mgr()->deallocateExceptionTable(ET
);
223 virtual bool CheckInvariants(std::string
&s
) {
224 return mgr()->CheckInvariants(s
);
226 virtual size_t GetDefaultCodeSlabSize() {
227 return mgr()->GetDefaultCodeSlabSize();
229 virtual size_t GetDefaultDataSlabSize() {
230 return mgr()->GetDefaultDataSlabSize();
232 virtual size_t GetDefaultStubSlabSize() {
233 return mgr()->GetDefaultStubSlabSize();
235 virtual unsigned GetNumCodeSlabs() {
236 return mgr()->GetNumCodeSlabs();
238 virtual unsigned GetNumDataSlabs() {
239 return mgr()->GetNumDataSlabs();
241 virtual unsigned GetNumStubSlabs() {
242 return mgr()->GetNumStubSlabs();
246 * From RTDyldMemoryManager
248 #if HAVE_LLVM >= 0x0304
249 virtual uint8_t *allocateCodeSection(uintptr_t Size
,
252 llvm::StringRef SectionName
) {
253 return mgr()->allocateCodeSection(Size
, Alignment
, SectionID
,
257 virtual uint8_t *allocateCodeSection(uintptr_t Size
,
259 unsigned SectionID
) {
260 return mgr()->allocateCodeSection(Size
, Alignment
, SectionID
);
263 #if HAVE_LLVM >= 0x0303
264 virtual uint8_t *allocateDataSection(uintptr_t Size
,
267 #if HAVE_LLVM >= 0x0304
268 llvm::StringRef SectionName
,
271 return mgr()->allocateDataSection(Size
, Alignment
, SectionID
,
272 #if HAVE_LLVM >= 0x0304
277 #if HAVE_LLVM >= 0x0304
278 virtual void registerEHFrames(uint8_t *Addr
, uint64_t LoadAddr
, size_t Size
) {
279 mgr()->registerEHFrames(Addr
, LoadAddr
, Size
);
281 virtual void deregisterEHFrames(uint8_t *Addr
, uint64_t LoadAddr
, size_t Size
) {
282 mgr()->deregisterEHFrames(Addr
, LoadAddr
, Size
);
285 virtual void registerEHFrames(llvm::StringRef SectionData
) {
286 mgr()->registerEHFrames(SectionData
);
290 virtual uint8_t *allocateDataSection(uintptr_t Size
,
292 unsigned SectionID
) {
293 return mgr()->allocateDataSection(Size
, Alignment
, SectionID
);
296 virtual void *getPointerToNamedFunction(const std::string
&Name
,
297 bool AbortOnFailure
=true) {
298 return mgr()->getPointerToNamedFunction(Name
, AbortOnFailure
);
300 #if HAVE_LLVM == 0x0303
301 virtual bool applyPermissions(std::string
*ErrMsg
= 0) {
302 return mgr()->applyPermissions(ErrMsg
);
304 #elif HAVE_LLVM > 0x0303
305 virtual bool finalizeMemory(std::string
*ErrMsg
= 0) {
306 return mgr()->finalizeMemory(ErrMsg
);
313 * Delegate memory management to one shared manager for more efficient use
314 * of memory than creating a separate pool for each LLVM engine.
315 * Keep generated code until freeGeneratedCode() is called, instead of when
316 * memory manager is destroyed, which happens during engine destruction.
317 * This allows additional memory savings as we don't have to keep the engine
318 * around in order to use the code.
319 * All methods are delegated to the shared manager except destruction and
320 * deallocating code. For the latter we just remember what needs to be
321 * deallocated later. The shared manager is deleted once it is empty.
323 class ShaderMemoryManager
: public DelegatingJITMemoryManager
{
325 static llvm::JITMemoryManager
*TheMM
;
326 static unsigned NumUsers
;
328 struct GeneratedCode
{
329 typedef std::vector
<void *> Vec
;
330 Vec FunctionBody
, ExceptionTable
;
338 * Deallocate things as previously requested and
339 * free shared manager when no longer used.
344 for ( i
= FunctionBody
.begin(); i
!= FunctionBody
.end(); ++i
)
345 TheMM
->deallocateFunctionBody(*i
);
346 #if HAVE_LLVM < 0x0304
347 for ( i
= ExceptionTable
.begin(); i
!= ExceptionTable
.end(); ++i
)
348 TheMM
->deallocateExceptionTable(*i
);
360 llvm::JITMemoryManager
*mgr() const {
362 TheMM
= CreateDefaultMemManager();
369 ShaderMemoryManager() {
370 code
= new GeneratedCode
;
373 virtual ~ShaderMemoryManager() {
375 * 'code' is purposely not deleted. It is the user's responsibility
376 * to call getGeneratedCode() and freeGeneratedCode().
380 struct lp_generated_code
*getGeneratedCode() {
381 return (struct lp_generated_code
*) code
;
384 static void freeGeneratedCode(struct lp_generated_code
*code
) {
385 delete (GeneratedCode
*) code
;
388 #if HAVE_LLVM < 0x0304
389 virtual void deallocateExceptionTable(void *ET
) {
390 // remember for later deallocation
391 code
->ExceptionTable
.push_back(ET
);
395 virtual void deallocateFunctionBody(void *Body
) {
396 // remember for later deallocation
397 code
->FunctionBody
.push_back(Body
);
401 llvm::JITMemoryManager
*ShaderMemoryManager::TheMM
= 0;
402 unsigned ShaderMemoryManager::NumUsers
= 0;
407 * Same as LLVMCreateJITCompilerForModule, but:
408 * - allows using MCJIT and enabling AVX feature where available.
409 * - set target options
412 * - llvm/lib/ExecutionEngine/ExecutionEngineBindings.cpp
413 * - llvm/tools/lli/lli.cpp
414 * - http://markmail.org/message/ttkuhvgj4cxxy2on#query:+page:1+mid:aju2dggerju3ivd3+state:results
418 lp_build_create_jit_compiler_for_module(LLVMExecutionEngineRef
*OutJIT
,
419 lp_generated_code
**OutCode
,
425 using namespace llvm
;
427 #if HAVE_LLVM >= 0x0306
428 *OutError
= strdup("MCJIT not supported");
433 #if HAVE_LLVM >= 0x0306
434 EngineBuilder
builder(std::unique_ptr
<Module
>(unwrap(M
)));
436 EngineBuilder
builder(unwrap(M
));
440 * LLVM 3.1+ haven't more "extern unsigned llvm::StackAlignmentOverride" and
441 * friends for configuring code generation options, like stack alignment.
443 TargetOptions options
;
444 #if defined(PIPE_ARCH_X86)
445 options
.StackAlignmentOverride
= 4;
446 #if HAVE_LLVM < 0x0304
447 options
.RealignStack
= true;
452 options
.JITEmitDebugInfo
= true;
455 #if defined(DEBUG) || defined(PROFILE)
456 #if HAVE_LLVM < 0x0304
457 options
.NoFramePointerElimNonLeaf
= true;
459 options
.NoFramePointerElim
= true;
462 builder
.setEngineKind(EngineKind::JIT
)
464 .setTargetOptions(options
)
465 .setOptLevel((CodeGenOpt::Level
)OptLevel
);
468 #if HAVE_LLVM < 0x0306
469 builder
.setUseMCJIT(true);
473 * MCJIT works on Windows, but currently only through ELF object format.
475 std::string targetTriple
= llvm::sys::getProcessTriple();
476 targetTriple
.append("-elf");
477 unwrap(M
)->setTargetTriple(targetTriple
);
481 llvm::SmallVector
<std::string
, 1> MAttrs
;
482 if (util_cpu_caps
.has_avx
) {
484 * AVX feature is not automatically detected from CPUID by the X86 target
485 * yet, because the old (yet default) JIT engine is not capable of
486 * emitting the opcodes. On newer llvm versions it is and at least some
487 * versions (tested with 3.3) will emit avx opcodes without this anyway.
489 MAttrs
.push_back("+avx");
490 if (util_cpu_caps
.has_f16c
) {
491 MAttrs
.push_back("+f16c");
493 builder
.setMAttrs(MAttrs
);
496 #if HAVE_LLVM >= 0x0305
497 StringRef MCPU
= llvm::sys::getHostCPUName();
499 * The cpu bits are no longer set automatically, so need to set mcpu manually.
500 * Note that the MAttrs set above will be sort of ignored (since we should
501 * not set any which would not be set by specifying the cpu anyway).
502 * It ought to be safe though since getHostCPUName() should include bits
503 * not only from the cpu but environment as well (for instance if it's safe
504 * to use avx instructions which need OS support). According to
505 * http://llvm.org/bugs/show_bug.cgi?id=19429 however if I understand this
506 * right it may be necessary to specify older cpu (or disable mattrs) though
507 * when not using MCJIT so no instructions are generated which the old JIT
508 * can't handle. Not entirely sure if we really need to do anything yet.
510 builder
.setMCPU(MCPU
);
513 ShaderMemoryManager
*MM
= new ShaderMemoryManager();
514 *OutCode
= MM
->getGeneratedCode();
516 builder
.setJITMemoryManager(MM
);
518 ExecutionEngine
*JIT
;
520 #if HAVE_LLVM >= 0x0302
521 JIT
= builder
.create();
524 * Workaround http://llvm.org/PR12833
526 StringRef MArch
= "";
528 Triple
TT(unwrap(M
)->getTargetTriple());
529 JIT
= builder
.create(builder
.selectTarget(TT
, MArch
, MCPU
, MAttrs
));
535 lp_free_generated_code(*OutCode
);
538 *OutError
= strdup(Error
.c_str());
546 lp_free_generated_code(struct lp_generated_code
*code
)
548 #if HAVE_LLVM < 0x0306
549 ShaderMemoryManager::freeGeneratedCode(code
);