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