gallivm: add support for a cache object
authorDave Airlie <airlied@redhat.com>
Tue, 12 May 2020 23:30:44 +0000 (09:30 +1000)
committerDave Airlie <airlied@redhat.com>
Wed, 10 Jun 2020 20:05:40 +0000 (06:05 +1000)
This plumbs the cache object into the gallivm API, nothing uses
it yet.

Reviewed-by: Roland Scheidegger <sroland@vmware.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/5049>

14 files changed:
src/gallium/auxiliary/draw/draw_llvm.c
src/gallium/auxiliary/gallivm/lp_bld_init.c
src/gallium/auxiliary/gallivm/lp_bld_init.h
src/gallium/auxiliary/gallivm/lp_bld_misc.cpp
src/gallium/auxiliary/gallivm/lp_bld_misc.h
src/gallium/drivers/llvmpipe/lp_state_cs.c
src/gallium/drivers/llvmpipe/lp_state_fs.c
src/gallium/drivers/llvmpipe/lp_state_setup.c
src/gallium/drivers/llvmpipe/lp_test_arit.c
src/gallium/drivers/llvmpipe/lp_test_blend.c
src/gallium/drivers/llvmpipe/lp_test_conv.c
src/gallium/drivers/llvmpipe/lp_test_format.c
src/gallium/drivers/llvmpipe/lp_test_printf.c
src/gallium/drivers/swr/swr_shader.cpp

