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