gallivm: fix build on LLVM 12 due to LLVMAddConstantPropagationPass removal
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_init.c
index ed919c48838525e09d9cf2a688c26e77e37bafb6..1eb4892a4a6cf3c6d9465b1d9b1e7d878e5f6c76 100644 (file)
 #endif
 #include <llvm-c/BitWriter.h>
 #if GALLIVM_HAVE_CORO
-#include <llvm-c/Transforms/Coroutines.h>
-#endif
-
-/* Only MCJIT is available as of LLVM SVN r216982 */
-#if LLVM_VERSION_MAJOR > 3 || (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 6)
-#  define USE_MCJIT 1
-#elif defined(PIPE_ARCH_PPC_64) || defined(PIPE_ARCH_S390) || defined(PIPE_ARCH_ARM) || defined(PIPE_ARCH_AARCH64)
-#  define USE_MCJIT 1
+#if LLVM_VERSION_MAJOR <= 8 && (defined(PIPE_ARCH_AARCH64) || defined (PIPE_ARCH_ARM) || defined(PIPE_ARCH_S390))
+#include <llvm-c/Transforms/IPO.h>
 #endif
-
-#if defined(USE_MCJIT)
-static const bool use_mcjit = USE_MCJIT;
-#else
-static bool use_mcjit = FALSE;
+#include <llvm-c/Transforms/Coroutines.h>
 #endif
 
 unsigned gallivm_perf = 0;
@@ -138,11 +128,6 @@ create_pass_manager(struct gallivm_state *gallivm)
     * simple, or constant propagation into them, etc.
     */
 
-#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR < 9
-   // Old versions of LLVM get the DataLayout from the pass manager.
-   LLVMAddTargetData(gallivm->target, gallivm->passmgr);
-#endif
-
    {
       char *td_str;
       // New ones from the Module.
@@ -152,6 +137,10 @@ create_pass_manager(struct gallivm_state *gallivm)
    }
 
 #if GALLIVM_HAVE_CORO
+#if LLVM_VERSION_MAJOR <= 8 && (defined(PIPE_ARCH_AARCH64) || defined (PIPE_ARCH_ARM) || defined(PIPE_ARCH_S390))
+   LLVMAddArgumentPromotionPass(gallivm->cgpassmgr);
+   LLVMAddFunctionAttrsPass(gallivm->cgpassmgr);
+#endif
    LLVMAddCoroEarlyPass(gallivm->cgpassmgr);
    LLVMAddCoroSplitPass(gallivm->cgpassmgr);
    LLVMAddCoroElidePass(gallivm->cgpassmgr);
@@ -180,12 +169,11 @@ create_pass_manager(struct gallivm_state *gallivm)
        */
       LLVMAddReassociatePass(gallivm->passmgr);
       LLVMAddPromoteMemoryToRegisterPass(gallivm->passmgr);
+#if LLVM_VERSION_MAJOR <= 11
       LLVMAddConstantPropagationPass(gallivm->passmgr);
+#endif
       LLVMAddInstructionCombiningPass(gallivm->passmgr);
       LLVMAddGVNPass(gallivm->passmgr);
-#if GALLIVM_HAVE_CORO
-      LLVMAddCoroCleanupPass(gallivm->passmgr);
-#endif
    }
    else {
       /* We need at least this pass to prevent the backends to fail in
@@ -193,6 +181,9 @@ create_pass_manager(struct gallivm_state *gallivm)
        */
       LLVMAddPromoteMemoryToRegisterPass(gallivm->passmgr);
    }
+#if GALLIVM_HAVE_CORO
+   LLVMAddCoroCleanupPass(gallivm->passmgr);
+#endif
 
    return TRUE;
 }
@@ -222,14 +213,14 @@ gallivm_free_ir(struct gallivm_state *gallivm)
       LLVMDisposeModule(gallivm->module);
    }
 
+   if (gallivm->cache) {
+      lp_free_objcache(gallivm->cache->jit_obj_cache);
+      free(gallivm->cache->data);
+   }
    FREE(gallivm->module_name);
 
-   if (!use_mcjit) {
-      /* Don't free the TargetData, it's owned by the exec engine */
-   } else {
-      if (gallivm->target) {
-         LLVMDisposeTargetData(gallivm->target);
-      }
+   if (gallivm->target) {
+      LLVMDisposeTargetData(gallivm->target);
    }
 
    if (gallivm->builder)
@@ -245,6 +236,7 @@ gallivm_free_ir(struct gallivm_state *gallivm)
    gallivm->passmgr = NULL;
    gallivm->context = NULL;
    gallivm->builder = NULL;
+   gallivm->cache = NULL;
 }
 
 