index db8e24f43069e07080620d96fef0f5eeb559bf47..445f2e11130def78e45900f8017bd07573b71251 100644 (file)
@@ -850,7 +850,7 @@ draw_llvm_create_variant(struct draw_llvm *llvm,
    snprintf(module_name, sizeof(module_name), "draw_llvm_vs_variant%u",
             variant->shader->variants_cached);
 
-   variant->gallivm = gallivm_create(module_name, llvm->context);
+   variant->gallivm = gallivm_create(module_name, llvm->context, NULL);
 
    create_jit_types(variant);
 
@@ -2848,7 +2848,7 @@ draw_gs_llvm_create_variant(struct draw_llvm *llvm,
    snprintf(module_name, sizeof(module_name), "draw_llvm_gs_variant%u",
             variant->shader->variants_cached);
 
-   variant->gallivm = gallivm_create(module_name, llvm->context);
+   variant->gallivm = gallivm_create(module_name, llvm->context, NULL);
 
    create_gs_jit_types(variant);
 
@@ -3453,7 +3453,7 @@ draw_tcs_llvm_create_variant(struct draw_llvm *llvm,
    snprintf(module_name, sizeof(module_name), "draw_llvm_tcs_variant%u",
             variant->shader->variants_cached);
 
-   variant->gallivm = gallivm_create(module_name, llvm->context);
+   variant->gallivm = gallivm_create(module_name, llvm->context, NULL);
 
    create_tcs_jit_types(variant);
 
@@ -3928,7 +3928,7 @@ draw_tes_llvm_create_variant(struct draw_llvm *llvm,
    snprintf(module_name, sizeof(module_name), "draw_llvm_tes_variant%u",
             variant->shader->variants_cached);
 
-   variant->gallivm = gallivm_create(module_name, llvm->context);
+   variant->gallivm = gallivm_create(module_name, llvm->context, NULL);
 
    create_tes_jit_types(variant);
 
index ce522806669672e063866a807883e4ac183e9e99..06214014290430207c47b8d3101dee36a71f1a8a 100644 (file)
@@ -211,6 +211,9 @@ gallivm_free_ir(struct gallivm_state *gallivm)
       LLVMDisposeModule(gallivm->module);
    }
 
+   if (gallivm->cache) {
+      free(gallivm->cache->data);
+   }
    FREE(gallivm->module_name);
 
    if (gallivm->target) {
@@ -230,6 +233,7 @@ gallivm_free_ir(struct gallivm_state *gallivm)
    gallivm->passmgr = NULL;
    gallivm->context = NULL;
    gallivm->builder = NULL;
+   gallivm->cache = NULL;
 }
 
 
@@ -265,6 +269,7 @@ 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,
@@ -310,7 +315,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);
@@ -319,7 +324,7 @@ init_gallivm_state(struct gallivm_state *gallivm, const char *name,
       return FALSE;
 
    gallivm->context = context;
-
+   gallivm->cache = cache;
    if (!gallivm->context)
       goto fail;
 
@@ -496,13 +501,14 @@ 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;
       }
index cebf6a793d536dc26961e51966d6c2118bfe2458..4b00cebaef5bca4710ea6d914a8de997e14e67b6 100644 (file)
@@ -39,6 +39,7 @@
 extern "C" {
 #endif
 
+struct lp_cached_code;
 struct gallivm_state
 {
    char *module_name;
@@ -51,6 +52,7 @@ struct gallivm_state
    LLVMBuilderRef builder;
    LLVMMCJITMemoryManagerRef memorymgr;
    struct lp_generated_code *code;
+   struct lp_cached_code *cache;
    unsigned compiled;
    LLVMValueRef coro_malloc_hook;
    LLVMValueRef coro_free_hook;
@@ -63,7 +65,8 @@ lp_build_init(void);
 
 
 struct gallivm_state *
-gallivm_create(const char *name, LLVMContextRef context);
+gallivm_create(const char *name, LLVMContextRef context,
+               struct lp_cached_code *cache);
 
 void
 gallivm_destroy(struct gallivm_state *gallivm);
index b079b96b6deafb80fb530699e92d616534d3d97c..186965f32bb27e85c7e3a834368aa6f7f65270de 100644 (file)
@@ -304,6 +304,7 @@ extern "C"
 LLVMBool
 lp_build_create_jit_compiler_for_module(LLVMExecutionEngineRef *OutJIT,
                                         lp_generated_code **OutCode,
+                                        struct lp_cached_code *cache_out,
                                         LLVMModuleRef M,
                                         LLVMMCJITMemoryManagerRef CMM,
                                         unsigned OptLevel,
index 363fbded728be51cfe44841b8f2fadd1236f9859..f3be195554b88d08889f4b806d13c3fb44431bbd 100644 (file)
 extern "C" {
 #endif
 
+/*
+ * some shaders use function pointers incorrectly so can't be relinked
+ * properly. (mostly the fallback fetch shaders).
+ * We should fix them, but the dont_cache flag can be set for now,
+ * so they don't end up getting cached at all.
+ */
+struct lp_cached_code {
+   void *data;
+   size_t data_size;
+   bool dont_cache;
+};
 
 struct lp_generated_code;
 
@@ -56,6 +67,7 @@ lp_set_target_options(void);
 extern int
 lp_build_create_jit_compiler_for_module(LLVMExecutionEngineRef *OutJIT,
                                         struct lp_generated_code **OutCode,
+                                        struct lp_cached_code *cache_out,
                                         LLVMModuleRef M,
                                         LLVMMCJITMemoryManagerRef MM,
                                         unsigned OptLevel,
index 97c479ef45590bf86785aa2f473238baa62ee4a5..9270761acd652795caac409a05c4fe51b48c3418 100644 (file)
@@ -688,7 +688,7 @@ generate_variant(struct llvmpipe_context *lp,
    snprintf(module_name, sizeof(module_name), "cs%u_variant%u",
             shader->no, shader->variants_created);
 
-   variant->gallivm = gallivm_create(module_name, lp->context);
+   variant->gallivm = gallivm_create(module_name, lp->context, NULL);
    if (!variant->gallivm) {
       FREE(variant);
       return NULL;
index d7b77d51eb1361cf54edd0b5f940b8e793acce56..4bd201de581f13f8e7dc5ab3f4292f40b87f369b 100644 (file)
@@ -3270,7 +3270,7 @@ generate_variant(struct llvmpipe_context *lp,
    snprintf(module_name, sizeof(module_name), "fs%u_variant%u",
             shader->no, shader->variants_created);
 
-   variant->gallivm = gallivm_create(module_name, lp->context);
+   variant->gallivm = gallivm_create(module_name, lp->context, NULL);
    if (!variant->gallivm) {
       FREE(variant);
       return NULL;
index 8e46013a028634c2178d54b4aadb2eacf3cfb105..a2f70681c0073d55ea1382ba5fa1f014161ed9b6 100644 (file)
@@ -730,7 +730,7 @@ generate_setup_variant(struct lp_setup_variant_key *key,
    snprintf(func_name, sizeof(func_name), "setup_variant_%u",
             variant->no);
 
-   variant->gallivm = gallivm = gallivm_create(func_name, lp->context);
+   variant->gallivm = gallivm = gallivm_create(func_name, lp->context, NULL);
    if (!variant->gallivm) {
       goto fail;
    }
index be8cb0afb48210ac8deb9c4e3bcde7d25804e9ed..873dcf37fac0ae87a8263ba2fac66c04e468b26e 100644 (file)
@@ -417,7 +417,7 @@ test_unary(unsigned verbose, FILE *fp, const struct unary_test_t *test, unsigned
    }
 
    context = LLVMContextCreate();
-   gallivm = gallivm_create("test_module", context);
+   gallivm = gallivm_create("test_module", context, NULL);
 
    test_func = build_unary_test_func(gallivm, test, length, test_name);
 
index 98d6bbe50d4ecffe8fedb84436be2e4669fe8d2d..a549b0a22ffeaabaabba4641d9be64235dc546e6 100644 (file)
@@ -452,7 +452,7 @@ test_one(unsigned verbose,
       dump_blend_type(stdout, blend, type);
 
    context = LLVMContextCreate();
-   gallivm = gallivm_create("test_module", context);
+   gallivm = gallivm_create("test_module", context, NULL);
 
    func = add_blend_test(gallivm, blend, type);
 
index a4f313a0bb3049ecda05f32017381c06a67d93de..9f928973be96201c525257969652ace35d0a1aa3 100644 (file)
@@ -221,7 +221,7 @@ test_one(unsigned verbose,
    }
 
    context = LLVMContextCreate();
-   gallivm = gallivm_create("test_module", context);
+   gallivm = gallivm_create("test_module", context, NULL);
 
    func = add_conv_test(gallivm, src_type, num_srcs, dst_type, num_dsts);
 
index 9877a219c48ed964ae418e284ad7f399f92b7388..f18db724bb9b3d2a5fc84b2d9ee0023691e22e06 100644 (file)
@@ -150,7 +150,7 @@ test_format_float(unsigned verbose, FILE *fp,
    unsigned i, j, k, l;
 
    context = LLVMContextCreate();
-   gallivm = gallivm_create("test_module_float", context);
+   gallivm = gallivm_create("test_module_float", context, NULL);
 
    fetch = add_fetch_rgba_test(gallivm, verbose, desc,
                                lp_float32_vec4_type(), use_cache);
@@ -251,7 +251,7 @@ test_format_unorm8(unsigned verbose, FILE *fp,
    unsigned i, j, k, l;
 
    context = LLVMContextCreate();
-   gallivm = gallivm_create("test_module_unorm8", context);
+   gallivm = gallivm_create("test_module_unorm8", context, NULL);
 
    fetch = add_fetch_rgba_test(gallivm, verbose, desc,
                                lp_unorm8_vec4_type(), use_cache);
index 3d3dc5838dcb053a38f2f002f2989e3a9d542763..85ce1cda6956dfa932ae8a84ced4cf647ec38a8a 100644 (file)
@@ -96,7 +96,7 @@ test_printf(unsigned verbose, FILE *fp,
    boolean success = TRUE;
 
    context = LLVMContextCreate();
-   gallivm = gallivm_create("test_module", context);
+   gallivm = gallivm_create("test_module", context, NULL);
 
    test = add_printf_test(gallivm);
 
index 90333536421f0e7de68836a6014be1ad573081b5..5f4ba348629f3ecad71fcc46cd1dec44999c6dba 100644 (file)
@@ -327,7 +327,7 @@ struct BuilderSWR : public Builder {
       : Builder(pJitMgr)
    {
       pJitMgr->SetupNewModule();
-      gallivm = gallivm_create(pName, wrap(&JM()->mContext));
+      gallivm = gallivm_create(pName, wrap(&JM()->mContext), NULL);
       pJitMgr->mpCurrentModule = unwrap(gallivm->module);
    }