gallivm: Wrap deleted inlcude in if HAVE_LLVM < 0x0306
[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 #include <llvm-c/Core.h>
54 #include <llvm-c/ExecutionEngine.h>
55 #include <llvm/Target/TargetOptions.h>
56 #include <llvm/ExecutionEngine/ExecutionEngine.h>
57 #include <llvm/ADT/Triple.h>
58 #if HAVE_LLVM < 0x0306
59 #include <llvm/ExecutionEngine/JITMemoryManager.h>
60 #endif
61 #include <llvm/Support/CommandLine.h>
62 #include <llvm/Support/Host.h>
63 #include <llvm/Support/PrettyStackTrace.h>
64
65 #include <llvm/Support/TargetSelect.h>
66
67 #if HAVE_LLVM >= 0x0303
68 #include <llvm/IR/IRBuilder.h>
69 #include <llvm/IR/Module.h>
70 #include <llvm/Support/CBindingWrapping.h>
71 #endif
72
73 #include "pipe/p_config.h"
74 #include "util/u_debug.h"
75 #include "util/u_cpu_detect.h"
76
77 #include "lp_bld_misc.h"
78
79 namespace {
80
81 class LLVMEnsureMultithreaded {
82 public:
83 LLVMEnsureMultithreaded()
84 {
85 #if HAVE_LLVM < 0x0303
86 if (!llvm::llvm_is_multithreaded()) {
87 llvm::llvm_start_multithreaded();
88 }
89 #else
90 if (!LLVMIsMultithreaded()) {
91 LLVMStartMultithreaded();
92 }
93 #endif
94 }
95 };
96
97 static LLVMEnsureMultithreaded lLVMEnsureMultithreaded;
98
99 }
100
101 extern "C" void
102 lp_set_target_options(void)
103 {
104 #if HAVE_LLVM < 0x0304
105 /*
106 * By default LLVM adds a signal handler to output a pretty stack trace.
107 * This signal handler is never removed, causing problems when unloading the
108 * shared object where the gallium driver resides.
109 */
110 llvm::DisablePrettyStackTrace = true;
111 #endif
112
113 // If we have a native target, initialize it to ensure it is linked in and
114 // usable by the JIT.
115 llvm::InitializeNativeTarget();
116
117 llvm::InitializeNativeTargetAsmPrinter();
118
119 llvm::InitializeNativeTargetDisassembler();
120 }
121
122
123 extern "C"
124 LLVMValueRef
125 lp_build_load_volatile(LLVMBuilderRef B, LLVMValueRef PointerVal,
126 const char *Name)
127 {
128 return llvm::wrap(llvm::unwrap(B)->CreateLoad(llvm::unwrap(PointerVal), true, Name));
129 }
130
131
132 extern "C"
133 void
134 lp_set_load_alignment(LLVMValueRef Inst,
135 unsigned Align)
136 {
137 llvm::unwrap<llvm::LoadInst>(Inst)->setAlignment(Align);
138 }
139
140 extern "C"
141 void
142 lp_set_store_alignment(LLVMValueRef Inst,
143 unsigned Align)
144 {
145 llvm::unwrap<llvm::StoreInst>(Inst)->setAlignment(Align);
146 }
147
148 #if HAVE_LLVM < 0x0306
149
150 /*
151 * Delegating is tedious but the default manager class is hidden in an
152 * anonymous namespace in LLVM, so we cannot just derive from it to change
153 * its behavior.
154 */
155 class DelegatingJITMemoryManager : public llvm::JITMemoryManager {
156
157 protected:
158 virtual llvm::JITMemoryManager *mgr() const = 0;
159
160 public:
161 /*
162 * From JITMemoryManager
163 */
164 virtual void setMemoryWritable() {
165 mgr()->setMemoryWritable();
166 }
167 virtual void setMemoryExecutable() {
168 mgr()->setMemoryExecutable();
169 }
170 virtual void setPoisonMemory(bool poison) {
171 mgr()->setPoisonMemory(poison);
172 }
173 virtual void AllocateGOT() {
174 mgr()->AllocateGOT();
175 /*
176 * isManagingGOT() is not virtual in base class so we can't delegate.
177 * Instead we mirror the value of HasGOT in our instance.
178 */
179 HasGOT = mgr()->isManagingGOT();
180 }
181 virtual uint8_t *getGOTBase() const {
182 return mgr()->getGOTBase();
183 }
184 virtual uint8_t *startFunctionBody(const llvm::Function *F,
185 uintptr_t &ActualSize) {
186 return mgr()->startFunctionBody(F, ActualSize);
187 }
188 virtual uint8_t *allocateStub(const llvm::GlobalValue *F,
189 unsigned StubSize,
190 unsigned Alignment) {
191 return mgr()->allocateStub(F, StubSize, Alignment);
192 }
193 virtual void endFunctionBody(const llvm::Function *F,
194 uint8_t *FunctionStart,
195 uint8_t *FunctionEnd) {
196 mgr()->endFunctionBody(F, FunctionStart, FunctionEnd);
197 }
198 virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) {
199 return mgr()->allocateSpace(Size, Alignment);
200 }
201 virtual uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) {
202 return mgr()->allocateGlobal(Size, Alignment);
203 }
204 virtual void deallocateFunctionBody(void *Body) {
205 mgr()->deallocateFunctionBody(Body);
206 }
207 #if HAVE_LLVM < 0x0304
208 virtual uint8_t *startExceptionTable(const llvm::Function *F,
209 uintptr_t &ActualSize) {
210 return mgr()->startExceptionTable(F, ActualSize);
211 }
212 virtual void endExceptionTable(const llvm::Function *F,
213 uint8_t *TableStart,
214 uint8_t *TableEnd,
215 uint8_t *FrameRegister) {
216 mgr()->endExceptionTable(F, TableStart, TableEnd,
217 FrameRegister);
218 }
219 virtual void deallocateExceptionTable(void *ET) {
220 mgr()->deallocateExceptionTable(ET);
221 }
222 #endif
223 virtual bool CheckInvariants(std::string &s) {
224 return mgr()->CheckInvariants(s);
225 }
226 virtual size_t GetDefaultCodeSlabSize() {
227 return mgr()->GetDefaultCodeSlabSize();
228 }
229 virtual size_t GetDefaultDataSlabSize() {
230 return mgr()->GetDefaultDataSlabSize();
231 }
232 virtual size_t GetDefaultStubSlabSize() {
233 return mgr()->GetDefaultStubSlabSize();
234 }
235 virtual unsigned GetNumCodeSlabs() {
236 return mgr()->GetNumCodeSlabs();
237 }
238 virtual unsigned GetNumDataSlabs() {
239 return mgr()->GetNumDataSlabs();
240 }
241 virtual unsigned GetNumStubSlabs() {
242 return mgr()->GetNumStubSlabs();
243 }
244
245 /*
246 * From RTDyldMemoryManager
247 */
248 #if HAVE_LLVM >= 0x0304
249 virtual uint8_t *allocateCodeSection(uintptr_t Size,
250 unsigned Alignment,
251 unsigned SectionID,
252 llvm::StringRef SectionName) {
253 return mgr()->allocateCodeSection(Size, Alignment, SectionID,
254 SectionName);
255 }
256 #else
257 virtual uint8_t *allocateCodeSection(uintptr_t Size,
258 unsigned Alignment,
259 unsigned SectionID) {
260 return mgr()->allocateCodeSection(Size, Alignment, SectionID);
261 }
262 #endif
263 #if HAVE_LLVM >= 0x0303
264 virtual uint8_t *allocateDataSection(uintptr_t Size,
265 unsigned Alignment,
266 unsigned SectionID,
267 #if HAVE_LLVM >= 0x0304
268 llvm::StringRef SectionName,
269 #endif
270 bool IsReadOnly) {
271 return mgr()->allocateDataSection(Size, Alignment, SectionID,
272 #if HAVE_LLVM >= 0x0304
273 SectionName,
274 #endif
275 IsReadOnly);
276 }
277 #if HAVE_LLVM >= 0x0304
278 virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) {
279 mgr()->registerEHFrames(Addr, LoadAddr, Size);
280 }
281 virtual void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) {
282 mgr()->deregisterEHFrames(Addr, LoadAddr, Size);
283 }
284 #else
285 virtual void registerEHFrames(llvm::StringRef SectionData) {
286 mgr()->registerEHFrames(SectionData);
287 }
288 #endif
289 #else
290 virtual uint8_t *allocateDataSection(uintptr_t Size,
291 unsigned Alignment,
292 unsigned SectionID) {
293 return mgr()->allocateDataSection(Size, Alignment, SectionID);
294 }
295 #endif
296 virtual void *getPointerToNamedFunction(const std::string &Name,
297 bool AbortOnFailure=true) {
298 return mgr()->getPointerToNamedFunction(Name, AbortOnFailure);
299 }
300 #if HAVE_LLVM == 0x0303
301 virtual bool applyPermissions(std::string *ErrMsg = 0) {
302 return mgr()->applyPermissions(ErrMsg);
303 }
304 #elif HAVE_LLVM > 0x0303
305 virtual bool finalizeMemory(std::string *ErrMsg = 0) {
306 return mgr()->finalizeMemory(ErrMsg);
307 }
308 #endif
309 };
310
311
312 /*
313 * Delegate memory management to one shared manager for more efficient use
314 * of memory than creating a separate pool for each LLVM engine.
315 * Keep generated code until freeGeneratedCode() is called, instead of when
316 * memory manager is destroyed, which happens during engine destruction.
317 * This allows additional memory savings as we don't have to keep the engine
318 * around in order to use the code.
319 * All methods are delegated to the shared manager except destruction and
320 * deallocating code. For the latter we just remember what needs to be
321 * deallocated later. The shared manager is deleted once it is empty.
322 */
323 class ShaderMemoryManager : public DelegatingJITMemoryManager {
324
325 static llvm::JITMemoryManager *TheMM;
326 static unsigned NumUsers;
327
328 struct GeneratedCode {
329 typedef std::vector<void *> Vec;
330 Vec FunctionBody, ExceptionTable;
331
332 GeneratedCode() {
333 ++NumUsers;
334 }
335
336 ~GeneratedCode() {
337 /*
338 * Deallocate things as previously requested and
339 * free shared manager when no longer used.
340 */
341 Vec::iterator i;
342
343 assert(TheMM);
344 for ( i = FunctionBody.begin(); i != FunctionBody.end(); ++i )
345 TheMM->deallocateFunctionBody(*i);
346 #if HAVE_LLVM < 0x0304
347 for ( i = ExceptionTable.begin(); i != ExceptionTable.end(); ++i )
348 TheMM->deallocateExceptionTable(*i);
349 #endif
350 --NumUsers;
351 if (NumUsers == 0) {
352 delete TheMM;
353 TheMM = 0;
354 }
355 }
356 };
357
358 GeneratedCode *code;
359
360 llvm::JITMemoryManager *mgr() const {
361 if (!TheMM) {
362 TheMM = CreateDefaultMemManager();
363 }
364 return TheMM;
365 }
366
367 public:
368
369 ShaderMemoryManager() {
370 code = new GeneratedCode;
371 }
372
373 virtual ~ShaderMemoryManager() {
374 /*
375 * 'code' is purposely not deleted. It is the user's responsibility
376 * to call getGeneratedCode() and freeGeneratedCode().
377 */
378 }
379
380 struct lp_generated_code *getGeneratedCode() {
381 return (struct lp_generated_code *) code;
382 }
383
384 static void freeGeneratedCode(struct lp_generated_code *code) {
385 delete (GeneratedCode *) code;
386 }
387
388 #if HAVE_LLVM < 0x0304
389 virtual void deallocateExceptionTable(void *ET) {
390 // remember for later deallocation
391 code->ExceptionTable.push_back(ET);
392 }
393 #endif
394
395 virtual void deallocateFunctionBody(void *Body) {
396 // remember for later deallocation
397 code->FunctionBody.push_back(Body);
398 }
399 };
400
401 llvm::JITMemoryManager *ShaderMemoryManager::TheMM = 0;
402 unsigned ShaderMemoryManager::NumUsers = 0;
403
404 #endif
405
406 /**
407 * Same as LLVMCreateJITCompilerForModule, but:
408 * - allows using MCJIT and enabling AVX feature where available.
409 * - set target options
410 *
411 * See also:
412 * - llvm/lib/ExecutionEngine/ExecutionEngineBindings.cpp
413 * - llvm/tools/lli/lli.cpp
414 * - http://markmail.org/message/ttkuhvgj4cxxy2on#query:+page:1+mid:aju2dggerju3ivd3+state:results
415 */
416 extern "C"
417 LLVMBool
418 lp_build_create_jit_compiler_for_module(LLVMExecutionEngineRef *OutJIT,
419 lp_generated_code **OutCode,
420 LLVMModuleRef M,
421 unsigned OptLevel,
422 int useMCJIT,
423 char **OutError)
424 {
425 using namespace llvm;
426
427 #if HAVE_LLVM >= 0x0306
428 *OutError = strdup("MCJIT not supported");
429 return 1;
430 #else
431
432 std::string Error;
433 #if HAVE_LLVM >= 0x0306
434 EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
435 #else
436 EngineBuilder builder(unwrap(M));
437 #endif
438
439 /**
440 * LLVM 3.1+ haven't more "extern unsigned llvm::StackAlignmentOverride" and
441 * friends for configuring code generation options, like stack alignment.
442 */
443 TargetOptions options;
444 #if defined(PIPE_ARCH_X86)
445 options.StackAlignmentOverride = 4;
446 #if HAVE_LLVM < 0x0304
447 options.RealignStack = true;
448 #endif
449 #endif
450
451 #if defined(DEBUG)
452 options.JITEmitDebugInfo = true;
453 #endif
454
455 #if defined(DEBUG) || defined(PROFILE)
456 #if HAVE_LLVM < 0x0304
457 options.NoFramePointerElimNonLeaf = true;
458 #endif
459 options.NoFramePointerElim = true;
460 #endif
461
462 builder.setEngineKind(EngineKind::JIT)
463 .setErrorStr(&Error)
464 .setTargetOptions(options)
465 .setOptLevel((CodeGenOpt::Level)OptLevel);
466
467 if (useMCJIT) {
468 #if HAVE_LLVM < 0x0306
469 builder.setUseMCJIT(true);
470 #endif
471 #ifdef _WIN32
472 /*
473 * MCJIT works on Windows, but currently only through ELF object format.
474 */
475 std::string targetTriple = llvm::sys::getProcessTriple();
476 targetTriple.append("-elf");
477 unwrap(M)->setTargetTriple(targetTriple);
478 #endif
479 }
480
481 llvm::SmallVector<std::string, 1> MAttrs;
482 if (util_cpu_caps.has_avx) {
483 /*
484 * AVX feature is not automatically detected from CPUID by the X86 target
485 * yet, because the old (yet default) JIT engine is not capable of
486 * emitting the opcodes. On newer llvm versions it is and at least some
487 * versions (tested with 3.3) will emit avx opcodes without this anyway.
488 */
489 MAttrs.push_back("+avx");
490 if (util_cpu_caps.has_f16c) {
491 MAttrs.push_back("+f16c");
492 }
493 builder.setMAttrs(MAttrs);
494 }
495
496 #if HAVE_LLVM >= 0x0305
497 StringRef MCPU = llvm::sys::getHostCPUName();
498 /*
499 * The cpu bits are no longer set automatically, so need to set mcpu manually.
500 * Note that the MAttrs set above will be sort of ignored (since we should
501 * not set any which would not be set by specifying the cpu anyway).
502 * It ought to be safe though since getHostCPUName() should include bits
503 * not only from the cpu but environment as well (for instance if it's safe
504 * to use avx instructions which need OS support). According to
505 * http://llvm.org/bugs/show_bug.cgi?id=19429 however if I understand this
506 * right it may be necessary to specify older cpu (or disable mattrs) though
507 * when not using MCJIT so no instructions are generated which the old JIT
508 * can't handle. Not entirely sure if we really need to do anything yet.
509 */
510 builder.setMCPU(MCPU);
511 #endif
512
513 ShaderMemoryManager *MM = new ShaderMemoryManager();
514 *OutCode = MM->getGeneratedCode();
515
516 builder.setJITMemoryManager(MM);
517
518 ExecutionEngine *JIT;
519
520 #if HAVE_LLVM >= 0x0302
521 JIT = builder.create();
522 #else
523 /*
524 * Workaround http://llvm.org/PR12833
525 */
526 StringRef MArch = "";
527 StringRef MCPU = "";
528 Triple TT(unwrap(M)->getTargetTriple());
529 JIT = builder.create(builder.selectTarget(TT, MArch, MCPU, MAttrs));
530 #endif
531 if (JIT) {
532 *OutJIT = wrap(JIT);
533 return 0;
534 }
535 lp_free_generated_code(*OutCode);
536 *OutCode = 0;
537 delete MM;
538 *OutError = strdup(Error.c_str());
539 return 1;
540 #endif
541 }
542
543
544 extern "C"
545 void
546 lp_free_generated_code(struct lp_generated_code *code)
547 {
548 #if HAVE_LLVM < 0x0306
549 ShaderMemoryManager::freeGeneratedCode(code);
550 #endif
551 }