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);
 
    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);
 
    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);
 
    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);
 
 
       LLVMDisposeModule(gallivm->module);
    }
 
+   if (gallivm->cache) {
+      free(gallivm->cache->data);
+   }
    FREE(gallivm->module_name);
 
    if (gallivm->target) {
    gallivm->passmgr = NULL;
    gallivm->context = NULL;
    gallivm->builder = NULL;
+   gallivm->cache = NULL;
 }
 
 
 
       ret = lp_build_create_jit_compiler_for_module(&gallivm->engine,
                                                     &gallivm->code,
+                                                    gallivm->cache,
                                                     gallivm->module,
                                                     gallivm->memorymgr,
                                                     (unsigned) optlevel,
  */
 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);
       return FALSE;
 
    gallivm->context = context;
-
+   gallivm->cache = cache;
    if (!gallivm->context)
       goto fail;
 
  * 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;
       }
 
 extern "C" {
 #endif
 
+struct lp_cached_code;
 struct gallivm_state
 {
    char *module_name;
    LLVMBuilderRef builder;
    LLVMMCJITMemoryManagerRef memorymgr;
    struct lp_generated_code *code;
+   struct lp_cached_code *cache;
    unsigned compiled;
    LLVMValueRef coro_malloc_hook;
    LLVMValueRef coro_free_hook;
 
 
 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);
 
 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,
 
 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;
 
 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,
 
    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;
 
    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;
 
    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;
    }
 
    }
 
    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);
 
 
       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);
 
 
    }
 
    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);
 
 
    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);
    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);
 
    boolean success = TRUE;
 
    context = LLVMContextCreate();
-   gallivm = gallivm_create("test_module", context);
+   gallivm = gallivm_create("test_module", context, NULL);
 
    test = add_printf_test(gallivm);
 
 
       : Builder(pJitMgr)
    {
       pJitMgr->SetupNewModule();
-      gallivm = gallivm_create(pName, wrap(&JM()->mContext));
+      gallivm = gallivm_create(pName, wrap(&JM()->mContext), NULL);
       pJitMgr->mpCurrentModule = unwrap(gallivm->module);
    }