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