gallivm: try to fix build with LLVM <= 3.4 due to missing CallSite.h
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_misc.cpp
1 /**************************************************************************
2 *
3 * Copyright 2010 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
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.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 *
26 **************************************************************************/
27
28
29 /**
30 * The purpose of this module is to expose LLVM functionality not available
31 * through the C++ bindings.
32 */
33
34
35 #ifndef __STDC_LIMIT_MACROS
36 #define __STDC_LIMIT_MACROS
37 #endif
38
39 #ifndef __STDC_CONSTANT_MACROS
40 #define __STDC_CONSTANT_MACROS
41 #endif
42
43 // Undef these vars just to silence warnings
44 #undef PACKAGE_BUGREPORT
45 #undef PACKAGE_NAME
46 #undef PACKAGE_STRING
47 #undef PACKAGE_TARNAME
48 #undef PACKAGE_VERSION
49
50
51 #include <stddef.h>
52
53 // Workaround http://llvm.org/PR23628
54 #if HAVE_LLVM >= 0x0307
55 # pragma push_macro("DEBUG")
56 # undef DEBUG
57 #endif
58
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>
66 #else
67 #include <llvm/Target/TargetLibraryInfo.h>
68 #endif
69 #if HAVE_LLVM < 0x0306
70 #include <llvm/ExecutionEngine/JITMemoryManager.h>
71 #else
72 #include <llvm/ExecutionEngine/SectionMemoryManager.h>
73 #endif
74 #include <llvm/Support/CommandLine.h>
75 #include <llvm/Support/Host.h>
76 #include <llvm/Support/PrettyStackTrace.h>
77
78 #include <llvm/Support/TargetSelect.h>
79
80 #if HAVE_LLVM >= 0x0305
81 #include <llvm/IR/CallSite.h>
82 #endif
83 #include <llvm/IR/IRBuilder.h>
84 #include <llvm/IR/Module.h>
85 #include <llvm/Support/CBindingWrapping.h>
86
87 #include <llvm/Config/llvm-config.h>
88 #if LLVM_USE_INTEL_JITEVENTS
89 #include <llvm/ExecutionEngine/JITEventListener.h>
90 #endif
91
92 // Workaround http://llvm.org/PR23628
93 #if HAVE_LLVM >= 0x0307
94 # pragma pop_macro("DEBUG")
95 #endif
96
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"
102
103 #include "lp_bld_misc.h"
104
105 namespace {
106
107 class LLVMEnsureMultithreaded {
108 public:
109 LLVMEnsureMultithreaded()
110 {
111 if (!LLVMIsMultithreaded()) {
112 LLVMStartMultithreaded();
113 }
114 }
115 };
116
117 static LLVMEnsureMultithreaded lLVMEnsureMultithreaded;
118
119 }
120
121 static once_flag init_native_targets_once_flag = ONCE_FLAG_INIT;
122
123 static void init_native_targets()
124 {
125 // If we have a native target, initialize it to ensure it is linked in and
126 // usable by the JIT.
127 llvm::InitializeNativeTarget();
128
129 llvm::InitializeNativeTargetAsmPrinter();
130
131 llvm::InitializeNativeTargetDisassembler();
132 }
133
134 /**
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.
138 *
139 * LLVM targets should be initialized before the driver or state-tracker tries
140 * to access the registry.
141 */
142 extern "C" void
143 gallivm_init_llvm_targets(void)
144 {
145 call_once(&init_native_targets_once_flag, init_native_targets);
146 }
147
148 extern "C" void
149 lp_set_target_options(void)
150 {
151 #if HAVE_LLVM < 0x0304
152 /*
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.
156 */
157 llvm::DisablePrettyStackTrace = true;
158 #endif
159
160 gallivm_init_llvm_targets();
161 }
162
163 extern "C"
164 LLVMTargetLibraryInfoRef
165 gallivm_create_target_library_info(const char *triple)
166 {
167 return reinterpret_cast<LLVMTargetLibraryInfoRef>(
168 #if HAVE_LLVM < 0x0307
169 new llvm::TargetLibraryInfo(
170 #else
171 new llvm::TargetLibraryInfoImpl(
172 #endif
173 llvm::Triple(triple)));
174 }
175
176 extern "C"
177 void
178 gallivm_dispose_target_library_info(LLVMTargetLibraryInfoRef library_info)
179 {
180 delete reinterpret_cast<
181 #if HAVE_LLVM < 0x0307
182 llvm::TargetLibraryInfo
183 #else
184 llvm::TargetLibraryInfoImpl
185 #endif
186 *>(library_info);
187 }
188
189
190 #if HAVE_LLVM < 0x0304
191
192 extern "C"
193 void
194 LLVMSetAlignmentBackport(LLVMValueRef V,
195 unsigned Bytes)
196 {
197 switch (LLVMGetInstructionOpcode(V)) {
198 case LLVMLoad:
199 llvm::unwrap<llvm::LoadInst>(V)->setAlignment(Bytes);
200 break;
201 case LLVMStore:
202 llvm::unwrap<llvm::StoreInst>(V)->setAlignment(Bytes);
203 break;
204 default:
205 assert(0);
206 break;
207 }
208 }
209
210 #endif
211
212
213 #if HAVE_LLVM < 0x0306
214 typedef llvm::JITMemoryManager BaseMemoryManager;
215 #else
216 typedef llvm::RTDyldMemoryManager BaseMemoryManager;
217 #endif
218
219
220 /*
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
223 * its behavior.
224 */
225 class DelegatingJITMemoryManager : public BaseMemoryManager {
226
227 protected:
228 virtual BaseMemoryManager *mgr() const = 0;
229
230 public:
231 #if HAVE_LLVM < 0x0306
232 /*
233 * From JITMemoryManager
234 */
235 virtual void setMemoryWritable() {
236 mgr()->setMemoryWritable();
237 }
238 virtual void setMemoryExecutable() {
239 mgr()->setMemoryExecutable();
240 }
241 virtual void setPoisonMemory(bool poison) {
242 mgr()->setPoisonMemory(poison);
243 }
244 virtual void AllocateGOT() {
245 mgr()->AllocateGOT();
246 /*
247 * isManagingGOT() is not virtual in base class so we can't delegate.
248 * Instead we mirror the value of HasGOT in our instance.
249 */
250 HasGOT = mgr()->isManagingGOT();
251 }
252 virtual uint8_t *getGOTBase() const {
253 return mgr()->getGOTBase();
254 }
255 virtual uint8_t *startFunctionBody(const llvm::Function *F,
256 uintptr_t &ActualSize) {
257 return mgr()->startFunctionBody(F, ActualSize);
258 }
259 virtual uint8_t *allocateStub(const llvm::GlobalValue *F,
260 unsigned StubSize,
261 unsigned Alignment) {
262 return mgr()->allocateStub(F, StubSize, Alignment);
263 }
264 virtual void endFunctionBody(const llvm::Function *F,
265 uint8_t *FunctionStart,
266 uint8_t *FunctionEnd) {
267 mgr()->endFunctionBody(F, FunctionStart, FunctionEnd);
268 }
269 virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) {
270 return mgr()->allocateSpace(Size, Alignment);
271 }
272 virtual uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) {
273 return mgr()->allocateGlobal(Size, Alignment);
274 }
275 virtual void deallocateFunctionBody(void *Body) {
276 mgr()->deallocateFunctionBody(Body);
277 }
278 #if HAVE_LLVM < 0x0304
279 virtual uint8_t *startExceptionTable(const llvm::Function *F,
280 uintptr_t &ActualSize) {
281 return mgr()->startExceptionTable(F, ActualSize);
282 }
283 virtual void endExceptionTable(const llvm::Function *F,
284 uint8_t *TableStart,
285 uint8_t *TableEnd,
286 uint8_t *FrameRegister) {
287 mgr()->endExceptionTable(F, TableStart, TableEnd,
288 FrameRegister);
289 }
290 virtual void deallocateExceptionTable(void *ET) {
291 mgr()->deallocateExceptionTable(ET);
292 }
293 #endif
294 virtual bool CheckInvariants(std::string &s) {
295 return mgr()->CheckInvariants(s);
296 }
297 virtual size_t GetDefaultCodeSlabSize() {
298 return mgr()->GetDefaultCodeSlabSize();
299 }
300 virtual size_t GetDefaultDataSlabSize() {
301 return mgr()->GetDefaultDataSlabSize();
302 }
303 virtual size_t GetDefaultStubSlabSize() {
304 return mgr()->GetDefaultStubSlabSize();
305 }
306 virtual unsigned GetNumCodeSlabs() {
307 return mgr()->GetNumCodeSlabs();
308 }
309 virtual unsigned GetNumDataSlabs() {
310 return mgr()->GetNumDataSlabs();
311 }
312 virtual unsigned GetNumStubSlabs() {
313 return mgr()->GetNumStubSlabs();
314 }
315 #endif
316
317 /*
318 * From RTDyldMemoryManager
319 */
320 #if HAVE_LLVM >= 0x0304
321 virtual uint8_t *allocateCodeSection(uintptr_t Size,
322 unsigned Alignment,
323 unsigned SectionID,
324 llvm::StringRef SectionName) {
325 return mgr()->allocateCodeSection(Size, Alignment, SectionID,
326 SectionName);
327 }
328 #else
329 virtual uint8_t *allocateCodeSection(uintptr_t Size,
330 unsigned Alignment,
331 unsigned SectionID) {
332 return mgr()->allocateCodeSection(Size, Alignment, SectionID);
333 }
334 #endif
335 virtual uint8_t *allocateDataSection(uintptr_t Size,
336 unsigned Alignment,
337 unsigned SectionID,
338 #if HAVE_LLVM >= 0x0304
339 llvm::StringRef SectionName,
340 #endif
341 bool IsReadOnly) {
342 return mgr()->allocateDataSection(Size, Alignment, SectionID,
343 #if HAVE_LLVM >= 0x0304
344 SectionName,
345 #endif
346 IsReadOnly);
347 }
348 #if HAVE_LLVM >= 0x0304
349 virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) {
350 mgr()->registerEHFrames(Addr, LoadAddr, Size);
351 }
352 virtual void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) {
353 mgr()->deregisterEHFrames(Addr, LoadAddr, Size);
354 }
355 #else
356 virtual void registerEHFrames(llvm::StringRef SectionData) {
357 mgr()->registerEHFrames(SectionData);
358 }
359 #endif
360 virtual void *getPointerToNamedFunction(const std::string &Name,
361 bool AbortOnFailure=true) {
362 return mgr()->getPointerToNamedFunction(Name, AbortOnFailure);
363 }
364 #if HAVE_LLVM <= 0x0303
365 virtual bool applyPermissions(std::string *ErrMsg = 0) {
366 return mgr()->applyPermissions(ErrMsg);
367 }
368 #else
369 virtual bool finalizeMemory(std::string *ErrMsg = 0) {
370 return mgr()->finalizeMemory(ErrMsg);
371 }
372 #endif
373 };
374
375
376 /*
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.
386 */
387 class ShaderMemoryManager : public DelegatingJITMemoryManager {
388
389 BaseMemoryManager *TheMM;
390
391 struct GeneratedCode {
392 typedef std::vector<void *> Vec;
393 Vec FunctionBody, ExceptionTable;
394 BaseMemoryManager *TheMM;
395
396 GeneratedCode(BaseMemoryManager *MM) {
397 TheMM = MM;
398 }
399
400 ~GeneratedCode() {
401 /*
402 * Deallocate things as previously requested and
403 * free shared manager when no longer used.
404 */
405 #if HAVE_LLVM < 0x0306
406 Vec::iterator i;
407
408 assert(TheMM);
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 */
416 }
417 };
418
419 GeneratedCode *code;
420
421 BaseMemoryManager *mgr() const {
422 return TheMM;
423 }
424
425 public:
426
427 ShaderMemoryManager(BaseMemoryManager* MM) {
428 TheMM = MM;
429 code = new GeneratedCode(MM);
430 }
431
432 virtual ~ShaderMemoryManager() {
433 /*
434 * 'code' is purposely not deleted. It is the user's responsibility
435 * to call getGeneratedCode() and freeGeneratedCode().
436 */
437 }
438
439 struct lp_generated_code *getGeneratedCode() {
440 return (struct lp_generated_code *) code;
441 }
442
443 static void freeGeneratedCode(struct lp_generated_code *code) {
444 delete (GeneratedCode *) code;
445 }
446
447 #if HAVE_LLVM < 0x0304
448 virtual void deallocateExceptionTable(void *ET) {
449 // remember for later deallocation
450 code->ExceptionTable.push_back(ET);
451 }
452 #endif
453
454 virtual void deallocateFunctionBody(void *Body) {
455 // remember for later deallocation
456 code->FunctionBody.push_back(Body);
457 }
458 };
459
460
461 /**
462 * Same as LLVMCreateJITCompilerForModule, but:
463 * - allows using MCJIT and enabling AVX feature where available.
464 * - set target options
465 *
466 * See also:
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
470 */
471 extern "C"
472 LLVMBool
473 lp_build_create_jit_compiler_for_module(LLVMExecutionEngineRef *OutJIT,
474 lp_generated_code **OutCode,
475 LLVMModuleRef M,
476 LLVMMCJITMemoryManagerRef CMM,
477 unsigned OptLevel,
478 int useMCJIT,
479 char **OutError)
480 {
481 using namespace llvm;
482
483 std::string Error;
484 #if HAVE_LLVM >= 0x0306
485 EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
486 #else
487 EngineBuilder builder(unwrap(M));
488 #endif
489
490 /**
491 * LLVM 3.1+ haven't more "extern unsigned llvm::StackAlignmentOverride" and
492 * friends for configuring code generation options, like stack alignment.
493 */
494 TargetOptions options;
495 #if defined(PIPE_ARCH_X86)
496 options.StackAlignmentOverride = 4;
497 #if HAVE_LLVM < 0x0304
498 options.RealignStack = true;
499 #endif
500 #endif
501
502 #if defined(DEBUG) && HAVE_LLVM < 0x0307
503 options.JITEmitDebugInfo = true;
504 #endif
505
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;
511 #endif
512 #if HAVE_LLVM < 0x0307
513 options.NoFramePointerElim = true;
514 #endif
515 #endif
516
517 builder.setEngineKind(EngineKind::JIT)
518 .setErrorStr(&Error)
519 .setTargetOptions(options)
520 .setOptLevel((CodeGenOpt::Level)OptLevel);
521
522 if (useMCJIT) {
523 #if HAVE_LLVM < 0x0306
524 builder.setUseMCJIT(true);
525 #endif
526 #ifdef _WIN32
527 /*
528 * MCJIT works on Windows, but currently only through ELF object format.
529 *
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
532 * explicit.
533 */
534 # ifdef _WIN64
535 LLVMSetTarget(M, "x86_64-pc-win32-elf");
536 # else
537 LLVMSetTarget(M, "i686-pc-win32-elf");
538 # endif
539 #endif
540 }
541
542 llvm::SmallVector<std::string, 16> MAttrs;
543
544 #if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
545 /*
546 * We need to unset attributes because sometimes LLVM mistakenly assumes
547 * certain features are present given the processor name.
548 *
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
553 */
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");
560 #else
561 MAttrs.push_back(util_cpu_caps.has_sse4_1 ? "+sse41" : "-sse41" );
562 #endif
563 #if HAVE_LLVM >= 0x0304
564 MAttrs.push_back(util_cpu_caps.has_sse4_2 ? "+sse4.2" : "-sse4.2");
565 #else
566 MAttrs.push_back(util_cpu_caps.has_sse4_2 ? "+sse42" : "-sse42" );
567 #endif
568 /*
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.
573 */
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");
578 } else {
579 /*
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.
582 */
583 MAttrs.push_back("-fma");
584 }
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");
592 #endif
593 #if HAVE_LLVM >= 0x0305
594 MAttrs.push_back("-avx512bw");
595 MAttrs.push_back("-avx512dq");
596 MAttrs.push_back("-avx512vl");
597 #endif
598 #endif
599
600 #if defined(PIPE_ARCH_PPC)
601 MAttrs.push_back(util_cpu_caps.has_altivec ? "+altivec" : "-altivec");
602 #if HAVE_LLVM >= 0x0304
603 /*
604 * Make sure VSX instructions are disabled
605 * See LLVM bug https://llvm.org/bugs/show_bug.cgi?id=25503#c7
606 */
607 if (util_cpu_caps.has_altivec) {
608 MAttrs.push_back("-vsx");
609 }
610 #endif
611 #endif
612
613 builder.setMAttrs(MAttrs);
614
615 #if HAVE_LLVM >= 0x0305
616 StringRef MCPU = llvm::sys::getHostCPUName();
617 /*
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.
628 */
629 builder.setMCPU(MCPU);
630 #endif
631
632 ShaderMemoryManager *MM = NULL;
633 if (useMCJIT) {
634 BaseMemoryManager* JMM = reinterpret_cast<BaseMemoryManager*>(CMM);
635 MM = new ShaderMemoryManager(JMM);
636 *OutCode = MM->getGeneratedCode();
637
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);
643 #else
644 builder.setJITMemoryManager(MM);
645 #endif
646 } else {
647 #if HAVE_LLVM < 0x0306
648 BaseMemoryManager* JMM = reinterpret_cast<BaseMemoryManager*>(CMM);
649 MM = new ShaderMemoryManager(JMM);
650 *OutCode = MM->getGeneratedCode();
651
652 builder.setJITMemoryManager(MM);
653 #else
654 assert(0);
655 #endif
656 }
657
658 ExecutionEngine *JIT;
659
660 JIT = builder.create();
661 #if LLVM_USE_INTEL_JITEVENTS
662 JITEventListener *JEL = JITEventListener::createIntelJITEventListener();
663 JIT->RegisterJITEventListener(JEL);
664 #endif
665 if (JIT) {
666 *OutJIT = wrap(JIT);
667 return 0;
668 }
669 lp_free_generated_code(*OutCode);
670 *OutCode = 0;
671 delete MM;
672 *OutError = strdup(Error.c_str());
673 return 1;
674 }
675
676
677 extern "C"
678 void
679 lp_free_generated_code(struct lp_generated_code *code)
680 {
681 ShaderMemoryManager::freeGeneratedCode(code);
682 }
683
684 extern "C"
685 LLVMMCJITMemoryManagerRef
686 lp_get_default_memory_manager()
687 {
688 BaseMemoryManager *mm;
689 #if HAVE_LLVM < 0x0306
690 mm = llvm::JITMemoryManager::CreateDefaultMemManager();
691 #else
692 mm = new llvm::SectionMemoryManager();
693 #endif
694 return reinterpret_cast<LLVMMCJITMemoryManagerRef>(mm);
695 }
696
697 extern "C"
698 void
699 lp_free_memory_manager(LLVMMCJITMemoryManagerRef memorymgr)
700 {
701 delete reinterpret_cast<BaseMemoryManager*>(memorymgr);
702 }
703
704 extern "C" void
705 lp_add_attr_dereferenceable(LLVMValueRef val, uint64_t bytes)
706 {
707 #if HAVE_LLVM >= 0x0306
708 llvm::Argument *A = llvm::unwrap<llvm::Argument>(val);
709 llvm::AttrBuilder B;
710 B.addDereferenceableAttr(bytes);
711 A->addAttr(llvm::AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
712 #endif
713 }
714
715 extern "C" LLVMValueRef
716 lp_get_called_value(LLVMValueRef call)
717 {
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());
722 #else
723 return NULL; /* radeonsi doesn't support so old LLVM. */
724 #endif
725 }
726
727 extern "C" bool
728 lp_is_function(LLVMValueRef v)
729 {
730 #if HAVE_LLVM >= 0x0309
731 return LLVMGetValueKind(v) == LLVMFunctionValueKind;
732 #else
733 return llvm::isa<llvm::Function>(llvm::unwrap(v));
734 #endif
735 }