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 * The llvm target registry is not thread-safe, so drivers and state-trackers
129 * that want to initialize targets should use the gallivm_init_llvm_targets()
130 * function to safely initialize targets.
132 * LLVM targets should be initialized before the driver or state-tracker tries
133 * to access the registry.
136 gallivm_init_llvm_targets(void)
138 call_once(&init_native_targets_once_flag
, init_native_targets
);
142 lp_set_target_options(void)
144 #if HAVE_LLVM < 0x0304
146 * By default LLVM adds a signal handler to output a pretty stack trace.
147 * This signal handler is never removed, causing problems when unloading the
148 * shared object where the gallium driver resides.
150 llvm::DisablePrettyStackTrace
= true;
153 gallivm_init_llvm_targets();
157 LLVMTargetLibraryInfoRef
158 gallivm_create_target_library_info(const char *triple
)
160 return reinterpret_cast<LLVMTargetLibraryInfoRef
>(
161 #if HAVE_LLVM < 0x0307
162 new llvm::TargetLibraryInfo(
164 new llvm::TargetLibraryInfoImpl(
166 llvm::Triple(triple
)));
171 gallivm_dispose_target_library_info(LLVMTargetLibraryInfoRef library_info
)
173 delete reinterpret_cast<
174 #if HAVE_LLVM < 0x0307
175 llvm::TargetLibraryInfo
177 llvm::TargetLibraryInfoImpl
183 #if HAVE_LLVM < 0x0304
187 LLVMSetAlignmentBackport(LLVMValueRef V
,
190 switch (LLVMGetInstructionOpcode(V
)) {
192 llvm::unwrap
<llvm::LoadInst
>(V
)->setAlignment(Bytes
);
195 llvm::unwrap
<llvm::StoreInst
>(V
)->setAlignment(Bytes
);
206 #if HAVE_LLVM < 0x0306
207 typedef llvm::JITMemoryManager BaseMemoryManager
;
209 typedef llvm::RTDyldMemoryManager BaseMemoryManager
;
214 * Delegating is tedious but the default manager class is hidden in an
215 * anonymous namespace in LLVM, so we cannot just derive from it to change
218 class DelegatingJITMemoryManager
: public BaseMemoryManager
{
221 virtual BaseMemoryManager
*mgr() const = 0;
224 #if HAVE_LLVM < 0x0306
226 * From JITMemoryManager
228 virtual void setMemoryWritable() {
229 mgr()->setMemoryWritable();
231 virtual void setMemoryExecutable() {
232 mgr()->setMemoryExecutable();
234 virtual void setPoisonMemory(bool poison
) {
235 mgr()->setPoisonMemory(poison
);
237 virtual void AllocateGOT() {
238 mgr()->AllocateGOT();
240 * isManagingGOT() is not virtual in base class so we can't delegate.
241 * Instead we mirror the value of HasGOT in our instance.
243 HasGOT
= mgr()->isManagingGOT();
245 virtual uint8_t *getGOTBase() const {
246 return mgr()->getGOTBase();
248 virtual uint8_t *startFunctionBody(const llvm::Function
*F
,
249 uintptr_t &ActualSize
) {
250 return mgr()->startFunctionBody(F
, ActualSize
);
252 virtual uint8_t *allocateStub(const llvm::GlobalValue
*F
,
254 unsigned Alignment
) {
255 return mgr()->allocateStub(F
, StubSize
, Alignment
);
257 virtual void endFunctionBody(const llvm::Function
*F
,
258 uint8_t *FunctionStart
,
259 uint8_t *FunctionEnd
) {
260 mgr()->endFunctionBody(F
, FunctionStart
, FunctionEnd
);
262 virtual uint8_t *allocateSpace(intptr_t Size
, unsigned Alignment
) {
263 return mgr()->allocateSpace(Size
, Alignment
);
265 virtual uint8_t *allocateGlobal(uintptr_t Size
, unsigned Alignment
) {
266 return mgr()->allocateGlobal(Size
, Alignment
);
268 virtual void deallocateFunctionBody(void *Body
) {
269 mgr()->deallocateFunctionBody(Body
);
271 #if HAVE_LLVM < 0x0304
272 virtual uint8_t *startExceptionTable(const llvm::Function
*F
,
273 uintptr_t &ActualSize
) {
274 return mgr()->startExceptionTable(F
, ActualSize
);
276 virtual void endExceptionTable(const llvm::Function
*F
,
279 uint8_t *FrameRegister
) {
280 mgr()->endExceptionTable(F
, TableStart
, TableEnd
,
283 virtual void deallocateExceptionTable(void *ET
) {
284 mgr()->deallocateExceptionTable(ET
);
287 virtual bool CheckInvariants(std::string
&s
) {
288 return mgr()->CheckInvariants(s
);
290 virtual size_t GetDefaultCodeSlabSize() {
291 return mgr()->GetDefaultCodeSlabSize();
293 virtual size_t GetDefaultDataSlabSize() {
294 return mgr()->GetDefaultDataSlabSize();
296 virtual size_t GetDefaultStubSlabSize() {
297 return mgr()->GetDefaultStubSlabSize();
299 virtual unsigned GetNumCodeSlabs() {
300 return mgr()->GetNumCodeSlabs();
302 virtual unsigned GetNumDataSlabs() {
303 return mgr()->GetNumDataSlabs();
305 virtual unsigned GetNumStubSlabs() {
306 return mgr()->GetNumStubSlabs();
311 * From RTDyldMemoryManager
313 #if HAVE_LLVM >= 0x0304
314 virtual uint8_t *allocateCodeSection(uintptr_t Size
,
317 llvm::StringRef SectionName
) {
318 return mgr()->allocateCodeSection(Size
, Alignment
, SectionID
,
322 virtual uint8_t *allocateCodeSection(uintptr_t Size
,
324 unsigned SectionID
) {
325 return mgr()->allocateCodeSection(Size
, Alignment
, SectionID
);
328 virtual uint8_t *allocateDataSection(uintptr_t Size
,
331 #if HAVE_LLVM >= 0x0304
332 llvm::StringRef SectionName
,
335 return mgr()->allocateDataSection(Size
, Alignment
, SectionID
,
336 #if HAVE_LLVM >= 0x0304
341 #if HAVE_LLVM >= 0x0304
342 virtual void registerEHFrames(uint8_t *Addr
, uint64_t LoadAddr
, size_t Size
) {
343 mgr()->registerEHFrames(Addr
, LoadAddr
, Size
);
345 virtual void deregisterEHFrames(uint8_t *Addr
, uint64_t LoadAddr
, size_t Size
) {
346 mgr()->deregisterEHFrames(Addr
, LoadAddr
, Size
);
349 virtual void registerEHFrames(llvm::StringRef SectionData
) {
350 mgr()->registerEHFrames(SectionData
);
353 virtual void *getPointerToNamedFunction(const std::string
&Name
,
354 bool AbortOnFailure
=true) {
355 return mgr()->getPointerToNamedFunction(Name
, AbortOnFailure
);
357 #if HAVE_LLVM <= 0x0303
358 virtual bool applyPermissions(std::string
*ErrMsg
= 0) {
359 return mgr()->applyPermissions(ErrMsg
);
362 virtual bool finalizeMemory(std::string
*ErrMsg
= 0) {
363 return mgr()->finalizeMemory(ErrMsg
);
370 * Delegate memory management to one shared manager for more efficient use
371 * of memory than creating a separate pool for each LLVM engine.
372 * Keep generated code until freeGeneratedCode() is called, instead of when
373 * memory manager is destroyed, which happens during engine destruction.
374 * This allows additional memory savings as we don't have to keep the engine
375 * around in order to use the code.
376 * All methods are delegated to the shared manager except destruction and
377 * deallocating code. For the latter we just remember what needs to be
378 * deallocated later. The shared manager is deleted once it is empty.
380 class ShaderMemoryManager
: public DelegatingJITMemoryManager
{
382 BaseMemoryManager
*TheMM
;
384 struct GeneratedCode
{
385 typedef std::vector
<void *> Vec
;
386 Vec FunctionBody
, ExceptionTable
;
387 BaseMemoryManager
*TheMM
;
389 GeneratedCode(BaseMemoryManager
*MM
) {
395 * Deallocate things as previously requested and
396 * free shared manager when no longer used.
398 #if HAVE_LLVM < 0x0306
402 for ( i
= FunctionBody
.begin(); i
!= FunctionBody
.end(); ++i
)
403 TheMM
->deallocateFunctionBody(*i
);
404 #if HAVE_LLVM < 0x0304
405 for ( i
= ExceptionTable
.begin(); i
!= ExceptionTable
.end(); ++i
)
406 TheMM
->deallocateExceptionTable(*i
);
407 #endif /* HAVE_LLVM < 0x0304 */
408 #endif /* HAVE_LLVM < 0x0306 */
414 BaseMemoryManager
*mgr() const {
420 ShaderMemoryManager(BaseMemoryManager
* MM
) {
422 code
= new GeneratedCode(MM
);
425 virtual ~ShaderMemoryManager() {
427 * 'code' is purposely not deleted. It is the user's responsibility
428 * to call getGeneratedCode() and freeGeneratedCode().
432 struct lp_generated_code
*getGeneratedCode() {
433 return (struct lp_generated_code
*) code
;
436 static void freeGeneratedCode(struct lp_generated_code
*code
) {
437 delete (GeneratedCode
*) code
;
440 #if HAVE_LLVM < 0x0304
441 virtual void deallocateExceptionTable(void *ET
) {
442 // remember for later deallocation
443 code
->ExceptionTable
.push_back(ET
);
447 virtual void deallocateFunctionBody(void *Body
) {
448 // remember for later deallocation
449 code
->FunctionBody
.push_back(Body
);
455 * Same as LLVMCreateJITCompilerForModule, but:
456 * - allows using MCJIT and enabling AVX feature where available.
457 * - set target options
460 * - llvm/lib/ExecutionEngine/ExecutionEngineBindings.cpp
461 * - llvm/tools/lli/lli.cpp
462 * - http://markmail.org/message/ttkuhvgj4cxxy2on#query:+page:1+mid:aju2dggerju3ivd3+state:results
466 lp_build_create_jit_compiler_for_module(LLVMExecutionEngineRef
*OutJIT
,
467 lp_generated_code
**OutCode
,
469 LLVMMCJITMemoryManagerRef CMM
,
474 using namespace llvm
;
477 #if HAVE_LLVM >= 0x0306
478 EngineBuilder
builder(std::unique_ptr
<Module
>(unwrap(M
)));
480 EngineBuilder
builder(unwrap(M
));
484 * LLVM 3.1+ haven't more "extern unsigned llvm::StackAlignmentOverride" and
485 * friends for configuring code generation options, like stack alignment.
487 TargetOptions options
;
488 #if defined(PIPE_ARCH_X86)
489 options
.StackAlignmentOverride
= 4;
490 #if HAVE_LLVM < 0x0304
491 options
.RealignStack
= true;
495 #if defined(DEBUG) && HAVE_LLVM < 0x0307
496 options
.JITEmitDebugInfo
= true;
499 /* XXX: Workaround http://llvm.org/PR21435 */
500 #if defined(DEBUG) || defined(PROFILE) || \
501 (HAVE_LLVM >= 0x0303 && (defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)))
502 #if HAVE_LLVM < 0x0304
503 options
.NoFramePointerElimNonLeaf
= true;
505 #if HAVE_LLVM < 0x0307
506 options
.NoFramePointerElim
= true;
510 builder
.setEngineKind(EngineKind::JIT
)
512 .setTargetOptions(options
)
513 .setOptLevel((CodeGenOpt::Level
)OptLevel
);
516 #if HAVE_LLVM < 0x0306
517 builder
.setUseMCJIT(true);
521 * MCJIT works on Windows, but currently only through ELF object format.
523 * XXX: We could use `LLVM_HOST_TRIPLE "-elf"` but LLVM_HOST_TRIPLE has
524 * different strings for MinGW/MSVC, so better play it safe and be
528 LLVMSetTarget(M
, "x86_64-pc-win32-elf");
530 LLVMSetTarget(M
, "i686-pc-win32-elf");
535 llvm::SmallVector
<std::string
, 16> MAttrs
;
537 #if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
538 #if HAVE_LLVM >= 0x0400
539 /* llvm-3.7+ implements sys::getHostCPUFeatures for x86,
540 * which allows us to enable/disable code generation based
541 * on the results of cpuid.
543 llvm::StringMap
<bool> features
;
544 llvm::sys::getHostCPUFeatures(features
);
546 for (StringMapIterator
<bool> f
= features
.begin();
549 MAttrs
.push_back(((*f
).second
? "+" : "-") + (*f
).first().str());
553 * We need to unset attributes because sometimes LLVM mistakenly assumes
554 * certain features are present given the processor name.
556 * https://bugs.freedesktop.org/show_bug.cgi?id=92214
557 * http://llvm.org/PR25021
558 * http://llvm.org/PR19429
559 * http://llvm.org/PR16721
561 MAttrs
.push_back(util_cpu_caps
.has_sse
? "+sse" : "-sse" );
562 MAttrs
.push_back(util_cpu_caps
.has_sse2
? "+sse2" : "-sse2" );
563 MAttrs
.push_back(util_cpu_caps
.has_sse3
? "+sse3" : "-sse3" );
564 MAttrs
.push_back(util_cpu_caps
.has_ssse3
? "+ssse3" : "-ssse3" );
565 #if HAVE_LLVM >= 0x0304
566 MAttrs
.push_back(util_cpu_caps
.has_sse4_1
? "+sse4.1" : "-sse4.1");
568 MAttrs
.push_back(util_cpu_caps
.has_sse4_1
? "+sse41" : "-sse41" );
570 #if HAVE_LLVM >= 0x0304
571 MAttrs
.push_back(util_cpu_caps
.has_sse4_2
? "+sse4.2" : "-sse4.2");
573 MAttrs
.push_back(util_cpu_caps
.has_sse4_2
? "+sse42" : "-sse42" );
576 * AVX feature is not automatically detected from CPUID by the X86 target
577 * yet, because the old (yet default) JIT engine is not capable of
578 * emitting the opcodes. On newer llvm versions it is and at least some
579 * versions (tested with 3.3) will emit avx opcodes without this anyway.
581 MAttrs
.push_back(util_cpu_caps
.has_avx
? "+avx" : "-avx");
582 MAttrs
.push_back(util_cpu_caps
.has_f16c
? "+f16c" : "-f16c");
583 if (HAVE_LLVM
>= 0x0304) {
584 MAttrs
.push_back(util_cpu_caps
.has_fma
? "+fma" : "-fma");
587 * The old JIT in LLVM 3.3 has a bug encoding llvm.fmuladd.f32 and
588 * llvm.fmuladd.v2f32 intrinsics when FMA is available.
590 MAttrs
.push_back("-fma");
592 MAttrs
.push_back(util_cpu_caps
.has_avx2
? "+avx2" : "-avx2");
593 /* disable avx512 and all subvariants */
594 #if HAVE_LLVM >= 0x0304
595 MAttrs
.push_back("-avx512cd");
596 MAttrs
.push_back("-avx512er");
597 MAttrs
.push_back("-avx512f");
598 MAttrs
.push_back("-avx512pf");
600 #if HAVE_LLVM >= 0x0305
601 MAttrs
.push_back("-avx512bw");
602 MAttrs
.push_back("-avx512dq");
603 MAttrs
.push_back("-avx512vl");
608 #if defined(PIPE_ARCH_PPC)
609 MAttrs
.push_back(util_cpu_caps
.has_altivec
? "+altivec" : "-altivec");
610 #if (HAVE_LLVM >= 0x0304)
611 #if (HAVE_LLVM <= 0x0307) || (HAVE_LLVM == 0x0308 && MESA_LLVM_VERSION_PATCH == 0)
613 * Make sure VSX instructions are disabled
614 * See LLVM bug https://llvm.org/bugs/show_bug.cgi?id=25503#c7
616 if (util_cpu_caps
.has_altivec
) {
617 MAttrs
.push_back("-vsx");
621 * However, bug 25503 is fixed, by the same fix that fixed
622 * bug 26775, in versions of LLVM later than 3.8 (starting with 3.8.1):
623 * Make sure VSX instructions are ENABLED
624 * See LLVM bug https://llvm.org/bugs/show_bug.cgi?id=26775
626 if (util_cpu_caps
.has_altivec
) {
627 MAttrs
.push_back("+vsx");
633 builder
.setMAttrs(MAttrs
);
635 if (gallivm_debug
& (GALLIVM_DEBUG_IR
| GALLIVM_DEBUG_ASM
| GALLIVM_DEBUG_DUMP_BC
)) {
636 int n
= MAttrs
.size();
638 debug_printf("llc -mattr option(s): ");
639 for (int i
= 0; i
< n
; i
++)
640 debug_printf("%s%s", MAttrs
[i
].c_str(), (i
< n
- 1) ? "," : "");
645 #if HAVE_LLVM >= 0x0305
646 StringRef MCPU
= llvm::sys::getHostCPUName();
648 * The cpu bits are no longer set automatically, so need to set mcpu manually.
649 * Note that the MAttrs set above will be sort of ignored (since we should
650 * not set any which would not be set by specifying the cpu anyway).
651 * It ought to be safe though since getHostCPUName() should include bits
652 * not only from the cpu but environment as well (for instance if it's safe
653 * to use avx instructions which need OS support). According to
654 * http://llvm.org/bugs/show_bug.cgi?id=19429 however if I understand this
655 * right it may be necessary to specify older cpu (or disable mattrs) though
656 * when not using MCJIT so no instructions are generated which the old JIT
657 * can't handle. Not entirely sure if we really need to do anything yet.
659 #if defined(PIPE_ARCH_LITTLE_ENDIAN) && defined(PIPE_ARCH_PPC_64)
661 * Versions of LLVM prior to 4.0 lacked a table entry for "POWER8NVL",
662 * resulting in (big-endian) "generic" being returned on
663 * little-endian Power8NVL systems. The result was that code that
664 * attempted to load the least significant 32 bits of a 64-bit quantity
665 * from memory loaded the wrong half. This resulted in failures in some
667 * .../arb_gpu_shader_fp64/execution/conversion/frag-conversion-explicit-double-uint
669 if (MCPU
== "generic")
672 builder
.setMCPU(MCPU
);
673 if (gallivm_debug
& (GALLIVM_DEBUG_IR
| GALLIVM_DEBUG_ASM
| GALLIVM_DEBUG_DUMP_BC
)) {
674 debug_printf("llc -mcpu option: %s\n", MCPU
.str().c_str());
678 ShaderMemoryManager
*MM
= NULL
;
680 BaseMemoryManager
* JMM
= reinterpret_cast<BaseMemoryManager
*>(CMM
);
681 MM
= new ShaderMemoryManager(JMM
);
682 *OutCode
= MM
->getGeneratedCode();
684 #if HAVE_LLVM >= 0x0306
685 builder
.setMCJITMemoryManager(std::unique_ptr
<RTDyldMemoryManager
>(MM
));
686 MM
= NULL
; // ownership taken by std::unique_ptr
687 #elif HAVE_LLVM > 0x0303
688 builder
.setMCJITMemoryManager(MM
);
690 builder
.setJITMemoryManager(MM
);
693 #if HAVE_LLVM < 0x0306
694 BaseMemoryManager
* JMM
= reinterpret_cast<BaseMemoryManager
*>(CMM
);
695 MM
= new ShaderMemoryManager(JMM
);
696 *OutCode
= MM
->getGeneratedCode();
698 builder
.setJITMemoryManager(MM
);
704 ExecutionEngine
*JIT
;
706 JIT
= builder
.create();
707 #if LLVM_USE_INTEL_JITEVENTS
708 JITEventListener
*JEL
= JITEventListener::createIntelJITEventListener();
709 JIT
->RegisterJITEventListener(JEL
);
715 lp_free_generated_code(*OutCode
);
718 *OutError
= strdup(Error
.c_str());
725 lp_free_generated_code(struct lp_generated_code
*code
)
727 ShaderMemoryManager::freeGeneratedCode(code
);
731 LLVMMCJITMemoryManagerRef
732 lp_get_default_memory_manager()
734 BaseMemoryManager
*mm
;
735 #if HAVE_LLVM < 0x0306
736 mm
= llvm::JITMemoryManager::CreateDefaultMemManager();
738 mm
= new llvm::SectionMemoryManager();
740 return reinterpret_cast<LLVMMCJITMemoryManagerRef
>(mm
);
745 lp_free_memory_manager(LLVMMCJITMemoryManagerRef memorymgr
)
747 delete reinterpret_cast<BaseMemoryManager
*>(memorymgr
);
750 extern "C" LLVMValueRef
751 lp_get_called_value(LLVMValueRef call
)
753 #if HAVE_LLVM >= 0x0309
754 return LLVMGetCalledValue(call
);
755 #elif HAVE_LLVM >= 0x0305
756 return llvm::wrap(llvm::CallSite(llvm::unwrap
<llvm::Instruction
>(call
)).getCalledValue());
758 return NULL
; /* radeonsi doesn't support so old LLVM. */
763 lp_is_function(LLVMValueRef v
)
765 #if HAVE_LLVM >= 0x0309
766 return LLVMGetValueKind(v
) == LLVMFunctionValueKind
;
768 return llvm::isa
<llvm::Function
>(llvm::unwrap(v
));
772 extern "C" LLVMBuilderRef
773 lp_create_builder(LLVMContextRef ctx
, enum lp_float_mode float_mode
)
775 LLVMBuilderRef builder
= LLVMCreateBuilderInContext(ctx
);
777 #if HAVE_LLVM >= 0x0308
778 llvm::FastMathFlags flags
;
780 switch (float_mode
) {
781 case LP_FLOAT_MODE_DEFAULT
:
783 case LP_FLOAT_MODE_NO_SIGNED_ZEROS_FP_MATH
:
784 flags
.setNoSignedZeros();
785 llvm::unwrap(builder
)->setFastMathFlags(flags
);
787 case LP_FLOAT_MODE_UNSAFE_FP_MATH
:
788 flags
.setUnsafeAlgebra();
789 llvm::unwrap(builder
)->setFastMathFlags(flags
);