1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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 "os/os_time.h"
37 #include "lp_bld_debug.h"
38 #include "lp_bld_misc.h"
39 #include "lp_bld_init.h"
41 #include <llvm-c/Analysis.h>
42 #include <llvm-c/Transforms/Scalar.h>
43 #include <llvm-c/BitWriter.h>
46 /* Only MCJIT is available as of LLVM SVN r216982 */
47 #if HAVE_LLVM >= 0x0306
49 #elif defined(PIPE_ARCH_PPC_64) || defined(PIPE_ARCH_S390) || defined(PIPE_ARCH_ARM) || defined(PIPE_ARCH_AARCH64)
53 #if defined(USE_MCJIT)
54 static const bool use_mcjit
= USE_MCJIT
;
56 static bool use_mcjit
= FALSE
;
61 unsigned gallivm_debug
= 0;
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
},
77 DEBUG_GET_ONCE_FLAGS_OPTION(gallivm_debug
, "GALLIVM_DEBUG", lp_bld_debug_flags
, 0)
81 static boolean gallivm_initialized
= FALSE
;
83 unsigned lp_native_vector_width
;
87 * Optimization values are:
90 * - 2: Default (-O2, -Os)
91 * - 3: Aggressive (-O3)
93 * See also CodeGenOpt::Level in llvm/Target/TargetMachine.h
95 enum LLVM_CodeGenOpt_Level
{
104 * Create the LLVM (optimization) pass manager and install
105 * relevant optimization passes.
106 * \return TRUE for success, FALSE for failure
109 create_pass_manager(struct gallivm_state
*gallivm
)
111 assert(!gallivm
->passmgr
);
112 assert(gallivm
->target
);
114 gallivm
->passmgr
= LLVMCreateFunctionPassManagerForModule(gallivm
->module
);
115 if (!gallivm
->passmgr
)
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.
123 #if HAVE_LLVM < 0x0309
124 // Old versions of LLVM get the DataLayout from the pass manager.
125 LLVMAddTargetData(gallivm
->target
, gallivm
->passmgr
);
128 /* Setting the module's DataLayout to an empty string will cause the
129 * ExecutionEngine to copy to the DataLayout string from its target
130 * machine to the module. As of LLVM 3.8 the module and the execution
131 * engine are required to have the same DataLayout.
133 * TODO: This is just a temporary work-around. The correct solution is
134 * for gallivm_init_state() to create a TargetMachine and pull the
135 * DataLayout from there. Currently, the TargetMachine used by llvmpipe
136 * is being implicitly created by the EngineBuilder in
137 * lp_build_create_jit_compiler_for_module()
140 #if HAVE_LLVM < 0x0308
143 // New ones from the Module.
144 td_str
= LLVMCopyStringRepOfTargetData(gallivm
->target
);
145 LLVMSetDataLayout(gallivm
->module
, td_str
);
149 LLVMSetDataLayout(gallivm
->module
, "");
152 if ((gallivm_debug
& GALLIVM_DEBUG_NO_OPT
) == 0) {
153 /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
154 * but there are more on SVN.
155 * TODO: Add more passes.
157 LLVMAddScalarReplAggregatesPass(gallivm
->passmgr
);
158 LLVMAddLICMPass(gallivm
->passmgr
);
159 LLVMAddCFGSimplificationPass(gallivm
->passmgr
);
160 LLVMAddReassociatePass(gallivm
->passmgr
);
161 LLVMAddPromoteMemoryToRegisterPass(gallivm
->passmgr
);
162 LLVMAddConstantPropagationPass(gallivm
->passmgr
);
163 LLVMAddInstructionCombiningPass(gallivm
->passmgr
);
164 LLVMAddGVNPass(gallivm
->passmgr
);
167 /* We need at least this pass to prevent the backends to fail in
170 LLVMAddPromoteMemoryToRegisterPass(gallivm
->passmgr
);
178 * Free gallivm object's LLVM allocations, but not any generated code
179 * nor the gallivm object itself.
182 gallivm_free_ir(struct gallivm_state
*gallivm
)
184 if (gallivm
->passmgr
) {
185 LLVMDisposePassManager(gallivm
->passmgr
);
188 if (gallivm
->engine
) {
189 /* This will already destroy any associated module */
190 LLVMDisposeExecutionEngine(gallivm
->engine
);
191 } else if (gallivm
->module
) {
192 LLVMDisposeModule(gallivm
->module
);
195 FREE(gallivm
->module_name
);
198 /* Don't free the TargetData, it's owned by the exec engine */
200 if (gallivm
->target
) {
201 LLVMDisposeTargetData(gallivm
->target
);
205 if (gallivm
->builder
)
206 LLVMDisposeBuilder(gallivm
->builder
);
208 /* The LLVMContext should be owned by the parent of gallivm. */
210 gallivm
->engine
= NULL
;
211 gallivm
->target
= NULL
;
212 gallivm
->module
= NULL
;
213 gallivm
->module_name
= NULL
;
214 gallivm
->passmgr
= NULL
;
215 gallivm
->context
= NULL
;
216 gallivm
->builder
= NULL
;
221 * Free LLVM-generated code. Should be done AFTER gallivm_free_ir().
224 gallivm_free_code(struct gallivm_state
*gallivm
)
226 assert(!gallivm
->module
);
227 assert(!gallivm
->engine
);
228 lp_free_generated_code(gallivm
->code
);
229 gallivm
->code
= NULL
;
230 lp_free_memory_manager(gallivm
->memorymgr
);
231 gallivm
->memorymgr
= NULL
;
236 init_gallivm_engine(struct gallivm_state
*gallivm
)
239 enum LLVM_CodeGenOpt_Level optlevel
;
243 if (gallivm_debug
& GALLIVM_DEBUG_NO_OPT
) {
250 ret
= lp_build_create_jit_compiler_for_module(&gallivm
->engine
,
258 _debug_printf("%s\n", error
);
259 LLVMDisposeMessage(error
);
265 gallivm
->target
= LLVMGetExecutionEngineTargetData(gallivm
->engine
);
266 if (!gallivm
->target
)
271 * Dump the data layout strings.
274 LLVMTargetDataRef target
= LLVMGetExecutionEngineTargetData(gallivm
->engine
);
276 char *engine_data_layout
;
278 data_layout
= LLVMCopyStringRepOfTargetData(gallivm
->target
);
279 engine_data_layout
= LLVMCopyStringRepOfTargetData(target
);
282 debug_printf("module target data = %s\n", data_layout
);
283 debug_printf("engine target data = %s\n", engine_data_layout
);
287 free(engine_data_layout
);
299 * Allocate gallivm LLVM objects.
300 * \return TRUE for success, FALSE for failure
303 init_gallivm_state(struct gallivm_state
*gallivm
, const char *name
,
304 LLVMContextRef context
)
306 assert(!gallivm
->context
);
307 assert(!gallivm
->module
);
309 if (!lp_build_init())
312 gallivm
->context
= context
;
314 if (!gallivm
->context
)
317 gallivm
->module_name
= NULL
;
319 size_t size
= strlen(name
) + 1;
320 gallivm
->module_name
= MALLOC(size
);
321 if (gallivm
->module_name
) {
322 memcpy(gallivm
->module_name
, name
, size
);
326 gallivm
->module
= LLVMModuleCreateWithNameInContext(name
,
328 if (!gallivm
->module
)
331 gallivm
->builder
= LLVMCreateBuilderInContext(gallivm
->context
);
332 if (!gallivm
->builder
)
335 gallivm
->memorymgr
= lp_get_default_memory_manager();
336 if (!gallivm
->memorymgr
)
339 /* FIXME: MC-JIT only allows compiling one module at a time, and it must be
340 * complete when MC-JIT is created. So defer the MC-JIT engine creation for
344 if (!init_gallivm_engine(gallivm
)) {
349 * MC-JIT engine compiles the module immediately on creation, so we can't
350 * obtain the target data from it. Instead we create a target data layout
353 * The produced layout strings are not precisely the same, but should make
354 * no difference for the kind of optimization passes we run.
356 * For reference this is the layout string on x64:
358 * 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
361 * - http://llvm.org/docs/LangRef.html#datalayout
365 const unsigned pointer_size
= 8 * sizeof(void *);
367 util_snprintf(layout
, sizeof layout
, "%c-p:%u:%u:%u-i64:64:64-a0:0:%u-s0:%u:%u",
368 #ifdef PIPE_ARCH_LITTLE_ENDIAN
369 'e', // little endian
373 pointer_size
, pointer_size
, pointer_size
, // pointer size, abi alignment, preferred alignment
374 pointer_size
, // aggregate preferred alignment
375 pointer_size
, pointer_size
); // stack objects abi alignment, preferred alignment
377 gallivm
->target
= LLVMCreateTargetData(layout
);
378 if (!gallivm
->target
) {
384 if (!create_pass_manager(gallivm
))
390 gallivm_free_ir(gallivm
);
391 gallivm_free_code(gallivm
);
399 if (gallivm_initialized
)
403 /* LLVMLinkIn* are no-ops at runtime. They just ensure the respective
404 * component is linked at buildtime, which is sufficient for its static
405 * constructors to be called at load time.
407 #if defined(USE_MCJIT)
414 use_mcjit
= debug_get_bool_option("GALLIVM_MCJIT", FALSE
);
420 gallivm_debug
= debug_get_option_gallivm_debug();
423 lp_set_target_options();
427 /* For simulating less capable machines */
429 if (debug_get_bool_option("LP_FORCE_SSE2", FALSE
)) {
430 assert(util_cpu_caps
.has_sse2
);
431 util_cpu_caps
.has_sse3
= 0;
432 util_cpu_caps
.has_ssse3
= 0;
433 util_cpu_caps
.has_sse4_1
= 0;
434 util_cpu_caps
.has_sse4_2
= 0;
435 util_cpu_caps
.has_avx
= 0;
436 util_cpu_caps
.has_avx2
= 0;
437 util_cpu_caps
.has_f16c
= 0;
438 util_cpu_caps
.has_fma
= 0;
442 /* AMD Bulldozer AVX's throughput is the same as SSE2; and because using
443 * 8-wide vector needs more floating ops than 4-wide (due to padding), it is
444 * actually more efficient to use 4-wide vectors on this processor.
447 * - http://www.anandtech.com/show/4955/the-bulldozer-review-amd-fx8150-tested/2
449 if (util_cpu_caps
.has_avx
&&
450 util_cpu_caps
.has_intel
) {
451 lp_native_vector_width
= 256;
453 /* Leave it at 128, even when no SIMD extensions are available.
454 * Really needs to be a multiple of 128 so can fit 4 floats.
456 lp_native_vector_width
= 128;
459 lp_native_vector_width
= debug_get_num_option("LP_NATIVE_VECTOR_WIDTH",
460 lp_native_vector_width
);
462 if (lp_native_vector_width
<= 128) {
463 /* Hide AVX support, as often LLVM AVX intrinsics are only guarded by
464 * "util_cpu_caps.has_avx" predicate, and lack the
465 * "lp_native_vector_width > 128" predicate. And also to ensure a more
466 * consistent behavior, allowing one to test SSE2 on AVX machines.
467 * XXX: should not play games with util_cpu_caps directly as it might
468 * get used for other things outside llvm too.
470 util_cpu_caps
.has_avx
= 0;
471 util_cpu_caps
.has_avx2
= 0;
472 util_cpu_caps
.has_f16c
= 0;
473 util_cpu_caps
.has_fma
= 0;
475 if (HAVE_LLVM
< 0x0304 || !use_mcjit
) {
476 /* AVX2 support has only been tested with LLVM 3.4, and it requires
478 util_cpu_caps
.has_avx2
= 0;
481 #ifdef PIPE_ARCH_PPC_64
482 /* Set the NJ bit in VSCR to 0 so denormalized values are handled as
483 * specified by IEEE standard (PowerISA 2.06 - Section 6.3). This guarantees
484 * that some rounding and half-float to float handling does not round
486 * XXX: should eventually follow same logic on all platforms.
487 * Right now denorms get explicitly disabled (but elsewhere) for x86,
488 * whereas ppc64 explicitly enables them...
490 if (util_cpu_caps
.has_altivec
) {
491 unsigned short mask
[] = { 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
492 0xFFFF, 0xFFFF, 0xFFFE, 0xFFFF };
503 gallivm_initialized
= TRUE
;
511 * Create a new gallivm_state object.
513 struct gallivm_state
*
514 gallivm_create(const char *name
, LLVMContextRef context
)
516 struct gallivm_state
*gallivm
;
518 gallivm
= CALLOC_STRUCT(gallivm_state
);
520 if (!init_gallivm_state(gallivm
, name
, context
)) {
531 * Destroy a gallivm_state object.
534 gallivm_destroy(struct gallivm_state
*gallivm
)
536 gallivm_free_ir(gallivm
);
537 gallivm_free_code(gallivm
);
543 * Validate a function.
544 * Verification is only done with debug builds.
547 gallivm_verify_function(struct gallivm_state
*gallivm
,
550 /* Verify the LLVM IR. If invalid, dump and abort */
552 if (LLVMVerifyFunction(func
, LLVMPrintMessageAction
)) {
553 lp_debug_dump_value(func
);
559 if (gallivm_debug
& GALLIVM_DEBUG_IR
) {
560 /* Print the LLVM IR to stderr */
561 lp_debug_dump_value(func
);
569 * This does IR optimization on all functions in the module.
572 gallivm_compile_module(struct gallivm_state
*gallivm
)
575 int64_t time_begin
= 0;
577 assert(!gallivm
->compiled
);
579 if (gallivm
->builder
) {
580 LLVMDisposeBuilder(gallivm
->builder
);
581 gallivm
->builder
= NULL
;
584 if (gallivm_debug
& GALLIVM_DEBUG_PERF
)
585 time_begin
= os_time_get();
587 /* Run optimization passes */
588 LLVMInitializeFunctionPassManager(gallivm
->passmgr
);
589 func
= LLVMGetFirstFunction(gallivm
->module
);
592 debug_printf("optimizing func %s...\n", LLVMGetValueName(func
));
595 /* Disable frame pointer omission on debug/profile builds */
596 /* XXX: And workaround http://llvm.org/PR21435 */
597 #if HAVE_LLVM >= 0x0307 && \
598 (defined(DEBUG) || defined(PROFILE) || \
599 defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64))
600 LLVMAddTargetDependentFunctionAttr(func
, "no-frame-pointer-elim", "true");
601 LLVMAddTargetDependentFunctionAttr(func
, "no-frame-pointer-elim-non-leaf", "true");
604 LLVMRunFunctionPassManager(gallivm
->passmgr
, func
);
605 func
= LLVMGetNextFunction(func
);
607 LLVMFinalizeFunctionPassManager(gallivm
->passmgr
);
609 if (gallivm_debug
& GALLIVM_DEBUG_PERF
) {
610 int64_t time_end
= os_time_get();
611 int time_msec
= (int)(time_end
- time_begin
) / 1000;
612 assert(gallivm
->module_name
);
613 debug_printf("optimizing module %s took %d msec\n",
614 gallivm
->module_name
, time_msec
);
617 /* Dump byte code to a file */
618 if (gallivm_debug
& GALLIVM_DEBUG_DUMP_BC
) {
620 assert(gallivm
->module_name
);
621 util_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 \"llc %s%s -o - %s\"\n",
625 (HAVE_LLVM
>= 0x0305) ? "[-mcpu=<-mcpu option] " : "",
626 "[-mattr=<-mattr option(s)>]",
631 assert(!gallivm
->engine
);
632 if (!init_gallivm_engine(gallivm
)) {
636 assert(gallivm
->engine
);
640 if (gallivm_debug
& GALLIVM_DEBUG_ASM
) {
641 LLVMValueRef llvm_func
= LLVMGetFirstFunction(gallivm
->module
);
645 * Need to filter out functions which don't have an implementation,
646 * such as the intrinsics. May not be sufficient in case of IPO?
647 * LLVMGetPointerToGlobal() will abort otherwise.
649 if (!LLVMIsDeclaration(llvm_func
)) {
650 void *func_code
= LLVMGetPointerToGlobal(gallivm
->engine
, llvm_func
);
651 lp_disassemble(llvm_func
, func_code
);
653 llvm_func
= LLVMGetNextFunction(llvm_func
);
659 LLVMValueRef llvm_func
= LLVMGetFirstFunction(gallivm
->module
);
662 if (!LLVMIsDeclaration(llvm_func
)) {
663 void *func_code
= LLVMGetPointerToGlobal(gallivm
->engine
, llvm_func
);
664 lp_profile(llvm_func
, func_code
);
666 llvm_func
= LLVMGetNextFunction(llvm_func
);
675 gallivm_jit_function(struct gallivm_state
*gallivm
,
679 func_pointer jit_func
;
680 int64_t time_begin
= 0;
682 assert(gallivm
->compiled
);
683 assert(gallivm
->engine
);
685 if (gallivm_debug
& GALLIVM_DEBUG_PERF
)
686 time_begin
= os_time_get();
688 code
= LLVMGetPointerToGlobal(gallivm
->engine
, func
);
690 jit_func
= pointer_to_func(code
);
692 if (gallivm_debug
& GALLIVM_DEBUG_PERF
) {
693 int64_t time_end
= os_time_get();
694 int time_msec
= (int)(time_end
- time_begin
) / 1000;
695 debug_printf(" jitting func %s took %d msec\n",
696 LLVMGetValueName(func
), time_msec
);