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 #include <llvm-c/ExecutionEngine.h>
53 #include <llvm/Target/TargetOptions.h>
54 #include <llvm/ExecutionEngine/ExecutionEngine.h>
55 #include <llvm/ADT/Triple.h>
56 #if HAVE_LLVM >= 0x0307
57 #include <llvm/Analysis/TargetLibraryInfo.h>
59 #include <llvm/Target/TargetLibraryInfo.h>
61 #if HAVE_LLVM < 0x0306
62 #include <llvm/ExecutionEngine/JITMemoryManager.h>
64 #include <llvm/ExecutionEngine/SectionMemoryManager.h>
66 #include <llvm/Support/CommandLine.h>
67 #include <llvm/Support/Host.h>
68 #include <llvm/Support/PrettyStackTrace.h>
70 #include <llvm/Support/TargetSelect.h>
72 #if HAVE_LLVM >= 0x0305
73 #include <llvm/IR/CallSite.h>
75 #include <llvm/IR/IRBuilder.h>
76 #include <llvm/IR/Module.h>
77 #include <llvm/Support/CBindingWrapping.h>
79 #include <llvm/Config/llvm-config.h>
80 #if LLVM_USE_INTEL_JITEVENTS
81 #include <llvm/ExecutionEngine/JITEventListener.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"
96 #include "lp_bld_debug.h"
100 class LLVMEnsureMultithreaded
{
102 LLVMEnsureMultithreaded()
104 if (!LLVMIsMultithreaded()) {
105 LLVMStartMultithreaded();
110 static LLVMEnsureMultithreaded lLVMEnsureMultithreaded
;
114 static once_flag init_native_targets_once_flag
= ONCE_FLAG_INIT
;
116 static void init_native_targets()
118 // If we have a native target, initialize it to ensure it is linked in and
119 // usable by the JIT.
120 llvm::InitializeNativeTarget();
122 llvm::InitializeNativeTargetAsmPrinter();
124 llvm::InitializeNativeTargetDisassembler();
128 lp_set_target_options(void)
130 #if HAVE_LLVM < 0x0304
132 * By default LLVM adds a signal handler to output a pretty stack trace.
133 * This signal handler is never removed, causing problems when unloading the
134 * shared object where the gallium driver resides.
136 llvm::DisablePrettyStackTrace
= true;
139 /* The llvm target registry is not thread-safe, so drivers and state-trackers
140 * that want to initialize targets should use the lp_set_target_options()
141 * function to safely initialize targets.
143 * LLVM targets should be initialized before the driver or state-tracker tries
144 * to access the registry.
146 call_once(&init_native_targets_once_flag
, init_native_targets
);
150 LLVMTargetLibraryInfoRef
151 gallivm_create_target_library_info(const char *triple
)
153 return reinterpret_cast<LLVMTargetLibraryInfoRef
>(
154 #if HAVE_LLVM < 0x0307
155 new llvm::TargetLibraryInfo(
157 new llvm::TargetLibraryInfoImpl(
159 llvm::Triple(triple
)));
164 gallivm_dispose_target_library_info(LLVMTargetLibraryInfoRef library_info
)
166 delete reinterpret_cast<
167 #if HAVE_LLVM < 0x0307
168 llvm::TargetLibraryInfo
170 llvm::TargetLibraryInfoImpl
176 #if HAVE_LLVM < 0x0304
180 LLVMSetAlignmentBackport(LLVMValueRef V
,
183 switch (LLVMGetInstructionOpcode(V
)) {
185 llvm::unwrap
<llvm::LoadInst
>(V
)->setAlignment(Bytes
);
188 llvm::unwrap
<llvm::StoreInst
>(V
)->setAlignment(Bytes
);
199 #if HAVE_LLVM < 0x0306
200 typedef llvm::JITMemoryManager BaseMemoryManager
;
202 typedef llvm::RTDyldMemoryManager BaseMemoryManager
;
207 * Delegating is tedious but the default manager class is hidden in an
208 * anonymous namespace in LLVM, so we cannot just derive from it to change
211 class DelegatingJITMemoryManager
: public BaseMemoryManager
{
214 virtual BaseMemoryManager
*mgr() const = 0;
217 #if HAVE_LLVM < 0x0306
219 * From JITMemoryManager
221 virtual void setMemoryWritable() {
222 mgr()->setMemoryWritable();
224 virtual void setMemoryExecutable() {
225 mgr()->setMemoryExecutable();
227 virtual void setPoisonMemory(bool poison
) {
228 mgr()->setPoisonMemory(poison
);
230 virtual void AllocateGOT() {
231 mgr()->AllocateGOT();
233 * isManagingGOT() is not virtual in base class so we can't delegate.
234 * Instead we mirror the value of HasGOT in our instance.
236 HasGOT
= mgr()->isManagingGOT();
238 virtual uint8_t *getGOTBase() const {
239 return mgr()->getGOTBase();
241 virtual uint8_t *startFunctionBody(const llvm::Function
*F
,
242 uintptr_t &ActualSize
) {
243 return mgr()->startFunctionBody(F
, ActualSize
);
245 virtual uint8_t *allocateStub(const llvm::GlobalValue
*F
,
247 unsigned Alignment
) {
248 return mgr()->allocateStub(F
, StubSize
, Alignment
);
250 virtual void endFunctionBody(const llvm::Function
*F
,
251 uint8_t *FunctionStart
,
252 uint8_t *FunctionEnd
) {
253 mgr()->endFunctionBody(F
, FunctionStart
, FunctionEnd
);
255 virtual uint8_t *allocateSpace(intptr_t Size
, unsigned Alignment
) {
256 return mgr()->allocateSpace(Size
, Alignment
);
258 virtual uint8_t *allocateGlobal(uintptr_t Size
, unsigned Alignment
) {
259 return mgr()->allocateGlobal(Size
, Alignment
);
261 virtual void deallocateFunctionBody(void *Body
) {
262 mgr()->deallocateFunctionBody(Body
);
264 #if HAVE_LLVM < 0x0304
265 virtual uint8_t *startExceptionTable(const llvm::Function
*F
,
266 uintptr_t &ActualSize
) {
267 return mgr()->startExceptionTable(F
, ActualSize
);
269 virtual void endExceptionTable(const llvm::Function
*F
,
272 uint8_t *FrameRegister
) {
273 mgr()->endExceptionTable(F
, TableStart
, TableEnd
,
276 virtual void deallocateExceptionTable(void *ET
) {
277 mgr()->deallocateExceptionTable(ET
);
280 virtual bool CheckInvariants(std::string
&s
) {
281 return mgr()->CheckInvariants(s
);
283 virtual size_t GetDefaultCodeSlabSize() {
284 return mgr()->GetDefaultCodeSlabSize();
286 virtual size_t GetDefaultDataSlabSize() {
287 return mgr()->GetDefaultDataSlabSize();
289 virtual size_t GetDefaultStubSlabSize() {
290 return mgr()->GetDefaultStubSlabSize();
292 virtual unsigned GetNumCodeSlabs() {
293 return mgr()->GetNumCodeSlabs();
295 virtual unsigned GetNumDataSlabs() {
296 return mgr()->GetNumDataSlabs();
298 virtual unsigned GetNumStubSlabs() {
299 return mgr()->GetNumStubSlabs();
304 * From RTDyldMemoryManager
306 #if HAVE_LLVM >= 0x0304
307 virtual uint8_t *allocateCodeSection(uintptr_t Size
,
310 llvm::StringRef SectionName
) {
311 return mgr()->allocateCodeSection(Size
, Alignment
, SectionID
,
315 virtual uint8_t *allocateCodeSection(uintptr_t Size
,
317 unsigned SectionID
) {
318 return mgr()->allocateCodeSection(Size
, Alignment
, SectionID
);
321 virtual uint8_t *allocateDataSection(uintptr_t Size
,
324 #if HAVE_LLVM >= 0x0304
325 llvm::StringRef SectionName
,
328 return mgr()->allocateDataSection(Size
, Alignment
, SectionID
,
329 #if HAVE_LLVM >= 0x0304
334 #if HAVE_LLVM >= 0x0304
335 virtual void registerEHFrames(uint8_t *Addr
, uint64_t LoadAddr
, size_t Size
) {
336 mgr()->registerEHFrames(Addr
, LoadAddr
, Size
);
339 virtual void registerEHFrames(llvm::StringRef SectionData
) {
340 mgr()->registerEHFrames(SectionData
);
343 #if HAVE_LLVM >= 0x0500
344 virtual void deregisterEHFrames() {
345 mgr()->deregisterEHFrames();
347 #elif HAVE_LLVM >= 0x0304
348 virtual void deregisterEHFrames(uint8_t *Addr
, uint64_t LoadAddr
, size_t Size
) {
349 mgr()->deregisterEHFrames(Addr
, LoadAddr
, Size
);
352 virtual void *getPointerToNamedFunction(const std::string
&Name
,
353 bool AbortOnFailure
=true) {
354 return mgr()->getPointerToNamedFunction(Name
, AbortOnFailure
);
356 #if HAVE_LLVM <= 0x0303
357 virtual bool applyPermissions(std::string
*ErrMsg
= 0) {
358 return mgr()->applyPermissions(ErrMsg
);
361 virtual bool finalizeMemory(std::string
*ErrMsg
= 0) {
362 return mgr()->finalizeMemory(ErrMsg
);
369 * Delegate memory management to one shared manager for more efficient use
370 * of memory than creating a separate pool for each LLVM engine.
371 * Keep generated code until freeGeneratedCode() is called, instead of when
372 * memory manager is destroyed, which happens during engine destruction.
373 * This allows additional memory savings as we don't have to keep the engine
374 * around in order to use the code.
375 * All methods are delegated to the shared manager except destruction and
376 * deallocating code. For the latter we just remember what needs to be
377 * deallocated later. The shared manager is deleted once it is empty.
379 class ShaderMemoryManager
: public DelegatingJITMemoryManager
{
381 BaseMemoryManager
*TheMM
;
383 struct GeneratedCode
{
384 typedef std::vector
<void *> Vec
;
385 Vec FunctionBody
, ExceptionTable
;
386 BaseMemoryManager
*TheMM
;
388 GeneratedCode(BaseMemoryManager
*MM
) {
394 * Deallocate things as previously requested and
395 * free shared manager when no longer used.
397 #if HAVE_LLVM < 0x0306
401 for ( i
= FunctionBody
.begin(); i
!= FunctionBody
.end(); ++i
)
402 TheMM
->deallocateFunctionBody(*i
);
403 #if HAVE_LLVM < 0x0304
404 for ( i
= ExceptionTable
.begin(); i
!= ExceptionTable
.end(); ++i
)
405 TheMM
->deallocateExceptionTable(*i
);
406 #endif /* HAVE_LLVM < 0x0304 */
407 #endif /* HAVE_LLVM < 0x0306 */
413 BaseMemoryManager
*mgr() const {
419 ShaderMemoryManager(BaseMemoryManager
* MM
) {
421 code
= new GeneratedCode(MM
);
424 virtual ~ShaderMemoryManager() {
426 * 'code' is purposely not deleted. It is the user's responsibility
427 * to call getGeneratedCode() and freeGeneratedCode().
431 struct lp_generated_code
*getGeneratedCode() {
432 return (struct lp_generated_code
*) code
;
435 static void freeGeneratedCode(struct lp_generated_code
*code
) {
436 delete (GeneratedCode
*) code
;
439 #if HAVE_LLVM < 0x0304
440 virtual void deallocateExceptionTable(void *ET
) {
441 // remember for later deallocation
442 code
->ExceptionTable
.push_back(ET
);
446 virtual void deallocateFunctionBody(void *Body
) {
447 // remember for later deallocation
448 code
->FunctionBody
.push_back(Body
);
454 * Same as LLVMCreateJITCompilerForModule, but:
455 * - allows using MCJIT and enabling AVX feature where available.
456 * - set target options
459 * - llvm/lib/ExecutionEngine/ExecutionEngineBindings.cpp
460 * - llvm/tools/lli/lli.cpp
461 * - http://markmail.org/message/ttkuhvgj4cxxy2on#query:+page:1+mid:aju2dggerju3ivd3+state:results
465 lp_build_create_jit_compiler_for_module(LLVMExecutionEngineRef
*OutJIT
,
466 lp_generated_code
**OutCode
,
468 LLVMMCJITMemoryManagerRef CMM
,
473 using namespace llvm
;
476 #if HAVE_LLVM >= 0x0306
477 EngineBuilder
builder(std::unique_ptr
<Module
>(unwrap(M
)));
479 EngineBuilder
builder(unwrap(M
));
483 * LLVM 3.1+ haven't more "extern unsigned llvm::StackAlignmentOverride" and
484 * friends for configuring code generation options, like stack alignment.
486 TargetOptions options
;
487 #if defined(PIPE_ARCH_X86)
488 options
.StackAlignmentOverride
= 4;
489 #if HAVE_LLVM < 0x0304
490 options
.RealignStack
= true;
494 #if defined(DEBUG) && HAVE_LLVM < 0x0307
495 options
.JITEmitDebugInfo
= true;
498 /* XXX: Workaround http://llvm.org/PR21435 */
499 #if defined(DEBUG) || defined(PROFILE) || \
500 (HAVE_LLVM >= 0x0303 && (defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)))
501 #if HAVE_LLVM < 0x0304
502 options
.NoFramePointerElimNonLeaf
= true;
504 #if HAVE_LLVM < 0x0307
505 options
.NoFramePointerElim
= true;
509 builder
.setEngineKind(EngineKind::JIT
)
511 .setTargetOptions(options
)
512 .setOptLevel((CodeGenOpt::Level
)OptLevel
);
515 #if HAVE_LLVM < 0x0306
516 builder
.setUseMCJIT(true);
520 * MCJIT works on Windows, but currently only through ELF object format.
522 * XXX: We could use `LLVM_HOST_TRIPLE "-elf"` but LLVM_HOST_TRIPLE has
523 * different strings for MinGW/MSVC, so better play it safe and be
527 LLVMSetTarget(M
, "x86_64-pc-win32-elf");
529 LLVMSetTarget(M
, "i686-pc-win32-elf");
534 llvm::SmallVector
<std::string
, 16> MAttrs
;
536 #if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
537 #if HAVE_LLVM >= 0x0400
538 /* llvm-3.7+ implements sys::getHostCPUFeatures for x86,
539 * which allows us to enable/disable code generation based
540 * on the results of cpuid.
542 llvm::StringMap
<bool> features
;
543 llvm::sys::getHostCPUFeatures(features
);
545 for (StringMapIterator
<bool> f
= features
.begin();
548 MAttrs
.push_back(((*f
).second
? "+" : "-") + (*f
).first().str());
552 * We need to unset attributes because sometimes LLVM mistakenly assumes
553 * certain features are present given the processor name.
555 * https://bugs.freedesktop.org/show_bug.cgi?id=92214
556 * http://llvm.org/PR25021
557 * http://llvm.org/PR19429
558 * http://llvm.org/PR16721
560 MAttrs
.push_back(util_cpu_caps
.has_sse
? "+sse" : "-sse" );
561 MAttrs
.push_back(util_cpu_caps
.has_sse2
? "+sse2" : "-sse2" );
562 MAttrs
.push_back(util_cpu_caps
.has_sse3
? "+sse3" : "-sse3" );
563 MAttrs
.push_back(util_cpu_caps
.has_ssse3
? "+ssse3" : "-ssse3" );
564 #if HAVE_LLVM >= 0x0304
565 MAttrs
.push_back(util_cpu_caps
.has_sse4_1
? "+sse4.1" : "-sse4.1");
567 MAttrs
.push_back(util_cpu_caps
.has_sse4_1
? "+sse41" : "-sse41" );
569 #if HAVE_LLVM >= 0x0304
570 MAttrs
.push_back(util_cpu_caps
.has_sse4_2
? "+sse4.2" : "-sse4.2");
572 MAttrs
.push_back(util_cpu_caps
.has_sse4_2
? "+sse42" : "-sse42" );
575 * AVX feature is not automatically detected from CPUID by the X86 target
576 * yet, because the old (yet default) JIT engine is not capable of
577 * emitting the opcodes. On newer llvm versions it is and at least some
578 * versions (tested with 3.3) will emit avx opcodes without this anyway.
580 MAttrs
.push_back(util_cpu_caps
.has_avx
? "+avx" : "-avx");
581 MAttrs
.push_back(util_cpu_caps
.has_f16c
? "+f16c" : "-f16c");
582 if (HAVE_LLVM
>= 0x0304) {
583 MAttrs
.push_back(util_cpu_caps
.has_fma
? "+fma" : "-fma");
586 * The old JIT in LLVM 3.3 has a bug encoding llvm.fmuladd.f32 and
587 * llvm.fmuladd.v2f32 intrinsics when FMA is available.
589 MAttrs
.push_back("-fma");
591 MAttrs
.push_back(util_cpu_caps
.has_avx2
? "+avx2" : "-avx2");
592 /* disable avx512 and all subvariants */
593 #if HAVE_LLVM >= 0x0304
594 MAttrs
.push_back("-avx512cd");
595 MAttrs
.push_back("-avx512er");
596 MAttrs
.push_back("-avx512f");
597 MAttrs
.push_back("-avx512pf");
599 #if HAVE_LLVM >= 0x0305
600 MAttrs
.push_back("-avx512bw");
601 MAttrs
.push_back("-avx512dq");
602 MAttrs
.push_back("-avx512vl");
607 #if defined(PIPE_ARCH_PPC)
608 MAttrs
.push_back(util_cpu_caps
.has_altivec
? "+altivec" : "-altivec");
609 #if (HAVE_LLVM >= 0x0304)
610 #if (HAVE_LLVM <= 0x0307) || (HAVE_LLVM == 0x0308 && MESA_LLVM_VERSION_PATCH == 0)
612 * Make sure VSX instructions are disabled
613 * See LLVM bug https://llvm.org/bugs/show_bug.cgi?id=25503#c7
615 if (util_cpu_caps
.has_altivec
) {
616 MAttrs
.push_back("-vsx");
620 * However, bug 25503 is fixed, by the same fix that fixed
621 * bug 26775, in versions of LLVM later than 3.8 (starting with 3.8.1):
622 * Make sure VSX instructions are ENABLED
623 * See LLVM bug https://llvm.org/bugs/show_bug.cgi?id=26775
625 if (util_cpu_caps
.has_altivec
) {
626 MAttrs
.push_back("+vsx");
632 builder
.setMAttrs(MAttrs
);
634 if (gallivm_debug
& (GALLIVM_DEBUG_IR
| GALLIVM_DEBUG_ASM
| GALLIVM_DEBUG_DUMP_BC
)) {
635 int n
= MAttrs
.size();
637 debug_printf("llc -mattr option(s): ");
638 for (int i
= 0; i
< n
; i
++)
639 debug_printf("%s%s", MAttrs
[i
].c_str(), (i
< n
- 1) ? "," : "");
644 #if HAVE_LLVM >= 0x0305
645 StringRef MCPU
= llvm::sys::getHostCPUName();
647 * The cpu bits are no longer set automatically, so need to set mcpu manually.
648 * Note that the MAttrs set above will be sort of ignored (since we should
649 * not set any which would not be set by specifying the cpu anyway).
650 * It ought to be safe though since getHostCPUName() should include bits
651 * not only from the cpu but environment as well (for instance if it's safe
652 * to use avx instructions which need OS support). According to
653 * http://llvm.org/bugs/show_bug.cgi?id=19429 however if I understand this
654 * right it may be necessary to specify older cpu (or disable mattrs) though
655 * when not using MCJIT so no instructions are generated which the old JIT
656 * can't handle. Not entirely sure if we really need to do anything yet.
658 #if defined(PIPE_ARCH_LITTLE_ENDIAN) && defined(PIPE_ARCH_PPC_64)
660 * Versions of LLVM prior to 4.0 lacked a table entry for "POWER8NVL",
661 * resulting in (big-endian) "generic" being returned on
662 * little-endian Power8NVL systems. The result was that code that
663 * attempted to load the least significant 32 bits of a 64-bit quantity
664 * from memory loaded the wrong half. This resulted in failures in some
666 * .../arb_gpu_shader_fp64/execution/conversion/frag-conversion-explicit-double-uint
668 if (MCPU
== "generic")
671 builder
.setMCPU(MCPU
);
672 if (gallivm_debug
& (GALLIVM_DEBUG_IR
| GALLIVM_DEBUG_ASM
| GALLIVM_DEBUG_DUMP_BC
)) {
673 debug_printf("llc -mcpu option: %s\n", MCPU
.str().c_str());
677 ShaderMemoryManager
*MM
= NULL
;
679 BaseMemoryManager
* JMM
= reinterpret_cast<BaseMemoryManager
*>(CMM
);
680 MM
= new ShaderMemoryManager(JMM
);
681 *OutCode
= MM
->getGeneratedCode();
683 #if HAVE_LLVM >= 0x0306
684 builder
.setMCJITMemoryManager(std::unique_ptr
<RTDyldMemoryManager
>(MM
));
685 MM
= NULL
; // ownership taken by std::unique_ptr
686 #elif HAVE_LLVM > 0x0303
687 builder
.setMCJITMemoryManager(MM
);
689 builder
.setJITMemoryManager(MM
);
692 #if HAVE_LLVM < 0x0306
693 BaseMemoryManager
* JMM
= reinterpret_cast<BaseMemoryManager
*>(CMM
);
694 MM
= new ShaderMemoryManager(JMM
);
695 *OutCode
= MM
->getGeneratedCode();
697 builder
.setJITMemoryManager(MM
);
703 ExecutionEngine
*JIT
;
705 JIT
= builder
.create();
706 #if LLVM_USE_INTEL_JITEVENTS
707 JITEventListener
*JEL
= JITEventListener::createIntelJITEventListener();
708 JIT
->RegisterJITEventListener(JEL
);
714 lp_free_generated_code(*OutCode
);
717 *OutError
= strdup(Error
.c_str());
724 lp_free_generated_code(struct lp_generated_code
*code
)
726 ShaderMemoryManager::freeGeneratedCode(code
);
730 LLVMMCJITMemoryManagerRef
731 lp_get_default_memory_manager()
733 BaseMemoryManager
*mm
;
734 #if HAVE_LLVM < 0x0306
735 mm
= llvm::JITMemoryManager::CreateDefaultMemManager();
737 mm
= new llvm::SectionMemoryManager();
739 return reinterpret_cast<LLVMMCJITMemoryManagerRef
>(mm
);
744 lp_free_memory_manager(LLVMMCJITMemoryManagerRef memorymgr
)
746 delete reinterpret_cast<BaseMemoryManager
*>(memorymgr
);
749 extern "C" LLVMValueRef
750 lp_get_called_value(LLVMValueRef call
)
752 #if HAVE_LLVM >= 0x0309
753 return LLVMGetCalledValue(call
);
754 #elif HAVE_LLVM >= 0x0305
755 return llvm::wrap(llvm::CallSite(llvm::unwrap
<llvm::Instruction
>(call
)).getCalledValue());
757 return NULL
; /* radeonsi doesn't support so old LLVM. */
762 lp_is_function(LLVMValueRef v
)
764 #if HAVE_LLVM >= 0x0309
765 return LLVMGetValueKind(v
) == LLVMFunctionValueKind
;
767 return llvm::isa
<llvm::Function
>(llvm::unwrap(v
));
771 extern "C" LLVMBuilderRef
772 lp_create_builder(LLVMContextRef ctx
, enum lp_float_mode float_mode
)
774 LLVMBuilderRef builder
= LLVMCreateBuilderInContext(ctx
);
776 #if HAVE_LLVM >= 0x0308
777 llvm::FastMathFlags flags
;
779 switch (float_mode
) {
780 case LP_FLOAT_MODE_DEFAULT
:
782 case LP_FLOAT_MODE_NO_SIGNED_ZEROS_FP_MATH
:
783 flags
.setNoSignedZeros();
784 llvm::unwrap(builder
)->setFastMathFlags(flags
);
786 case LP_FLOAT_MODE_UNSAFE_FP_MATH
:
787 flags
.setUnsafeAlgebra();
788 llvm::unwrap(builder
)->setFastMathFlags(flags
);