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