1 /**************************************************************************
2 *
3 * Copyright 2009 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 above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 #include "pipe/p_config.h"
30 #include "pipe/p_compiler.h"
31 #include "util/u_cpu_detect.h"
32 #include "util/u_debug.h"
33 #include "util/u_memory.h"
34 #include "util/simple_list.h"
35 #include "util/os_time.h"
36 #include "lp_bld.h"
37 #include "lp_bld_debug.h"
38 #include "lp_bld_misc.h"
39 #include "lp_bld_init.h"
40
41 #include <llvm/Config/llvm-config.h>
42 #include <llvm-c/Analysis.h>
43 #include <llvm-c/Transforms/Scalar.h>
44 #if LLVM_VERSION_MAJOR >= 7
45 #include <llvm-c/Transforms/Utils.h>
46 #endif
47 #include <llvm-c/BitWriter.h>
48 #if GALLIVM_HAVE_CORO
49 #if LLVM_VERSION_MAJOR <= 8 && (defined(PIPE_ARCH_AARCH64) || defined (PIPE_ARCH_ARM) || defined(PIPE_ARCH_S390) || defined(PIPE_ARCH_MIPS64))
50 #include <llvm-c/Transforms/IPO.h>
51 #endif
52 #include <llvm-c/Transforms/Coroutines.h>
53 #endif
54
55 unsigned gallivm_perf = 0;
56
57 static const struct debug_named_value lp_bld_perf_flags[] = {
58 { "brilinear", GALLIVM_PERF_BRILINEAR, "enable brilinear optimization" },
59 { "rho_approx", GALLIVM_PERF_RHO_APPROX, "enable rho_approx optimization" },
60 { "no_quad_lod", GALLIVM_PERF_NO_QUAD_LOD, "disable quad_lod optimization" },
61 { "no_aos_sampling", GALLIVM_PERF_NO_AOS_SAMPLING, "disable aos sampling optimization" },
62 { "nopt", GALLIVM_PERF_NO_OPT, "disable optimization passes to speed up shader compilation" },
63 DEBUG_NAMED_VALUE_END
64 };
65
66 #ifdef DEBUG
67 unsigned gallivm_debug = 0;
68
69 static const struct debug_named_value lp_bld_debug_flags[] = {
70 { "tgsi", GALLIVM_DEBUG_TGSI, NULL },
71 { "ir", GALLIVM_DEBUG_IR, NULL },
72 { "asm", GALLIVM_DEBUG_ASM, NULL },
73 { "perf", GALLIVM_DEBUG_PERF, NULL },
74 { "gc", GALLIVM_DEBUG_GC, NULL },
75 { "dumpbc", GALLIVM_DEBUG_DUMP_BC, NULL },
76 DEBUG_NAMED_VALUE_END
77 };
78
79 DEBUG_GET_ONCE_FLAGS_OPTION(gallivm_debug, "GALLIVM_DEBUG", lp_bld_debug_flags, 0)
80 #endif
81
82
83 static boolean gallivm_initialized = FALSE;
84
85 unsigned lp_native_vector_width;
86
87
88 /*
89 * Optimization values are:
90 * - 0: None (-O0)
91 * - 1: Less (-O1)
92 * - 2: Default (-O2, -Os)
93 * - 3: Aggressive (-O3)
94 *
95 * See also CodeGenOpt::Level in llvm/Target/TargetMachine.h
96 */
97 enum LLVM_CodeGenOpt_Level {
98 None, // -O0
99 Less, // -O1
100 Default, // -O2, -Os
101 Aggressive // -O3
102 };
103
104
105 /**
106 * Create the LLVM (optimization) pass manager and install
107 * relevant optimization passes.
108 * \return TRUE for success, FALSE for failure
109 */
110 static boolean
create_pass_manager(struct gallivm_state * gallivm)111 create_pass_manager(struct gallivm_state *gallivm)
112 {
113 assert(!gallivm->passmgr);
114 assert(gallivm->target);
115
116 gallivm->passmgr = LLVMCreateFunctionPassManagerForModule(gallivm->module);
117 if (!gallivm->passmgr)
118 return FALSE;
119
120 #if GALLIVM_HAVE_CORO
121 gallivm->cgpassmgr = LLVMCreatePassManager();
122 #endif
123 /*
124 * TODO: some per module pass manager with IPO passes might be helpful -
125 * the generated texture functions may benefit from inlining if they are
126 * simple, or constant propagation into them, etc.
127 */
128
129 {
130 char *td_str;
131 // New ones from the Module.
132 td_str = LLVMCopyStringRepOfTargetData(gallivm->target);
133 LLVMSetDataLayout(gallivm->module, td_str);
134 free(td_str);
135 }
136
137 #if GALLIVM_HAVE_CORO
138 #if LLVM_VERSION_MAJOR <= 8 && (defined(PIPE_ARCH_AARCH64) || defined (PIPE_ARCH_ARM) || defined(PIPE_ARCH_S390) || defined(PIPE_ARCH_MIPS64))
139 LLVMAddArgumentPromotionPass(gallivm->cgpassmgr);
140 LLVMAddFunctionAttrsPass(gallivm->cgpassmgr);
141 #endif
142 LLVMAddCoroEarlyPass(gallivm->cgpassmgr);
143 LLVMAddCoroSplitPass(gallivm->cgpassmgr);
144 LLVMAddCoroElidePass(gallivm->cgpassmgr);
145 #endif
146
147 if ((gallivm_perf & GALLIVM_PERF_NO_OPT) == 0) {
148 /*
149 * TODO: Evaluate passes some more - keeping in mind
150 * both quality of generated code and compile times.
151 */
152 /*
153 * NOTE: if you change this, don't forget to change the output
154 * with GALLIVM_DEBUG_DUMP_BC in gallivm_compile_module.
155 */
156 LLVMAddScalarReplAggregatesPass(gallivm->passmgr);
157 LLVMAddEarlyCSEPass(gallivm->passmgr);
158 LLVMAddCFGSimplificationPass(gallivm->passmgr);
159 /*
160 * FIXME: LICM is potentially quite useful. However, for some
161 * rather crazy shaders the compile time can reach _hours_ per shader,
162 * due to licm implying lcssa (since llvm 3.5), which can take forever.
163 * Even for sane shaders, the cost of licm is rather high (and not just
164 * due to lcssa, licm itself too), though mostly only in cases when it
165 * can actually move things, so having to disable it is a pity.
166 * LLVMAddLICMPass(gallivm->passmgr);
167 */
168 LLVMAddReassociatePass(gallivm->passmgr);
169 LLVMAddPromoteMemoryToRegisterPass(gallivm->passmgr);
170 #if LLVM_VERSION_MAJOR <= 11
171 LLVMAddConstantPropagationPass(gallivm->passmgr);
172 #else
173 LLVMAddInstructionSimplifyPass(gallivm->passmgr);
174 #endif
175 LLVMAddInstructionCombiningPass(gallivm->passmgr);
176 LLVMAddGVNPass(gallivm->passmgr);
177 }
178 else {
179 /* We need at least this pass to prevent the backends to fail in
180 * unexpected ways.
181 */
182 LLVMAddPromoteMemoryToRegisterPass(gallivm->passmgr);
183 }
184 #if GALLIVM_HAVE_CORO
185 LLVMAddCoroCleanupPass(gallivm->passmgr);
186 #endif
187
188 return TRUE;
189 }
190
191
192 /**
193 * Free gallivm object's LLVM allocations, but not any generated code
194 * nor the gallivm object itself.
195 */
196 void
gallivm_free_ir(struct gallivm_state * gallivm)197 gallivm_free_ir(struct gallivm_state *gallivm)
198 {
199 if (gallivm->passmgr) {
200 LLVMDisposePassManager(gallivm->passmgr);
201 }
202
203 #if GALLIVM_HAVE_CORO
204 if (gallivm->cgpassmgr) {
205 LLVMDisposePassManager(gallivm->cgpassmgr);
206 }
207 #endif
208
209 if (gallivm->engine) {
210 /* This will already destroy any associated module */
211 LLVMDisposeExecutionEngine(gallivm->engine);
212 } else if (gallivm->module) {
213 LLVMDisposeModule(gallivm->module);
214 }
215
216 if (gallivm->cache) {
217 lp_free_objcache(gallivm->cache->jit_obj_cache);
218 free(gallivm->cache->data);
219 }
220 FREE(gallivm->module_name);
221
222 if (gallivm->target) {
223 LLVMDisposeTargetData(gallivm->target);
224 }
225
226 if (gallivm->builder)
227 LLVMDisposeBuilder(gallivm->builder);
228
229 /* The LLVMContext should be owned by the parent of gallivm. */
230
231 gallivm->engine = NULL;
232 gallivm->target = NULL;
233 gallivm->module = NULL;
234 gallivm->module_name = NULL;
235 gallivm->cgpassmgr = NULL;
236 gallivm->passmgr = NULL;
237 gallivm->context = NULL;
238 gallivm->builder = NULL;
239 gallivm->cache = NULL;
240 }
241
242
243 /**
244 * Free LLVM-generated code. Should be done AFTER gallivm_free_ir().
245 */
246 static void
gallivm_free_code(struct gallivm_state * gallivm)247 gallivm_free_code(struct gallivm_state *gallivm)
248 {
249 assert(!gallivm->module);
250 assert(!gallivm->engine);
251 lp_free_generated_code(gallivm->code);
252 gallivm->code = NULL;
253 lp_free_memory_manager(gallivm->memorymgr);
254 gallivm->memorymgr = NULL;
255 }
256
257
258 static boolean
init_gallivm_engine(struct gallivm_state * gallivm)259 init_gallivm_engine(struct gallivm_state *gallivm)
260 {
261 if (1) {
262 enum LLVM_CodeGenOpt_Level optlevel;
263 char *error = NULL;
264 int ret;
265
266 if (gallivm_perf & GALLIVM_PERF_NO_OPT) {
267 optlevel = None;
268 }
269 else {
270 optlevel = Default;
271 }
272
273 ret = lp_build_create_jit_compiler_for_module(&gallivm->engine,
274 &gallivm->code,
275 gallivm->cache,
276 gallivm->module,
277 gallivm->memorymgr,
278 (unsigned) optlevel,
279 &error);
280 if (ret) {
281 _debug_printf("%s\n", error);
282 LLVMDisposeMessage(error);
283 goto fail;
284 }
285 }
286
287 if (0) {
288 /*
289 * Dump the data layout strings.
290 */
291
292 LLVMTargetDataRef target = LLVMGetExecutionEngineTargetData(gallivm->engine);
293 char *data_layout;
294 char *engine_data_layout;
295
296 data_layout = LLVMCopyStringRepOfTargetData(gallivm->target);
297 engine_data_layout = LLVMCopyStringRepOfTargetData(target);
298
299 if (1) {
300 debug_printf("module target data = %s\n", data_layout);
301 debug_printf("engine target data = %s\n", engine_data_layout);
302 }
303
304 free(data_layout);
305 free(engine_data_layout);
306 }
307
308 return TRUE;
309
310 fail:
311 return FALSE;
312 }
313
314
315 /**
316 * Allocate gallivm LLVM objects.
317 * \return TRUE for success, FALSE for failure
318 */
319 static boolean
init_gallivm_state(struct gallivm_state * gallivm,const char * name,LLVMContextRef context,struct lp_cached_code * cache)320 init_gallivm_state(struct gallivm_state *gallivm, const char *name,
321 LLVMContextRef context, struct lp_cached_code *cache)
322 {
323 assert(!gallivm->context);
324 assert(!gallivm->module);
325
326 if (!lp_build_init())
327 return FALSE;
328
329 gallivm->context = context;
330 gallivm->cache = cache;
331 if (!gallivm->context)
332 goto fail;
333
334 gallivm->module_name = NULL;
335 if (name) {
336 size_t size = strlen(name) + 1;
337 gallivm->module_name = MALLOC(size);
338 if (gallivm->module_name) {
339 memcpy(gallivm->module_name, name, size);
340 }
341 }
342
343 gallivm->module = LLVMModuleCreateWithNameInContext(name,
344 gallivm->context);
345 if (!gallivm->module)
346 goto fail;
347
348 #if defined(PIPE_ARCH_X86)
349 lp_set_module_stack_alignment_override(gallivm->module, 4);
350 #endif
351
352 gallivm->builder = LLVMCreateBuilderInContext(gallivm->context);
353 if (!gallivm->builder)
354 goto fail;
355
356 gallivm->memorymgr = lp_get_default_memory_manager();
357 if (!gallivm->memorymgr)
358 goto fail;
359
360 /* FIXME: MC-JIT only allows compiling one module at a time, and it must be
361 * complete when MC-JIT is created. So defer the MC-JIT engine creation for
362 * now.
363 */
364
365 /*
366 * MC-JIT engine compiles the module immediately on creation, so we can't
367 * obtain the target data from it. Instead we create a target data layout
368 * from a string.
369 *
370 * The produced layout strings are not precisely the same, but should make
371 * no difference for the kind of optimization passes we run.
372 *
373 * For reference this is the layout string on x64:
374 *
375 * e-p:64:64:64-S128-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f16:16:16-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-f128:128:128-n8:16:32:64
376 *
377 * See also:
378 * - http://llvm.org/docs/LangRef.html#datalayout
379 */
380
381 {
382 const unsigned pointer_size = 8 * sizeof(void *);
383 char layout[512];
384 snprintf(layout, sizeof layout, "%c-p:%u:%u:%u-i64:64:64-a0:0:%u-s0:%u:%u",
385 #if UTIL_ARCH_LITTLE_ENDIAN
386 'e', // little endian
387 #else
388 'E', // big endian
389 #endif
390 pointer_size, pointer_size, pointer_size, // pointer size, abi alignment, preferred alignment
391 pointer_size, // aggregate preferred alignment
392 pointer_size, pointer_size); // stack objects abi alignment, preferred alignment
393
394 gallivm->target = LLVMCreateTargetData(layout);
395 if (!gallivm->target) {
396 return FALSE;
397 }
398 }
399
400 if (!create_pass_manager(gallivm))
401 goto fail;
402
403 return TRUE;
404
405 fail:
406 gallivm_free_ir(gallivm);
407 gallivm_free_code(gallivm);
408 return FALSE;
409 }
410
411
412 boolean
lp_build_init(void)413 lp_build_init(void)
414 {
415 if (gallivm_initialized)
416 return TRUE;
417
418
419 /* LLVMLinkIn* are no-ops at runtime. They just ensure the respective
420 * component is linked at buildtime, which is sufficient for its static
421 * constructors to be called at load time.
422 */
423 LLVMLinkInMCJIT();
424
425 #ifdef DEBUG
426 gallivm_debug = debug_get_option_gallivm_debug();
427 #endif
428
429 gallivm_perf = debug_get_flags_option("GALLIVM_PERF", lp_bld_perf_flags, 0 );
430
431 lp_set_target_options();
432
433 util_cpu_detect();
434
435 /* For simulating less capable machines */
436 #ifdef DEBUG
437 if (debug_get_bool_option("LP_FORCE_SSE2", FALSE)) {
438 extern struct util_cpu_caps_t util_cpu_caps;
439 assert(util_cpu_caps.has_sse2);
440 util_cpu_caps.has_sse3 = 0;
441 util_cpu_caps.has_ssse3 = 0;
442 util_cpu_caps.has_sse4_1 = 0;
443 util_cpu_caps.has_sse4_2 = 0;
444 util_cpu_caps.has_avx = 0;
445 util_cpu_caps.has_avx2 = 0;
446 util_cpu_caps.has_f16c = 0;
447 util_cpu_caps.has_fma = 0;
448 }
449 #endif
450
451 if (util_get_cpu_caps()->has_avx2 || util_get_cpu_caps()->has_avx) {
452 lp_native_vector_width = 256;
453 } else {
454 /* Leave it at 128, even when no SIMD extensions are available.
455 * Really needs to be a multiple of 128 so can fit 4 floats.
456 */
457 lp_native_vector_width = 128;
458 }
459
460 lp_native_vector_width = debug_get_num_option("LP_NATIVE_VECTOR_WIDTH",
461 lp_native_vector_width);
462
463 #if LLVM_VERSION_MAJOR < 4
464 if (lp_native_vector_width <= 128) {
465 /* Hide AVX support, as often LLVM AVX intrinsics are only guarded by
466 * "util_get_cpu_caps()->has_avx" predicate, and lack the
467 * "lp_native_vector_width > 128" predicate. And also to ensure a more
468 * consistent behavior, allowing one to test SSE2 on AVX machines.
469 * XXX: should not play games with util_cpu_caps directly as it might
470 * get used for other things outside llvm too.
471 */
472 util_get_cpu_caps()->has_avx = 0;
473 util_get_cpu_caps()->has_avx2 = 0;
474 util_get_cpu_caps()->has_f16c = 0;
475 util_get_cpu_caps()->has_fma = 0;
476 }
477 #endif
478
479 #ifdef PIPE_ARCH_PPC_64
480 /* Set the NJ bit in VSCR to 0 so denormalized values are handled as
481 * specified by IEEE standard (PowerISA 2.06 - Section 6.3). This guarantees
482 * that some rounding and half-float to float handling does not round
483 * incorrectly to 0.
484 * XXX: should eventually follow same logic on all platforms.
485 * Right now denorms get explicitly disabled (but elsewhere) for x86,
486 * whereas ppc64 explicitly enables them...
487 */
488 if (util_get_cpu_caps()->has_altivec) {
489 unsigned short mask[] = { 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
490 0xFFFF, 0xFFFF, 0xFFFE, 0xFFFF };
491 __asm (
492 "mfvscr %%v1\n"
493 "vand %0,%%v1,%0\n"
494 "mtvscr %0"
495 :
496 : "r" (*mask)
497 );
498 }
499 #endif
500
501 gallivm_initialized = TRUE;
502
503 return TRUE;
504 }
505
506
507
508 /**
509 * Create a new gallivm_state object.
510 */
511 struct gallivm_state *
gallivm_create(const char * name,LLVMContextRef context,struct lp_cached_code * cache)512 gallivm_create(const char *name, LLVMContextRef context,
513 struct lp_cached_code *cache)
514 {
515 struct gallivm_state *gallivm;
516
517 gallivm = CALLOC_STRUCT(gallivm_state);
518 if (gallivm) {
519 if (!init_gallivm_state(gallivm, name, context, cache)) {
520 FREE(gallivm);
521 gallivm = NULL;
522 }
523 }
524
525 assert(gallivm != NULL);
526 return gallivm;
527 }
528
529
530 /**
531 * Destroy a gallivm_state object.
532 */
533 void
gallivm_destroy(struct gallivm_state * gallivm)534 gallivm_destroy(struct gallivm_state *gallivm)
535 {
536 gallivm_free_ir(gallivm);
537 gallivm_free_code(gallivm);
538 FREE(gallivm);
539 }
540
541
542 /**
543 * Validate a function.
544 * Verification is only done with debug builds.
545 */
546 void
gallivm_verify_function(struct gallivm_state * gallivm,LLVMValueRef func)547 gallivm_verify_function(struct gallivm_state *gallivm,
548 LLVMValueRef func)
549 {
550 /* Verify the LLVM IR. If invalid, dump and abort */
551 #ifdef DEBUG
552 if (LLVMVerifyFunction(func, LLVMPrintMessageAction)) {
553 lp_debug_dump_value(func);
554 assert(0);
555 return;
556 }
557 #endif
558
559 if (gallivm_debug & GALLIVM_DEBUG_IR) {
560 /* Print the LLVM IR to stderr */
561 lp_debug_dump_value(func);
562 debug_printf("\n");
563 }
564 }
565
566
567 /**
568 * Compile a module.
569 * This does IR optimization on all functions in the module.
570 */
571 void
gallivm_compile_module(struct gallivm_state * gallivm)572 gallivm_compile_module(struct gallivm_state *gallivm)
573 {
574 LLVMValueRef func;
575 int64_t time_begin = 0;
576
577 assert(!gallivm->compiled);
578
579 if (gallivm->builder) {
580 LLVMDisposeBuilder(gallivm->builder);
581 gallivm->builder = NULL;
582 }
583
584 if (gallivm->cache && gallivm->cache->data_size) {
585 goto skip_cached;
586 }
587
588 /* Dump bitcode to a file */
589 if (gallivm_debug & GALLIVM_DEBUG_DUMP_BC) {
590 char filename[256];
591 assert(gallivm->module_name);
592 snprintf(filename, sizeof(filename), "ir_%s.bc", gallivm->module_name);
593 LLVMWriteBitcodeToFile(gallivm->module, filename);
594 debug_printf("%s written\n", filename);
595 debug_printf("Invoke as \"opt %s %s | llc -O%d %s%s\"\n",
596 gallivm_perf & GALLIVM_PERF_NO_OPT ? "-mem2reg" :
597 "-sroa -early-cse -simplifycfg -reassociate "
598 "-mem2reg -constprop -instcombine -gvn",
599 filename, gallivm_perf & GALLIVM_PERF_NO_OPT ? 0 : 2,
600 "[-mcpu=<-mcpu option>] ",
601 "[-mattr=<-mattr option(s)>]");
602 }
603
604 if (gallivm_debug & GALLIVM_DEBUG_PERF)
605 time_begin = os_time_get();
606
607 #if GALLIVM_HAVE_CORO
608 LLVMRunPassManager(gallivm->cgpassmgr, gallivm->module);
609 #endif
610 /* Run optimization passes */
611 LLVMInitializeFunctionPassManager(gallivm->passmgr);
612 func = LLVMGetFirstFunction(gallivm->module);
613 while (func) {
614 if (0) {
615 debug_printf("optimizing func %s...\n", LLVMGetValueName(func));
616 }
617
618 /* Disable frame pointer omission on debug/profile builds */
619 /* XXX: And workaround http://llvm.org/PR21435 */
620 #if defined(DEBUG) || defined(PROFILE) || defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
621 LLVMAddTargetDependentFunctionAttr(func, "no-frame-pointer-elim", "true");
622 LLVMAddTargetDependentFunctionAttr(func, "no-frame-pointer-elim-non-leaf", "true");
623 #endif
624
625 LLVMRunFunctionPassManager(gallivm->passmgr, func);
626 func = LLVMGetNextFunction(func);
627 }
628 LLVMFinalizeFunctionPassManager(gallivm->passmgr);
629
630 if (gallivm_debug & GALLIVM_DEBUG_PERF) {
631 int64_t time_end = os_time_get();
632 int time_msec = (int)((time_end - time_begin) / 1000);
633 assert(gallivm->module_name);
634 debug_printf("optimizing module %s took %d msec\n",
635 gallivm->module_name, time_msec);
636 }
637
638 /* Setting the module's DataLayout to an empty string will cause the
639 * ExecutionEngine to copy to the DataLayout string from its target machine
640 * to the module. As of LLVM 3.8 the module and the execution engine are
641 * required to have the same DataLayout.
642 *
643 * We must make sure we do this after running the optimization passes,
644 * because those passes need a correct datalayout string. For example, if
645 * those optimization passes see an empty datalayout, they will assume this
646 * is a little endian target and will do optimizations that break big endian
647 * machines.
648 *
649 * TODO: This is just a temporary work-around. The correct solution is for
650 * gallivm_init_state() to create a TargetMachine and pull the DataLayout
651 * from there. Currently, the TargetMachine used by llvmpipe is being
652 * implicitly created by the EngineBuilder in
653 * lp_build_create_jit_compiler_for_module()
654 */
655 skip_cached:
656 LLVMSetDataLayout(gallivm->module, "");
657 assert(!gallivm->engine);
658 if (!init_gallivm_engine(gallivm)) {
659 assert(0);
660 }
661 assert(gallivm->engine);
662
663 ++gallivm->compiled;
664
665 if (gallivm->debug_printf_hook)
666 LLVMAddGlobalMapping(gallivm->engine, gallivm->debug_printf_hook, debug_printf);
667
668 if (gallivm_debug & GALLIVM_DEBUG_ASM) {
669 LLVMValueRef llvm_func = LLVMGetFirstFunction(gallivm->module);
670
671 while (llvm_func) {
672 /*
673 * Need to filter out functions which don't have an implementation,
674 * such as the intrinsics. May not be sufficient in case of IPO?
675 * LLVMGetPointerToGlobal() will abort otherwise.
676 */
677 if (!LLVMIsDeclaration(llvm_func)) {
678 void *func_code = LLVMGetPointerToGlobal(gallivm->engine, llvm_func);
679 lp_disassemble(llvm_func, func_code);
680 }
681 llvm_func = LLVMGetNextFunction(llvm_func);
682 }
683 }
684
685 #if defined(PROFILE)
686 {
687 LLVMValueRef llvm_func = LLVMGetFirstFunction(gallivm->module);
688
689 while (llvm_func) {
690 if (!LLVMIsDeclaration(llvm_func)) {
691 void *func_code = LLVMGetPointerToGlobal(gallivm->engine, llvm_func);
692 lp_profile(llvm_func, func_code);
693 }
694 llvm_func = LLVMGetNextFunction(llvm_func);
695 }
696 }
697 #endif
698 }
699
700
701
702 func_pointer
gallivm_jit_function(struct gallivm_state * gallivm,LLVMValueRef func)703 gallivm_jit_function(struct gallivm_state *gallivm,
704 LLVMValueRef func)
705 {
706 void *code;
707 func_pointer jit_func;
708 int64_t time_begin = 0;
709
710 assert(gallivm->compiled);
711 assert(gallivm->engine);
712
713 if (gallivm_debug & GALLIVM_DEBUG_PERF)
714 time_begin = os_time_get();
715
716 code = LLVMGetPointerToGlobal(gallivm->engine, func);
717 assert(code);
718 jit_func = pointer_to_func(code);
719
720 if (gallivm_debug & GALLIVM_DEBUG_PERF) {
721 int64_t time_end = os_time_get();
722 int time_msec = (int)(time_end - time_begin) / 1000;
723 debug_printf(" jitting func %s took %d msec\n",
724 LLVMGetValueName(func), time_msec);
725 }
726
727 return jit_func;
728 }
729
gallivm_get_perf_flags(void)730 unsigned gallivm_get_perf_flags(void)
731 {
732 return gallivm_perf;
733 }
734