Added few more stubs so that control reaches to DestroyDevice().
[mesa.git] / 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 // Undef these vars just to silence warnings
36 #undef PACKAGE_BUGREPORT
37 #undef PACKAGE_NAME
38 #undef PACKAGE_STRING
39 #undef PACKAGE_TARNAME
40 #undef PACKAGE_VERSION
41
42
43 #include <stddef.h>
44
45 // Workaround http://llvm.org/PR23628
46 #if HAVE_LLVM >= 0x0307
47 # pragma push_macro("DEBUG")
48 # undef DEBUG
49 #endif
50
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>
58 #else
59 #include <llvm/Target/TargetLibraryInfo.h>
60 #endif
61 #if HAVE_LLVM < 0x0306
62 #include <llvm/ExecutionEngine/JITMemoryManager.h>
63 #else
64 #include <llvm/ExecutionEngine/SectionMemoryManager.h>
65 #endif
66 #include <llvm/Support/CommandLine.h>
67 #include <llvm/Support/Host.h>
68 #include <llvm/Support/PrettyStackTrace.h>
69
70 #include <llvm/Support/TargetSelect.h>
71
72 #if HAVE_LLVM >= 0x0305
73 #include <llvm/IR/CallSite.h>
74 #endif
75 #include <llvm/IR/IRBuilder.h>
76 #include <llvm/IR/Module.h>
77 #include <llvm/Support/CBindingWrapping.h>
78
79 #include <llvm/Config/llvm-config.h>
80 #if LLVM_USE_INTEL_JITEVENTS
81 #include <llvm/ExecutionEngine/JITEventListener.h>
82 #endif
83
84 // Workaround http://llvm.org/PR23628
85 #if HAVE_LLVM >= 0x0307
86 # pragma pop_macro("DEBUG")
87 #endif
88
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"
94
95 #include "lp_bld_misc.h"
96 #include "lp_bld_debug.h"
97
98 namespace {
99
100 class LLVMEnsureMultithreaded {
101 public:
102 LLVMEnsureMultithreaded()
103 {
104 if (!LLVMIsMultithreaded()) {
105 LLVMStartMultithreaded();
106 }
107 }
108 };
109
110 static LLVMEnsureMultithreaded lLVMEnsureMultithreaded;
111
112 }
113
114 static once_flag init_native_targets_once_flag = ONCE_FLAG_INIT;
115
116 static void init_native_targets()
117 {
118 // If we have a native target, initialize it to ensure it is linked in and
119 // usable by the JIT.
120 llvm::InitializeNativeTarget();
121
122 llvm::InitializeNativeTargetAsmPrinter();
123
124 llvm::InitializeNativeTargetDisassembler();
125 }
126
127 /**
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.
131 *
132 * LLVM targets should be initialized before the driver or state-tracker tries
133 * to access the registry.
134 */
135 extern "C" void
136 gallivm_init_llvm_targets(void)
137 {
138 call_once(&init_native_targets_once_flag, init_native_targets);
139 }
140
141 extern "C" void
142 lp_set_target_options(void)
143 {
144 #if HAVE_LLVM < 0x0304
145 /*
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.
149 */
150 llvm::DisablePrettyStackTrace = true;
151 #endif
152
153 gallivm_init_llvm_targets();
154 }
155
156 extern "C"
157 LLVMTargetLibraryInfoRef
158 gallivm_create_target_library_info(const char *triple)
159 {
160 return reinterpret_cast<LLVMTargetLibraryInfoRef>(
161 #if HAVE_LLVM < 0x0307
162 new llvm::TargetLibraryInfo(
163 #else
164 new llvm::TargetLibraryInfoImpl(
165 #endif
166 llvm::Triple(triple)));
167 }
168
169 extern "C"
170 void
171 gallivm_dispose_target_library_info(LLVMTargetLibraryInfoRef library_info)
172 {
173 delete reinterpret_cast<
174 #if HAVE_LLVM < 0x0307
175 llvm::TargetLibraryInfo
176 #else
177 llvm::TargetLibraryInfoImpl
178 #endif
179 *>(library_info);
180 }
181
182
183 #if HAVE_LLVM < 0x0304
184
185 extern "C"
186 void
187 LLVMSetAlignmentBackport(LLVMValueRef V,
188 unsigned Bytes)
189 {
190 switch (LLVMGetInstructionOpcode(V)) {
191 case LLVMLoad:
192 llvm::unwrap<llvm::LoadInst>(V)->setAlignment(Bytes);
193 break;
194 case LLVMStore:
195 llvm::unwrap<llvm::StoreInst>(V)->setAlignment(Bytes);
196 break;
197 default:
198 assert(0);
199 break;
200 }
201 }
202
203 #endif
204
205
206 #if HAVE_LLVM < 0x0306
207 typedef llvm::JITMemoryManager BaseMemoryManager;
208 #else
209 typedef llvm::RTDyldMemoryManager BaseMemoryManager;
210 #endif
211
212
213 /*
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
216 * its behavior.
217 */
218 class DelegatingJITMemoryManager : public BaseMemoryManager {
219
220 protected:
221 virtual BaseMemoryManager *mgr() const = 0;
222
223 public:
224 #if HAVE_LLVM < 0x0306
225 /*
226 * From JITMemoryManager
227 */
228 virtual void setMemoryWritable() {
229 mgr()->setMemoryWritable();
230 }
231 virtual void setMemoryExecutable() {
232 mgr()->setMemoryExecutable();
233 }
234 virtual void setPoisonMemory(bool poison) {
235 mgr()->setPoisonMemory(poison);
236 }
237 virtual void AllocateGOT() {
238 mgr()->AllocateGOT();
239 /*
240 * isManagingGOT() is not virtual in base class so we can't delegate.
241 * Instead we mirror the value of HasGOT in our instance.
242 */
243 HasGOT = mgr()->isManagingGOT();
244 }
245 virtual uint8_t *getGOTBase() const {
246 return mgr()->getGOTBase();
247 }
248 virtual uint8_t *startFunctionBody(const llvm::Function *F,
249 uintptr_t &ActualSize) {
250 return mgr()->startFunctionBody(F, ActualSize);
251 }
252 virtual uint8_t *allocateStub(const llvm::GlobalValue *F,
253 unsigned StubSize,
254 unsigned Alignment) {
255 return mgr()->allocateStub(F, StubSize, Alignment);
256 }
257 virtual void endFunctionBody(const llvm::Function *F,
258 uint8_t *FunctionStart,
259 uint8_t *FunctionEnd) {
260 mgr()->endFunctionBody(F, FunctionStart, FunctionEnd);
261 }
262 virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) {
263 return mgr()->allocateSpace(Size, Alignment);
264 }
265 virtual uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) {
266 return mgr()->allocateGlobal(Size, Alignment);
267 }
268 virtual void deallocateFunctionBody(void *Body) {
269 mgr()->deallocateFunctionBody(Body);
270 }
271 #if HAVE_LLVM < 0x0304
272 virtual uint8_t *startExceptionTable(const llvm::Function *F,
273 uintptr_t &ActualSize) {
274 return mgr()->startExceptionTable(F, ActualSize);
275 }
276 virtual void endExceptionTable(const llvm::Function *F,
277 uint8_t *TableStart,
278 uint8_t *TableEnd,
279 uint8_t *FrameRegister) {
280 mgr()->endExceptionTable(F, TableStart, TableEnd,
281 FrameRegister);
282 }
283 virtual void deallocateExceptionTable(void *ET) {
284 mgr()->deallocateExceptionTable(ET);
285 }
286 #endif
287 virtual bool CheckInvariants(std::string &s) {
288 return mgr()->CheckInvariants(s);
289 }
290 virtual size_t GetDefaultCodeSlabSize() {
291 return mgr()->GetDefaultCodeSlabSize();
292 }
293 virtual size_t GetDefaultDataSlabSize() {
294 return mgr()->GetDefaultDataSlabSize();
295 }
296 virtual size_t GetDefaultStubSlabSize() {
297 return mgr()->GetDefaultStubSlabSize();
298 }
299 virtual unsigned GetNumCodeSlabs() {
300 return mgr()->GetNumCodeSlabs();
301 }
302 virtual unsigned GetNumDataSlabs() {
303 return mgr()->GetNumDataSlabs();
304 }
305 virtual unsigned GetNumStubSlabs() {
306 return mgr()->GetNumStubSlabs();
307 }
308 #endif
309
310 /*
311 * From RTDyldMemoryManager
312 */
313 #if HAVE_LLVM >= 0x0304
314 virtual uint8_t *allocateCodeSection(uintptr_t Size,
315 unsigned Alignment,
316 unsigned SectionID,
317 llvm::StringRef SectionName) {
318 return mgr()->allocateCodeSection(Size, Alignment, SectionID,
319 SectionName);
320 }
321 #else
322 virtual uint8_t *allocateCodeSection(uintptr_t Size,
323 unsigned Alignment,
324 unsigned SectionID) {
325 return mgr()->allocateCodeSection(Size, Alignment, SectionID);
326 }
327 #endif
328 virtual uint8_t *allocateDataSection(uintptr_t Size,
329 unsigned Alignment,
330 unsigned SectionID,
331 #if HAVE_LLVM >= 0x0304
332 llvm::StringRef SectionName,
333 #endif
334 bool IsReadOnly) {
335 return mgr()->allocateDataSection(Size, Alignment, SectionID,
336 #if HAVE_LLVM >= 0x0304
337 SectionName,
338 #endif
339 IsReadOnly);
340 }
341 #if HAVE_LLVM >= 0x0304
342 virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) {
343 mgr()->registerEHFrames(Addr, LoadAddr, Size);
344 }
345 virtual void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) {
346 mgr()->deregisterEHFrames(Addr, LoadAddr, Size);
347 }
348 #else
349 virtual void registerEHFrames(llvm::StringRef SectionData) {
350 mgr()->registerEHFrames(SectionData);
351 }
352 #endif
353 virtual void *getPointerToNamedFunction(const std::string &Name,
354 bool AbortOnFailure=true) {
355 return mgr()->getPointerToNamedFunction(Name, AbortOnFailure);
356 }
357 #if HAVE_LLVM <= 0x0303
358 virtual bool applyPermissions(std::string *ErrMsg = 0) {
359 return mgr()->applyPermissions(ErrMsg);
360 }
361 #else
362 virtual bool finalizeMemory(std::string *ErrMsg = 0) {
363 return mgr()->finalizeMemory(ErrMsg);
364 }
365 #endif
366 };
367
368
369 /*
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.
379 */
380 class ShaderMemoryManager : public DelegatingJITMemoryManager {
381
382 BaseMemoryManager *TheMM;
383
384 struct GeneratedCode {
385 typedef std::vector<void *> Vec;
386 Vec FunctionBody, ExceptionTable;
387 BaseMemoryManager *TheMM;
388
389 GeneratedCode(BaseMemoryManager *MM) {
390 TheMM = MM;
391 }
392
393 ~GeneratedCode() {
394 /*
395 * Deallocate things as previously requested and
396 * free shared manager when no longer used.
397 */
398 #if HAVE_LLVM < 0x0306
399 Vec::iterator i;
400
401 assert(TheMM);
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 */
409 }
410 };
411
412 GeneratedCode *code;
413
414 BaseMemoryManager *mgr() const {
415 return TheMM;
416 }
417
418 public:
419
420 ShaderMemoryManager(BaseMemoryManager* MM) {
421 TheMM = MM;
422 code = new GeneratedCode(MM);
423 }
424
425 virtual ~ShaderMemoryManager() {
426 /*
427 * 'code' is purposely not deleted. It is the user's responsibility
428 * to call getGeneratedCode() and freeGeneratedCode().
429 */
430 }
431
432 struct lp_generated_code *getGeneratedCode() {
433 return (struct lp_generated_code *) code;
434 }
435
436 static void freeGeneratedCode(struct lp_generated_code *code) {
437 delete (GeneratedCode *) code;
438 }
439
440 #if HAVE_LLVM < 0x0304
441 virtual void deallocateExceptionTable(void *ET) {
442 // remember for later deallocation
443 code->ExceptionTable.push_back(ET);
444 }
445 #endif
446
447 virtual void deallocateFunctionBody(void *Body) {
448 // remember for later deallocation
449 code->FunctionBody.push_back(Body);
450 }
451 };
452
453
454 /**
455 * Same as LLVMCreateJITCompilerForModule, but:
456 * - allows using MCJIT and enabling AVX feature where available.
457 * - set target options
458 *
459 * See also:
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
463 */
464 extern "C"
465 LLVMBool
466 lp_build_create_jit_compiler_for_module(LLVMExecutionEngineRef *OutJIT,
467 lp_generated_code **OutCode,
468 LLVMModuleRef M,
469 LLVMMCJITMemoryManagerRef CMM,
470 unsigned OptLevel,
471 int useMCJIT,
472 char **OutError)
473 {
474 using namespace llvm;
475
476 std::string Error;
477 #if HAVE_LLVM >= 0x0306
478 EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
479 #else
480 EngineBuilder builder(unwrap(M));
481 #endif
482
483 /**
484 * LLVM 3.1+ haven't more "extern unsigned llvm::StackAlignmentOverride" and
485 * friends for configuring code generation options, like stack alignment.
486 */
487 TargetOptions options;
488 #if defined(PIPE_ARCH_X86)
489 options.StackAlignmentOverride = 4;
490 #if HAVE_LLVM < 0x0304
491 options.RealignStack = true;
492 #endif
493 #endif
494
495 #if defined(DEBUG) && HAVE_LLVM < 0x0307
496 options.JITEmitDebugInfo = true;
497 #endif
498
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;
504 #endif
505 #if HAVE_LLVM < 0x0307
506 options.NoFramePointerElim = true;
507 #endif
508 #endif
509
510 builder.setEngineKind(EngineKind::JIT)
511 .setErrorStr(&Error)
512 .setTargetOptions(options)
513 .setOptLevel((CodeGenOpt::Level)OptLevel);
514
515 if (useMCJIT) {
516 #if HAVE_LLVM < 0x0306
517 builder.setUseMCJIT(true);
518 #endif
519 #ifdef _WIN32
520 /*
521 * MCJIT works on Windows, but currently only through ELF object format.
522 *
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
525 * explicit.
526 */
527 # ifdef _WIN64
528 LLVMSetTarget(M, "x86_64-pc-win32-elf");
529 # else
530 LLVMSetTarget(M, "i686-pc-win32-elf");
531 # endif
532 #endif
533 }
534
535 llvm::SmallVector<std::string, 16> MAttrs;
536
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.
542 */
543 llvm::StringMap<bool> features;
544 llvm::sys::getHostCPUFeatures(features);
545
546 for (StringMapIterator<bool> f = features.begin();
547 f != features.end();
548 ++f) {
549 MAttrs.push_back(((*f).second ? "+" : "-") + (*f).first().str());
550 }
551 #else
552 /*
553 * We need to unset attributes because sometimes LLVM mistakenly assumes
554 * certain features are present given the processor name.
555 *
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
560 */
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");
567 #else
568 MAttrs.push_back(util_cpu_caps.has_sse4_1 ? "+sse41" : "-sse41" );
569 #endif
570 #if HAVE_LLVM >= 0x0304
571 MAttrs.push_back(util_cpu_caps.has_sse4_2 ? "+sse4.2" : "-sse4.2");
572 #else
573 MAttrs.push_back(util_cpu_caps.has_sse4_2 ? "+sse42" : "-sse42" );
574 #endif
575 /*
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.
580 */
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");
585 } else {
586 /*
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.
589 */
590 MAttrs.push_back("-fma");
591 }
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");
599 #endif
600 #if HAVE_LLVM >= 0x0305
601 MAttrs.push_back("-avx512bw");
602 MAttrs.push_back("-avx512dq");
603 MAttrs.push_back("-avx512vl");
604 #endif
605 #endif
606 #endif
607
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)
612 /*
613 * Make sure VSX instructions are disabled
614 * See LLVM bug https://llvm.org/bugs/show_bug.cgi?id=25503#c7
615 */
616 if (util_cpu_caps.has_altivec) {
617 MAttrs.push_back("-vsx");
618 }
619 #else
620 /*
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
625 */
626 if (util_cpu_caps.has_altivec) {
627 MAttrs.push_back("+vsx");
628 }
629 #endif
630 #endif
631 #endif
632
633 builder.setMAttrs(MAttrs);
634
635 if (gallivm_debug & (GALLIVM_DEBUG_IR | GALLIVM_DEBUG_ASM | GALLIVM_DEBUG_DUMP_BC)) {
636 int n = MAttrs.size();
637 if (n > 0) {
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) ? "," : "");
641 debug_printf("\n");
642 }
643 }
644
645 #if HAVE_LLVM >= 0x0305
646 StringRef MCPU = llvm::sys::getHostCPUName();
647 /*
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.
658 */
659 #if defined(PIPE_ARCH_LITTLE_ENDIAN) && defined(PIPE_ARCH_PPC_64)
660 /*
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
666 * Piglit tests, e.g.
667 * .../arb_gpu_shader_fp64/execution/conversion/frag-conversion-explicit-double-uint
668 */
669 if (MCPU == "generic")
670 MCPU = "pwr8";
671 #endif
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());
675 }
676 #endif
677
678 ShaderMemoryManager *MM = NULL;
679 if (useMCJIT) {
680 BaseMemoryManager* JMM = reinterpret_cast<BaseMemoryManager*>(CMM);
681 MM = new ShaderMemoryManager(JMM);
682 *OutCode = MM->getGeneratedCode();
683
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);
689 #else
690 builder.setJITMemoryManager(MM);
691 #endif
692 } else {
693 #if HAVE_LLVM < 0x0306
694 BaseMemoryManager* JMM = reinterpret_cast<BaseMemoryManager*>(CMM);
695 MM = new ShaderMemoryManager(JMM);
696 *OutCode = MM->getGeneratedCode();
697
698 builder.setJITMemoryManager(MM);
699 #else
700 assert(0);
701 #endif
702 }
703
704 ExecutionEngine *JIT;
705
706 JIT = builder.create();
707 #if LLVM_USE_INTEL_JITEVENTS
708 JITEventListener *JEL = JITEventListener::createIntelJITEventListener();
709 JIT->RegisterJITEventListener(JEL);
710 #endif
711 if (JIT) {
712 *OutJIT = wrap(JIT);
713 return 0;
714 }
715 lp_free_generated_code(*OutCode);
716 *OutCode = 0;
717 delete MM;
718 *OutError = strdup(Error.c_str());
719 return 1;
720 }
721
722
723 extern "C"
724 void
725 lp_free_generated_code(struct lp_generated_code *code)
726 {
727 ShaderMemoryManager::freeGeneratedCode(code);
728 }
729
730 extern "C"
731 LLVMMCJITMemoryManagerRef
732 lp_get_default_memory_manager()
733 {
734 BaseMemoryManager *mm;
735 #if HAVE_LLVM < 0x0306
736 mm = llvm::JITMemoryManager::CreateDefaultMemManager();
737 #else
738 mm = new llvm::SectionMemoryManager();
739 #endif
740 return reinterpret_cast<LLVMMCJITMemoryManagerRef>(mm);
741 }
742
743 extern "C"
744 void
745 lp_free_memory_manager(LLVMMCJITMemoryManagerRef memorymgr)
746 {
747 delete reinterpret_cast<BaseMemoryManager*>(memorymgr);
748 }
749
750 extern "C" LLVMValueRef
751 lp_get_called_value(LLVMValueRef call)
752 {
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());
757 #else
758 return NULL; /* radeonsi doesn't support so old LLVM. */
759 #endif
760 }
761
762 extern "C" bool
763 lp_is_function(LLVMValueRef v)
764 {
765 #if HAVE_LLVM >= 0x0309
766 return LLVMGetValueKind(v) == LLVMFunctionValueKind;
767 #else
768 return llvm::isa<llvm::Function>(llvm::unwrap(v));
769 #endif
770 }
771
772 extern "C" LLVMBuilderRef
773 lp_create_builder(LLVMContextRef ctx, enum lp_float_mode float_mode)
774 {
775 LLVMBuilderRef builder = LLVMCreateBuilderInContext(ctx);
776
777 #if HAVE_LLVM >= 0x0308
778 llvm::FastMathFlags flags;
779
780 switch (float_mode) {
781 case LP_FLOAT_MODE_DEFAULT:
782 break;
783 case LP_FLOAT_MODE_NO_SIGNED_ZEROS_FP_MATH:
784 flags.setNoSignedZeros();
785 llvm::unwrap(builder)->setFastMathFlags(flags);
786 break;
787 case LP_FLOAT_MODE_UNSAFE_FP_MATH:
788 flags.setUnsafeAlgebra();
789 llvm::unwrap(builder)->setFastMathFlags(flags);
790 break;
791 }
792 #endif
793
794 return builder;
795 }