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"
36 #include "lp_bld_debug.h"
37 #include "lp_bld_misc.h"
38 #include "lp_bld_init.h"
40 #include <llvm-c/Analysis.h>
41 #include <llvm-c/Transforms/Scalar.h>
42 #include <llvm-c/BitWriter.h>
46 * AVX is supported in:
47 * - standard JIT from LLVM 3.2 onwards
48 * - MC-JIT from LLVM 3.1
49 * - MC-JIT supports limited OSes (MacOSX and Linux)
50 * - standard JIT in LLVM 3.1, with backports
52 #if defined(PIPE_ARCH_PPC_64) || defined(PIPE_ARCH_S390) || defined(PIPE_ARCH_ARM) || defined(PIPE_ARCH_AARCH64)
55 #elif HAVE_LLVM >= 0x0302 || (HAVE_LLVM == 0x0301 && defined(HAVE_JIT_AVX_SUPPORT))
58 #elif HAVE_LLVM == 0x0301 && (defined(PIPE_OS_LINUX) || defined(PIPE_OS_APPLE))
68 void LLVMLinkInMCJIT();
72 * LLVM has several global caches which pointing/derived from objects
73 * owned by the context, so if we freeing contexts causes
74 * memory leaks and false cache hits when these objects are destroyed.
76 * TODO: For thread safety on multi-threaded OpenGL we should use one LLVM
77 * context per thread, and put them in a pool when threads are destroyed.
79 #define USE_GLOBAL_CONTEXT 1
83 unsigned gallivm_debug
= 0;
85 static const struct debug_named_value lp_bld_debug_flags
[] = {
86 { "tgsi", GALLIVM_DEBUG_TGSI
, NULL
},
87 { "ir", GALLIVM_DEBUG_IR
, NULL
},
88 { "asm", GALLIVM_DEBUG_ASM
, NULL
},
89 { "nopt", GALLIVM_DEBUG_NO_OPT
, NULL
},
90 { "perf", GALLIVM_DEBUG_PERF
, NULL
},
91 { "no_brilinear", GALLIVM_DEBUG_NO_BRILINEAR
, NULL
},
92 { "no_rho_approx", GALLIVM_DEBUG_NO_RHO_APPROX
, NULL
},
93 { "no_quad_lod", GALLIVM_DEBUG_NO_QUAD_LOD
, NULL
},
94 { "gc", GALLIVM_DEBUG_GC
, NULL
},
98 DEBUG_GET_ONCE_FLAGS_OPTION(gallivm_debug
, "GALLIVM_DEBUG", lp_bld_debug_flags
, 0)
102 static boolean gallivm_initialized
= FALSE
;
104 unsigned lp_native_vector_width
;
108 * Optimization values are:
111 * - 2: Default (-O2, -Os)
112 * - 3: Aggressive (-O3)
114 * See also CodeGenOpt::Level in llvm/Target/TargetMachine.h
116 enum LLVM_CodeGenOpt_Level
{
125 * Create the LLVM (optimization) pass manager and install
126 * relevant optimization passes.
127 * \return TRUE for success, FALSE for failure
130 create_pass_manager(struct gallivm_state
*gallivm
)
132 assert(!gallivm
->passmgr
);
133 assert(gallivm
->target
);
135 gallivm
->passmgr
= LLVMCreateFunctionPassManagerForModule(gallivm
->module
);
136 if (!gallivm
->passmgr
)
139 LLVMAddTargetData(gallivm
->target
, gallivm
->passmgr
);
141 if ((gallivm_debug
& GALLIVM_DEBUG_NO_OPT
) == 0) {
142 /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
143 * but there are more on SVN.
144 * TODO: Add more passes.
146 LLVMAddScalarReplAggregatesPass(gallivm
->passmgr
);
147 LLVMAddLICMPass(gallivm
->passmgr
);
148 LLVMAddCFGSimplificationPass(gallivm
->passmgr
);
149 LLVMAddReassociatePass(gallivm
->passmgr
);
150 LLVMAddPromoteMemoryToRegisterPass(gallivm
->passmgr
);
151 LLVMAddConstantPropagationPass(gallivm
->passmgr
);
152 LLVMAddInstructionCombiningPass(gallivm
->passmgr
);
153 LLVMAddGVNPass(gallivm
->passmgr
);
156 /* We need at least this pass to prevent the backends to fail in
159 LLVMAddPromoteMemoryToRegisterPass(gallivm
->passmgr
);
167 * Free gallivm object's LLVM allocations, but not any generated code
168 * nor the gallivm object itself.
171 gallivm_free_ir(struct gallivm_state
*gallivm
)
173 if (gallivm
->passmgr
) {
174 LLVMDisposePassManager(gallivm
->passmgr
);
177 if (gallivm
->engine
) {
178 /* This will already destroy any associated module */
179 LLVMDisposeExecutionEngine(gallivm
->engine
);
180 } else if (gallivm
->module
) {
181 LLVMDisposeModule(gallivm
->module
);
185 /* Don't free the TargetData, it's owned by the exec engine */
187 if (gallivm
->target
) {
188 LLVMDisposeTargetData(gallivm
->target
);
192 if (gallivm
->builder
)
193 LLVMDisposeBuilder(gallivm
->builder
);
195 if (!USE_GLOBAL_CONTEXT
&& gallivm
->context
)
196 LLVMContextDispose(gallivm
->context
);
198 gallivm
->engine
= NULL
;
199 gallivm
->target
= NULL
;
200 gallivm
->module
= NULL
;
201 gallivm
->passmgr
= NULL
;
202 gallivm
->context
= NULL
;
203 gallivm
->builder
= NULL
;
208 * Free LLVM-generated code. Should be done AFTER gallivm_free_ir().
211 gallivm_free_code(struct gallivm_state
*gallivm
)
213 assert(!gallivm
->module
);
214 assert(!gallivm
->engine
);
215 lp_free_generated_code(gallivm
->code
);
216 gallivm
->code
= NULL
;
221 init_gallivm_engine(struct gallivm_state
*gallivm
)
224 enum LLVM_CodeGenOpt_Level optlevel
;
228 if (gallivm_debug
& GALLIVM_DEBUG_NO_OPT
) {
235 ret
= lp_build_create_jit_compiler_for_module(&gallivm
->engine
,
242 _debug_printf("%s\n", error
);
243 LLVMDisposeMessage(error
);
249 gallivm
->target
= LLVMGetExecutionEngineTargetData(gallivm
->engine
);
250 if (!gallivm
->target
)
255 * Dump the data layout strings.
258 LLVMTargetDataRef target
= LLVMGetExecutionEngineTargetData(gallivm
->engine
);
260 char *engine_data_layout
;
262 data_layout
= LLVMCopyStringRepOfTargetData(gallivm
->target
);
263 engine_data_layout
= LLVMCopyStringRepOfTargetData(target
);
266 debug_printf("module target data = %s\n", data_layout
);
267 debug_printf("engine target data = %s\n", engine_data_layout
);
271 free(engine_data_layout
);
283 * Allocate gallivm LLVM objects.
284 * \return TRUE for success, FALSE for failure
287 init_gallivm_state(struct gallivm_state
*gallivm
, const char *name
)
289 assert(!gallivm
->context
);
290 assert(!gallivm
->module
);
294 if (USE_GLOBAL_CONTEXT
) {
295 gallivm
->context
= LLVMGetGlobalContext();
297 gallivm
->context
= LLVMContextCreate();
299 if (!gallivm
->context
)
302 gallivm
->module
= LLVMModuleCreateWithNameInContext(name
,
304 if (!gallivm
->module
)
307 gallivm
->builder
= LLVMCreateBuilderInContext(gallivm
->context
);
308 if (!gallivm
->builder
)
311 /* FIXME: MC-JIT only allows compiling one module at a time, and it must be
312 * complete when MC-JIT is created. So defer the MC-JIT engine creation for
316 if (!init_gallivm_engine(gallivm
)) {
321 * MC-JIT engine compiles the module immediately on creation, so we can't
322 * obtain the target data from it. Instead we create a target data layout
325 * The produced layout strings are not precisely the same, but should make
326 * no difference for the kind of optimization passes we run.
328 * For reference this is the layout string on x64:
330 * 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
333 * - http://llvm.org/docs/LangRef.html#datalayout
337 const unsigned pointer_size
= 8 * sizeof(void *);
339 util_snprintf(layout
, sizeof layout
, "%c-p:%u:%u:%u-i64:64:64-a0:0:%u-s0:%u:%u",
340 #ifdef PIPE_ARCH_LITTLE_ENDIAN
341 'e', // little endian
345 pointer_size
, pointer_size
, pointer_size
, // pointer size, abi alignment, preferred alignment
346 pointer_size
, // aggregate preferred alignment
347 pointer_size
, pointer_size
); // stack objects abi alignment, preferred alignment
349 gallivm
->target
= LLVMCreateTargetData(layout
);
350 if (!gallivm
->target
) {
356 if (!create_pass_manager(gallivm
))
362 gallivm_free_ir(gallivm
);
363 gallivm_free_code(gallivm
);
371 if (gallivm_initialized
)
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
396 util_cpu_caps
.has_avx
&&
397 util_cpu_caps
.has_intel
) {
398 lp_native_vector_width
= 256;
400 /* Leave it at 128, even when no SIMD extensions are available.
401 * Really needs to be a multiple of 128 so can fit 4 floats.
403 lp_native_vector_width
= 128;
406 lp_native_vector_width
= debug_get_num_option("LP_NATIVE_VECTOR_WIDTH",
407 lp_native_vector_width
);
409 if (lp_native_vector_width
<= 128) {
410 /* Hide AVX support, as often LLVM AVX intrinsics are only guarded by
411 * "util_cpu_caps.has_avx" predicate, and lack the
412 * "lp_native_vector_width > 128" predicate. And also to ensure a more
413 * consistent behavior, allowing one to test SSE2 on AVX machines.
414 * XXX: should not play games with util_cpu_caps directly as it might
415 * get used for other things outside llvm too.
417 util_cpu_caps
.has_avx
= 0;
418 util_cpu_caps
.has_avx2
= 0;
423 * note these instructions are VEX-only, so can only emit if we use
424 * avx (don't want to base it on has_avx & has_f16c later as that would
425 * omit it unnecessarily on amd cpus, see above).
427 util_cpu_caps
.has_f16c
= 0;
428 util_cpu_caps
.has_xop
= 0;
431 #ifdef PIPE_ARCH_PPC_64
432 /* Set the NJ bit in VSCR to 0 so denormalized values are handled as
433 * specified by IEEE standard (PowerISA 2.06 - Section 6.3). This guarantees
434 * that some rounding and half-float to float handling does not round
436 * XXX: should eventually follow same logic on all platforms.
437 * Right now denorms get explicitly disabled (but elsewhere) for x86,
438 * whereas ppc64 explicitly enables them...
440 if (util_cpu_caps
.has_altivec
) {
441 unsigned short mask
[] = { 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
442 0xFFFF, 0xFFFF, 0xFFFE, 0xFFFF };
453 gallivm_initialized
= TRUE
;
456 /* For simulating less capable machines */
457 util_cpu_caps
.has_sse3
= 0;
458 util_cpu_caps
.has_ssse3
= 0;
459 util_cpu_caps
.has_sse4_1
= 0;
460 util_cpu_caps
.has_avx
= 0;
461 util_cpu_caps
.has_f16c
= 0;
468 * Create a new gallivm_state object.
470 struct gallivm_state
*
471 gallivm_create(const char *name
)
473 struct gallivm_state
*gallivm
;
475 gallivm
= CALLOC_STRUCT(gallivm_state
);
477 if (!init_gallivm_state(gallivm
, name
)) {
488 * Destroy a gallivm_state object.
491 gallivm_destroy(struct gallivm_state
*gallivm
)
493 gallivm_free_ir(gallivm
);
494 gallivm_free_code(gallivm
);
500 * Validate a function.
503 gallivm_verify_function(struct gallivm_state
*gallivm
,
506 /* Verify the LLVM IR. If invalid, dump and abort */
508 if (LLVMVerifyFunction(func
, LLVMPrintMessageAction
)) {
509 lp_debug_dump_value(func
);
515 if (gallivm_debug
& GALLIVM_DEBUG_IR
) {
516 /* Print the LLVM IR to stderr */
517 lp_debug_dump_value(func
);
524 gallivm_compile_module(struct gallivm_state
*gallivm
)
528 assert(!gallivm
->compiled
);
530 if (gallivm
->builder
) {
531 LLVMDisposeBuilder(gallivm
->builder
);
532 gallivm
->builder
= NULL
;
535 /* Run optimization passes */
536 LLVMInitializeFunctionPassManager(gallivm
->passmgr
);
537 func
= LLVMGetFirstFunction(gallivm
->module
);
540 debug_printf("optimizing %s...\n", LLVMGetValueName(func
));
542 LLVMRunFunctionPassManager(gallivm
->passmgr
, func
);
543 func
= LLVMGetNextFunction(func
);
545 LLVMFinalizeFunctionPassManager(gallivm
->passmgr
);
547 /* Dump byte code to a file */
549 LLVMWriteBitcodeToFile(gallivm
->module
, "llvmpipe.bc");
550 debug_printf("llvmpipe.bc written\n");
551 debug_printf("Invoke as \"llc -o - llvmpipe.bc\"\n");
555 assert(!gallivm
->engine
);
556 if (!init_gallivm_engine(gallivm
)) {
560 assert(gallivm
->engine
);
568 gallivm_jit_function(struct gallivm_state
*gallivm
,
572 func_pointer jit_func
;
574 assert(gallivm
->compiled
);
575 assert(gallivm
->engine
);
577 code
= LLVMGetPointerToGlobal(gallivm
->engine
, func
);
579 jit_func
= pointer_to_func(code
);
581 if (gallivm_debug
& GALLIVM_DEBUG_ASM
) {
582 lp_disassemble(func
, code
);
586 lp_profile(func
, code
);