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