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 #if HAVE_LLVM >= 0x0305
81 #include <llvm/IR/CallSite.h>
83 #include <llvm/IR/IRBuilder.h>
84 #include <llvm/IR/Module.h>
85 #include <llvm/Support/CBindingWrapping.h>
87 #include <llvm/Config/llvm-config.h>
88 #if LLVM_USE_INTEL_JITEVENTS
89 #include <llvm/ExecutionEngine/JITEventListener.h>
92 // Workaround http://llvm.org/PR23628
93 #if HAVE_LLVM >= 0x0307
94 # pragma pop_macro("DEBUG")
97 #include "c11/threads.h"
98 #include "os/os_thread.h"
99 #include "pipe/p_config.h"
100 #include "util/u_debug.h"
101 #include "util/u_cpu_detect.h"
103 #include "lp_bld_misc.h"
107 class LLVMEnsureMultithreaded
{
109 LLVMEnsureMultithreaded()
111 if (!LLVMIsMultithreaded()) {
112 LLVMStartMultithreaded();
117 static LLVMEnsureMultithreaded lLVMEnsureMultithreaded
;
121 static once_flag init_native_targets_once_flag
= ONCE_FLAG_INIT
;
123 static void init_native_targets()
125 // If we have a native target, initialize it to ensure it is linked in and
126 // usable by the JIT.
127 llvm::InitializeNativeTarget();
129 llvm::InitializeNativeTargetAsmPrinter();
131 llvm::InitializeNativeTargetDisassembler();
135 * The llvm target registry is not thread-safe, so drivers and state-trackers
136 * that want to initialize targets should use the gallivm_init_llvm_targets()
137 * function to safely initialize targets.
139 * LLVM targets should be initialized before the driver or state-tracker tries
140 * to access the registry.
143 gallivm_init_llvm_targets(void)
145 call_once(&init_native_targets_once_flag
, init_native_targets
);
149 lp_set_target_options(void)
151 #if HAVE_LLVM < 0x0304
153 * By default LLVM adds a signal handler to output a pretty stack trace.
154 * This signal handler is never removed, causing problems when unloading the
155 * shared object where the gallium driver resides.
157 llvm::DisablePrettyStackTrace
= true;
160 gallivm_init_llvm_targets();
164 LLVMTargetLibraryInfoRef
165 gallivm_create_target_library_info(const char *triple
)
167 return reinterpret_cast<LLVMTargetLibraryInfoRef
>(
168 #if HAVE_LLVM < 0x0307
169 new llvm::TargetLibraryInfo(
171 new llvm::TargetLibraryInfoImpl(
173 llvm::Triple(triple
)));
178 gallivm_dispose_target_library_info(LLVMTargetLibraryInfoRef library_info
)
180 delete reinterpret_cast<
181 #if HAVE_LLVM < 0x0307
182 llvm::TargetLibraryInfo
184 llvm::TargetLibraryInfoImpl
190 #if HAVE_LLVM < 0x0304
194 LLVMSetAlignmentBackport(LLVMValueRef V
,
197 switch (LLVMGetInstructionOpcode(V
)) {
199 llvm::unwrap
<llvm::LoadInst
>(V
)->setAlignment(Bytes
);
202 llvm::unwrap
<llvm::StoreInst
>(V
)->setAlignment(Bytes
);
213 #if HAVE_LLVM < 0x0306
214 typedef llvm::JITMemoryManager BaseMemoryManager
;
216 typedef llvm::RTDyldMemoryManager BaseMemoryManager
;
221 * Delegating is tedious but the default manager class is hidden in an
222 * anonymous namespace in LLVM, so we cannot just derive from it to change
225 class DelegatingJITMemoryManager
: public BaseMemoryManager
{
228 virtual BaseMemoryManager
*mgr() const = 0;
231 #if HAVE_LLVM < 0x0306
233 * From JITMemoryManager
235 virtual void setMemoryWritable() {
236 mgr()->setMemoryWritable();
238 virtual void setMemoryExecutable() {
239 mgr()->setMemoryExecutable();
241 virtual void setPoisonMemory(bool poison
) {
242 mgr()->setPoisonMemory(poison
);
244 virtual void AllocateGOT() {
245 mgr()->AllocateGOT();
247 * isManagingGOT() is not virtual in base class so we can't delegate.
248 * Instead we mirror the value of HasGOT in our instance.
250 HasGOT
= mgr()->isManagingGOT();
252 virtual uint8_t *getGOTBase() const {
253 return mgr()->getGOTBase();
255 virtual uint8_t *startFunctionBody(const llvm::Function
*F
,
256 uintptr_t &ActualSize
) {
257 return mgr()->startFunctionBody(F
, ActualSize
);
259 virtual uint8_t *allocateStub(const llvm::GlobalValue
*F
,
261 unsigned Alignment
) {
262 return mgr()->allocateStub(F
, StubSize
, Alignment
);
264 virtual void endFunctionBody(const llvm::Function
*F
,
265 uint8_t *FunctionStart
,
266 uint8_t *FunctionEnd
) {
267 mgr()->endFunctionBody(F
, FunctionStart
, FunctionEnd
);
269 virtual uint8_t *allocateSpace(intptr_t Size
, unsigned Alignment
) {
270 return mgr()->allocateSpace(Size
, Alignment
);
272 virtual uint8_t *allocateGlobal(uintptr_t Size
, unsigned Alignment
) {
273 return mgr()->allocateGlobal(Size
, Alignment
);
275 virtual void deallocateFunctionBody(void *Body
) {
276 mgr()->deallocateFunctionBody(Body
);
278 #if HAVE_LLVM < 0x0304
279 virtual uint8_t *startExceptionTable(const llvm::Function
*F
,
280 uintptr_t &ActualSize
) {
281 return mgr()->startExceptionTable(F
, ActualSize
);
283 virtual void endExceptionTable(const llvm::Function
*F
,
286 uint8_t *FrameRegister
) {
287 mgr()->endExceptionTable(F
, TableStart
, TableEnd
,
290 virtual void deallocateExceptionTable(void *ET
) {
291 mgr()->deallocateExceptionTable(ET
);
294 virtual bool CheckInvariants(std::string
&s
) {
295 return mgr()->CheckInvariants(s
);
297 virtual size_t GetDefaultCodeSlabSize() {
298 return mgr()->GetDefaultCodeSlabSize();
300 virtual size_t GetDefaultDataSlabSize() {
301 return mgr()->GetDefaultDataSlabSize();
303 virtual size_t GetDefaultStubSlabSize() {
304 return mgr()->GetDefaultStubSlabSize();
306 virtual unsigned GetNumCodeSlabs() {
307 return mgr()->GetNumCodeSlabs();
309 virtual unsigned GetNumDataSlabs() {
310 return mgr()->GetNumDataSlabs();
312 virtual unsigned GetNumStubSlabs() {
313 return mgr()->GetNumStubSlabs();
318 * From RTDyldMemoryManager
320 #if HAVE_LLVM >= 0x0304
321 virtual uint8_t *allocateCodeSection(uintptr_t Size
,
324 llvm::StringRef SectionName
) {
325 return mgr()->allocateCodeSection(Size
, Alignment
, SectionID
,
329 virtual uint8_t *allocateCodeSection(uintptr_t Size
,
331 unsigned SectionID
) {
332 return mgr()->allocateCodeSection(Size
, Alignment
, SectionID
);
335 virtual uint8_t *allocateDataSection(uintptr_t Size
,
338 #if HAVE_LLVM >= 0x0304
339 llvm::StringRef SectionName
,
342 return mgr()->allocateDataSection(Size
, Alignment
, SectionID
,
343 #if HAVE_LLVM >= 0x0304
348 #if HAVE_LLVM >= 0x0304
349 virtual void registerEHFrames(uint8_t *Addr
, uint64_t LoadAddr
, size_t Size
) {
350 mgr()->registerEHFrames(Addr
, LoadAddr
, Size
);
352 virtual void deregisterEHFrames(uint8_t *Addr
, uint64_t LoadAddr
, size_t Size
) {
353 mgr()->deregisterEHFrames(Addr
, LoadAddr
, Size
);
356 virtual void registerEHFrames(llvm::StringRef SectionData
) {
357 mgr()->registerEHFrames(SectionData
);
360 virtual void *getPointerToNamedFunction(const std::string
&Name
,
361 bool AbortOnFailure
=true) {
362 return mgr()->getPointerToNamedFunction(Name
, AbortOnFailure
);
364 #if HAVE_LLVM <= 0x0303
365 virtual bool applyPermissions(std::string
*ErrMsg
= 0) {
366 return mgr()->applyPermissions(ErrMsg
);
369 virtual bool finalizeMemory(std::string
*ErrMsg
= 0) {
370 return mgr()->finalizeMemory(ErrMsg
);
377 * Delegate memory management to one shared manager for more efficient use
378 * of memory than creating a separate pool for each LLVM engine.
379 * Keep generated code until freeGeneratedCode() is called, instead of when
380 * memory manager is destroyed, which happens during engine destruction.
381 * This allows additional memory savings as we don't have to keep the engine
382 * around in order to use the code.
383 * All methods are delegated to the shared manager except destruction and
384 * deallocating code. For the latter we just remember what needs to be
385 * deallocated later. The shared manager is deleted once it is empty.
387 class ShaderMemoryManager
: public DelegatingJITMemoryManager
{
389 BaseMemoryManager
*TheMM
;
391 struct GeneratedCode
{
392 typedef std::vector
<void *> Vec
;
393 Vec FunctionBody
, ExceptionTable
;
394 BaseMemoryManager
*TheMM
;
396 GeneratedCode(BaseMemoryManager
*MM
) {
402 * Deallocate things as previously requested and
403 * free shared manager when no longer used.
405 #if HAVE_LLVM < 0x0306
409 for ( i
= FunctionBody
.begin(); i
!= FunctionBody
.end(); ++i
)
410 TheMM
->deallocateFunctionBody(*i
);
411 #if HAVE_LLVM < 0x0304
412 for ( i
= ExceptionTable
.begin(); i
!= ExceptionTable
.end(); ++i
)
413 TheMM
->deallocateExceptionTable(*i
);
414 #endif /* HAVE_LLVM < 0x0304 */
415 #endif /* HAVE_LLVM < 0x0306 */
421 BaseMemoryManager
*mgr() const {
427 ShaderMemoryManager(BaseMemoryManager
* MM
) {
429 code
= new GeneratedCode(MM
);
432 virtual ~ShaderMemoryManager() {
434 * 'code' is purposely not deleted. It is the user's responsibility
435 * to call getGeneratedCode() and freeGeneratedCode().
439 struct lp_generated_code
*getGeneratedCode() {
440 return (struct lp_generated_code
*) code
;
443 static void freeGeneratedCode(struct lp_generated_code
*code
) {
444 delete (GeneratedCode
*) code
;
447 #if HAVE_LLVM < 0x0304
448 virtual void deallocateExceptionTable(void *ET
) {
449 // remember for later deallocation
450 code
->ExceptionTable
.push_back(ET
);
454 virtual void deallocateFunctionBody(void *Body
) {
455 // remember for later deallocation
456 code
->FunctionBody
.push_back(Body
);
462 * Same as LLVMCreateJITCompilerForModule, but:
463 * - allows using MCJIT and enabling AVX feature where available.
464 * - set target options
467 * - llvm/lib/ExecutionEngine/ExecutionEngineBindings.cpp
468 * - llvm/tools/lli/lli.cpp
469 * - http://markmail.org/message/ttkuhvgj4cxxy2on#query:+page:1+mid:aju2dggerju3ivd3+state:results
473 lp_build_create_jit_compiler_for_module(LLVMExecutionEngineRef
*OutJIT
,
474 lp_generated_code
**OutCode
,
476 LLVMMCJITMemoryManagerRef CMM
,
481 using namespace llvm
;
484 #if HAVE_LLVM >= 0x0306
485 EngineBuilder
builder(std::unique_ptr
<Module
>(unwrap(M
)));
487 EngineBuilder
builder(unwrap(M
));
491 * LLVM 3.1+ haven't more "extern unsigned llvm::StackAlignmentOverride" and
492 * friends for configuring code generation options, like stack alignment.
494 TargetOptions options
;
495 #if defined(PIPE_ARCH_X86)
496 options
.StackAlignmentOverride
= 4;
497 #if HAVE_LLVM < 0x0304
498 options
.RealignStack
= true;
502 #if defined(DEBUG) && HAVE_LLVM < 0x0307
503 options
.JITEmitDebugInfo
= true;
506 /* XXX: Workaround http://llvm.org/PR21435 */
507 #if defined(DEBUG) || defined(PROFILE) || \
508 (HAVE_LLVM >= 0x0303 && (defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)))
509 #if HAVE_LLVM < 0x0304
510 options
.NoFramePointerElimNonLeaf
= true;
512 #if HAVE_LLVM < 0x0307
513 options
.NoFramePointerElim
= true;
517 builder
.setEngineKind(EngineKind::JIT
)
519 .setTargetOptions(options
)
520 .setOptLevel((CodeGenOpt::Level
)OptLevel
);
523 #if HAVE_LLVM < 0x0306
524 builder
.setUseMCJIT(true);
528 * MCJIT works on Windows, but currently only through ELF object format.
530 * XXX: We could use `LLVM_HOST_TRIPLE "-elf"` but LLVM_HOST_TRIPLE has
531 * different strings for MinGW/MSVC, so better play it safe and be
535 LLVMSetTarget(M
, "x86_64-pc-win32-elf");
537 LLVMSetTarget(M
, "i686-pc-win32-elf");
542 llvm::SmallVector
<std::string
, 16> MAttrs
;
544 #if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
546 * We need to unset attributes because sometimes LLVM mistakenly assumes
547 * certain features are present given the processor name.
549 * https://bugs.freedesktop.org/show_bug.cgi?id=92214
550 * http://llvm.org/PR25021
551 * http://llvm.org/PR19429
552 * http://llvm.org/PR16721
554 MAttrs
.push_back(util_cpu_caps
.has_sse
? "+sse" : "-sse" );
555 MAttrs
.push_back(util_cpu_caps
.has_sse2
? "+sse2" : "-sse2" );
556 MAttrs
.push_back(util_cpu_caps
.has_sse3
? "+sse3" : "-sse3" );
557 MAttrs
.push_back(util_cpu_caps
.has_ssse3
? "+ssse3" : "-ssse3" );
558 #if HAVE_LLVM >= 0x0304
559 MAttrs
.push_back(util_cpu_caps
.has_sse4_1
? "+sse4.1" : "-sse4.1");
561 MAttrs
.push_back(util_cpu_caps
.has_sse4_1
? "+sse41" : "-sse41" );
563 #if HAVE_LLVM >= 0x0304
564 MAttrs
.push_back(util_cpu_caps
.has_sse4_2
? "+sse4.2" : "-sse4.2");
566 MAttrs
.push_back(util_cpu_caps
.has_sse4_2
? "+sse42" : "-sse42" );
569 * AVX feature is not automatically detected from CPUID by the X86 target
570 * yet, because the old (yet default) JIT engine is not capable of
571 * emitting the opcodes. On newer llvm versions it is and at least some
572 * versions (tested with 3.3) will emit avx opcodes without this anyway.
574 MAttrs
.push_back(util_cpu_caps
.has_avx
? "+avx" : "-avx");
575 MAttrs
.push_back(util_cpu_caps
.has_f16c
? "+f16c" : "-f16c");
576 if (HAVE_LLVM
>= 0x0304) {
577 MAttrs
.push_back(util_cpu_caps
.has_fma
? "+fma" : "-fma");
580 * The old JIT in LLVM 3.3 has a bug encoding llvm.fmuladd.f32 and
581 * llvm.fmuladd.v2f32 intrinsics when FMA is available.
583 MAttrs
.push_back("-fma");
585 MAttrs
.push_back(util_cpu_caps
.has_avx2
? "+avx2" : "-avx2");
586 /* disable avx512 and all subvariants */
587 #if HAVE_LLVM >= 0x0304
588 MAttrs
.push_back("-avx512cd");
589 MAttrs
.push_back("-avx512er");
590 MAttrs
.push_back("-avx512f");
591 MAttrs
.push_back("-avx512pf");
593 #if HAVE_LLVM >= 0x0305
594 MAttrs
.push_back("-avx512bw");
595 MAttrs
.push_back("-avx512dq");
596 MAttrs
.push_back("-avx512vl");
600 #if defined(PIPE_ARCH_PPC)
601 MAttrs
.push_back(util_cpu_caps
.has_altivec
? "+altivec" : "-altivec");
602 #if HAVE_LLVM >= 0x0304
604 * Make sure VSX instructions are disabled
605 * See LLVM bug https://llvm.org/bugs/show_bug.cgi?id=25503#c7
607 if (util_cpu_caps
.has_altivec
) {
608 MAttrs
.push_back("-vsx");
613 builder
.setMAttrs(MAttrs
);
615 #if HAVE_LLVM >= 0x0305
616 StringRef MCPU
= llvm::sys::getHostCPUName();
618 * The cpu bits are no longer set automatically, so need to set mcpu manually.
619 * Note that the MAttrs set above will be sort of ignored (since we should
620 * not set any which would not be set by specifying the cpu anyway).
621 * It ought to be safe though since getHostCPUName() should include bits
622 * not only from the cpu but environment as well (for instance if it's safe
623 * to use avx instructions which need OS support). According to
624 * http://llvm.org/bugs/show_bug.cgi?id=19429 however if I understand this
625 * right it may be necessary to specify older cpu (or disable mattrs) though
626 * when not using MCJIT so no instructions are generated which the old JIT
627 * can't handle. Not entirely sure if we really need to do anything yet.
629 builder
.setMCPU(MCPU
);
632 ShaderMemoryManager
*MM
= NULL
;
634 BaseMemoryManager
* JMM
= reinterpret_cast<BaseMemoryManager
*>(CMM
);
635 MM
= new ShaderMemoryManager(JMM
);
636 *OutCode
= MM
->getGeneratedCode();
638 #if HAVE_LLVM >= 0x0306
639 builder
.setMCJITMemoryManager(std::unique_ptr
<RTDyldMemoryManager
>(MM
));
640 MM
= NULL
; // ownership taken by std::unique_ptr
641 #elif HAVE_LLVM > 0x0303
642 builder
.setMCJITMemoryManager(MM
);
644 builder
.setJITMemoryManager(MM
);
647 #if HAVE_LLVM < 0x0306
648 BaseMemoryManager
* JMM
= reinterpret_cast<BaseMemoryManager
*>(CMM
);
649 MM
= new ShaderMemoryManager(JMM
);
650 *OutCode
= MM
->getGeneratedCode();
652 builder
.setJITMemoryManager(MM
);
658 ExecutionEngine
*JIT
;
660 JIT
= builder
.create();
661 #if LLVM_USE_INTEL_JITEVENTS
662 JITEventListener
*JEL
= JITEventListener::createIntelJITEventListener();
663 JIT
->RegisterJITEventListener(JEL
);
669 lp_free_generated_code(*OutCode
);
672 *OutError
= strdup(Error
.c_str());
679 lp_free_generated_code(struct lp_generated_code
*code
)
681 ShaderMemoryManager::freeGeneratedCode(code
);
685 LLVMMCJITMemoryManagerRef
686 lp_get_default_memory_manager()
688 BaseMemoryManager
*mm
;
689 #if HAVE_LLVM < 0x0306
690 mm
= llvm::JITMemoryManager::CreateDefaultMemManager();
692 mm
= new llvm::SectionMemoryManager();
694 return reinterpret_cast<LLVMMCJITMemoryManagerRef
>(mm
);
699 lp_free_memory_manager(LLVMMCJITMemoryManagerRef memorymgr
)
701 delete reinterpret_cast<BaseMemoryManager
*>(memorymgr
);
705 lp_add_attr_dereferenceable(LLVMValueRef val
, uint64_t bytes
)
707 #if HAVE_LLVM >= 0x0306
708 llvm::Argument
*A
= llvm::unwrap
<llvm::Argument
>(val
);
710 B
.addDereferenceableAttr(bytes
);
711 A
->addAttr(llvm::AttributeSet::get(A
->getContext(), A
->getArgNo() + 1, B
));
715 extern "C" LLVMValueRef
716 lp_get_called_value(LLVMValueRef call
)
718 #if HAVE_LLVM >= 0x0309
719 return LLVMGetCalledValue(call
);
720 #elif HAVE_LLVM >= 0x0305
721 return llvm::wrap(llvm::CallSite(llvm::unwrap
<llvm::Instruction
>(call
)).getCalledValue());
723 return NULL
; /* radeonsi doesn't support so old LLVM. */
728 lp_is_function(LLVMValueRef v
)
730 #if HAVE_LLVM >= 0x0309
731 return LLVMGetValueKind(v
) == LLVMFunctionValueKind
;
733 return llvm::isa
<llvm::Function
>(llvm::unwrap(v
));