gallivm,llvmpipe: Use 4-wide vectors on AMD Bulldozer.
[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/u_simple_list.h"
35 #include "lp_bld.h"
36 #include "lp_bld_debug.h"
37 #include "lp_bld_misc.h"
38 #include "lp_bld_init.h"
39
40 #include <llvm-c/Analysis.h>
41 #include <llvm-c/Transforms/Scalar.h>
42 #include <llvm-c/BitWriter.h>
43
44
45 /**
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
51 */
52 #if HAVE_LLVM >= 0x0302 || (HAVE_LLVM == 0x0301 && defined(HAVE_JIT_AVX_SUPPORT))
53 # define USE_MCJIT 0
54 # define HAVE_AVX 1
55 #elif HAVE_LLVM == 0x0301 && (defined(PIPE_OS_LINUX) || defined(PIPE_OS_APPLE))
56 # define USE_MCJIT 1
57 # define HAVE_AVX 1
58 #else
59 # define USE_MCJIT 0
60 # define HAVE_AVX 0
61 #endif
62
63
64 #if USE_MCJIT
65 void LLVMLinkInMCJIT();
66 #endif
67
68
69 #ifdef DEBUG
70 unsigned gallivm_debug = 0;
71
72 static const struct debug_named_value lp_bld_debug_flags[] = {
73 { "tgsi", GALLIVM_DEBUG_TGSI, NULL },
74 { "ir", GALLIVM_DEBUG_IR, NULL },
75 { "asm", GALLIVM_DEBUG_ASM, NULL },
76 { "nopt", GALLIVM_DEBUG_NO_OPT, NULL },
77 { "perf", GALLIVM_DEBUG_PERF, NULL },
78 { "no_brilinear", GALLIVM_DEBUG_NO_BRILINEAR, NULL },
79 { "gc", GALLIVM_DEBUG_GC, NULL },
80 DEBUG_NAMED_VALUE_END
81 };
82
83 DEBUG_GET_ONCE_FLAGS_OPTION(gallivm_debug, "GALLIVM_DEBUG", lp_bld_debug_flags, 0)
84 #endif
85
86
87 static boolean gallivm_initialized = FALSE;
88
89 unsigned lp_native_vector_width;
90
91
92 /*
93 * Optimization values are:
94 * - 0: None (-O0)
95 * - 1: Less (-O1)
96 * - 2: Default (-O2, -Os)
97 * - 3: Aggressive (-O3)
98 *
99 * See also CodeGenOpt::Level in llvm/Target/TargetMachine.h
100 */
101 enum LLVM_CodeGenOpt_Level {
102 #if HAVE_LLVM >= 0x207
103 None, // -O0
104 Less, // -O1
105 Default, // -O2, -Os
106 Aggressive // -O3
107 #else
108 Default,
109 None,
110 Aggressive
111 #endif
112 };
113
114
115 #if HAVE_LLVM <= 0x0206
116 /**
117 * LLVM 2.6 permits only one ExecutionEngine to be created. So use the
118 * same gallivm state everywhere.
119 */
120 static struct gallivm_state *GlobalGallivm = NULL;
121 #endif
122
123
124 /**
125 * Create the LLVM (optimization) pass manager and install
126 * relevant optimization passes.
127 * \return TRUE for success, FALSE for failure
128 */
129 static boolean
130 create_pass_manager(struct gallivm_state *gallivm)
131 {
132 assert(!gallivm->passmgr);
133 assert(gallivm->target);
134
135 gallivm->passmgr = LLVMCreateFunctionPassManager(gallivm->provider);
136 if (!gallivm->passmgr)
137 return FALSE;
138
139 LLVMAddTargetData(gallivm->target, gallivm->passmgr);
140
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.
145 */
146 LLVMAddCFGSimplificationPass(gallivm->passmgr);
147
148 if (HAVE_LLVM >= 0x207 && sizeof(void*) == 4) {
149 /* For LLVM >= 2.7 and 32-bit build, use this order of passes to
150 * avoid generating bad code.
151 * Test with piglit glsl-vs-sqrt-zero test.
152 */
153 LLVMAddConstantPropagationPass(gallivm->passmgr);
154 LLVMAddPromoteMemoryToRegisterPass(gallivm->passmgr);
155 }
156 else {
157 LLVMAddPromoteMemoryToRegisterPass(gallivm->passmgr);
158 LLVMAddConstantPropagationPass(gallivm->passmgr);
159 }
160
161 if (util_cpu_caps.has_sse4_1) {
162 /* FIXME: There is a bug in this pass, whereby the combination
163 * of fptosi and sitofp (necessary for trunc/floor/ceil/round
164 * implementation) somehow becomes invalid code.
165 */
166 LLVMAddInstructionCombiningPass(gallivm->passmgr);
167 }
168 LLVMAddGVNPass(gallivm->passmgr);
169 }
170 else {
171 /* We need at least this pass to prevent the backends to fail in
172 * unexpected ways.
173 */
174 LLVMAddPromoteMemoryToRegisterPass(gallivm->passmgr);
175 }
176
177 return TRUE;
178 }
179
180
181 /**
182 * Free gallivm object's LLVM allocations, but not the gallivm object itself.
183 */
184 static void
185 free_gallivm_state(struct gallivm_state *gallivm)
186 {
187 #if HAVE_LLVM >= 0x207 /* XXX or 0x208? */
188 /* This leads to crashes w/ some versions of LLVM */
189 LLVMModuleRef mod;
190 char *error;
191
192 if (gallivm->engine && gallivm->provider)
193 LLVMRemoveModuleProvider(gallivm->engine, gallivm->provider,
194 &mod, &error);
195 #endif
196
197 if (gallivm->passmgr) {
198 LLVMDisposePassManager(gallivm->passmgr);
199 }
200
201 #if 0
202 /* XXX this seems to crash with all versions of LLVM */
203 if (gallivm->provider)
204 LLVMDisposeModuleProvider(gallivm->provider);
205 #endif
206
207 if (HAVE_LLVM >= 0x207 && gallivm->engine) {
208 /* This will already destroy any associated module */
209 LLVMDisposeExecutionEngine(gallivm->engine);
210 } else {
211 LLVMDisposeModule(gallivm->module);
212 }
213
214 #if !USE_MCJIT
215 /* Don't free the TargetData, it's owned by the exec engine */
216 #else
217 if (gallivm->target) {
218 LLVMDisposeTargetData(gallivm->target);
219 }
220 #endif
221
222 /* Never free the LLVM context.
223 */
224 #if 0
225 if (gallivm->context)
226 LLVMContextDispose(gallivm->context);
227 #endif
228
229 if (gallivm->builder)
230 LLVMDisposeBuilder(gallivm->builder);
231
232 gallivm->engine = NULL;
233 gallivm->target = NULL;
234 gallivm->module = NULL;
235 gallivm->provider = NULL;
236 gallivm->passmgr = NULL;
237 gallivm->context = NULL;
238 gallivm->builder = NULL;
239 }
240
241
242 static boolean
243 init_gallivm_engine(struct gallivm_state *gallivm)
244 {
245 if (1) {
246 /* We can only create one LLVMExecutionEngine (w/ LLVM 2.6 anyway) */
247 enum LLVM_CodeGenOpt_Level optlevel;
248 char *error = NULL;
249 int ret;
250
251 if (gallivm_debug & GALLIVM_DEBUG_NO_OPT) {
252 optlevel = None;
253 }
254 else {
255 optlevel = Default;
256 }
257
258 #if USE_MCJIT
259 ret = lp_build_create_mcjit_compiler_for_module(&gallivm->engine,
260 gallivm->module,
261 (unsigned) optlevel,
262 &error);
263 #else
264 ret = LLVMCreateJITCompiler(&gallivm->engine, gallivm->provider,
265 (unsigned) optlevel, &error);
266 #endif
267 if (ret) {
268 _debug_printf("%s\n", error);
269 LLVMDisposeMessage(error);
270 goto fail;
271 }
272
273 #if defined(DEBUG) || defined(PROFILE)
274 lp_register_oprofile_jit_event_listener(gallivm->engine);
275 #endif
276 }
277
278 LLVMAddModuleProvider(gallivm->engine, gallivm->provider);//new
279
280 #if !USE_MCJIT
281 gallivm->target = LLVMGetExecutionEngineTargetData(gallivm->engine);
282 if (!gallivm->target)
283 goto fail;
284 #else
285 if (0) {
286 /*
287 * Dump the data layout strings.
288 */
289
290 LLVMTargetDataRef target = LLVMGetExecutionEngineTargetData(gallivm->engine);
291 char *data_layout;
292 char *engine_data_layout;
293
294 data_layout = LLVMCopyStringRepOfTargetData(gallivm->target);
295 engine_data_layout = LLVMCopyStringRepOfTargetData(target);
296
297 if (1) {
298 debug_printf("module target data = %s\n", data_layout);
299 debug_printf("engine target data = %s\n", engine_data_layout);
300 }
301
302 free(data_layout);
303 free(engine_data_layout);
304 }
305 #endif
306
307 return TRUE;
308
309 fail:
310 return FALSE;
311 }
312
313
314 /**
315 * Singleton
316 *
317 * We must never free LLVM contexts, because LLVM has several global caches
318 * which pointing/derived from objects owned by the context, causing false
319 * memory leaks and false cache hits when these objects are destroyed.
320 *
321 * TODO: For thread safety on multi-threaded OpenGL we should use one LLVM
322 * context per thread, and put them in a pool when threads are destroyed.
323 */
324 static LLVMContextRef gallivm_context = NULL;
325
326
327 /**
328 * Allocate gallivm LLVM objects.
329 * \return TRUE for success, FALSE for failure
330 */
331 static boolean
332 init_gallivm_state(struct gallivm_state *gallivm)
333 {
334 assert(!gallivm->context);
335 assert(!gallivm->module);
336 assert(!gallivm->provider);
337
338 lp_build_init();
339
340 if (!gallivm_context) {
341 gallivm_context = LLVMContextCreate();
342 }
343 gallivm->context = gallivm_context;
344 if (!gallivm->context)
345 goto fail;
346
347 gallivm->module = LLVMModuleCreateWithNameInContext("gallivm",
348 gallivm->context);
349 if (!gallivm->module)
350 goto fail;
351
352 gallivm->provider =
353 LLVMCreateModuleProviderForExistingModule(gallivm->module);
354 if (!gallivm->provider)
355 goto fail;
356
357 gallivm->builder = LLVMCreateBuilderInContext(gallivm->context);
358 if (!gallivm->builder)
359 goto fail;
360
361 /* FIXME: MC-JIT only allows compiling one module at a time, and it must be
362 * complete when MC-JIT is created. So defer the MC-JIT engine creation for
363 * now.
364 */
365 #if !USE_MCJIT
366 if (!init_gallivm_engine(gallivm)) {
367 goto fail;
368 }
369 #else
370 /*
371 * MC-JIT engine compiles the module immediately on creation, so we can't
372 * obtain the target data from it. Instead we create a target data layout
373 * from a string.
374 *
375 * The produced layout strings are not precisely the same, but should make
376 * no difference for the kind of optimization passes we run.
377 *
378 * For reference this is the layout string on x64:
379 *
380 * 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
381 *
382 * See also:
383 * - http://llvm.org/docs/LangRef.html#datalayout
384 */
385
386 {
387 const unsigned pointer_size = 8 * sizeof(void *);
388 char layout[512];
389 util_snprintf(layout, sizeof layout, "%c-p:%u:%u:%u-i64:64:64-a0:0:%u-s0:%u:%u",
390 #ifdef PIPE_ARCH_LITTLE_ENDIAN
391 'e', // little endian
392 #else
393 'E', // big endian
394 #endif
395 pointer_size, pointer_size, pointer_size, // pointer size, abi alignment, preferred alignment
396 pointer_size, // aggregate preferred alignment
397 pointer_size, pointer_size); // stack objects abi alignment, preferred alignment
398
399 gallivm->target = LLVMCreateTargetData(layout);
400 if (!gallivm->target) {
401 return FALSE;
402 }
403 }
404 #endif
405
406 if (!create_pass_manager(gallivm))
407 goto fail;
408
409 return TRUE;
410
411 fail:
412 free_gallivm_state(gallivm);
413 return FALSE;
414 }
415
416
417 void
418 lp_build_init(void)
419 {
420 if (gallivm_initialized)
421 return;
422
423 #ifdef DEBUG
424 gallivm_debug = debug_get_option_gallivm_debug();
425 #endif
426
427 lp_set_target_options();
428
429 #if USE_MCJIT
430 LLVMLinkInMCJIT();
431 #else
432 LLVMLinkInJIT();
433 #endif
434
435 util_cpu_detect();
436
437 /* AMD Bulldozer AVX's throughput is the same as SSE2; and because using
438 * 8-wide vector needs more floating ops than 4-wide (due to padding), it is
439 * actually more efficient to use 4-wide vectors on this processor.
440 *
441 * See also:
442 * - http://www.anandtech.com/show/4955/the-bulldozer-review-amd-fx8150-tested/2
443 */
444 if (HAVE_AVX &&
445 util_cpu_caps.has_avx &&
446 util_cpu_caps.has_intel) {
447 lp_native_vector_width = 256;
448 } else {
449 /* Leave it at 128, even when no SIMD extensions are available.
450 * Really needs to be a multiple of 128 so can fit 4 floats.
451 */
452 lp_native_vector_width = 128;
453 }
454
455 lp_native_vector_width = debug_get_num_option("LP_NATIVE_VECTOR_WIDTH",
456 lp_native_vector_width);
457
458 gallivm_initialized = TRUE;
459
460 #if 0
461 /* For simulating less capable machines */
462 util_cpu_caps.has_sse3 = 0;
463 util_cpu_caps.has_ssse3 = 0;
464 util_cpu_caps.has_sse4_1 = 0;
465 #endif
466 }
467
468
469
470 /**
471 * Create a new gallivm_state object.
472 * Note that we return a singleton.
473 */
474 struct gallivm_state *
475 gallivm_create(void)
476 {
477 struct gallivm_state *gallivm;
478
479 #if HAVE_LLVM <= 0x206
480 if (GlobalGallivm) {
481 return GlobalGallivm;
482 }
483 #endif
484
485 gallivm = CALLOC_STRUCT(gallivm_state);
486 if (gallivm) {
487 if (!init_gallivm_state(gallivm)) {
488 FREE(gallivm);
489 gallivm = NULL;
490 }
491 }
492
493 #if HAVE_LLVM <= 0x206
494 GlobalGallivm = gallivm;
495 #endif
496
497 return gallivm;
498 }
499
500
501 /**
502 * Destroy a gallivm_state object.
503 */
504 void
505 gallivm_destroy(struct gallivm_state *gallivm)
506 {
507 #if HAVE_LLVM <= 0x0206
508 /* No-op: don't destroy the singleton */
509 (void) gallivm;
510 #else
511 free_gallivm_state(gallivm);
512 FREE(gallivm);
513 #endif
514 }
515
516
517 /**
518 * Validate and optimze a function.
519 */
520 static void
521 gallivm_optimize_function(struct gallivm_state *gallivm,
522 LLVMValueRef func)
523 {
524 if (0) {
525 debug_printf("optimizing %s...\n", LLVMGetValueName(func));
526 }
527
528 assert(gallivm->passmgr);
529
530 /* Apply optimizations to LLVM IR */
531 LLVMRunFunctionPassManager(gallivm->passmgr, func);
532
533 if (0) {
534 if (gallivm_debug & GALLIVM_DEBUG_IR) {
535 /* Print the LLVM IR to stderr */
536 lp_debug_dump_value(func);
537 debug_printf("\n");
538 }
539 }
540 }
541
542
543 /**
544 * Validate a function.
545 */
546 void
547 gallivm_verify_function(struct gallivm_state *gallivm,
548 LLVMValueRef func)
549 {
550 /* Verify the LLVM IR. If invalid, dump and abort */
551 #ifdef DEBUG
552 if (LLVMVerifyFunction(func, LLVMPrintMessageAction)) {
553 lp_debug_dump_value(func);
554 assert(0);
555 return;
556 }
557 #endif
558
559 gallivm_optimize_function(gallivm, func);
560
561 if (gallivm_debug & GALLIVM_DEBUG_IR) {
562 /* Print the LLVM IR to stderr */
563 lp_debug_dump_value(func);
564 debug_printf("\n");
565 }
566 }
567
568
569 void
570 gallivm_compile_module(struct gallivm_state *gallivm)
571 {
572 #if HAVE_LLVM > 0x206
573 assert(!gallivm->compiled);
574 #endif
575
576 /* Dump byte code to a file */
577 if (0) {
578 LLVMWriteBitcodeToFile(gallivm->module, "llvmpipe.bc");
579 debug_printf("llvmpipe.bc written\n");
580 debug_printf("Invoke as \"llc -o - llvmpipe.bc\"\n");
581 }
582
583 #if USE_MCJIT
584 assert(!gallivm->engine);
585 if (!init_gallivm_engine(gallivm)) {
586 assert(0);
587 }
588 #endif
589 assert(gallivm->engine);
590
591 ++gallivm->compiled;
592 }
593
594
595 func_pointer
596 gallivm_jit_function(struct gallivm_state *gallivm,
597 LLVMValueRef func)
598 {
599 void *code;
600 func_pointer jit_func;
601
602 assert(gallivm->compiled);
603 assert(gallivm->engine);
604
605 code = LLVMGetPointerToGlobal(gallivm->engine, func);
606 assert(code);
607 jit_func = pointer_to_func(code);
608
609 if (gallivm_debug & GALLIVM_DEBUG_ASM) {
610 lp_disassemble(code);
611 }
612
613 /* Free the function body to save memory */
614 lp_func_delete_body(func);
615
616 return jit_func;
617 }
618
619
620 /**
621 * Free the function (and its machine code).
622 */
623 void
624 gallivm_free_function(struct gallivm_state *gallivm,
625 LLVMValueRef func,
626 const void *code)
627 {
628 #if !USE_MCJIT
629 if (code) {
630 LLVMFreeMachineCodeForFunction(gallivm->engine, func);
631 }
632
633 LLVMDeleteFunction(func);
634 #endif
635 }