"Fossies" - the Fresh Open Source Software Archive

Member "mesa-20.1.8/src/gallium/auxiliary/gallivm/lp_bld_init.c" (16 Sep 2020, 21302 Bytes) of package /linux/misc/mesa-20.1.8.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "lp_bld_init.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 20.2.0-rc3_vs_20.2.0-rc4.

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