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