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 if (HAVE_AVX && 438 util_cpu_caps.has_avx) { 439 lp_native_vector_width = 256; 440 } else { 441 /* Leave it at 128, even when no SIMD extensions are available. 442 * Really needs to be a multiple of 128 so can fit 4 floats. 443 */ 444 lp_native_vector_width = 128; 445 } 446 447 lp_native_vector_width = debug_get_num_option("LP_NATIVE_VECTOR_WIDTH", 448 lp_native_vector_width); 449 450 gallivm_initialized = TRUE; 451 452 #if 0 453 /* For simulating less capable machines */ 454 util_cpu_caps.has_sse3 = 0; 455 util_cpu_caps.has_ssse3 = 0; 456 util_cpu_caps.has_sse4_1 = 0; 457 #endif 458 } 459 460 461 462 /** 463 * Create a new gallivm_state object. 464 * Note that we return a singleton. 465 */ 466 struct gallivm_state * 467 gallivm_create(void) 468 { 469 struct gallivm_state *gallivm; 470 471 #if HAVE_LLVM <= 0x206 472 if (GlobalGallivm) { 473 return GlobalGallivm; 474 } 475 #endif 476 477 gallivm = CALLOC_STRUCT(gallivm_state); 478 if (gallivm) { 479 if (!init_gallivm_state(gallivm)) { 480 FREE(gallivm); 481 gallivm = NULL; 482 } 483 } 484 485 #if HAVE_LLVM <= 0x206 486 GlobalGallivm = gallivm; 487 #endif 488 489 return gallivm; 490 } 491 492 493 /** 494 * Destroy a gallivm_state object. 495 */ 496 void 497 gallivm_destroy(struct gallivm_state *gallivm) 498 { 499 #if HAVE_LLVM <= 0x0206 500 /* No-op: don't destroy the singleton */ 501 (void) gallivm; 502 #else 503 free_gallivm_state(gallivm); 504 FREE(gallivm); 505 #endif 506 } 507 508 509 /** 510 * Validate and optimze a function. 511 */ 512 static void 513 gallivm_optimize_function(struct gallivm_state *gallivm, 514 LLVMValueRef func) 515 { 516 if (0) { 517 debug_printf("optimizing %s...\n", LLVMGetValueName(func)); 518 } 519 520 assert(gallivm->passmgr); 521 522 /* Apply optimizations to LLVM IR */ 523 LLVMRunFunctionPassManager(gallivm->passmgr, func); 524 525 if (0) { 526 if (gallivm_debug & GALLIVM_DEBUG_IR) { 527 /* Print the LLVM IR to stderr */ 528 lp_debug_dump_value(func); 529 debug_printf("\n"); 530 } 531 } 532 } 533 534 535 /** 536 * Validate a function. 537 */ 538 void 539 gallivm_verify_function(struct gallivm_state *gallivm, 540 LLVMValueRef func) 541 { 542 /* Verify the LLVM IR. If invalid, dump and abort */ 543 #ifdef DEBUG 544 if (LLVMVerifyFunction(func, LLVMPrintMessageAction)) { 545 lp_debug_dump_value(func); 546 assert(0); 547 return; 548 } 549 #endif 550 551 gallivm_optimize_function(gallivm, func); 552 553 if (gallivm_debug & GALLIVM_DEBUG_IR) { 554 /* Print the LLVM IR to stderr */ 555 lp_debug_dump_value(func); 556 debug_printf("\n"); 557 } 558 } 559 560 561 void 562 gallivm_compile_module(struct gallivm_state *gallivm) 563 { 564 #if HAVE_LLVM > 0x206 565 assert(!gallivm->compiled); 566 #endif 567 568 /* Dump byte code to a file */ 569 if (0) { 570 LLVMWriteBitcodeToFile(gallivm->module, "llvmpipe.bc"); 571 debug_printf("llvmpipe.bc written\n"); 572 debug_printf("Invoke as \"llc -o - llvmpipe.bc\"\n"); 573 } 574 575 #if USE_MCJIT 576 assert(!gallivm->engine); 577 if (!init_gallivm_engine(gallivm)) { 578 assert(0); 579 } 580 #endif 581 assert(gallivm->engine); 582 583 ++gallivm->compiled; 584 } 585 586 587 func_pointer 588 gallivm_jit_function(struct gallivm_state *gallivm, 589 LLVMValueRef func) 590 { 591 void *code; 592 func_pointer jit_func; 593 594 assert(gallivm->compiled); 595 assert(gallivm->engine); 596 597 code = LLVMGetPointerToGlobal(gallivm->engine, func); 598 assert(code); 599 jit_func = pointer_to_func(code); 600 601 if (gallivm_debug & GALLIVM_DEBUG_ASM) { 602 lp_disassemble(code); 603 } 604 605 /* Free the function body to save memory */ 606 lp_func_delete_body(func); 607 608 return jit_func; 609 } 610 611 612 /** 613 * Free the function (and its machine code). 614 */ 615 void 616 gallivm_free_function(struct gallivm_state *gallivm, 617 LLVMValueRef func, 618 const void *code) 619 { 620 #if !USE_MCJIT 621 if (code) { 622 LLVMFreeMachineCodeForFunction(gallivm->engine, func); 623 } 624 625 LLVMDeleteFunction(func); 626 #endif 627 } 628