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