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