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/os_time.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#include "lp_bld_coro.h" 40#include "lp_bld_printf.h" 41 42#include <llvm/Config/llvm-config.h> 43#include <llvm-c/Analysis.h> 44#include <llvm-c/Transforms/Scalar.h> 45#if LLVM_VERSION_MAJOR >= 7 46#include <llvm-c/Transforms/Utils.h> 47#endif 48#include <llvm-c/BitWriter.h> 49#if GALLIVM_USE_NEW_PASS == 1 50#include <llvm-c/Transforms/PassBuilder.h> 51#elif GALLIVM_HAVE_CORO == 1 52#if LLVM_VERSION_MAJOR <= 8 && (defined(PIPE_ARCH_AARCH64) || defined (PIPE_ARCH_ARM) || defined(PIPE_ARCH_S390) || defined(PIPE_ARCH_MIPS64)) 53#include <llvm-c/Transforms/IPO.h> 54#endif 55#include <llvm-c/Transforms/Coroutines.h> 56#endif 57 58unsigned gallivm_perf = 0; 59 60static const struct debug_named_value lp_bld_perf_flags[] = { 61 { "brilinear", GALLIVM_PERF_BRILINEAR, "enable brilinear optimization" }, 62 { "rho_approx", GALLIVM_PERF_RHO_APPROX, "enable rho_approx optimization" }, 63 { "no_quad_lod", GALLIVM_PERF_NO_QUAD_LOD, "disable quad_lod optimization" }, 64 { "no_aos_sampling", GALLIVM_PERF_NO_AOS_SAMPLING, "disable aos sampling optimization" }, 65 { "nopt", GALLIVM_PERF_NO_OPT, "disable optimization passes to speed up shader compilation" }, 66 DEBUG_NAMED_VALUE_END 67}; 68 69#ifdef DEBUG 70unsigned gallivm_debug = 0; 71 72static 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 { "perf", GALLIVM_DEBUG_PERF, NULL }, 77 { "gc", GALLIVM_DEBUG_GC, NULL }, 78 { "dumpbc", GALLIVM_DEBUG_DUMP_BC, NULL }, 79 DEBUG_NAMED_VALUE_END 80}; 81 82DEBUG_GET_ONCE_FLAGS_OPTION(gallivm_debug, "GALLIVM_DEBUG", lp_bld_debug_flags, 0) 83#endif 84 85 86static boolean gallivm_initialized = FALSE; 87 88unsigned lp_native_vector_width; 89 90 91/* 92 * Optimization values are: 93 * - 0: None (-O0) 94 * - 1: Less (-O1) 95 * - 2: Default (-O2, -Os) 96 * - 3: Aggressive (-O3) 97 * 98 * See also CodeGenOpt::Level in llvm/Target/TargetMachine.h 99 */ 100enum LLVM_CodeGenOpt_Level { 101 None, // -O0 102 Less, // -O1 103 Default, // -O2, -Os 104 Aggressive // -O3 105}; 106 107 108/** 109 * Create the LLVM (optimization) pass manager and install 110 * relevant optimization passes. 111 * \return TRUE for success, FALSE for failure 112 */ 113static boolean 114create_pass_manager(struct gallivm_state *gallivm) 115{ 116#if GALLIVM_USE_NEW_PASS == 0 117 assert(!gallivm->passmgr); 118 assert(gallivm->target); 119 120 gallivm->passmgr = LLVMCreateFunctionPassManagerForModule(gallivm->module); 121 if (!gallivm->passmgr) 122 return FALSE; 123 124#if GALLIVM_HAVE_CORO == 1 125 gallivm->cgpassmgr = LLVMCreatePassManager(); 126#endif 127 /* 128 * TODO: some per module pass manager with IPO passes might be helpful - 129 * the generated texture functions may benefit from inlining if they are 130 * simple, or constant propagation into them, etc. 131 */ 132 133 { 134 char *td_str; 135 // New ones from the Module. 136 td_str = LLVMCopyStringRepOfTargetData(gallivm->target); 137 LLVMSetDataLayout(gallivm->module, td_str); 138 free(td_str); 139 } 140 141#if GALLIVM_HAVE_CORO == 1 142#if LLVM_VERSION_MAJOR <= 8 && (defined(PIPE_ARCH_AARCH64) || defined (PIPE_ARCH_ARM) || defined(PIPE_ARCH_S390) || defined(PIPE_ARCH_MIPS64)) 143 LLVMAddArgumentPromotionPass(gallivm->cgpassmgr); 144 LLVMAddFunctionAttrsPass(gallivm->cgpassmgr); 145#endif 146 LLVMAddCoroEarlyPass(gallivm->cgpassmgr); 147 LLVMAddCoroSplitPass(gallivm->cgpassmgr); 148 LLVMAddCoroElidePass(gallivm->cgpassmgr); 149#endif 150 151 if ((gallivm_perf & GALLIVM_PERF_NO_OPT) == 0) { 152 /* 153 * TODO: Evaluate passes some more - keeping in mind 154 * both quality of generated code and compile times. 155 */ 156 /* 157 * NOTE: if you change this, don't forget to change the output 158 * with GALLIVM_DEBUG_DUMP_BC in gallivm_compile_module. 159 */ 160 LLVMAddScalarReplAggregatesPass(gallivm->passmgr); 161 LLVMAddEarlyCSEPass(gallivm->passmgr); 162 LLVMAddCFGSimplificationPass(gallivm->passmgr); 163 /* 164 * FIXME: LICM is potentially quite useful. However, for some 165 * rather crazy shaders the compile time can reach _hours_ per shader, 166 * due to licm implying lcssa (since llvm 3.5), which can take forever. 167 * Even for sane shaders, the cost of licm is rather high (and not just 168 * due to lcssa, licm itself too), though mostly only in cases when it 169 * can actually move things, so having to disable it is a pity. 170 * LLVMAddLICMPass(gallivm->passmgr); 171 */ 172 LLVMAddReassociatePass(gallivm->passmgr); 173 LLVMAddPromoteMemoryToRegisterPass(gallivm->passmgr); 174#if LLVM_VERSION_MAJOR <= 11 175 LLVMAddConstantPropagationPass(gallivm->passmgr); 176#else 177 LLVMAddInstructionSimplifyPass(gallivm->passmgr); 178#endif 179 LLVMAddInstructionCombiningPass(gallivm->passmgr); 180 LLVMAddGVNPass(gallivm->passmgr); 181 } 182 else { 183 /* We need at least this pass to prevent the backends to fail in 184 * unexpected ways. 185 */ 186 LLVMAddPromoteMemoryToRegisterPass(gallivm->passmgr); 187 } 188#if GALLIVM_HAVE_CORO == 1 189 LLVMAddCoroCleanupPass(gallivm->passmgr); 190#endif 191#endif 192 return TRUE; 193} 194 195/** 196 * Free gallivm object's LLVM allocations, but not any generated code 197 * nor the gallivm object itself. 198 */ 199void 200gallivm_free_ir(struct gallivm_state *gallivm) 201{ 202#if GALLIVM_USE_NEW_PASS == 0 203 if (gallivm->passmgr) { 204 LLVMDisposePassManager(gallivm->passmgr); 205 } 206 207#if GALLIVM_HAVE_CORO == 1 208 if (gallivm->cgpassmgr) { 209 LLVMDisposePassManager(gallivm->cgpassmgr); 210 } 211#endif 212#endif 213 214 if (gallivm->engine) { 215 /* This will already destroy any associated module */ 216 LLVMDisposeExecutionEngine(gallivm->engine); 217 } else if (gallivm->module) { 218 LLVMDisposeModule(gallivm->module); 219 } 220 221 if (gallivm->cache) { 222 lp_free_objcache(gallivm->cache->jit_obj_cache); 223 free(gallivm->cache->data); 224 } 225 FREE(gallivm->module_name); 226 227 if (gallivm->target) { 228 LLVMDisposeTargetData(gallivm->target); 229 } 230 231 if (gallivm->builder) 232 LLVMDisposeBuilder(gallivm->builder); 233 234 /* The LLVMContext should be owned by the parent of gallivm. */ 235 236 gallivm->engine = NULL; 237 gallivm->target = NULL; 238 gallivm->module = NULL; 239 gallivm->module_name = NULL; 240#if GALLIVM_USE_NEW_PASS == 0 241#if GALLIVM_HAVE_CORO == 1 242 gallivm->cgpassmgr = NULL; 243#endif 244 gallivm->passmgr = NULL; 245#endif 246 gallivm->context = NULL; 247 gallivm->builder = NULL; 248 gallivm->cache = NULL; 249} 250 251 252/** 253 * Free LLVM-generated code. Should be done AFTER gallivm_free_ir(). 254 */ 255static void 256gallivm_free_code(struct gallivm_state *gallivm) 257{ 258 assert(!gallivm->module); 259 assert(!gallivm->engine); 260 lp_free_generated_code(gallivm->code); 261 gallivm->code = NULL; 262 lp_free_memory_manager(gallivm->memorymgr); 263 gallivm->memorymgr = NULL; 264} 265 266 267static boolean 268init_gallivm_engine(struct gallivm_state *gallivm) 269{ 270 if (1) { 271 enum LLVM_CodeGenOpt_Level optlevel; 272 char *error = NULL; 273 int ret; 274 275 if (gallivm_perf & GALLIVM_PERF_NO_OPT) { 276 optlevel = None; 277 } 278 else { 279 optlevel = Default; 280 } 281 282 ret = lp_build_create_jit_compiler_for_module(&gallivm->engine, 283 &gallivm->code, 284 gallivm->cache, 285 gallivm->module, 286 gallivm->memorymgr, 287 (unsigned) optlevel, 288 &error); 289 if (ret) { 290 _debug_printf("%s\n", error); 291 LLVMDisposeMessage(error); 292 goto fail; 293 } 294 } 295 296 if (0) { 297 /* 298 * Dump the data layout strings. 299 */ 300 301 LLVMTargetDataRef target = LLVMGetExecutionEngineTargetData(gallivm->engine); 302 char *data_layout; 303 char *engine_data_layout; 304 305 data_layout = LLVMCopyStringRepOfTargetData(gallivm->target); 306 engine_data_layout = LLVMCopyStringRepOfTargetData(target); 307 308 if (1) { 309 debug_printf("module target data = %s\n", data_layout); 310 debug_printf("engine target data = %s\n", engine_data_layout); 311 } 312 313 free(data_layout); 314 free(engine_data_layout); 315 } 316 317 return TRUE; 318 319fail: 320 return FALSE; 321} 322 323 324/** 325 * Allocate gallivm LLVM objects. 326 * \return TRUE for success, FALSE for failure 327 */ 328static boolean 329init_gallivm_state(struct gallivm_state *gallivm, const char *name, 330 LLVMContextRef context, struct lp_cached_code *cache) 331{ 332 assert(!gallivm->context); 333 assert(!gallivm->module); 334 335 if (!lp_build_init()) 336 return FALSE; 337 338 gallivm->context = context; 339 gallivm->cache = cache; 340 if (!gallivm->context) 341 goto fail; 342 343 gallivm->module_name = NULL; 344 if (name) { 345 size_t size = strlen(name) + 1; 346 gallivm->module_name = MALLOC(size); 347 if (gallivm->module_name) { 348 memcpy(gallivm->module_name, name, size); 349 } 350 } 351 352 gallivm->module = LLVMModuleCreateWithNameInContext(name, 353 gallivm->context); 354 if (!gallivm->module) 355 goto fail; 356 357#if defined(PIPE_ARCH_X86) 358 lp_set_module_stack_alignment_override(gallivm->module, 4); 359#endif 360 361 gallivm->builder = LLVMCreateBuilderInContext(gallivm->context); 362 if (!gallivm->builder) 363 goto fail; 364 365 gallivm->memorymgr = lp_get_default_memory_manager(); 366 if (!gallivm->memorymgr) 367 goto fail; 368 369 /* FIXME: MC-JIT only allows compiling one module at a time, and it must be 370 * complete when MC-JIT is created. So defer the MC-JIT engine creation for 371 * now. 372 */ 373 374 /* 375 * MC-JIT engine compiles the module immediately on creation, so we can't 376 * obtain the target data from it. Instead we create a target data layout 377 * from a string. 378 * 379 * The produced layout strings are not precisely the same, but should make 380 * no difference for the kind of optimization passes we run. 381 * 382 * For reference this is the layout string on x64: 383 * 384 * 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 385 * 386 * See also: 387 * - http://llvm.org/docs/LangRef.html#datalayout 388 */ 389 390 { 391 const unsigned pointer_size = 8 * sizeof(void *); 392 char layout[512]; 393 snprintf(layout, sizeof layout, "%c-p:%u:%u:%u-i64:64:64-a0:0:%u-s0:%u:%u", 394#if UTIL_ARCH_LITTLE_ENDIAN 395 'e', // little endian 396#else 397 'E', // big endian 398#endif 399 pointer_size, pointer_size, pointer_size, // pointer size, abi alignment, preferred alignment 400 pointer_size, // aggregate preferred alignment 401 pointer_size, pointer_size); // stack objects abi alignment, preferred alignment 402 403 gallivm->target = LLVMCreateTargetData(layout); 404 if (!gallivm->target) { 405 return FALSE; 406 } 407 } 408 409 if (!create_pass_manager(gallivm)) 410 goto fail; 411 412 lp_build_coro_declare_malloc_hooks(gallivm); 413 return TRUE; 414 415fail: 416 gallivm_free_ir(gallivm); 417 gallivm_free_code(gallivm); 418 return FALSE; 419} 420 421 422boolean 423lp_build_init(void) 424{ 425 if (gallivm_initialized) 426 return TRUE; 427 428 429 /* LLVMLinkIn* are no-ops at runtime. They just ensure the respective 430 * component is linked at buildtime, which is sufficient for its static 431 * constructors to be called at load time. 432 */ 433 LLVMLinkInMCJIT(); 434 435#ifdef DEBUG 436 gallivm_debug = debug_get_option_gallivm_debug(); 437#endif 438 439 gallivm_perf = debug_get_flags_option("GALLIVM_PERF", lp_bld_perf_flags, 0 ); 440 441 lp_set_target_options(); 442 443 /* For simulating less capable machines */ 444#ifdef DEBUG 445 if (debug_get_bool_option("LP_FORCE_SSE2", FALSE)) { 446 extern struct util_cpu_caps_t util_cpu_caps; 447 assert(util_cpu_caps.has_sse2); 448 util_cpu_caps.has_sse3 = 0; 449 util_cpu_caps.has_ssse3 = 0; 450 util_cpu_caps.has_sse4_1 = 0; 451 util_cpu_caps.has_sse4_2 = 0; 452 util_cpu_caps.has_avx = 0; 453 util_cpu_caps.has_avx2 = 0; 454 util_cpu_caps.has_f16c = 0; 455 util_cpu_caps.has_fma = 0; 456 } 457#endif 458 459 if (util_get_cpu_caps()->has_avx2 || util_get_cpu_caps()->has_avx) { 460 lp_native_vector_width = 256; 461 } else { 462 /* Leave it at 128, even when no SIMD extensions are available. 463 * Really needs to be a multiple of 128 so can fit 4 floats. 464 */ 465 lp_native_vector_width = 128; 466 } 467 468 lp_native_vector_width = debug_get_num_option("LP_NATIVE_VECTOR_WIDTH", 469 lp_native_vector_width); 470 471#if LLVM_VERSION_MAJOR < 4 472 if (lp_native_vector_width <= 128) { 473 /* Hide AVX support, as often LLVM AVX intrinsics are only guarded by 474 * "util_get_cpu_caps()->has_avx" predicate, and lack the 475 * "lp_native_vector_width > 128" predicate. And also to ensure a more 476 * consistent behavior, allowing one to test SSE2 on AVX machines. 477 * XXX: should not play games with util_cpu_caps directly as it might 478 * get used for other things outside llvm too. 479 */ 480 util_get_cpu_caps()->has_avx = 0; 481 util_get_cpu_caps()->has_avx2 = 0; 482 util_get_cpu_caps()->has_f16c = 0; 483 util_get_cpu_caps()->has_fma = 0; 484 } 485#endif 486 487#ifdef PIPE_ARCH_PPC_64 488 /* Set the NJ bit in VSCR to 0 so denormalized values are handled as 489 * specified by IEEE standard (PowerISA 2.06 - Section 6.3). This guarantees 490 * that some rounding and half-float to float handling does not round 491 * incorrectly to 0. 492 * XXX: should eventually follow same logic on all platforms. 493 * Right now denorms get explicitly disabled (but elsewhere) for x86, 494 * whereas ppc64 explicitly enables them... 495 */ 496 if (util_get_cpu_caps()->has_altivec) { 497 unsigned short mask[] = { 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 498 0xFFFF, 0xFFFF, 0xFFFE, 0xFFFF }; 499 __asm ( 500 "mfvscr %%v1\n" 501 "vand %0,%%v1,%0\n" 502 "mtvscr %0" 503 : 504 : "r" (*mask) 505 ); 506 } 507#endif 508 509 gallivm_initialized = TRUE; 510 511 return TRUE; 512} 513 514 515 516/** 517 * Create a new gallivm_state object. 518 */ 519struct gallivm_state * 520gallivm_create(const char *name, LLVMContextRef context, 521 struct lp_cached_code *cache) 522{ 523 struct gallivm_state *gallivm; 524 525 gallivm = CALLOC_STRUCT(gallivm_state); 526 if (gallivm) { 527 if (!init_gallivm_state(gallivm, name, context, cache)) { 528 FREE(gallivm); 529 gallivm = NULL; 530 } 531 } 532 533 assert(gallivm != NULL); 534 return gallivm; 535} 536 537 538/** 539 * Destroy a gallivm_state object. 540 */ 541void 542gallivm_destroy(struct gallivm_state *gallivm) 543{ 544 gallivm_free_ir(gallivm); 545 gallivm_free_code(gallivm); 546 FREE(gallivm); 547} 548 549 550/** 551 * Validate a function. 552 * Verification is only done with debug builds. 553 */ 554void 555gallivm_verify_function(struct gallivm_state *gallivm, 556 LLVMValueRef func) 557{ 558 /* Verify the LLVM IR. If invalid, dump and abort */ 559#ifdef DEBUG 560 if (LLVMVerifyFunction(func, LLVMPrintMessageAction)) { 561 lp_debug_dump_value(func); 562 assert(0); 563 return; 564 } 565#endif 566 567 if (gallivm_debug & GALLIVM_DEBUG_IR) { 568 /* Print the LLVM IR to stderr */ 569 lp_debug_dump_value(func); 570 debug_printf("\n"); 571 } 572} 573 574 575/** 576 * Compile a module. 577 * This does IR optimization on all functions in the module. 578 */ 579void 580gallivm_compile_module(struct gallivm_state *gallivm) 581{ 582 int64_t time_begin = 0; 583 584 assert(!gallivm->compiled); 585 586 if (gallivm->builder) { 587 LLVMDisposeBuilder(gallivm->builder); 588 gallivm->builder = NULL; 589 } 590 591 LLVMSetDataLayout(gallivm->module, ""); 592 assert(!gallivm->engine); 593 if (!init_gallivm_engine(gallivm)) { 594 assert(0); 595 } 596 assert(gallivm->engine); 597 598 if (gallivm->cache && gallivm->cache->data_size) { 599 goto skip_cached; 600 } 601 602 /* Dump bitcode to a file */ 603 if (gallivm_debug & GALLIVM_DEBUG_DUMP_BC) { 604 char filename[256]; 605 assert(gallivm->module_name); 606 snprintf(filename, sizeof(filename), "ir_%s.bc", gallivm->module_name); 607 LLVMWriteBitcodeToFile(gallivm->module, filename); 608 debug_printf("%s written\n", filename); 609 debug_printf("Invoke as \"opt %s %s | llc -O%d %s%s\"\n", 610 gallivm_perf & GALLIVM_PERF_NO_OPT ? "-mem2reg" : 611 "-sroa -early-cse -simplifycfg -reassociate " 612 "-mem2reg -constprop -instcombine -gvn", 613 filename, gallivm_perf & GALLIVM_PERF_NO_OPT ? 0 : 2, 614 "[-mcpu=<-mcpu option>] ", 615 "[-mattr=<-mattr option(s)>]"); 616 } 617 618 if (gallivm_debug & GALLIVM_DEBUG_PERF) 619 time_begin = os_time_get(); 620 621#if GALLIVM_USE_NEW_PASS == 1 622 char passes[1024]; 623 passes[0] = 0; 624 625 /* 626 * there should be some way to combine these two pass runs but I'm not seeing it, 627 * at the time of writing. 628 */ 629 strcpy(passes, "default<O0>"); 630 631 LLVMPassBuilderOptionsRef opts = LLVMCreatePassBuilderOptions(); 632 LLVMRunPasses(gallivm->module, passes, LLVMGetExecutionEngineTargetMachine(gallivm->engine), opts); 633 634 if (!(gallivm_perf & GALLIVM_PERF_NO_OPT)) 635 strcpy(passes, "sroa,early-cse,simplifycfg,reassociate,mem2reg,instsimplify,instcombine"); 636 else 637 strcpy(passes, "mem2reg"); 638 639 LLVMRunPasses(gallivm->module, passes, LLVMGetExecutionEngineTargetMachine(gallivm->engine), opts); 640 LLVMDisposePassBuilderOptions(opts); 641#else 642#if GALLIVM_HAVE_CORO == 1 643 LLVMRunPassManager(gallivm->cgpassmgr, gallivm->module); 644#endif 645 /* Run optimization passes */ 646 LLVMInitializeFunctionPassManager(gallivm->passmgr); 647 LLVMValueRef func; 648 func = LLVMGetFirstFunction(gallivm->module); 649 while (func) { 650 if (0) { 651 debug_printf("optimizing func %s...\n", LLVMGetValueName(func)); 652 } 653 654 /* Disable frame pointer omission on debug/profile builds */ 655 /* XXX: And workaround http://llvm.org/PR21435 */ 656#if defined(DEBUG) || defined(PROFILE) || defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64) 657 LLVMAddTargetDependentFunctionAttr(func, "no-frame-pointer-elim", "true"); 658 LLVMAddTargetDependentFunctionAttr(func, "no-frame-pointer-elim-non-leaf", "true"); 659#endif 660 661 LLVMRunFunctionPassManager(gallivm->passmgr, func); 662 func = LLVMGetNextFunction(func); 663 } 664 LLVMFinalizeFunctionPassManager(gallivm->passmgr); 665#endif 666 if (gallivm_debug & GALLIVM_DEBUG_PERF) { 667 int64_t time_end = os_time_get(); 668 int time_msec = (int)((time_end - time_begin) / 1000); 669 assert(gallivm->module_name); 670 debug_printf("optimizing module %s took %d msec\n", 671 gallivm->module_name, time_msec); 672 } 673 674 /* Setting the module's DataLayout to an empty string will cause the 675 * ExecutionEngine to copy to the DataLayout string from its target machine 676 * to the module. As of LLVM 3.8 the module and the execution engine are 677 * required to have the same DataLayout. 678 * 679 * We must make sure we do this after running the optimization passes, 680 * because those passes need a correct datalayout string. For example, if 681 * those optimization passes see an empty datalayout, they will assume this 682 * is a little endian target and will do optimizations that break big endian 683 * machines. 684 * 685 * TODO: This is just a temporary work-around. The correct solution is for 686 * gallivm_init_state() to create a TargetMachine and pull the DataLayout 687 * from there. Currently, the TargetMachine used by llvmpipe is being 688 * implicitly created by the EngineBuilder in 689 * lp_build_create_jit_compiler_for_module() 690 */ 691 skip_cached: 692 693 ++gallivm->compiled; 694 695 lp_init_printf_hook(gallivm); 696 LLVMAddGlobalMapping(gallivm->engine, gallivm->debug_printf_hook, debug_printf); 697 698 699 lp_build_coro_add_malloc_hooks(gallivm); 700 701 if (gallivm_debug & GALLIVM_DEBUG_ASM) { 702 LLVMValueRef llvm_func = LLVMGetFirstFunction(gallivm->module); 703 704 while (llvm_func) { 705 /* 706 * Need to filter out functions which don't have an implementation, 707 * such as the intrinsics. May not be sufficient in case of IPO? 708 * LLVMGetPointerToGlobal() will abort otherwise. 709 */ 710 if (!LLVMIsDeclaration(llvm_func)) { 711 void *func_code = LLVMGetPointerToGlobal(gallivm->engine, llvm_func); 712 lp_disassemble(llvm_func, func_code); 713 } 714 llvm_func = LLVMGetNextFunction(llvm_func); 715 } 716 } 717 718#if defined(PROFILE) 719 { 720 LLVMValueRef llvm_func = LLVMGetFirstFunction(gallivm->module); 721 722 while (llvm_func) { 723 if (!LLVMIsDeclaration(llvm_func)) { 724 void *func_code = LLVMGetPointerToGlobal(gallivm->engine, llvm_func); 725 lp_profile(llvm_func, func_code); 726 } 727 llvm_func = LLVMGetNextFunction(llvm_func); 728 } 729 } 730#endif 731} 732 733 734 735func_pointer 736gallivm_jit_function(struct gallivm_state *gallivm, 737 LLVMValueRef func) 738{ 739 void *code; 740 func_pointer jit_func; 741 int64_t time_begin = 0; 742 743 assert(gallivm->compiled); 744 assert(gallivm->engine); 745 746 if (gallivm_debug & GALLIVM_DEBUG_PERF) 747 time_begin = os_time_get(); 748 749 code = LLVMGetPointerToGlobal(gallivm->engine, func); 750 assert(code); 751 jit_func = pointer_to_func(code); 752 753 if (gallivm_debug & GALLIVM_DEBUG_PERF) { 754 int64_t time_end = os_time_get(); 755 int time_msec = (int)(time_end - time_begin) / 1000; 756 debug_printf(" jitting func %s took %d msec\n", 757 LLVMGetValueName(func), time_msec); 758 } 759 760 return jit_func; 761} 762 763unsigned gallivm_get_perf_flags(void) 764{ 765 return gallivm_perf; 766} 767