@@ -280,10 +272,10 @@ init_gallivm_engine(struct gallivm_state *gallivm)
 
       ret = lp_build_create_jit_compiler_for_module(&gallivm->engine,
                                                     &gallivm->code,
+                                                    gallivm->cache,
                                                     gallivm->module,
                                                     gallivm->memorymgr,
                                                     (unsigned) optlevel,
-                                                    use_mcjit,
                                                     &error);
       if (ret) {
          _debug_printf("%s\n", error);
@@ -292,31 +284,25 @@ init_gallivm_engine(struct gallivm_state *gallivm)
       }
    }
 
-   if (!use_mcjit) {
-      gallivm->target = LLVMGetExecutionEngineTargetData(gallivm->engine);
-      if (!gallivm->target)
-         goto fail;
-   } else {
-      if (0) {
-          /*
-           * Dump the data layout strings.
-           */
+   if (0) {
+       /*
+        * Dump the data layout strings.
+        */
 
-          LLVMTargetDataRef target = LLVMGetExecutionEngineTargetData(gallivm->engine);
-          char *data_layout;
-          char *engine_data_layout;
+       LLVMTargetDataRef target = LLVMGetExecutionEngineTargetData(gallivm->engine);
+       char *data_layout;
+       char *engine_data_layout;
 
-          data_layout = LLVMCopyStringRepOfTargetData(gallivm->target);
-          engine_data_layout = LLVMCopyStringRepOfTargetData(target);
+       data_layout = LLVMCopyStringRepOfTargetData(gallivm->target);
+       engine_data_layout = LLVMCopyStringRepOfTargetData(target);
 
-          if (1) {
-             debug_printf("module target data = %s\n", data_layout);
-             debug_printf("engine target data = %s\n", engine_data_layout);
-          }
+       if (1) {
+          debug_printf("module target data = %s\n", data_layout);
+          debug_printf("engine target data = %s\n", engine_data_layout);
+       }
 
-          free(data_layout);
-          free(engine_data_layout);
-      }
+       free(data_layout);
+       free(engine_data_layout);
    }
 
    return TRUE;
@@ -332,7 +318,7 @@ fail:
  */
 static boolean
 init_gallivm_state(struct gallivm_state *gallivm, const char *name,
-                   LLVMContextRef context)
+                   LLVMContextRef context, struct lp_cached_code *cache)
 {
    assert(!gallivm->context);
    assert(!gallivm->module);
@@ -341,7 +327,7 @@ init_gallivm_state(struct gallivm_state *gallivm, const char *name,
       return FALSE;
 
    gallivm->context = context;
-
+   gallivm->cache = cache;
    if (!gallivm->context)
       goto fail;
 
@@ -371,44 +357,39 @@ init_gallivm_state(struct gallivm_state *gallivm, const char *name,
     * complete when MC-JIT is created. So defer the MC-JIT engine creation for
     * now.
     */
-   if (!use_mcjit) {
-      if (!init_gallivm_engine(gallivm)) {
-         goto fail;
-      }
-   } else {
-      /*
-       * MC-JIT engine compiles the module immediately on creation, so we can't
-       * obtain the target data from it.  Instead we create a target data layout
-       * from a string.
-       *
-       * The produced layout strings are not precisely the same, but should make
-       * no difference for the kind of optimization passes we run.
-       *
-       * For reference this is the layout string on x64:
-       *
-       *   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
-       *
-       * See also:
-       * - http://llvm.org/docs/LangRef.html#datalayout
-       */
 
-      {
-         const unsigned pointer_size = 8 * sizeof(void *);
-         char layout[512];
-         snprintf(layout, sizeof layout, "%c-p:%u:%u:%u-i64:64:64-a0:0:%u-s0:%u:%u",
-#ifdef PIPE_ARCH_LITTLE_ENDIAN
-                       'e', // little endian
+   /*
+    * MC-JIT engine compiles the module immediately on creation, so we can't
+    * obtain the target data from it.  Instead we create a target data layout
+    * from a string.
+    *
+    * The produced layout strings are not precisely the same, but should make
+    * no difference for the kind of optimization passes we run.
+    *
+    * For reference this is the layout string on x64:
+    *
+    *   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
+    *
+    * See also:
+    * - http://llvm.org/docs/LangRef.html#datalayout
+    */
+
+   {
+      const unsigned pointer_size = 8 * sizeof(void *);
+      char layout[512];
+      snprintf(layout, sizeof layout, "%c-p:%u:%u:%u-i64:64:64-a0:0:%u-s0:%u:%u",
+#if UTIL_ARCH_LITTLE_ENDIAN
+                    'e', // little endian
 #else
-                       'E', // big endian
+                    'E', // big endian
 #endif
-                       pointer_size, pointer_size, pointer_size, // pointer size, abi alignment, preferred alignment
-                       pointer_size, // aggregate preferred alignment
-                       pointer_size, pointer_size); // stack objects abi alignment, preferred alignment
+                    pointer_size, pointer_size, pointer_size, // pointer size, abi alignment, preferred alignment
+                    pointer_size, // aggregate preferred alignment
+                    pointer_size, pointer_size); // stack objects abi alignment, preferred alignment
 
-         gallivm->target = LLVMCreateTargetData(layout);
-         if (!gallivm->target) {
-            return FALSE;
-         }
+      gallivm->target = LLVMCreateTargetData(layout);
+      if (!gallivm->target) {
+         return FALSE;
       }
    }
 
@@ -435,17 +416,7 @@ lp_build_init(void)
     * component is linked at buildtime, which is sufficient for its static
     * constructors to be called at load time.
     */
-#if defined(USE_MCJIT)
-#  if USE_MCJIT
-      LLVMLinkInMCJIT();
-#  else
-      LLVMLinkInJIT();
-#  endif
-#else
-   use_mcjit = debug_get_bool_option("GALLIVM_MCJIT", FALSE);
-   LLVMLinkInJIT();
    LLVMLinkInMCJIT();
-#endif
 
 #ifdef DEBUG
    gallivm_debug = debug_get_option_gallivm_debug();
@@ -472,15 +443,7 @@ lp_build_init(void)
    }
 #endif
 
-   /* AMD Bulldozer AVX's throughput is the same as SSE2; and because using
-    * 8-wide vector needs more floating ops than 4-wide (due to padding), it is
-    * actually more efficient to use 4-wide vectors on this processor.
-    *
-    * See also:
-    * - http://www.anandtech.com/show/4955/the-bulldozer-review-amd-fx8150-tested/2
-    */
-   if (util_cpu_caps.has_avx &&
-       util_cpu_caps.has_intel) {
+   if (util_cpu_caps.has_avx2 || util_cpu_caps.has_avx) {
       lp_native_vector_width = 256;
    } else {
       /* Leave it at 128, even when no SIMD extensions are available.
@@ -488,10 +451,11 @@ lp_build_init(void)
        */
       lp_native_vector_width = 128;
    }
+
    lp_native_vector_width = debug_get_num_option("LP_NATIVE_VECTOR_WIDTH",
                                                  lp_native_vector_width);
 
+#if LLVM_VERSION_MAJOR < 4
    if (lp_native_vector_width <= 128) {
       /* Hide AVX support, as often LLVM AVX intrinsics are only guarded by
        * "util_cpu_caps.has_avx" predicate, and lack the
@@ -505,11 +469,7 @@ lp_build_init(void)
       util_cpu_caps.has_f16c = 0;
       util_cpu_caps.has_fma = 0;
    }
-   if ((LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR < 4) || !use_mcjit) {
-      /* AVX2 support has only been tested with LLVM 3.4, and it requires
-       * MCJIT. */
-      util_cpu_caps.has_avx2 = 0;
-   }
+#endif
 
 #ifdef PIPE_ARCH_PPC_64
    /* Set the NJ bit in VSCR to 0 so denormalized values are handled as
@@ -544,18 +504,20 @@ lp_build_init(void)
  * Create a new gallivm_state object.
  */
 struct gallivm_state *
-gallivm_create(const char *name, LLVMContextRef context)
+gallivm_create(const char *name, LLVMContextRef context,
+               struct lp_cached_code *cache)
 {
    struct gallivm_state *gallivm;
 
    gallivm = CALLOC_STRUCT(gallivm_state);
    if (gallivm) {
-      if (!init_gallivm_state(gallivm, name, context)) {
+      if (!init_gallivm_state(gallivm, name, context, cache)) {
          FREE(gallivm);
          gallivm = NULL;
       }
    }
 
+   assert(gallivm != NULL);
    return gallivm;
 }
 
@@ -614,6 +576,10 @@ gallivm_compile_module(struct gallivm_state *gallivm)
       gallivm->builder = NULL;
    }
 
+   if (gallivm->cache && gallivm->cache->data_size) {
+      goto skip_cached;
+   }
+
    /* Dump bitcode to a file */
    if (gallivm_debug & GALLIVM_DEBUG_DUMP_BC) {
       char filename[256];
@@ -626,7 +592,7 @@ gallivm_compile_module(struct gallivm_state *gallivm)
                    "-sroa -early-cse -simplifycfg -reassociate "
                    "-mem2reg -constprop -instcombine -gvn",
                    filename, gallivm_debug & GALLIVM_PERF_NO_OPT ? 0 : 2,
-                   (LLVM_VERSION_MAJOR > 3 || (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 5)) ? "[-mcpu=<-mcpu option>] " : "",
+                   "[-mcpu=<-mcpu option>] ",
                    "[-mattr=<-mattr option(s)>]");
    }
 
@@ -646,9 +612,7 @@ gallivm_compile_module(struct gallivm_state *gallivm)
 
    /* Disable frame pointer omission on debug/profile builds */
    /* XXX: And workaround http://llvm.org/PR21435 */
-#if (LLVM_VERSION_MAJOR > 3 || (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 7)) && \
-    (defined(DEBUG) || defined(PROFILE) || \
-     defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64))
+#if defined(DEBUG) || defined(PROFILE) || defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
       LLVMAddTargetDependentFunctionAttr(func, "no-frame-pointer-elim", "true");
       LLVMAddTargetDependentFunctionAttr(func, "no-frame-pointer-elim-non-leaf", "true");
 #endif
@@ -666,34 +630,36 @@ gallivm_compile_module(struct gallivm_state *gallivm)
                    gallivm->module_name, time_msec);
    }
 
-   if (use_mcjit) {
-      /* Setting the module's DataLayout to an empty string will cause the
-       * ExecutionEngine to copy to the DataLayout string from its target
-       * machine to the module.  As of LLVM 3.8 the module and the execution
-       * engine are required to have the same DataLayout.
-       *
-       * We must make sure we do this after running the optimization passes,
-       * because those passes need a correct datalayout string.  For example,
-       * if those optimization passes see an empty datalayout, they will assume
-       * this is a little endian target and will do optimizations that break big
-       * endian machines.
-       *
-       * TODO: This is just a temporary work-around.  The correct solution is
-       * for gallivm_init_state() to create a TargetMachine and pull the
-       * DataLayout from there.  Currently, the TargetMachine used by llvmpipe
-       * is being implicitly created by the EngineBuilder in
-       * lp_build_create_jit_compiler_for_module()
-       */
-      LLVMSetDataLayout(gallivm->module, "");
-      assert(!gallivm->engine);
-      if (!init_gallivm_engine(gallivm)) {
-         assert(0);
-      }
+   /* Setting the module's DataLayout to an empty string will cause the
+    * ExecutionEngine to copy to the DataLayout string from its target machine
+    * to the module.  As of LLVM 3.8 the module and the execution engine are
+    * required to have the same DataLayout.
+    *
+    * We must make sure we do this after running the optimization passes,
+    * because those passes need a correct datalayout string.  For example, if
+    * those optimization passes see an empty datalayout, they will assume this
+    * is a little endian target and will do optimizations that break big endian
+    * machines.
+    *
+    * TODO: This is just a temporary work-around.  The correct solution is for
+    * gallivm_init_state() to create a TargetMachine and pull the DataLayout
+    * from there.  Currently, the TargetMachine used by llvmpipe is being
+    * implicitly created by the EngineBuilder in
+    * lp_build_create_jit_compiler_for_module()
+    */
+ skip_cached:
+   LLVMSetDataLayout(gallivm->module, "");
+   assert(!gallivm->engine);
+   if (!init_gallivm_engine(gallivm)) {
+      assert(0);
    }
    assert(gallivm->engine);
 
    ++gallivm->compiled;
 
+   if (gallivm->debug_printf_hook)
+      LLVMAddGlobalMapping(gallivm->engine, gallivm->debug_printf_hook, debug_printf);
+
    if (gallivm_debug & GALLIVM_DEBUG_ASM) {
       LLVMValueRef llvm_func = LLVMGetFirstFunction(gallivm->module);