• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <llvm-c/BitWriter.h>
44 
45 
46 /* Only MCJIT is available as of LLVM SVN r216982 */
47 #if HAVE_LLVM >= 0x0306
48 #  define USE_MCJIT 1
49 #elif defined(PIPE_ARCH_PPC_64) || defined(PIPE_ARCH_S390) || defined(PIPE_ARCH_ARM) || defined(PIPE_ARCH_AARCH64)
50 #  define USE_MCJIT 1
51 #endif
52 
53 #if defined(USE_MCJIT)
54 static const bool use_mcjit = USE_MCJIT;
55 #else
56 static bool use_mcjit = FALSE;
57 #endif
58 
59 
60 #ifdef DEBUG
61 unsigned gallivm_debug = 0;
62 
63 static const struct debug_named_value lp_bld_debug_flags[] = {
64    { "tgsi",   GALLIVM_DEBUG_TGSI, NULL },
65    { "ir",     GALLIVM_DEBUG_IR, NULL },
66    { "asm",    GALLIVM_DEBUG_ASM, NULL },
67    { "nopt",   GALLIVM_DEBUG_NO_OPT, NULL },
68    { "perf",   GALLIVM_DEBUG_PERF, NULL },
69    { "no_brilinear", GALLIVM_DEBUG_NO_BRILINEAR, NULL },
70    { "no_rho_approx", GALLIVM_DEBUG_NO_RHO_APPROX, NULL },
71    { "no_quad_lod", GALLIVM_DEBUG_NO_QUAD_LOD, NULL },
72    { "gc",     GALLIVM_DEBUG_GC, NULL },
73    { "dumpbc", GALLIVM_DEBUG_DUMP_BC, NULL },
74    DEBUG_NAMED_VALUE_END
75 };
76 
77 DEBUG_GET_ONCE_FLAGS_OPTION(gallivm_debug, "GALLIVM_DEBUG", lp_bld_debug_flags, 0)
78 #endif
79 
80 
81 static boolean gallivm_initialized = FALSE;
82 
83 unsigned lp_native_vector_width;
84 
85 
86 /*
87  * Optimization values are:
88  * - 0: None (-O0)
89  * - 1: Less (-O1)
90  * - 2: Default (-O2, -Os)
91  * - 3: Aggressive (-O3)
92  *
93  * See also CodeGenOpt::Level in llvm/Target/TargetMachine.h
94  */
95 enum LLVM_CodeGenOpt_Level {
96    None,        // -O0
97    Less,        // -O1
98    Default,     // -O2, -Os
99    Aggressive   // -O3
100 };
101 
102 
103 /**
104  * Create the LLVM (optimization) pass manager and install
105  * relevant optimization passes.
106  * \return  TRUE for success, FALSE for failure
107  */
108 static boolean
create_pass_manager(struct gallivm_state * gallivm)109 create_pass_manager(struct gallivm_state *gallivm)
110 {
111    assert(!gallivm->passmgr);
112    assert(gallivm->target);
113 
114    gallivm->passmgr = LLVMCreateFunctionPassManagerForModule(gallivm->module);
115    if (!gallivm->passmgr)
116       return FALSE;
117    /*
118     * TODO: some per module pass manager with IPO passes might be helpful -
119     * the generated texture functions may benefit from inlining if they are
120     * simple, or constant propagation into them, etc.
121     */
122 
123 #if HAVE_LLVM < 0x0309
124    // Old versions of LLVM get the DataLayout from the pass manager.
125    LLVMAddTargetData(gallivm->target, gallivm->passmgr);
126 #endif
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_debug & GALLIVM_DEBUG_NO_OPT) == 0) {
137       /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
138        * but there are more on SVN.
139        * TODO: Add more passes.
140        */
141       LLVMAddScalarReplAggregatesPass(gallivm->passmgr);
142       LLVMAddLICMPass(gallivm->passmgr);
143       LLVMAddCFGSimplificationPass(gallivm->passmgr);
144       LLVMAddReassociatePass(gallivm->passmgr);
145       LLVMAddPromoteMemoryToRegisterPass(gallivm->passmgr);
146       LLVMAddConstantPropagationPass(gallivm->passmgr);
147       LLVMAddInstructionCombiningPass(gallivm->passmgr);
148       LLVMAddGVNPass(gallivm->passmgr);
149    }
150    else {
151       /* We need at least this pass to prevent the backends to fail in
152        * unexpected ways.
153        */
154       LLVMAddPromoteMemoryToRegisterPass(gallivm->passmgr);
155    }
156 
157    return TRUE;
158 }
159 
160 
161 /**
162  * Free gallivm object's LLVM allocations, but not any generated code
163  * nor the gallivm object itself.
164  */
165 void
gallivm_free_ir(struct gallivm_state * gallivm)166 gallivm_free_ir(struct gallivm_state *gallivm)
167 {
168    if (gallivm->passmgr) {
169       LLVMDisposePassManager(gallivm->passmgr);
170    }
171 
172    if (gallivm->engine) {
173       /* This will already destroy any associated module */
174       LLVMDisposeExecutionEngine(gallivm->engine);
175    } else if (gallivm->module) {
176       LLVMDisposeModule(gallivm->module);
177    }
178 
179    FREE(gallivm->module_name);
180 
181    if (!use_mcjit) {
182       /* Don't free the TargetData, it's owned by the exec engine */
183    } else {
184       if (gallivm->target) {
185          LLVMDisposeTargetData(gallivm->target);
186       }
187    }
188 
189    if (gallivm->builder)
190       LLVMDisposeBuilder(gallivm->builder);
191 
192    /* The LLVMContext should be owned by the parent of gallivm. */
193 
194    gallivm->engine = NULL;
195    gallivm->target = NULL;
196    gallivm->module = NULL;
197    gallivm->module_name = NULL;
198    gallivm->passmgr = NULL;
199    gallivm->context = NULL;
200    gallivm->builder = NULL;
201 }
202 
203 
204 /**
205  * Free LLVM-generated code.  Should be done AFTER gallivm_free_ir().
206  */
207 static void
gallivm_free_code(struct gallivm_state * gallivm)208 gallivm_free_code(struct gallivm_state *gallivm)
209 {
210    assert(!gallivm->module);
211    assert(!gallivm->engine);
212    lp_free_generated_code(gallivm->code);
213    gallivm->code = NULL;
214    lp_free_memory_manager(gallivm->memorymgr);
215    gallivm->memorymgr = NULL;
216 }
217 
218 
219 static boolean
init_gallivm_engine(struct gallivm_state * gallivm)220 init_gallivm_engine(struct gallivm_state *gallivm)
221 {
222    if (1) {
223       enum LLVM_CodeGenOpt_Level optlevel;
224       char *error = NULL;
225       int ret;
226 
227       if (gallivm_debug & GALLIVM_DEBUG_NO_OPT) {
228          optlevel = None;
229       }
230       else {
231          optlevel = Default;
232       }
233 
234       ret = lp_build_create_jit_compiler_for_module(&gallivm->engine,
235                                                     &gallivm->code,
236                                                     gallivm->module,
237                                                     gallivm->memorymgr,
238                                                     (unsigned) optlevel,
239                                                     use_mcjit,
240                                                     &error);
241       if (ret) {
242          _debug_printf("%s\n", error);
243          LLVMDisposeMessage(error);
244          goto fail;
245       }
246    }
247 
248    if (!use_mcjit) {
249       gallivm->target = LLVMGetExecutionEngineTargetData(gallivm->engine);
250       if (!gallivm->target)
251          goto fail;
252    } else {
253       if (0) {
254           /*
255            * Dump the data layout strings.
256            */
257 
258           LLVMTargetDataRef target = LLVMGetExecutionEngineTargetData(gallivm->engine);
259           char *data_layout;
260           char *engine_data_layout;
261 
262           data_layout = LLVMCopyStringRepOfTargetData(gallivm->target);
263           engine_data_layout = LLVMCopyStringRepOfTargetData(target);
264 
265           if (1) {
266              debug_printf("module target data = %s\n", data_layout);
267              debug_printf("engine target data = %s\n", engine_data_layout);
268           }
269 
270           free(data_layout);
271           free(engine_data_layout);
272       }
273    }
274 
275    return TRUE;
276 
277 fail:
278    return FALSE;
279 }
280 
281 
282 /**
283  * Allocate gallivm LLVM objects.
284  * \return  TRUE for success, FALSE for failure
285  */
286 static boolean
init_gallivm_state(struct gallivm_state * gallivm,const char * name,LLVMContextRef context)287 init_gallivm_state(struct gallivm_state *gallivm, const char *name,
288                    LLVMContextRef context)
289 {
290    assert(!gallivm->context);
291    assert(!gallivm->module);
292 
293    if (!lp_build_init())
294       return FALSE;
295 
296    gallivm->context = context;
297 
298    if (!gallivm->context)
299       goto fail;
300 
301    gallivm->module_name = NULL;
302    if (name) {
303       size_t size = strlen(name) + 1;
304       gallivm->module_name = MALLOC(size);
305       if (gallivm->module_name) {
306          memcpy(gallivm->module_name, name, size);
307       }
308    }
309 
310    gallivm->module = LLVMModuleCreateWithNameInContext(name,
311                                                        gallivm->context);
312    if (!gallivm->module)
313       goto fail;
314 
315    gallivm->builder = LLVMCreateBuilderInContext(gallivm->context);
316    if (!gallivm->builder)
317       goto fail;
318 
319    gallivm->memorymgr = lp_get_default_memory_manager();
320    if (!gallivm->memorymgr)
321       goto fail;
322 
323    /* FIXME: MC-JIT only allows compiling one module at a time, and it must be
324     * complete when MC-JIT is created. So defer the MC-JIT engine creation for
325     * now.
326     */
327    if (!use_mcjit) {
328       if (!init_gallivm_engine(gallivm)) {
329          goto fail;
330       }
331    } else {
332       /*
333        * MC-JIT engine compiles the module immediately on creation, so we can't
334        * obtain the target data from it.  Instead we create a target data layout
335        * from a string.
336        *
337        * The produced layout strings are not precisely the same, but should make
338        * no difference for the kind of optimization passes we run.
339        *
340        * For reference this is the layout string on x64:
341        *
342        *   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
343        *
344        * See also:
345        * - http://llvm.org/docs/LangRef.html#datalayout
346        */
347 
348       {
349          const unsigned pointer_size = 8 * sizeof(void *);
350          char layout[512];
351          util_snprintf(layout, sizeof layout, "%c-p:%u:%u:%u-i64:64:64-a0:0:%u-s0:%u:%u",
352 #ifdef PIPE_ARCH_LITTLE_ENDIAN
353                        'e', // little endian
354 #else
355                        'E', // big endian
356 #endif
357                        pointer_size, pointer_size, pointer_size, // pointer size, abi alignment, preferred alignment
358                        pointer_size, // aggregate preferred alignment
359                        pointer_size, pointer_size); // stack objects abi alignment, preferred alignment
360 
361          gallivm->target = LLVMCreateTargetData(layout);
362          if (!gallivm->target) {
363             return FALSE;
364          }
365       }
366    }
367 
368    if (!create_pass_manager(gallivm))
369       goto fail;
370 
371    return TRUE;
372 
373 fail:
374    gallivm_free_ir(gallivm);
375    gallivm_free_code(gallivm);
376    return FALSE;
377 }
378 
379 
380 boolean
lp_build_init(void)381 lp_build_init(void)
382 {
383    if (gallivm_initialized)
384       return TRUE;
385 
386 
387    /* LLVMLinkIn* are no-ops at runtime.  They just ensure the respective
388     * component is linked at buildtime, which is sufficient for its static
389     * constructors to be called at load time.
390     */
391 #if defined(USE_MCJIT)
392 #  if USE_MCJIT
393       LLVMLinkInMCJIT();
394 #  else
395       LLVMLinkInJIT();
396 #  endif
397 #else
398    use_mcjit = debug_get_bool_option("GALLIVM_MCJIT", FALSE);
399    LLVMLinkInJIT();
400    LLVMLinkInMCJIT();
401 #endif
402 
403 #ifdef DEBUG
404    gallivm_debug = debug_get_option_gallivm_debug();
405 #endif
406 
407    lp_set_target_options();
408 
409    util_cpu_detect();
410 
411    /* For simulating less capable machines */
412 #ifdef DEBUG
413    if (debug_get_bool_option("LP_FORCE_SSE2", FALSE)) {
414       assert(util_cpu_caps.has_sse2);
415       util_cpu_caps.has_sse3 = 0;
416       util_cpu_caps.has_ssse3 = 0;
417       util_cpu_caps.has_sse4_1 = 0;
418       util_cpu_caps.has_sse4_2 = 0;
419       util_cpu_caps.has_avx = 0;
420       util_cpu_caps.has_avx2 = 0;
421       util_cpu_caps.has_f16c = 0;
422       util_cpu_caps.has_fma = 0;
423    }
424 #endif
425 
426    /* AMD Bulldozer AVX's throughput is the same as SSE2; and because using
427     * 8-wide vector needs more floating ops than 4-wide (due to padding), it is
428     * actually more efficient to use 4-wide vectors on this processor.
429     *
430     * See also:
431     * - http://www.anandtech.com/show/4955/the-bulldozer-review-amd-fx8150-tested/2
432     */
433    if (util_cpu_caps.has_avx &&
434        util_cpu_caps.has_intel) {
435       lp_native_vector_width = 256;
436    } else {
437       /* Leave it at 128, even when no SIMD extensions are available.
438        * Really needs to be a multiple of 128 so can fit 4 floats.
439        */
440       lp_native_vector_width = 128;
441    }
442 
443    lp_native_vector_width = debug_get_num_option("LP_NATIVE_VECTOR_WIDTH",
444                                                  lp_native_vector_width);
445 
446    if (lp_native_vector_width <= 128) {
447       /* Hide AVX support, as often LLVM AVX intrinsics are only guarded by
448        * "util_cpu_caps.has_avx" predicate, and lack the
449        * "lp_native_vector_width > 128" predicate. And also to ensure a more
450        * consistent behavior, allowing one to test SSE2 on AVX machines.
451        * XXX: should not play games with util_cpu_caps directly as it might
452        * get used for other things outside llvm too.
453        */
454       util_cpu_caps.has_avx = 0;
455       util_cpu_caps.has_avx2 = 0;
456       util_cpu_caps.has_f16c = 0;
457       util_cpu_caps.has_fma = 0;
458    }
459    if (HAVE_LLVM < 0x0304 || !use_mcjit) {
460       /* AVX2 support has only been tested with LLVM 3.4, and it requires
461        * MCJIT. */
462       util_cpu_caps.has_avx2 = 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 *
gallivm_create(const char * name,LLVMContextRef context)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
gallivm_destroy(struct gallivm_state * gallivm)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
gallivm_verify_function(struct gallivm_state * gallivm,LLVMValueRef func)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
gallivm_compile_module(struct gallivm_state * gallivm)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    if (gallivm_debug & GALLIVM_DEBUG_PERF)
569       time_begin = os_time_get();
570 
571    /* Run optimization passes */
572    LLVMInitializeFunctionPassManager(gallivm->passmgr);
573    func = LLVMGetFirstFunction(gallivm->module);
574    while (func) {
575       if (0) {
576          debug_printf("optimizing func %s...\n", LLVMGetValueName(func));
577       }
578 
579    /* Disable frame pointer omission on debug/profile builds */
580    /* XXX: And workaround http://llvm.org/PR21435 */
581 #if HAVE_LLVM >= 0x0307 && \
582     (defined(DEBUG) || defined(PROFILE) || \
583      defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64))
584       LLVMAddTargetDependentFunctionAttr(func, "no-frame-pointer-elim", "true");
585       LLVMAddTargetDependentFunctionAttr(func, "no-frame-pointer-elim-non-leaf", "true");
586 #endif
587 
588       LLVMRunFunctionPassManager(gallivm->passmgr, func);
589       func = LLVMGetNextFunction(func);
590    }
591    LLVMFinalizeFunctionPassManager(gallivm->passmgr);
592 
593    if (gallivm_debug & GALLIVM_DEBUG_PERF) {
594       int64_t time_end = os_time_get();
595       int time_msec = (int)(time_end - time_begin) / 1000;
596       assert(gallivm->module_name);
597       debug_printf("optimizing module %s took %d msec\n",
598                    gallivm->module_name, time_msec);
599    }
600 
601    /* Dump byte code to a file */
602    if (gallivm_debug & GALLIVM_DEBUG_DUMP_BC) {
603       char filename[256];
604       assert(gallivm->module_name);
605       util_snprintf(filename, sizeof(filename), "ir_%s.bc", gallivm->module_name);
606       LLVMWriteBitcodeToFile(gallivm->module, filename);
607       debug_printf("%s written\n", filename);
608       debug_printf("Invoke as \"llc %s%s -o - %s\"\n",
609                    (HAVE_LLVM >= 0x0305) ? "[-mcpu=<-mcpu option>] " : "",
610                    "[-mattr=<-mattr option(s)>]",
611                    filename);
612    }
613 
614    if (use_mcjit) {
615       /* Setting the module's DataLayout to an empty string will cause the
616        * ExecutionEngine to copy to the DataLayout string from its target
617        * machine to the module.  As of LLVM 3.8 the module and the execution
618        * engine are required to have the same DataLayout.
619        *
620        * We must make sure we do this after running the optimization passes,
621        * because those passes need a correct datalayout string.  For example,
622        * if those optimization passes see an empty datalayout, they will assume
623        * this is a little endian target and will do optimizations that break big
624        * endian machines.
625        *
626        * TODO: This is just a temporary work-around.  The correct solution is
627        * for gallivm_init_state() to create a TargetMachine and pull the
628        * DataLayout from there.  Currently, the TargetMachine used by llvmpipe
629        * is being implicitly created by the EngineBuilder in
630        * lp_build_create_jit_compiler_for_module()
631        */
632       LLVMSetDataLayout(gallivm->module, "");
633       assert(!gallivm->engine);
634       if (!init_gallivm_engine(gallivm)) {
635          assert(0);
636       }
637    }
638    assert(gallivm->engine);
639 
640    ++gallivm->compiled;
641 
642    if (gallivm_debug & GALLIVM_DEBUG_ASM) {
643       LLVMValueRef llvm_func = LLVMGetFirstFunction(gallivm->module);
644 
645       while (llvm_func) {
646          /*
647           * Need to filter out functions which don't have an implementation,
648           * such as the intrinsics. May not be sufficient in case of IPO?
649           * LLVMGetPointerToGlobal() will abort otherwise.
650           */
651          if (!LLVMIsDeclaration(llvm_func)) {
652             void *func_code = LLVMGetPointerToGlobal(gallivm->engine, llvm_func);
653             lp_disassemble(llvm_func, func_code);
654          }
655          llvm_func = LLVMGetNextFunction(llvm_func);
656       }
657    }
658 
659 #if defined(PROFILE)
660    {
661       LLVMValueRef llvm_func = LLVMGetFirstFunction(gallivm->module);
662 
663       while (llvm_func) {
664          if (!LLVMIsDeclaration(llvm_func)) {
665             void *func_code = LLVMGetPointerToGlobal(gallivm->engine, llvm_func);
666             lp_profile(llvm_func, func_code);
667          }
668          llvm_func = LLVMGetNextFunction(llvm_func);
669       }
670    }
671 #endif
672 }
673 
674 
675 
676 func_pointer
gallivm_jit_function(struct gallivm_state * gallivm,LLVMValueRef func)677 gallivm_jit_function(struct gallivm_state *gallivm,
678                      LLVMValueRef func)
679 {
680    void *code;
681    func_pointer jit_func;
682    int64_t time_begin = 0;
683 
684    assert(gallivm->compiled);
685    assert(gallivm->engine);
686 
687    if (gallivm_debug & GALLIVM_DEBUG_PERF)
688       time_begin = os_time_get();
689 
690    code = LLVMGetPointerToGlobal(gallivm->engine, func);
691    assert(code);
692    jit_func = pointer_to_func(code);
693 
694    if (gallivm_debug & GALLIVM_DEBUG_PERF) {
695       int64_t time_end = os_time_get();
696       int time_msec = (int)(time_end - time_begin) / 1000;
697       debug_printf("   jitting func %s took %d msec\n",
698                    LLVMGetValueName(func), time_msec);
699    }
700 
701    return jit_func;
702 }
703