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/u_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)
56 void LLVMLinkInMCJIT();
60 unsigned gallivm_debug
= 0;
62 static const struct debug_named_value lp_bld_debug_flags
[] = {
63 { "tgsi", GALLIVM_DEBUG_TGSI
, NULL
},
64 { "ir", GALLIVM_DEBUG_IR
, NULL
},
65 { "asm", GALLIVM_DEBUG_ASM
, NULL
},
66 { "nopt", GALLIVM_DEBUG_NO_OPT
, NULL
},
67 { "perf", GALLIVM_DEBUG_PERF
, NULL
},
68 { "no_brilinear", GALLIVM_DEBUG_NO_BRILINEAR
, NULL
},
69 { "no_rho_approx", GALLIVM_DEBUG_NO_RHO_APPROX
, NULL
},
70 { "no_quad_lod", GALLIVM_DEBUG_NO_QUAD_LOD
, NULL
},
71 { "gc", GALLIVM_DEBUG_GC
, NULL
},
75 DEBUG_GET_ONCE_FLAGS_OPTION(gallivm_debug
, "GALLIVM_DEBUG", lp_bld_debug_flags
, 0)
79 static boolean gallivm_initialized
= FALSE
;
81 unsigned lp_native_vector_width
;
85 * Optimization values are:
88 * - 2: Default (-O2, -Os)
89 * - 3: Aggressive (-O3)
91 * See also CodeGenOpt::Level in llvm/Target/TargetMachine.h
93 enum LLVM_CodeGenOpt_Level
{
102 * Create the LLVM (optimization) pass manager and install
103 * relevant optimization passes.
104 * \return TRUE for success, FALSE for failure
107 create_pass_manager(struct gallivm_state
*gallivm
)
110 assert(!gallivm
->passmgr
);
111 assert(gallivm
->target
);
113 gallivm
->passmgr
= LLVMCreateFunctionPassManagerForModule(gallivm
->module
);
114 if (!gallivm
->passmgr
)
117 // Old versions of LLVM get the DataLayout from the pass manager.
118 LLVMAddTargetData(gallivm
->target
, gallivm
->passmgr
);
120 // New ones from the Module.
121 td_str
= LLVMCopyStringRepOfTargetData(gallivm
->target
);
122 LLVMSetDataLayout(gallivm
->module
, td_str
);
125 if ((gallivm_debug
& GALLIVM_DEBUG_NO_OPT
) == 0) {
126 /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
127 * but there are more on SVN.
128 * TODO: Add more passes.
130 LLVMAddScalarReplAggregatesPass(gallivm
->passmgr
);
131 LLVMAddLICMPass(gallivm
->passmgr
);
132 LLVMAddCFGSimplificationPass(gallivm
->passmgr
);
133 LLVMAddReassociatePass(gallivm
->passmgr
);
134 LLVMAddPromoteMemoryToRegisterPass(gallivm
->passmgr
);
135 LLVMAddConstantPropagationPass(gallivm
->passmgr
);
136 LLVMAddInstructionCombiningPass(gallivm
->passmgr
);
137 LLVMAddGVNPass(gallivm
->passmgr
);
140 /* We need at least this pass to prevent the backends to fail in
143 LLVMAddPromoteMemoryToRegisterPass(gallivm
->passmgr
);
151 * Free gallivm object's LLVM allocations, but not any generated code
152 * nor the gallivm object itself.
155 gallivm_free_ir(struct gallivm_state
*gallivm
)
157 if (gallivm
->passmgr
) {
158 LLVMDisposePassManager(gallivm
->passmgr
);
161 if (gallivm
->engine
) {
162 /* This will already destroy any associated module */
163 LLVMDisposeExecutionEngine(gallivm
->engine
);
164 } else if (gallivm
->module
) {
165 LLVMDisposeModule(gallivm
->module
);
169 /* Don't free the TargetData, it's owned by the exec engine */
171 if (gallivm
->target
) {
172 LLVMDisposeTargetData(gallivm
->target
);
176 if (gallivm
->builder
)
177 LLVMDisposeBuilder(gallivm
->builder
);
179 /* The LLVMContext should be owned by the parent of gallivm. */
181 gallivm
->engine
= NULL
;
182 gallivm
->target
= NULL
;
183 gallivm
->module
= NULL
;
184 gallivm
->passmgr
= NULL
;
185 gallivm
->context
= NULL
;
186 gallivm
->builder
= NULL
;
191 * Free LLVM-generated code. Should be done AFTER gallivm_free_ir().
194 gallivm_free_code(struct gallivm_state
*gallivm
)
196 assert(!gallivm
->module
);
197 assert(!gallivm
->engine
);
198 lp_free_generated_code(gallivm
->code
);
199 gallivm
->code
= NULL
;
200 lp_free_memory_manager(gallivm
->memorymgr
);
201 gallivm
->memorymgr
= NULL
;
206 init_gallivm_engine(struct gallivm_state
*gallivm
)
209 enum LLVM_CodeGenOpt_Level optlevel
;
213 if (gallivm_debug
& GALLIVM_DEBUG_NO_OPT
) {
220 ret
= lp_build_create_jit_compiler_for_module(&gallivm
->engine
,
228 _debug_printf("%s\n", error
);
229 LLVMDisposeMessage(error
);
235 gallivm
->target
= LLVMGetExecutionEngineTargetData(gallivm
->engine
);
236 if (!gallivm
->target
)
241 * Dump the data layout strings.
244 LLVMTargetDataRef target
= LLVMGetExecutionEngineTargetData(gallivm
->engine
);
246 char *engine_data_layout
;
248 data_layout
= LLVMCopyStringRepOfTargetData(gallivm
->target
);
249 engine_data_layout
= LLVMCopyStringRepOfTargetData(target
);
252 debug_printf("module target data = %s\n", data_layout
);
253 debug_printf("engine target data = %s\n", engine_data_layout
);
257 free(engine_data_layout
);
269 * Allocate gallivm LLVM objects.
270 * \return TRUE for success, FALSE for failure
273 init_gallivm_state(struct gallivm_state
*gallivm
, const char *name
,
274 LLVMContextRef context
)
276 assert(!gallivm
->context
);
277 assert(!gallivm
->module
);
279 if (!lp_build_init())
282 gallivm
->context
= context
;
284 if (!gallivm
->context
)
287 gallivm
->module
= LLVMModuleCreateWithNameInContext(name
,
289 if (!gallivm
->module
)
292 gallivm
->builder
= LLVMCreateBuilderInContext(gallivm
->context
);
293 if (!gallivm
->builder
)
296 #if HAVE_LLVM < 0x0306
297 gallivm
->memorymgr
= lp_get_default_memory_manager();
298 if (!gallivm
->memorymgr
)
301 gallivm
->memorymgr
= 0;
304 /* FIXME: MC-JIT only allows compiling one module at a time, and it must be
305 * complete when MC-JIT is created. So defer the MC-JIT engine creation for
309 if (!init_gallivm_engine(gallivm
)) {
314 * MC-JIT engine compiles the module immediately on creation, so we can't
315 * obtain the target data from it. Instead we create a target data layout
318 * The produced layout strings are not precisely the same, but should make
319 * no difference for the kind of optimization passes we run.
321 * For reference this is the layout string on x64:
323 * 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
326 * - http://llvm.org/docs/LangRef.html#datalayout
330 const unsigned pointer_size
= 8 * sizeof(void *);
332 util_snprintf(layout
, sizeof layout
, "%c-p:%u:%u:%u-i64:64:64-a0:0:%u-s0:%u:%u",
333 #ifdef PIPE_ARCH_LITTLE_ENDIAN
334 'e', // little endian
338 pointer_size
, pointer_size
, pointer_size
, // pointer size, abi alignment, preferred alignment
339 pointer_size
, // aggregate preferred alignment
340 pointer_size
, pointer_size
); // stack objects abi alignment, preferred alignment
342 gallivm
->target
= LLVMCreateTargetData(layout
);
343 if (!gallivm
->target
) {
349 if (!create_pass_manager(gallivm
))
355 gallivm_free_ir(gallivm
);
356 gallivm_free_code(gallivm
);
364 if (gallivm_initialized
)
367 /* XXX: Remove this once lp_bld_misc.cpp has been adapted to the removal
368 * of JITMemoryManager
370 #if HAVE_LLVM >= 0x0306
375 gallivm_debug
= debug_get_option_gallivm_debug();
378 lp_set_target_options();
388 /* AMD Bulldozer AVX's throughput is the same as SSE2; and because using
389 * 8-wide vector needs more floating ops than 4-wide (due to padding), it is
390 * actually more efficient to use 4-wide vectors on this processor.
393 * - http://www.anandtech.com/show/4955/the-bulldozer-review-amd-fx8150-tested/2
395 if (util_cpu_caps
.has_avx
&&
396 util_cpu_caps
.has_intel
) {
397 lp_native_vector_width
= 256;
399 /* Leave it at 128, even when no SIMD extensions are available.
400 * Really needs to be a multiple of 128 so can fit 4 floats.
402 lp_native_vector_width
= 128;
405 lp_native_vector_width
= debug_get_num_option("LP_NATIVE_VECTOR_WIDTH",
406 lp_native_vector_width
);
408 if (lp_native_vector_width
<= 128) {
409 /* Hide AVX support, as often LLVM AVX intrinsics are only guarded by
410 * "util_cpu_caps.has_avx" predicate, and lack the
411 * "lp_native_vector_width > 128" predicate. And also to ensure a more
412 * consistent behavior, allowing one to test SSE2 on AVX machines.
413 * XXX: should not play games with util_cpu_caps directly as it might
414 * get used for other things outside llvm too.
416 util_cpu_caps
.has_avx
= 0;
417 util_cpu_caps
.has_avx2
= 0;
420 #ifdef PIPE_ARCH_PPC_64
421 /* Set the NJ bit in VSCR to 0 so denormalized values are handled as
422 * specified by IEEE standard (PowerISA 2.06 - Section 6.3). This guarantees
423 * that some rounding and half-float to float handling does not round
425 * XXX: should eventually follow same logic on all platforms.
426 * Right now denorms get explicitly disabled (but elsewhere) for x86,
427 * whereas ppc64 explicitly enables them...
429 if (util_cpu_caps
.has_altivec
) {
430 unsigned short mask
[] = { 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
431 0xFFFF, 0xFFFF, 0xFFFE, 0xFFFF };
442 gallivm_initialized
= TRUE
;
445 /* For simulating less capable machines */
446 util_cpu_caps
.has_sse3
= 0;
447 util_cpu_caps
.has_ssse3
= 0;
448 util_cpu_caps
.has_sse4_1
= 0;
449 util_cpu_caps
.has_avx
= 0;
450 util_cpu_caps
.has_f16c
= 0;
459 * Create a new gallivm_state object.
461 struct gallivm_state
*
462 gallivm_create(const char *name
, LLVMContextRef context
)
464 struct gallivm_state
*gallivm
;
466 gallivm
= CALLOC_STRUCT(gallivm_state
);
468 if (!init_gallivm_state(gallivm
, name
, context
)) {
479 * Destroy a gallivm_state object.
482 gallivm_destroy(struct gallivm_state
*gallivm
)
484 gallivm_free_ir(gallivm
);
485 gallivm_free_code(gallivm
);
491 * Validate a function.
492 * Verification is only done with debug builds.
495 gallivm_verify_function(struct gallivm_state
*gallivm
,
498 /* Verify the LLVM IR. If invalid, dump and abort */
500 if (LLVMVerifyFunction(func
, LLVMPrintMessageAction
)) {
501 lp_debug_dump_value(func
);
507 if (gallivm_debug
& GALLIVM_DEBUG_IR
) {
508 /* Print the LLVM IR to stderr */
509 lp_debug_dump_value(func
);
517 * This does IR optimization on all functions in the module.
520 gallivm_compile_module(struct gallivm_state
*gallivm
)
525 assert(!gallivm
->compiled
);
527 if (gallivm
->builder
) {
528 LLVMDisposeBuilder(gallivm
->builder
);
529 gallivm
->builder
= NULL
;
532 if (gallivm_debug
& GALLIVM_DEBUG_PERF
)
533 time_begin
= os_time_get();
535 /* Run optimization passes */
536 LLVMInitializeFunctionPassManager(gallivm
->passmgr
);
537 func
= LLVMGetFirstFunction(gallivm
->module
);
540 debug_printf("optimizing func %s...\n", LLVMGetValueName(func
));
542 LLVMRunFunctionPassManager(gallivm
->passmgr
, func
);
543 func
= LLVMGetNextFunction(func
);
545 LLVMFinalizeFunctionPassManager(gallivm
->passmgr
);
547 if (gallivm_debug
& GALLIVM_DEBUG_PERF
) {
548 int64_t time_end
= os_time_get();
549 int time_msec
= (int)(time_end
- time_begin
) / 1000;
550 debug_printf("optimizing module %s took %d msec\n",
551 lp_get_module_id(gallivm
->module
), time_msec
);
554 /* Dump byte code to a file */
556 LLVMWriteBitcodeToFile(gallivm
->module
, "llvmpipe.bc");
557 debug_printf("llvmpipe.bc written\n");
558 debug_printf("Invoke as \"llc -o - llvmpipe.bc\"\n");
562 assert(!gallivm
->engine
);
563 if (!init_gallivm_engine(gallivm
)) {
567 assert(gallivm
->engine
);
575 gallivm_jit_function(struct gallivm_state
*gallivm
,
579 func_pointer jit_func
;
581 assert(gallivm
->compiled
);
582 assert(gallivm
->engine
);
584 code
= LLVMGetPointerToGlobal(gallivm
->engine
, func
);
586 jit_func
= pointer_to_func(code
);
588 if (gallivm_debug
& GALLIVM_DEBUG_ASM
) {
589 lp_disassemble(func
, code
);
593 lp_profile(func
, code
);