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 // Undef these vars just to silence warnings
36 #undef PACKAGE_BUGREPORT
39 #undef PACKAGE_TARNAME
40 #undef PACKAGE_VERSION
45 // Workaround http://llvm.org/PR23628
46 #if HAVE_LLVM >= 0x0307
47 # pragma push_macro("DEBUG")
51 #include <llvm-c/Core.h>
52 #if HAVE_LLVM >= 0x0306
53 #include <llvm-c/Support.h>
55 #include <llvm-c/ExecutionEngine.h>
56 #include <llvm/Target/TargetOptions.h>
57 #include <llvm/ExecutionEngine/ExecutionEngine.h>
58 #include <llvm/ADT/Triple.h>
59 #if HAVE_LLVM >= 0x0307
60 #include <llvm/Analysis/TargetLibraryInfo.h>
62 #include <llvm/Target/TargetLibraryInfo.h>
64 #if HAVE_LLVM < 0x0306
65 #include <llvm/ExecutionEngine/JITMemoryManager.h>
67 #include <llvm/ExecutionEngine/SectionMemoryManager.h>
69 #include <llvm/Support/CommandLine.h>
70 #include <llvm/Support/Host.h>
71 #include <llvm/Support/PrettyStackTrace.h>
73 #include <llvm/Support/TargetSelect.h>
75 #if HAVE_LLVM >= 0x0305
76 #include <llvm/IR/CallSite.h>
78 #include <llvm/IR/IRBuilder.h>
79 #include <llvm/IR/Module.h>
80 #include <llvm/Support/CBindingWrapping.h>
82 #include <llvm/Config/llvm-config.h>
83 #if LLVM_USE_INTEL_JITEVENTS
84 #include <llvm/ExecutionEngine/JITEventListener.h>
87 // Workaround http://llvm.org/PR23628
88 #if HAVE_LLVM >= 0x0307
89 # pragma pop_macro("DEBUG")
92 #include "c11/threads.h"
93 #include "os/os_thread.h"
94 #include "pipe/p_config.h"
95 #include "util/u_debug.h"
96 #include "util/u_cpu_detect.h"
98 #include "lp_bld_misc.h"
99 #include "lp_bld_debug.h"
103 class LLVMEnsureMultithreaded
{
105 LLVMEnsureMultithreaded()
107 if (!LLVMIsMultithreaded()) {
108 LLVMStartMultithreaded();
113 static LLVMEnsureMultithreaded lLVMEnsureMultithreaded
;
117 static once_flag init_native_targets_once_flag
= ONCE_FLAG_INIT
;
119 static void init_native_targets()
121 // If we have a native target, initialize it to ensure it is linked in and
122 // usable by the JIT.
123 llvm::InitializeNativeTarget();
125 llvm::InitializeNativeTargetAsmPrinter();
127 llvm::InitializeNativeTargetDisassembler();
128 #if DEBUG && HAVE_LLVM >= 0x0306
130 char *env_llc_options
= getenv("GALLIVM_LLC_OPTIONS");
131 if (env_llc_options
) {
133 char *options
[64] = {(char *) "llc"}; // Warning without cast
135 for (n
= 0, option
= strtok(env_llc_options
, " "); option
; n
++, option
= strtok(NULL
, " ")) {
136 options
[n
+ 1] = option
;
138 if (gallivm_debug
& (GALLIVM_DEBUG_IR
| GALLIVM_DEBUG_ASM
| GALLIVM_DEBUG_DUMP_BC
)) {
139 debug_printf("llc additional options (%d):\n", n
);
140 for (int i
= 1; i
<= n
; i
++)
141 debug_printf("\t%s\n", options
[i
]);
144 LLVMParseCommandLineOptions(n
+ 1, options
, NULL
);
151 lp_set_target_options(void)
153 #if HAVE_LLVM < 0x0304
155 * By default LLVM adds a signal handler to output a pretty stack trace.
156 * This signal handler is never removed, causing problems when unloading the
157 * shared object where the gallium driver resides.
159 llvm::DisablePrettyStackTrace
= true;
162 /* The llvm target registry is not thread-safe, so drivers and state-trackers
163 * that want to initialize targets should use the lp_set_target_options()
164 * function to safely initialize targets.
166 * LLVM targets should be initialized before the driver or state-tracker tries
167 * to access the registry.
169 call_once(&init_native_targets_once_flag
, init_native_targets
);
173 LLVMTargetLibraryInfoRef
174 gallivm_create_target_library_info(const char *triple
)
176 return reinterpret_cast<LLVMTargetLibraryInfoRef
>(
177 #if HAVE_LLVM < 0x0307
178 new llvm::TargetLibraryInfo(
180 new llvm::TargetLibraryInfoImpl(
182 llvm::Triple(triple
)));
187 gallivm_dispose_target_library_info(LLVMTargetLibraryInfoRef library_info
)
189 delete reinterpret_cast<
190 #if HAVE_LLVM < 0x0307
191 llvm::TargetLibraryInfo
193 llvm::TargetLibraryInfoImpl
199 #if HAVE_LLVM < 0x0304
203 LLVMSetAlignmentBackport(LLVMValueRef V
,
206 switch (LLVMGetInstructionOpcode(V
)) {
208 llvm::unwrap
<llvm::LoadInst
>(V
)->setAlignment(Bytes
);
211 llvm::unwrap
<llvm::StoreInst
>(V
)->setAlignment(Bytes
);
222 #if HAVE_LLVM < 0x0306
223 typedef llvm::JITMemoryManager BaseMemoryManager
;
225 typedef llvm::RTDyldMemoryManager BaseMemoryManager
;
230 * Delegating is tedious but the default manager class is hidden in an
231 * anonymous namespace in LLVM, so we cannot just derive from it to change
234 class DelegatingJITMemoryManager
: public BaseMemoryManager
{
237 virtual BaseMemoryManager
*mgr() const = 0;
240 #if HAVE_LLVM < 0x0306
242 * From JITMemoryManager
244 virtual void setMemoryWritable() {
245 mgr()->setMemoryWritable();
247 virtual void setMemoryExecutable() {
248 mgr()->setMemoryExecutable();
250 virtual void setPoisonMemory(bool poison
) {
251 mgr()->setPoisonMemory(poison
);
253 virtual void AllocateGOT() {
254 mgr()->AllocateGOT();
256 * isManagingGOT() is not virtual in base class so we can't delegate.
257 * Instead we mirror the value of HasGOT in our instance.
259 HasGOT
= mgr()->isManagingGOT();
261 virtual uint8_t *getGOTBase() const {
262 return mgr()->getGOTBase();
264 virtual uint8_t *startFunctionBody(const llvm::Function
*F
,
265 uintptr_t &ActualSize
) {
266 return mgr()->startFunctionBody(F
, ActualSize
);
268 virtual uint8_t *allocateStub(const llvm::GlobalValue
*F
,
270 unsigned Alignment
) {
271 return mgr()->allocateStub(F
, StubSize
, Alignment
);
273 virtual void endFunctionBody(const llvm::Function
*F
,
274 uint8_t *FunctionStart
,
275 uint8_t *FunctionEnd
) {
276 mgr()->endFunctionBody(F
, FunctionStart
, FunctionEnd
);
278 virtual uint8_t *allocateSpace(intptr_t Size
, unsigned Alignment
) {
279 return mgr()->allocateSpace(Size
, Alignment
);
281 virtual uint8_t *allocateGlobal(uintptr_t Size
, unsigned Alignment
) {
282 return mgr()->allocateGlobal(Size
, Alignment
);
284 virtual void deallocateFunctionBody(void *Body
) {
285 mgr()->deallocateFunctionBody(Body
);
287 #if HAVE_LLVM < 0x0304
288 virtual uint8_t *startExceptionTable(const llvm::Function
*F
,
289 uintptr_t &ActualSize
) {
290 return mgr()->startExceptionTable(F
, ActualSize
);
292 virtual void endExceptionTable(const llvm::Function
*F
,
295 uint8_t *FrameRegister
) {
296 mgr()->endExceptionTable(F
, TableStart
, TableEnd
,
299 virtual void deallocateExceptionTable(void *ET
) {
300 mgr()->deallocateExceptionTable(ET
);
303 virtual bool CheckInvariants(std::string
&s
) {
304 return mgr()->CheckInvariants(s
);
306 virtual size_t GetDefaultCodeSlabSize() {
307 return mgr()->GetDefaultCodeSlabSize();
309 virtual size_t GetDefaultDataSlabSize() {
310 return mgr()->GetDefaultDataSlabSize();
312 virtual size_t GetDefaultStubSlabSize() {
313 return mgr()->GetDefaultStubSlabSize();
315 virtual unsigned GetNumCodeSlabs() {
316 return mgr()->GetNumCodeSlabs();
318 virtual unsigned GetNumDataSlabs() {
319 return mgr()->GetNumDataSlabs();
321 virtual unsigned GetNumStubSlabs() {
322 return mgr()->GetNumStubSlabs();
327 * From RTDyldMemoryManager
329 #if HAVE_LLVM >= 0x0304
330 virtual uint8_t *allocateCodeSection(uintptr_t Size
,
333 llvm::StringRef SectionName
) {
334 return mgr()->allocateCodeSection(Size
, Alignment
, SectionID
,
338 virtual uint8_t *allocateCodeSection(uintptr_t Size
,
340 unsigned SectionID
) {
341 return mgr()->allocateCodeSection(Size
, Alignment
, SectionID
);
344 virtual uint8_t *allocateDataSection(uintptr_t Size
,
347 #if HAVE_LLVM >= 0x0304
348 llvm::StringRef SectionName
,
351 return mgr()->allocateDataSection(Size
, Alignment
, SectionID
,
352 #if HAVE_LLVM >= 0x0304
357 #if HAVE_LLVM >= 0x0304
358 virtual void registerEHFrames(uint8_t *Addr
, uint64_t LoadAddr
, size_t Size
) {
359 mgr()->registerEHFrames(Addr
, LoadAddr
, Size
);
362 virtual void registerEHFrames(llvm::StringRef SectionData
) {
363 mgr()->registerEHFrames(SectionData
);
366 #if HAVE_LLVM >= 0x0500
367 virtual void deregisterEHFrames() {
368 mgr()->deregisterEHFrames();
370 #elif HAVE_LLVM >= 0x0304
371 virtual void deregisterEHFrames(uint8_t *Addr
, uint64_t LoadAddr
, size_t Size
) {
372 mgr()->deregisterEHFrames(Addr
, LoadAddr
, Size
);
375 virtual void *getPointerToNamedFunction(const std::string
&Name
,
376 bool AbortOnFailure
=true) {
377 return mgr()->getPointerToNamedFunction(Name
, AbortOnFailure
);
379 #if HAVE_LLVM <= 0x0303
380 virtual bool applyPermissions(std::string
*ErrMsg
= 0) {
381 return mgr()->applyPermissions(ErrMsg
);
384 virtual bool finalizeMemory(std::string
*ErrMsg
= 0) {
385 return mgr()->finalizeMemory(ErrMsg
);
392 * Delegate memory management to one shared manager for more efficient use
393 * of memory than creating a separate pool for each LLVM engine.
394 * Keep generated code until freeGeneratedCode() is called, instead of when
395 * memory manager is destroyed, which happens during engine destruction.
396 * This allows additional memory savings as we don't have to keep the engine
397 * around in order to use the code.
398 * All methods are delegated to the shared manager except destruction and
399 * deallocating code. For the latter we just remember what needs to be
400 * deallocated later. The shared manager is deleted once it is empty.
402 class ShaderMemoryManager
: public DelegatingJITMemoryManager
{
404 BaseMemoryManager
*TheMM
;
406 struct GeneratedCode
{
407 typedef std::vector
<void *> Vec
;
408 Vec FunctionBody
, ExceptionTable
;
409 BaseMemoryManager
*TheMM
;
411 GeneratedCode(BaseMemoryManager
*MM
) {
417 * Deallocate things as previously requested and
418 * free shared manager when no longer used.
420 #if HAVE_LLVM < 0x0306
424 for ( i
= FunctionBody
.begin(); i
!= FunctionBody
.end(); ++i
)
425 TheMM
->deallocateFunctionBody(*i
);
426 #if HAVE_LLVM < 0x0304
427 for ( i
= ExceptionTable
.begin(); i
!= ExceptionTable
.end(); ++i
)
428 TheMM
->deallocateExceptionTable(*i
);
429 #endif /* HAVE_LLVM < 0x0304 */
430 #endif /* HAVE_LLVM < 0x0306 */
436 BaseMemoryManager
*mgr() const {
442 ShaderMemoryManager(BaseMemoryManager
* MM
) {
444 code
= new GeneratedCode(MM
);
447 virtual ~ShaderMemoryManager() {
449 * 'code' is purposely not deleted. It is the user's responsibility
450 * to call getGeneratedCode() and freeGeneratedCode().
454 struct lp_generated_code
*getGeneratedCode() {
455 return (struct lp_generated_code
*) code
;
458 static void freeGeneratedCode(struct lp_generated_code
*code
) {
459 delete (GeneratedCode
*) code
;
462 #if HAVE_LLVM < 0x0304
463 virtual void deallocateExceptionTable(void *ET
) {
464 // remember for later deallocation
465 code
->ExceptionTable
.push_back(ET
);
469 virtual void deallocateFunctionBody(void *Body
) {
470 // remember for later deallocation
471 code
->FunctionBody
.push_back(Body
);
477 * Same as LLVMCreateJITCompilerForModule, but:
478 * - allows using MCJIT and enabling AVX feature where available.
479 * - set target options
482 * - llvm/lib/ExecutionEngine/ExecutionEngineBindings.cpp
483 * - llvm/tools/lli/lli.cpp
484 * - http://markmail.org/message/ttkuhvgj4cxxy2on#query:+page:1+mid:aju2dggerju3ivd3+state:results
488 lp_build_create_jit_compiler_for_module(LLVMExecutionEngineRef
*OutJIT
,
489 lp_generated_code
**OutCode
,
491 LLVMMCJITMemoryManagerRef CMM
,
496 using namespace llvm
;
499 #if HAVE_LLVM >= 0x0306
500 EngineBuilder
builder(std::unique_ptr
<Module
>(unwrap(M
)));
502 EngineBuilder
builder(unwrap(M
));
506 * LLVM 3.1+ haven't more "extern unsigned llvm::StackAlignmentOverride" and
507 * friends for configuring code generation options, like stack alignment.
509 TargetOptions options
;
510 #if defined(PIPE_ARCH_X86)
511 options
.StackAlignmentOverride
= 4;
512 #if HAVE_LLVM < 0x0304
513 options
.RealignStack
= true;
517 #if defined(DEBUG) && HAVE_LLVM < 0x0307
518 options
.JITEmitDebugInfo
= true;
521 /* XXX: Workaround http://llvm.org/PR21435 */
522 #if defined(DEBUG) || defined(PROFILE) || \
523 (HAVE_LLVM >= 0x0303 && (defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)))
524 #if HAVE_LLVM < 0x0304
525 options
.NoFramePointerElimNonLeaf
= true;
527 #if HAVE_LLVM < 0x0307
528 options
.NoFramePointerElim
= true;
532 builder
.setEngineKind(EngineKind::JIT
)
534 .setTargetOptions(options
)
535 .setOptLevel((CodeGenOpt::Level
)OptLevel
);
538 #if HAVE_LLVM < 0x0306
539 builder
.setUseMCJIT(true);
543 * MCJIT works on Windows, but currently only through ELF object format.
545 * XXX: We could use `LLVM_HOST_TRIPLE "-elf"` but LLVM_HOST_TRIPLE has
546 * different strings for MinGW/MSVC, so better play it safe and be
550 LLVMSetTarget(M
, "x86_64-pc-win32-elf");
552 LLVMSetTarget(M
, "i686-pc-win32-elf");
557 llvm::SmallVector
<std::string
, 16> MAttrs
;
559 #if HAVE_LLVM >= 0x0400 && (defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64) || defined(PIPE_ARCH_ARM))
560 /* llvm-3.3+ implements sys::getHostCPUFeatures for Arm
561 * and llvm-3.7+ for x86, which allows us to enable/disable
562 * code generation based on the results of cpuid on these
565 llvm::StringMap
<bool> features
;
566 llvm::sys::getHostCPUFeatures(features
);
568 for (StringMapIterator
<bool> f
= features
.begin();
571 MAttrs
.push_back(((*f
).second
? "+" : "-") + (*f
).first().str());
573 #elif defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
575 * We need to unset attributes because sometimes LLVM mistakenly assumes
576 * certain features are present given the processor name.
578 * https://bugs.freedesktop.org/show_bug.cgi?id=92214
579 * http://llvm.org/PR25021
580 * http://llvm.org/PR19429
581 * http://llvm.org/PR16721
583 MAttrs
.push_back(util_cpu_caps
.has_sse
? "+sse" : "-sse" );
584 MAttrs
.push_back(util_cpu_caps
.has_sse2
? "+sse2" : "-sse2" );
585 MAttrs
.push_back(util_cpu_caps
.has_sse3
? "+sse3" : "-sse3" );
586 MAttrs
.push_back(util_cpu_caps
.has_ssse3
? "+ssse3" : "-ssse3" );
587 #if HAVE_LLVM >= 0x0304
588 MAttrs
.push_back(util_cpu_caps
.has_sse4_1
? "+sse4.1" : "-sse4.1");
590 MAttrs
.push_back(util_cpu_caps
.has_sse4_1
? "+sse41" : "-sse41" );
592 #if HAVE_LLVM >= 0x0304
593 MAttrs
.push_back(util_cpu_caps
.has_sse4_2
? "+sse4.2" : "-sse4.2");
595 MAttrs
.push_back(util_cpu_caps
.has_sse4_2
? "+sse42" : "-sse42" );
598 * AVX feature is not automatically detected from CPUID by the X86 target
599 * yet, because the old (yet default) JIT engine is not capable of
600 * emitting the opcodes. On newer llvm versions it is and at least some
601 * versions (tested with 3.3) will emit avx opcodes without this anyway.
603 MAttrs
.push_back(util_cpu_caps
.has_avx
? "+avx" : "-avx");
604 MAttrs
.push_back(util_cpu_caps
.has_f16c
? "+f16c" : "-f16c");
605 if (HAVE_LLVM
>= 0x0304) {
606 MAttrs
.push_back(util_cpu_caps
.has_fma
? "+fma" : "-fma");
609 * The old JIT in LLVM 3.3 has a bug encoding llvm.fmuladd.f32 and
610 * llvm.fmuladd.v2f32 intrinsics when FMA is available.
612 MAttrs
.push_back("-fma");
614 MAttrs
.push_back(util_cpu_caps
.has_avx2
? "+avx2" : "-avx2");
615 /* disable avx512 and all subvariants */
616 #if HAVE_LLVM >= 0x0304
617 MAttrs
.push_back("-avx512cd");
618 MAttrs
.push_back("-avx512er");
619 MAttrs
.push_back("-avx512f");
620 MAttrs
.push_back("-avx512pf");
622 #if HAVE_LLVM >= 0x0305
623 MAttrs
.push_back("-avx512bw");
624 MAttrs
.push_back("-avx512dq");
625 MAttrs
.push_back("-avx512vl");
629 #if defined(PIPE_ARCH_PPC)
630 MAttrs
.push_back(util_cpu_caps
.has_altivec
? "+altivec" : "-altivec");
631 #if (HAVE_LLVM >= 0x0304)
632 #if (HAVE_LLVM < 0x0400)
634 * Make sure VSX instructions are disabled
636 * https://llvm.org/bugs/show_bug.cgi?id=25503#c7 (fixed in 3.8.1)
637 * https://llvm.org/bugs/show_bug.cgi?id=26775 (fixed in 3.8.1)
638 * https://llvm.org/bugs/show_bug.cgi?id=33531 (fixed in 4.0)
639 * https://llvm.org/bugs/show_bug.cgi?id=34647 (llc performance on certain unusual shader IR; intro'd in 4.0, pending as of 5.0)
641 if (util_cpu_caps
.has_altivec
) {
642 MAttrs
.push_back("-vsx");
646 * Bug 25503 is fixed, by the same fix that fixed
647 * bug 26775, in versions of LLVM later than 3.8 (starting with 3.8.1).
648 * BZ 33531 actually comprises more than one bug, all of
649 * which are fixed in LLVM 4.0.
651 * With LLVM 4.0 or higher:
652 * Make sure VSX instructions are ENABLED (if supported), unless
653 * VSX instructions are explicitly enabled/disabled via GALLIVM_VSX=1 or 0.
655 if (util_cpu_caps
.has_altivec
) {
656 MAttrs
.push_back(util_cpu_caps
.has_vsx
? "+vsx" : "-vsx");
662 builder
.setMAttrs(MAttrs
);
664 if (gallivm_debug
& (GALLIVM_DEBUG_IR
| GALLIVM_DEBUG_ASM
| GALLIVM_DEBUG_DUMP_BC
)) {
665 int n
= MAttrs
.size();
667 debug_printf("llc -mattr option(s): ");
668 for (int i
= 0; i
< n
; i
++)
669 debug_printf("%s%s", MAttrs
[i
].c_str(), (i
< n
- 1) ? "," : "");
674 #if HAVE_LLVM >= 0x0305
675 StringRef MCPU
= llvm::sys::getHostCPUName();
677 * The cpu bits are no longer set automatically, so need to set mcpu manually.
678 * Note that the MAttrs set above will be sort of ignored (since we should
679 * not set any which would not be set by specifying the cpu anyway).
680 * It ought to be safe though since getHostCPUName() should include bits
681 * not only from the cpu but environment as well (for instance if it's safe
682 * to use avx instructions which need OS support). According to
683 * http://llvm.org/bugs/show_bug.cgi?id=19429 however if I understand this
684 * right it may be necessary to specify older cpu (or disable mattrs) though
685 * when not using MCJIT so no instructions are generated which the old JIT
686 * can't handle. Not entirely sure if we really need to do anything yet.
688 #if defined(PIPE_ARCH_LITTLE_ENDIAN) && defined(PIPE_ARCH_PPC_64)
690 * Versions of LLVM prior to 4.0 lacked a table entry for "POWER8NVL",
691 * resulting in (big-endian) "generic" being returned on
692 * little-endian Power8NVL systems. The result was that code that
693 * attempted to load the least significant 32 bits of a 64-bit quantity
694 * from memory loaded the wrong half. This resulted in failures in some
696 * .../arb_gpu_shader_fp64/execution/conversion/frag-conversion-explicit-double-uint
698 if (MCPU
== "generic")
701 builder
.setMCPU(MCPU
);
702 if (gallivm_debug
& (GALLIVM_DEBUG_IR
| GALLIVM_DEBUG_ASM
| GALLIVM_DEBUG_DUMP_BC
)) {
703 debug_printf("llc -mcpu option: %s\n", MCPU
.str().c_str());
707 ShaderMemoryManager
*MM
= NULL
;
709 BaseMemoryManager
* JMM
= reinterpret_cast<BaseMemoryManager
*>(CMM
);
710 MM
= new ShaderMemoryManager(JMM
);
711 *OutCode
= MM
->getGeneratedCode();
713 #if HAVE_LLVM >= 0x0306
714 builder
.setMCJITMemoryManager(std::unique_ptr
<RTDyldMemoryManager
>(MM
));
715 MM
= NULL
; // ownership taken by std::unique_ptr
716 #elif HAVE_LLVM > 0x0303
717 builder
.setMCJITMemoryManager(MM
);
719 builder
.setJITMemoryManager(MM
);
722 #if HAVE_LLVM < 0x0306
723 BaseMemoryManager
* JMM
= reinterpret_cast<BaseMemoryManager
*>(CMM
);
724 MM
= new ShaderMemoryManager(JMM
);
725 *OutCode
= MM
->getGeneratedCode();
727 builder
.setJITMemoryManager(MM
);
733 ExecutionEngine
*JIT
;
735 JIT
= builder
.create();
736 #if LLVM_USE_INTEL_JITEVENTS
737 JITEventListener
*JEL
= JITEventListener::createIntelJITEventListener();
738 JIT
->RegisterJITEventListener(JEL
);
744 lp_free_generated_code(*OutCode
);
747 *OutError
= strdup(Error
.c_str());
754 lp_free_generated_code(struct lp_generated_code
*code
)
756 ShaderMemoryManager::freeGeneratedCode(code
);
760 LLVMMCJITMemoryManagerRef
761 lp_get_default_memory_manager()
763 BaseMemoryManager
*mm
;
764 #if HAVE_LLVM < 0x0306
765 mm
= llvm::JITMemoryManager::CreateDefaultMemManager();
767 mm
= new llvm::SectionMemoryManager();
769 return reinterpret_cast<LLVMMCJITMemoryManagerRef
>(mm
);
774 lp_free_memory_manager(LLVMMCJITMemoryManagerRef memorymgr
)
776 delete reinterpret_cast<BaseMemoryManager
*>(memorymgr
);
779 extern "C" LLVMValueRef
780 lp_get_called_value(LLVMValueRef call
)
782 #if HAVE_LLVM >= 0x0309
783 return LLVMGetCalledValue(call
);
784 #elif HAVE_LLVM >= 0x0305
785 return llvm::wrap(llvm::CallSite(llvm::unwrap
<llvm::Instruction
>(call
)).getCalledValue());
787 return NULL
; /* radeonsi doesn't support so old LLVM. */
792 lp_is_function(LLVMValueRef v
)
794 #if HAVE_LLVM >= 0x0309
795 return LLVMGetValueKind(v
) == LLVMFunctionValueKind
;
797 return llvm::isa
<llvm::Function
>(llvm::unwrap(v
));