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 // Workaround http://llvm.org/PR23628
54 #if HAVE_LLVM >= 0x0307
55 # pragma push_macro("DEBUG")
59 #include <llvm-c/Core.h>
60 #include <llvm-c/ExecutionEngine.h>
61 #include <llvm/Target/TargetOptions.h>
62 #include <llvm/ExecutionEngine/ExecutionEngine.h>
63 #include <llvm/ADT/Triple.h>
64 #if HAVE_LLVM >= 0x0307
65 #include <llvm/Analysis/TargetLibraryInfo.h>
67 #include <llvm/Target/TargetLibraryInfo.h>
69 #if HAVE_LLVM < 0x0306
70 #include <llvm/ExecutionEngine/JITMemoryManager.h>
72 #include <llvm/ExecutionEngine/SectionMemoryManager.h>
74 #include <llvm/Support/CommandLine.h>
75 #include <llvm/Support/Host.h>
76 #include <llvm/Support/PrettyStackTrace.h>
78 #include <llvm/Support/TargetSelect.h>
80 #include <llvm/IR/IRBuilder.h>
81 #include <llvm/IR/Module.h>
82 #include <llvm/Support/CBindingWrapping.h>
84 // Workaround http://llvm.org/PR23628
85 #if HAVE_LLVM >= 0x0307
86 # pragma pop_macro("DEBUG")
89 #include "c11/threads.h"
90 #include "os/os_thread.h"
91 #include "pipe/p_config.h"
92 #include "util/u_debug.h"
93 #include "util/u_cpu_detect.h"
95 #include "lp_bld_misc.h"
99 class LLVMEnsureMultithreaded
{
101 LLVMEnsureMultithreaded()
103 if (!LLVMIsMultithreaded()) {
104 LLVMStartMultithreaded();
109 static LLVMEnsureMultithreaded lLVMEnsureMultithreaded
;
113 static once_flag init_native_targets_once_flag
;
115 static void init_native_targets()
117 // If we have a native target, initialize it to ensure it is linked in and
118 // usable by the JIT.
119 llvm::InitializeNativeTarget();
121 llvm::InitializeNativeTargetAsmPrinter();
123 llvm::InitializeNativeTargetDisassembler();
127 * The llvm target registry is not thread-safe, so drivers and state-trackers
128 * that want to initialize targets should use the gallivm_init_llvm_targets()
129 * function to safely initialize targets.
131 * LLVM targets should be initialized before the driver or state-tracker tries
132 * to access the registry.
135 gallivm_init_llvm_targets(void)
137 call_once(&init_native_targets_once_flag
, init_native_targets
);
141 lp_set_target_options(void)
143 #if HAVE_LLVM < 0x0304
145 * By default LLVM adds a signal handler to output a pretty stack trace.
146 * This signal handler is never removed, causing problems when unloading the
147 * shared object where the gallium driver resides.
149 llvm::DisablePrettyStackTrace
= true;
152 gallivm_init_llvm_targets();
156 LLVMTargetLibraryInfoRef
157 gallivm_create_target_library_info(const char *triple
)
159 return reinterpret_cast<LLVMTargetLibraryInfoRef
>(
160 #if HAVE_LLVM < 0x0307
161 new llvm::TargetLibraryInfo(
163 new llvm::TargetLibraryInfoImpl(
165 llvm::Triple(triple
)));
170 gallivm_dispose_target_library_info(LLVMTargetLibraryInfoRef library_info
)
172 delete reinterpret_cast<
173 #if HAVE_LLVM < 0x0307
174 llvm::TargetLibraryInfo
176 llvm::TargetLibraryInfoImpl
183 lp_build_load_volatile(LLVMBuilderRef B
, LLVMValueRef PointerVal
,
186 return llvm::wrap(llvm::unwrap(B
)->CreateLoad(llvm::unwrap(PointerVal
), true, Name
));
192 lp_set_load_alignment(LLVMValueRef Inst
,
195 llvm::unwrap
<llvm::LoadInst
>(Inst
)->setAlignment(Align
);
200 lp_set_store_alignment(LLVMValueRef Inst
,
203 llvm::unwrap
<llvm::StoreInst
>(Inst
)->setAlignment(Align
);
207 #if HAVE_LLVM < 0x0306
208 typedef llvm::JITMemoryManager BaseMemoryManager
;
210 typedef llvm::RTDyldMemoryManager BaseMemoryManager
;
215 * Delegating is tedious but the default manager class is hidden in an
216 * anonymous namespace in LLVM, so we cannot just derive from it to change
219 class DelegatingJITMemoryManager
: public BaseMemoryManager
{
222 virtual BaseMemoryManager
*mgr() const = 0;
225 #if HAVE_LLVM < 0x0306
227 * From JITMemoryManager
229 virtual void setMemoryWritable() {
230 mgr()->setMemoryWritable();
232 virtual void setMemoryExecutable() {
233 mgr()->setMemoryExecutable();
235 virtual void setPoisonMemory(bool poison
) {
236 mgr()->setPoisonMemory(poison
);
238 virtual void AllocateGOT() {
239 mgr()->AllocateGOT();
241 * isManagingGOT() is not virtual in base class so we can't delegate.
242 * Instead we mirror the value of HasGOT in our instance.
244 HasGOT
= mgr()->isManagingGOT();
246 virtual uint8_t *getGOTBase() const {
247 return mgr()->getGOTBase();
249 virtual uint8_t *startFunctionBody(const llvm::Function
*F
,
250 uintptr_t &ActualSize
) {
251 return mgr()->startFunctionBody(F
, ActualSize
);
253 virtual uint8_t *allocateStub(const llvm::GlobalValue
*F
,
255 unsigned Alignment
) {
256 return mgr()->allocateStub(F
, StubSize
, Alignment
);
258 virtual void endFunctionBody(const llvm::Function
*F
,
259 uint8_t *FunctionStart
,
260 uint8_t *FunctionEnd
) {
261 mgr()->endFunctionBody(F
, FunctionStart
, FunctionEnd
);
263 virtual uint8_t *allocateSpace(intptr_t Size
, unsigned Alignment
) {
264 return mgr()->allocateSpace(Size
, Alignment
);
266 virtual uint8_t *allocateGlobal(uintptr_t Size
, unsigned Alignment
) {
267 return mgr()->allocateGlobal(Size
, Alignment
);
269 virtual void deallocateFunctionBody(void *Body
) {
270 mgr()->deallocateFunctionBody(Body
);
272 #if HAVE_LLVM < 0x0304
273 virtual uint8_t *startExceptionTable(const llvm::Function
*F
,
274 uintptr_t &ActualSize
) {
275 return mgr()->startExceptionTable(F
, ActualSize
);
277 virtual void endExceptionTable(const llvm::Function
*F
,
280 uint8_t *FrameRegister
) {
281 mgr()->endExceptionTable(F
, TableStart
, TableEnd
,
284 virtual void deallocateExceptionTable(void *ET
) {
285 mgr()->deallocateExceptionTable(ET
);
288 virtual bool CheckInvariants(std::string
&s
) {
289 return mgr()->CheckInvariants(s
);
291 virtual size_t GetDefaultCodeSlabSize() {
292 return mgr()->GetDefaultCodeSlabSize();
294 virtual size_t GetDefaultDataSlabSize() {
295 return mgr()->GetDefaultDataSlabSize();
297 virtual size_t GetDefaultStubSlabSize() {
298 return mgr()->GetDefaultStubSlabSize();
300 virtual unsigned GetNumCodeSlabs() {
301 return mgr()->GetNumCodeSlabs();
303 virtual unsigned GetNumDataSlabs() {
304 return mgr()->GetNumDataSlabs();
306 virtual unsigned GetNumStubSlabs() {
307 return mgr()->GetNumStubSlabs();
312 * From RTDyldMemoryManager
314 #if HAVE_LLVM >= 0x0304
315 virtual uint8_t *allocateCodeSection(uintptr_t Size
,
318 llvm::StringRef SectionName
) {
319 return mgr()->allocateCodeSection(Size
, Alignment
, SectionID
,
323 virtual uint8_t *allocateCodeSection(uintptr_t Size
,
325 unsigned SectionID
) {
326 return mgr()->allocateCodeSection(Size
, Alignment
, SectionID
);
329 virtual uint8_t *allocateDataSection(uintptr_t Size
,
332 #if HAVE_LLVM >= 0x0304
333 llvm::StringRef SectionName
,
336 return mgr()->allocateDataSection(Size
, Alignment
, SectionID
,
337 #if HAVE_LLVM >= 0x0304
342 #if HAVE_LLVM >= 0x0304
343 virtual void registerEHFrames(uint8_t *Addr
, uint64_t LoadAddr
, size_t Size
) {
344 mgr()->registerEHFrames(Addr
, LoadAddr
, Size
);
346 virtual void deregisterEHFrames(uint8_t *Addr
, uint64_t LoadAddr
, size_t Size
) {
347 mgr()->deregisterEHFrames(Addr
, LoadAddr
, Size
);
350 virtual void registerEHFrames(llvm::StringRef SectionData
) {
351 mgr()->registerEHFrames(SectionData
);
354 virtual void *getPointerToNamedFunction(const std::string
&Name
,
355 bool AbortOnFailure
=true) {
356 return mgr()->getPointerToNamedFunction(Name
, AbortOnFailure
);
358 #if HAVE_LLVM <= 0x0303
359 virtual bool applyPermissions(std::string
*ErrMsg
= 0) {
360 return mgr()->applyPermissions(ErrMsg
);
363 virtual bool finalizeMemory(std::string
*ErrMsg
= 0) {
364 return mgr()->finalizeMemory(ErrMsg
);
371 * Delegate memory management to one shared manager for more efficient use
372 * of memory than creating a separate pool for each LLVM engine.
373 * Keep generated code until freeGeneratedCode() is called, instead of when
374 * memory manager is destroyed, which happens during engine destruction.
375 * This allows additional memory savings as we don't have to keep the engine
376 * around in order to use the code.
377 * All methods are delegated to the shared manager except destruction and
378 * deallocating code. For the latter we just remember what needs to be
379 * deallocated later. The shared manager is deleted once it is empty.
381 class ShaderMemoryManager
: public DelegatingJITMemoryManager
{
383 BaseMemoryManager
*TheMM
;
385 struct GeneratedCode
{
386 typedef std::vector
<void *> Vec
;
387 Vec FunctionBody
, ExceptionTable
;
388 BaseMemoryManager
*TheMM
;
390 GeneratedCode(BaseMemoryManager
*MM
) {
396 * Deallocate things as previously requested and
397 * free shared manager when no longer used.
399 #if HAVE_LLVM < 0x0306
403 for ( i
= FunctionBody
.begin(); i
!= FunctionBody
.end(); ++i
)
404 TheMM
->deallocateFunctionBody(*i
);
405 #if HAVE_LLVM < 0x0304
406 for ( i
= ExceptionTable
.begin(); i
!= ExceptionTable
.end(); ++i
)
407 TheMM
->deallocateExceptionTable(*i
);
408 #endif /* HAVE_LLVM < 0x0304 */
409 #endif /* HAVE_LLVM < 0x0306 */
415 BaseMemoryManager
*mgr() const {
421 ShaderMemoryManager(BaseMemoryManager
* MM
) {
423 code
= new GeneratedCode(MM
);
426 virtual ~ShaderMemoryManager() {
428 * 'code' is purposely not deleted. It is the user's responsibility
429 * to call getGeneratedCode() and freeGeneratedCode().
433 struct lp_generated_code
*getGeneratedCode() {
434 return (struct lp_generated_code
*) code
;
437 static void freeGeneratedCode(struct lp_generated_code
*code
) {
438 delete (GeneratedCode
*) code
;
441 #if HAVE_LLVM < 0x0304
442 virtual void deallocateExceptionTable(void *ET
) {
443 // remember for later deallocation
444 code
->ExceptionTable
.push_back(ET
);
448 virtual void deallocateFunctionBody(void *Body
) {
449 // remember for later deallocation
450 code
->FunctionBody
.push_back(Body
);
456 * Same as LLVMCreateJITCompilerForModule, but:
457 * - allows using MCJIT and enabling AVX feature where available.
458 * - set target options
461 * - llvm/lib/ExecutionEngine/ExecutionEngineBindings.cpp
462 * - llvm/tools/lli/lli.cpp
463 * - http://markmail.org/message/ttkuhvgj4cxxy2on#query:+page:1+mid:aju2dggerju3ivd3+state:results
467 lp_build_create_jit_compiler_for_module(LLVMExecutionEngineRef
*OutJIT
,
468 lp_generated_code
**OutCode
,
470 LLVMMCJITMemoryManagerRef CMM
,
475 using namespace llvm
;
478 #if HAVE_LLVM >= 0x0306
479 EngineBuilder
builder(std::unique_ptr
<Module
>(unwrap(M
)));
481 EngineBuilder
builder(unwrap(M
));
485 * LLVM 3.1+ haven't more "extern unsigned llvm::StackAlignmentOverride" and
486 * friends for configuring code generation options, like stack alignment.
488 TargetOptions options
;
489 #if defined(PIPE_ARCH_X86)
490 options
.StackAlignmentOverride
= 4;
491 #if HAVE_LLVM < 0x0304
492 options
.RealignStack
= true;
496 #if defined(DEBUG) && HAVE_LLVM < 0x0307
497 options
.JITEmitDebugInfo
= true;
500 /* XXX: Workaround http://llvm.org/PR21435 */
501 #if defined(DEBUG) || defined(PROFILE) || \
502 (HAVE_LLVM >= 0x0303 && (defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)))
503 #if HAVE_LLVM < 0x0304
504 options
.NoFramePointerElimNonLeaf
= true;
506 #if HAVE_LLVM < 0x0307
507 options
.NoFramePointerElim
= true;
511 builder
.setEngineKind(EngineKind::JIT
)
513 .setTargetOptions(options
)
514 .setOptLevel((CodeGenOpt::Level
)OptLevel
);
517 #if HAVE_LLVM < 0x0306
518 builder
.setUseMCJIT(true);
522 * MCJIT works on Windows, but currently only through ELF object format.
524 std::string targetTriple
= llvm::sys::getProcessTriple();
525 targetTriple
.append("-elf");
526 unwrap(M
)->setTargetTriple(targetTriple
);
530 llvm::SmallVector
<std::string
, 16> MAttrs
;
532 #if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
534 * We need to unset attributes because sometimes LLVM mistakenly assumes
535 * certain features are present given the processor name.
537 * https://bugs.freedesktop.org/show_bug.cgi?id=92214
538 * http://llvm.org/PR25021
539 * http://llvm.org/PR19429
540 * http://llvm.org/PR16721
542 MAttrs
.push_back(util_cpu_caps
.has_sse
? "+sse" : "-sse" );
543 MAttrs
.push_back(util_cpu_caps
.has_sse2
? "+sse2" : "-sse2" );
544 MAttrs
.push_back(util_cpu_caps
.has_sse3
? "+sse3" : "-sse3" );
545 MAttrs
.push_back(util_cpu_caps
.has_ssse3
? "+ssse3" : "-ssse3" );
546 #if HAVE_LLVM >= 0x0304
547 MAttrs
.push_back(util_cpu_caps
.has_sse4_1
? "+sse4.1" : "-sse4.1");
549 MAttrs
.push_back(util_cpu_caps
.has_sse4_1
? "+sse41" : "-sse41" );
551 #if HAVE_LLVM >= 0x0304
552 MAttrs
.push_back(util_cpu_caps
.has_sse4_2
? "+sse4.2" : "-sse4.2");
554 MAttrs
.push_back(util_cpu_caps
.has_sse4_2
? "+sse42" : "-sse42" );
557 * AVX feature is not automatically detected from CPUID by the X86 target
558 * yet, because the old (yet default) JIT engine is not capable of
559 * emitting the opcodes. On newer llvm versions it is and at least some
560 * versions (tested with 3.3) will emit avx opcodes without this anyway.
562 MAttrs
.push_back(util_cpu_caps
.has_avx
? "+avx" : "-avx");
563 MAttrs
.push_back(util_cpu_caps
.has_f16c
? "+f16c" : "-f16c");
564 MAttrs
.push_back(util_cpu_caps
.has_avx2
? "+avx2" : "-avx2");
567 #if defined(PIPE_ARCH_PPC)
568 MAttrs
.push_back(util_cpu_caps
.has_altivec
? "+altivec" : "-altivec");
569 #if HAVE_LLVM >= 0x0304
571 * Make sure VSX instructions are disabled
572 * See LLVM bug https://llvm.org/bugs/show_bug.cgi?id=25503#c7
574 if (util_cpu_caps
.has_altivec
) {
575 MAttrs
.push_back("-vsx");
580 builder
.setMAttrs(MAttrs
);
582 #if HAVE_LLVM >= 0x0305
583 StringRef MCPU
= llvm::sys::getHostCPUName();
585 * The cpu bits are no longer set automatically, so need to set mcpu manually.
586 * Note that the MAttrs set above will be sort of ignored (since we should
587 * not set any which would not be set by specifying the cpu anyway).
588 * It ought to be safe though since getHostCPUName() should include bits
589 * not only from the cpu but environment as well (for instance if it's safe
590 * to use avx instructions which need OS support). According to
591 * http://llvm.org/bugs/show_bug.cgi?id=19429 however if I understand this
592 * right it may be necessary to specify older cpu (or disable mattrs) though
593 * when not using MCJIT so no instructions are generated which the old JIT
594 * can't handle. Not entirely sure if we really need to do anything yet.
596 builder
.setMCPU(MCPU
);
599 ShaderMemoryManager
*MM
= NULL
;
601 #if HAVE_LLVM > 0x0303
602 BaseMemoryManager
* JMM
= reinterpret_cast<BaseMemoryManager
*>(CMM
);
603 MM
= new ShaderMemoryManager(JMM
);
604 *OutCode
= MM
->getGeneratedCode();
606 #if HAVE_LLVM >= 0x0306
607 builder
.setMCJITMemoryManager(std::unique_ptr
<RTDyldMemoryManager
>(MM
));
608 MM
= NULL
; // ownership taken by std::unique_ptr
610 builder
.setMCJITMemoryManager(MM
);
614 #if HAVE_LLVM < 0x0306
615 BaseMemoryManager
* JMM
= reinterpret_cast<BaseMemoryManager
*>(CMM
);
616 MM
= new ShaderMemoryManager(JMM
);
617 *OutCode
= MM
->getGeneratedCode();
619 builder
.setJITMemoryManager(MM
);
625 ExecutionEngine
*JIT
;
627 JIT
= builder
.create();
632 lp_free_generated_code(*OutCode
);
635 *OutError
= strdup(Error
.c_str());
642 lp_free_generated_code(struct lp_generated_code
*code
)
644 ShaderMemoryManager::freeGeneratedCode(code
);
648 LLVMMCJITMemoryManagerRef
649 lp_get_default_memory_manager()
651 BaseMemoryManager
*mm
;
652 #if HAVE_LLVM < 0x0306
653 mm
= llvm::JITMemoryManager::CreateDefaultMemManager();
655 mm
= new llvm::SectionMemoryManager();
657 return reinterpret_cast<LLVMMCJITMemoryManagerRef
>(mm
);
662 lp_free_memory_manager(LLVMMCJITMemoryManagerRef memorymgr
)
664 delete reinterpret_cast<BaseMemoryManager
*>(memorymgr
);