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