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