gallivm: add wrappers for missing functions in LLVM <= 3.8
[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 #include <llvm/IR/CallSite.h>
81 #include <llvm/IR/IRBuilder.h>
82 #include <llvm/IR/Module.h>
83 #include <llvm/Support/CBindingWrapping.h>
84
85 #include <llvm/Config/llvm-config.h>
86 #if LLVM_USE_INTEL_JITEVENTS
87 #include <llvm/ExecutionEngine/JITEventListener.h>
88 #endif
89
90 // Workaround http://llvm.org/PR23628
91 #if HAVE_LLVM >= 0x0307
92 # pragma pop_macro("DEBUG")
93 #endif
94
95 #include "c11/threads.h"
96 #include "os/os_thread.h"
97 #include "pipe/p_config.h"
98 #include "util/u_debug.h"
99 #include "util/u_cpu_detect.h"
100
101 #include "lp_bld_misc.h"
102
103 namespace {
104
105 class LLVMEnsureMultithreaded {
106 public:
107 LLVMEnsureMultithreaded()
108 {
109 if (!LLVMIsMultithreaded()) {
110 LLVMStartMultithreaded();
111 }
112 }
113 };
114
115 static LLVMEnsureMultithreaded lLVMEnsureMultithreaded;
116
117 }
118
119 static once_flag init_native_targets_once_flag = ONCE_FLAG_INIT;
120
121 static void init_native_targets()
122 {
123 // If we have a native target, initialize it to ensure it is linked in and
124 // usable by the JIT.
125 llvm::InitializeNativeTarget();
126
127 llvm::InitializeNativeTargetAsmPrinter();
128
129 llvm::InitializeNativeTargetDisassembler();
130 }
131
132 /**
133 * The llvm target registry is not thread-safe, so drivers and state-trackers
134 * that want to initialize targets should use the gallivm_init_llvm_targets()
135 * function to safely initialize targets.
136 *
137 * LLVM targets should be initialized before the driver or state-tracker tries
138 * to access the registry.
139 */
140 extern "C" void
141 gallivm_init_llvm_targets(void)
142 {
143 call_once(&init_native_targets_once_flag, init_native_targets);
144 }
145
146 extern "C" void
147 lp_set_target_options(void)
148 {
149 #if HAVE_LLVM < 0x0304
150 /*
151 * By default LLVM adds a signal handler to output a pretty stack trace.
152 * This signal handler is never removed, causing problems when unloading the
153 * shared object where the gallium driver resides.
154 */
155 llvm::DisablePrettyStackTrace = true;
156 #endif
157
158 gallivm_init_llvm_targets();
159 }
160
161 extern "C"
162 LLVMTargetLibraryInfoRef
163 gallivm_create_target_library_info(const char *triple)
164 {
165 return reinterpret_cast<LLVMTargetLibraryInfoRef>(
166 #if HAVE_LLVM < 0x0307
167 new llvm::TargetLibraryInfo(
168 #else
169 new llvm::TargetLibraryInfoImpl(
170 #endif
171 llvm::Triple(triple)));
172 }
173
174 extern "C"
175 void
176 gallivm_dispose_target_library_info(LLVMTargetLibraryInfoRef library_info)
177 {
178 delete reinterpret_cast<
179 #if HAVE_LLVM < 0x0307
180 llvm::TargetLibraryInfo
181 #else
182 llvm::TargetLibraryInfoImpl
183 #endif
184 *>(library_info);
185 }
186
187
188 #if HAVE_LLVM < 0x0304
189
190 extern "C"
191 void
192 LLVMSetAlignmentBackport(LLVMValueRef V,
193 unsigned Bytes)
194 {
195 switch (LLVMGetInstructionOpcode(V)) {
196 case LLVMLoad:
197 llvm::unwrap<llvm::LoadInst>(V)->setAlignment(Bytes);
198 break;
199 case LLVMStore:
200 llvm::unwrap<llvm::StoreInst>(V)->setAlignment(Bytes);
201 break;
202 default:
203 assert(0);
204 break;
205 }
206 }
207
208 #endif
209
210
211 #if HAVE_LLVM < 0x0306
212 typedef llvm::JITMemoryManager BaseMemoryManager;
213 #else
214 typedef llvm::RTDyldMemoryManager BaseMemoryManager;
215 #endif
216
217
218 /*
219 * Delegating is tedious but the default manager class is hidden in an
220 * anonymous namespace in LLVM, so we cannot just derive from it to change
221 * its behavior.
222 */
223 class DelegatingJITMemoryManager : public BaseMemoryManager {
224
225 protected:
226 virtual BaseMemoryManager *mgr() const = 0;
227
228 public:
229 #if HAVE_LLVM < 0x0306
230 /*
231 * From JITMemoryManager
232 */
233 virtual void setMemoryWritable() {
234 mgr()->setMemoryWritable();
235 }
236 virtual void setMemoryExecutable() {
237 mgr()->setMemoryExecutable();
238 }
239 virtual void setPoisonMemory(bool poison) {
240 mgr()->setPoisonMemory(poison);
241 }
242 virtual void AllocateGOT() {
243 mgr()->AllocateGOT();
244 /*
245 * isManagingGOT() is not virtual in base class so we can't delegate.
246 * Instead we mirror the value of HasGOT in our instance.
247 */
248 HasGOT = mgr()->isManagingGOT();
249 }
250 virtual uint8_t *getGOTBase() const {
251 return mgr()->getGOTBase();
252 }
253 virtual uint8_t *startFunctionBody(const llvm::Function *F,
254 uintptr_t &ActualSize) {
255 return mgr()->startFunctionBody(F, ActualSize);
256 }
257 virtual uint8_t *allocateStub(const llvm::GlobalValue *F,
258 unsigned StubSize,
259 unsigned Alignment) {
260 return mgr()->allocateStub(F, StubSize, Alignment);
261 }
262 virtual void endFunctionBody(const llvm::Function *F,
263 uint8_t *FunctionStart,
264 uint8_t *FunctionEnd) {
265 mgr()->endFunctionBody(F, FunctionStart, FunctionEnd);
266 }
267 virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) {
268 return mgr()->allocateSpace(Size, Alignment);
269 }
270 virtual uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) {
271 return mgr()->allocateGlobal(Size, Alignment);
272 }
273 virtual void deallocateFunctionBody(void *Body) {
274 mgr()->deallocateFunctionBody(Body);
275 }
276 #if HAVE_LLVM < 0x0304
277 virtual uint8_t *startExceptionTable(const llvm::Function *F,
278 uintptr_t &ActualSize) {
279 return mgr()->startExceptionTable(F, ActualSize);
280 }
281 virtual void endExceptionTable(const llvm::Function *F,
282 uint8_t *TableStart,
283 uint8_t *TableEnd,
284 uint8_t *FrameRegister) {
285 mgr()->endExceptionTable(F, TableStart, TableEnd,
286 FrameRegister);
287 }
288 virtual void deallocateExceptionTable(void *ET) {
289 mgr()->deallocateExceptionTable(ET);
290 }
291 #endif
292 virtual bool CheckInvariants(std::string &s) {
293 return mgr()->CheckInvariants(s);
294 }
295 virtual size_t GetDefaultCodeSlabSize() {
296 return mgr()->GetDefaultCodeSlabSize();
297 }
298 virtual size_t GetDefaultDataSlabSize() {
299 return mgr()->GetDefaultDataSlabSize();
300 }
301 virtual size_t GetDefaultStubSlabSize() {
302 return mgr()->GetDefaultStubSlabSize();
303 }
304 virtual unsigned GetNumCodeSlabs() {
305 return mgr()->GetNumCodeSlabs();
306 }
307 virtual unsigned GetNumDataSlabs() {
308 return mgr()->GetNumDataSlabs();
309 }
310 virtual unsigned GetNumStubSlabs() {
311 return mgr()->GetNumStubSlabs();
312 }
313 #endif
314
315 /*
316 * From RTDyldMemoryManager
317 */
318 #if HAVE_LLVM >= 0x0304
319 virtual uint8_t *allocateCodeSection(uintptr_t Size,
320 unsigned Alignment,
321 unsigned SectionID,
322 llvm::StringRef SectionName) {
323 return mgr()->allocateCodeSection(Size, Alignment, SectionID,
324 SectionName);
325 }
326 #else
327 virtual uint8_t *allocateCodeSection(uintptr_t Size,
328 unsigned Alignment,
329 unsigned SectionID) {
330 return mgr()->allocateCodeSection(Size, Alignment, SectionID);
331 }
332 #endif
333 virtual uint8_t *allocateDataSection(uintptr_t Size,
334 unsigned Alignment,
335 unsigned SectionID,
336 #if HAVE_LLVM >= 0x0304
337 llvm::StringRef SectionName,
338 #endif
339 bool IsReadOnly) {
340 return mgr()->allocateDataSection(Size, Alignment, SectionID,
341 #if HAVE_LLVM >= 0x0304
342 SectionName,
343 #endif
344 IsReadOnly);
345 }
346 #if HAVE_LLVM >= 0x0304
347 virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) {
348 mgr()->registerEHFrames(Addr, LoadAddr, Size);
349 }
350 virtual void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) {
351 mgr()->deregisterEHFrames(Addr, LoadAddr, Size);
352 }
353 #else
354 virtual void registerEHFrames(llvm::StringRef SectionData) {
355 mgr()->registerEHFrames(SectionData);
356 }
357 #endif
358 virtual void *getPointerToNamedFunction(const std::string &Name,
359 bool AbortOnFailure=true) {
360 return mgr()->getPointerToNamedFunction(Name, AbortOnFailure);
361 }
362 #if HAVE_LLVM <= 0x0303
363 virtual bool applyPermissions(std::string *ErrMsg = 0) {
364 return mgr()->applyPermissions(ErrMsg);
365 }
366 #else
367 virtual bool finalizeMemory(std::string *ErrMsg = 0) {
368 return mgr()->finalizeMemory(ErrMsg);
369 }
370 #endif
371 };
372
373
374 /*
375 * Delegate memory management to one shared manager for more efficient use
376 * of memory than creating a separate pool for each LLVM engine.
377 * Keep generated code until freeGeneratedCode() is called, instead of when
378 * memory manager is destroyed, which happens during engine destruction.
379 * This allows additional memory savings as we don't have to keep the engine
380 * around in order to use the code.
381 * All methods are delegated to the shared manager except destruction and
382 * deallocating code. For the latter we just remember what needs to be
383 * deallocated later. The shared manager is deleted once it is empty.
384 */
385 class ShaderMemoryManager : public DelegatingJITMemoryManager {
386
387 BaseMemoryManager *TheMM;
388
389 struct GeneratedCode {
390 typedef std::vector<void *> Vec;
391 Vec FunctionBody, ExceptionTable;
392 BaseMemoryManager *TheMM;
393
394 GeneratedCode(BaseMemoryManager *MM) {
395 TheMM = MM;
396 }
397
398 ~GeneratedCode() {
399 /*
400 * Deallocate things as previously requested and
401 * free shared manager when no longer used.
402 */
403 #if HAVE_LLVM < 0x0306
404 Vec::iterator i;
405
406 assert(TheMM);
407 for ( i = FunctionBody.begin(); i != FunctionBody.end(); ++i )
408 TheMM->deallocateFunctionBody(*i);
409 #if HAVE_LLVM < 0x0304
410 for ( i = ExceptionTable.begin(); i != ExceptionTable.end(); ++i )
411 TheMM->deallocateExceptionTable(*i);
412 #endif /* HAVE_LLVM < 0x0304 */
413 #endif /* HAVE_LLVM < 0x0306 */
414 }
415 };
416
417 GeneratedCode *code;
418
419 BaseMemoryManager *mgr() const {
420 return TheMM;
421 }
422
423 public:
424
425 ShaderMemoryManager(BaseMemoryManager* MM) {
426 TheMM = MM;
427 code = new GeneratedCode(MM);
428 }
429
430 virtual ~ShaderMemoryManager() {
431 /*
432 * 'code' is purposely not deleted. It is the user's responsibility
433 * to call getGeneratedCode() and freeGeneratedCode().
434 */
435 }
436
437 struct lp_generated_code *getGeneratedCode() {
438 return (struct lp_generated_code *) code;
439 }
440
441 static void freeGeneratedCode(struct lp_generated_code *code) {
442 delete (GeneratedCode *) code;
443 }
444
445 #if HAVE_LLVM < 0x0304
446 virtual void deallocateExceptionTable(void *ET) {
447 // remember for later deallocation
448 code->ExceptionTable.push_back(ET);
449 }
450 #endif
451
452 virtual void deallocateFunctionBody(void *Body) {
453 // remember for later deallocation
454 code->FunctionBody.push_back(Body);
455 }
456 };
457
458
459 /**
460 * Same as LLVMCreateJITCompilerForModule, but:
461 * - allows using MCJIT and enabling AVX feature where available.
462 * - set target options
463 *
464 * See also:
465 * - llvm/lib/ExecutionEngine/ExecutionEngineBindings.cpp
466 * - llvm/tools/lli/lli.cpp
467 * - http://markmail.org/message/ttkuhvgj4cxxy2on#query:+page:1+mid:aju2dggerju3ivd3+state:results
468 */
469 extern "C"
470 LLVMBool
471 lp_build_create_jit_compiler_for_module(LLVMExecutionEngineRef *OutJIT,
472 lp_generated_code **OutCode,
473 LLVMModuleRef M,
474 LLVMMCJITMemoryManagerRef CMM,
475 unsigned OptLevel,
476 int useMCJIT,
477 char **OutError)
478 {
479 using namespace llvm;
480
481 std::string Error;
482 #if HAVE_LLVM >= 0x0306
483 EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
484 #else
485 EngineBuilder builder(unwrap(M));
486 #endif
487
488 /**
489 * LLVM 3.1+ haven't more "extern unsigned llvm::StackAlignmentOverride" and
490 * friends for configuring code generation options, like stack alignment.
491 */
492 TargetOptions options;
493 #if defined(PIPE_ARCH_X86)
494 options.StackAlignmentOverride = 4;
495 #if HAVE_LLVM < 0x0304
496 options.RealignStack = true;
497 #endif
498 #endif
499
500 #if defined(DEBUG) && HAVE_LLVM < 0x0307
501 options.JITEmitDebugInfo = true;
502 #endif
503
504 /* XXX: Workaround http://llvm.org/PR21435 */
505 #if defined(DEBUG) || defined(PROFILE) || \
506 (HAVE_LLVM >= 0x0303 && (defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)))
507 #if HAVE_LLVM < 0x0304
508 options.NoFramePointerElimNonLeaf = true;
509 #endif
510 #if HAVE_LLVM < 0x0307
511 options.NoFramePointerElim = true;
512 #endif
513 #endif
514
515 builder.setEngineKind(EngineKind::JIT)
516 .setErrorStr(&Error)
517 .setTargetOptions(options)
518 .setOptLevel((CodeGenOpt::Level)OptLevel);
519
520 if (useMCJIT) {
521 #if HAVE_LLVM < 0x0306
522 builder.setUseMCJIT(true);
523 #endif
524 #ifdef _WIN32
525 /*
526 * MCJIT works on Windows, but currently only through ELF object format.
527 *
528 * XXX: We could use `LLVM_HOST_TRIPLE "-elf"` but LLVM_HOST_TRIPLE has
529 * different strings for MinGW/MSVC, so better play it safe and be
530 * explicit.
531 */
532 # ifdef _WIN64
533 LLVMSetTarget(M, "x86_64-pc-win32-elf");
534 # else
535 LLVMSetTarget(M, "i686-pc-win32-elf");
536 # endif
537 #endif
538 }
539
540 llvm::SmallVector<std::string, 16> MAttrs;
541
542 #if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
543 /*
544 * We need to unset attributes because sometimes LLVM mistakenly assumes
545 * certain features are present given the processor name.
546 *
547 * https://bugs.freedesktop.org/show_bug.cgi?id=92214
548 * http://llvm.org/PR25021
549 * http://llvm.org/PR19429
550 * http://llvm.org/PR16721
551 */
552 MAttrs.push_back(util_cpu_caps.has_sse ? "+sse" : "-sse" );
553 MAttrs.push_back(util_cpu_caps.has_sse2 ? "+sse2" : "-sse2" );
554 MAttrs.push_back(util_cpu_caps.has_sse3 ? "+sse3" : "-sse3" );
555 MAttrs.push_back(util_cpu_caps.has_ssse3 ? "+ssse3" : "-ssse3" );
556 #if HAVE_LLVM >= 0x0304
557 MAttrs.push_back(util_cpu_caps.has_sse4_1 ? "+sse4.1" : "-sse4.1");
558 #else
559 MAttrs.push_back(util_cpu_caps.has_sse4_1 ? "+sse41" : "-sse41" );
560 #endif
561 #if HAVE_LLVM >= 0x0304
562 MAttrs.push_back(util_cpu_caps.has_sse4_2 ? "+sse4.2" : "-sse4.2");
563 #else
564 MAttrs.push_back(util_cpu_caps.has_sse4_2 ? "+sse42" : "-sse42" );
565 #endif
566 /*
567 * AVX feature is not automatically detected from CPUID by the X86 target
568 * yet, because the old (yet default) JIT engine is not capable of
569 * emitting the opcodes. On newer llvm versions it is and at least some
570 * versions (tested with 3.3) will emit avx opcodes without this anyway.
571 */
572 MAttrs.push_back(util_cpu_caps.has_avx ? "+avx" : "-avx");
573 MAttrs.push_back(util_cpu_caps.has_f16c ? "+f16c" : "-f16c");
574 if (HAVE_LLVM >= 0x0304) {
575 MAttrs.push_back(util_cpu_caps.has_fma ? "+fma" : "-fma");
576 } else {
577 /*
578 * The old JIT in LLVM 3.3 has a bug encoding llvm.fmuladd.f32 and
579 * llvm.fmuladd.v2f32 intrinsics when FMA is available.
580 */
581 MAttrs.push_back("-fma");
582 }
583 MAttrs.push_back(util_cpu_caps.has_avx2 ? "+avx2" : "-avx2");
584 /* disable avx512 and all subvariants */
585 #if HAVE_LLVM >= 0x0304
586 MAttrs.push_back("-avx512cd");
587 MAttrs.push_back("-avx512er");
588 MAttrs.push_back("-avx512f");
589 MAttrs.push_back("-avx512pf");
590 #endif
591 #if HAVE_LLVM >= 0x0305
592 MAttrs.push_back("-avx512bw");
593 MAttrs.push_back("-avx512dq");
594 MAttrs.push_back("-avx512vl");
595 #endif
596 #endif
597
598 #if defined(PIPE_ARCH_PPC)
599 MAttrs.push_back(util_cpu_caps.has_altivec ? "+altivec" : "-altivec");
600 #if HAVE_LLVM >= 0x0304
601 /*
602 * Make sure VSX instructions are disabled
603 * See LLVM bug https://llvm.org/bugs/show_bug.cgi?id=25503#c7
604 */
605 if (util_cpu_caps.has_altivec) {
606 MAttrs.push_back("-vsx");
607 }
608 #endif
609 #endif
610
611 builder.setMAttrs(MAttrs);
612
613 #if HAVE_LLVM >= 0x0305
614 StringRef MCPU = llvm::sys::getHostCPUName();
615 /*
616 * The cpu bits are no longer set automatically, so need to set mcpu manually.
617 * Note that the MAttrs set above will be sort of ignored (since we should
618 * not set any which would not be set by specifying the cpu anyway).
619 * It ought to be safe though since getHostCPUName() should include bits
620 * not only from the cpu but environment as well (for instance if it's safe
621 * to use avx instructions which need OS support). According to
622 * http://llvm.org/bugs/show_bug.cgi?id=19429 however if I understand this
623 * right it may be necessary to specify older cpu (or disable mattrs) though
624 * when not using MCJIT so no instructions are generated which the old JIT
625 * can't handle. Not entirely sure if we really need to do anything yet.
626 */
627 builder.setMCPU(MCPU);
628 #endif
629
630 ShaderMemoryManager *MM = NULL;
631 if (useMCJIT) {
632 BaseMemoryManager* JMM = reinterpret_cast<BaseMemoryManager*>(CMM);
633 MM = new ShaderMemoryManager(JMM);
634 *OutCode = MM->getGeneratedCode();
635
636 #if HAVE_LLVM >= 0x0306
637 builder.setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager>(MM));
638 MM = NULL; // ownership taken by std::unique_ptr
639 #elif HAVE_LLVM > 0x0303
640 builder.setMCJITMemoryManager(MM);
641 #else
642 builder.setJITMemoryManager(MM);
643 #endif
644 } else {
645 #if HAVE_LLVM < 0x0306
646 BaseMemoryManager* JMM = reinterpret_cast<BaseMemoryManager*>(CMM);
647 MM = new ShaderMemoryManager(JMM);
648 *OutCode = MM->getGeneratedCode();
649
650 builder.setJITMemoryManager(MM);
651 #else
652 assert(0);
653 #endif
654 }
655
656 ExecutionEngine *JIT;
657
658 JIT = builder.create();
659 #if LLVM_USE_INTEL_JITEVENTS
660 JITEventListener *JEL = JITEventListener::createIntelJITEventListener();
661 JIT->RegisterJITEventListener(JEL);
662 #endif
663 if (JIT) {
664 *OutJIT = wrap(JIT);
665 return 0;
666 }
667 lp_free_generated_code(*OutCode);
668 *OutCode = 0;
669 delete MM;
670 *OutError = strdup(Error.c_str());
671 return 1;
672 }
673
674
675 extern "C"
676 void
677 lp_free_generated_code(struct lp_generated_code *code)
678 {
679 ShaderMemoryManager::freeGeneratedCode(code);
680 }
681
682 extern "C"
683 LLVMMCJITMemoryManagerRef
684 lp_get_default_memory_manager()
685 {
686 BaseMemoryManager *mm;
687 #if HAVE_LLVM < 0x0306
688 mm = llvm::JITMemoryManager::CreateDefaultMemManager();
689 #else
690 mm = new llvm::SectionMemoryManager();
691 #endif
692 return reinterpret_cast<LLVMMCJITMemoryManagerRef>(mm);
693 }
694
695 extern "C"
696 void
697 lp_free_memory_manager(LLVMMCJITMemoryManagerRef memorymgr)
698 {
699 delete reinterpret_cast<BaseMemoryManager*>(memorymgr);
700 }
701
702 extern "C" void
703 lp_add_attr_dereferenceable(LLVMValueRef val, uint64_t bytes)
704 {
705 #if HAVE_LLVM >= 0x0306
706 llvm::Argument *A = llvm::unwrap<llvm::Argument>(val);
707 llvm::AttrBuilder B;
708 B.addDereferenceableAttr(bytes);
709 A->addAttr(llvm::AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
710 #endif
711 }
712
713 extern "C" LLVMValueRef
714 lp_get_called_value(LLVMValueRef call)
715 {
716 #if HAVE_LLVM >= 0x0309
717 return LLVMGetCalledValue(call);
718 #else
719 return llvm::wrap(llvm::CallSite(llvm::unwrap<llvm::Instruction>(call)).getCalledValue());
720 #endif
721 }
722
723 extern "C" bool
724 lp_is_function(LLVMValueRef v)
725 {
726 #if HAVE_LLVM >= 0x0309
727 return LLVMGetValueKind(v) == LLVMFunctionValueKind;
728 #else
729 return llvm::isa<llvm::Function>(llvm::unwrap(v));
730 #endif
731 }