ac/radv: move llvm compiler info to struct and init in one place
authorDave Airlie <airlied@redhat.com>
Tue, 26 Jun 2018 23:27:03 +0000 (09:27 +1000)
committerDave Airlie <airlied@redhat.com>
Wed, 4 Jul 2018 00:29:16 +0000 (10:29 +1000)
This ports radv to the shared code, however due to a bug in LLVM
version prior to 7, radv cannot add target info at this stage,
as it would leak one for every shader compile, however I'd prefer
to keep this llvm damage in the shared code, since it isn't the
driver at fault here. We just add a flag to denote if the driver
can support leaking the target info or not, and the common code
does the right thing depending on the llvm version.

Reviewed-by: Marek Olšák <marek.olsak@amd.com>
src/amd/common/ac_llvm_util.c
src/amd/common/ac_llvm_util.h
src/amd/vulkan/radv_nir_to_llvm.c
src/amd/vulkan/radv_private.h
src/amd/vulkan/radv_shader.c
src/gallium/drivers/radeonsi/si_pipe.c

index fa85c625b336907caa7475c455562588f817aed5..2be2edf1e6acb9a58942eb91b961ab91b1a3894a 100644 (file)
@@ -285,6 +285,7 @@ ac_count_scratch_private_memory(LLVMValueRef function)
 
 bool
 ac_init_llvm_compiler(struct ac_llvm_compiler *compiler,
+                     bool okay_to_leak_target_library_info,
                      enum radeon_family family,
                      enum ac_target_machine_options tm_options)
 {
@@ -296,10 +297,12 @@ ac_init_llvm_compiler(struct ac_llvm_compiler *compiler,
        if (!compiler->tm)
                return false;
 
-       compiler->target_library_info =
-               ac_create_target_library_info(triple);
-       if (!compiler->target_library_info)
-               goto fail;
+       if (okay_to_leak_target_library_info || (HAVE_LLVM >= 0x0700)) {
+               compiler->target_library_info =
+                       ac_create_target_library_info(triple);
+               if (!compiler->target_library_info)
+                       goto fail;
+       }
 
        compiler->passmgr = ac_create_passmgr(compiler->target_library_info,
                                              tm_options & AC_TM_CHECK_IR);
index cd56ff5f41cf31199c977a86b4324142f13706bb..ad463c695642efebf0dfe2de5ed337edcf47df73 100644 (file)
@@ -127,6 +127,7 @@ void ac_init_llvm_once(void);
 
 
 bool ac_init_llvm_compiler(struct ac_llvm_compiler *compiler,
+                          bool okay_to_leak_target_library_info,
                           enum radeon_family family,
                           enum ac_target_machine_options tm_options);
 void ac_destroy_llvm_compiler(struct ac_llvm_compiler *compiler);
index 9cd29e257fb743594bc578d89baa5e50628698b5..45ac0854c17a74f3e6d7775b30d54d562c18ae8e 100644 (file)
@@ -3127,8 +3127,7 @@ static void prepare_gs_input_vgprs(struct radv_shader_context *ctx)
 
 
 static
-LLVMModuleRef ac_translate_nir_to_llvm(LLVMTargetMachineRef tm,
-                                       LLVMPassManagerRef passmgr,
+LLVMModuleRef ac_translate_nir_to_llvm(struct ac_llvm_compiler *ac_llvm,
                                        struct nir_shader *const *shaders,
                                        int shader_count,
                                        struct radv_shader_variant_info *shader_info,
@@ -3142,7 +3141,7 @@ LLVMModuleRef ac_translate_nir_to_llvm(LLVMTargetMachineRef tm,
 
        ac_llvm_context_init(&ctx.ac, ctx.context, options->chip_class,
                             options->family);
-       ctx.ac.module = ac_create_module(tm, ctx.context);
+       ctx.ac.module = ac_create_module(ac_llvm->tm, ctx.context);
 
        enum ac_float_mode float_mode =
                options->unsafe_math ? AC_FLOAT_MODE_UNSAFE_FP_MATH :
@@ -3297,7 +3296,7 @@ LLVMModuleRef ac_translate_nir_to_llvm(LLVMTargetMachineRef tm,
        if (options->dump_preoptir)
                ac_dump_module(ctx.ac.module);
 
-       ac_llvm_finalize_module(&ctx, passmgr, options);
+       ac_llvm_finalize_module(&ctx, ac_llvm->passmgr, options);
 
        if (shader_count == 1)
                ac_nir_eliminate_const_vs_outputs(&ctx);
@@ -3327,7 +3326,7 @@ static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di, void *context)
 
 static unsigned ac_llvm_compile(LLVMModuleRef M,
                                 struct ac_shader_binary *binary,
-                                LLVMTargetMachineRef tm)
+                                struct ac_llvm_compiler *ac_llvm)
 {
        unsigned retval = 0;
        char *err;
@@ -3344,7 +3343,7 @@ static unsigned ac_llvm_compile(LLVMModuleRef M,
                                        &retval);
 
        /* Compile IR*/
-       mem_err = LLVMTargetMachineEmitToMemoryBuffer(tm, M, LLVMObjectFile,
+       mem_err = LLVMTargetMachineEmitToMemoryBuffer(ac_llvm->tm, M, LLVMObjectFile,
                                                      &err, &out_buffer);
 
        /* Process Errors/Warnings */
@@ -3368,7 +3367,7 @@ out:
        return retval;
 }
 
-static void ac_compile_llvm_module(LLVMTargetMachineRef tm,
+static void ac_compile_llvm_module(struct ac_llvm_compiler *ac_llvm,
                                   LLVMModuleRef llvm_module,
                                   struct ac_shader_binary *binary,
                                   struct ac_shader_config *config,
@@ -3387,7 +3386,7 @@ static void ac_compile_llvm_module(LLVMTargetMachineRef tm,
                LLVMDisposeMessage(llvm_ir);
        }
 
-       int v = ac_llvm_compile(llvm_module, binary, tm);
+       int v = ac_llvm_compile(llvm_module, binary, ac_llvm);
        if (v) {
                fprintf(stderr, "compile failed\n");
        }
@@ -3497,8 +3496,7 @@ ac_fill_shader_info(struct radv_shader_variant_info *shader_info, struct nir_sha
 }
 
 void
-radv_compile_nir_shader(LLVMTargetMachineRef tm,
-                       LLVMPassManagerRef passmgr,
+radv_compile_nir_shader(struct ac_llvm_compiler *ac_llvm,
                        struct ac_shader_binary *binary,
                        struct ac_shader_config *config,
                        struct radv_shader_variant_info *shader_info,
@@ -3509,10 +3507,10 @@ radv_compile_nir_shader(LLVMTargetMachineRef tm,
 
        LLVMModuleRef llvm_module;
 
-       llvm_module = ac_translate_nir_to_llvm(tm, passmgr, nir, nir_count, shader_info,
+       llvm_module = ac_translate_nir_to_llvm(ac_llvm, nir, nir_count, shader_info,
                                               options);
 
-       ac_compile_llvm_module(tm, llvm_module, binary, config, shader_info,
+       ac_compile_llvm_module(ac_llvm, llvm_module, binary, config, shader_info,
                               nir[0]->info.stage, options);
 
        for (int i = 0; i < nir_count; ++i)
@@ -3570,8 +3568,7 @@ ac_gs_copy_shader_emit(struct radv_shader_context *ctx)
 }
 
 void
-radv_compile_gs_copy_shader(LLVMTargetMachineRef tm,
-                           LLVMPassManagerRef passmgr,
+radv_compile_gs_copy_shader(struct ac_llvm_compiler *ac_llvm,
                            struct nir_shader *geom_shader,
                            struct ac_shader_binary *binary,
                            struct ac_shader_config *config,
@@ -3585,7 +3582,7 @@ radv_compile_gs_copy_shader(LLVMTargetMachineRef tm,
 
        ac_llvm_context_init(&ctx.ac, ctx.context, options->chip_class,
                             options->family);
-       ctx.ac.module = ac_create_module(tm, ctx.context);
+       ctx.ac.module = ac_create_module(ac_llvm->tm, ctx.context);
 
        ctx.is_gs_copy_shader = true;
 
@@ -3616,8 +3613,8 @@ radv_compile_gs_copy_shader(LLVMTargetMachineRef tm,
 
        LLVMBuildRetVoid(ctx.ac.builder);
 
-       ac_llvm_finalize_module(&ctx, passmgr, options);
+       ac_llvm_finalize_module(&ctx, ac_llvm->passmgr, options);
 
-       ac_compile_llvm_module(tm, ctx.ac.module, binary, config, shader_info,
+       ac_compile_llvm_module(ac_llvm, ctx.ac.module, binary, config, shader_info,
                               MESA_SHADER_VERTEX, options);
 }
index cf416fbe8f6677b5fb730f9546d7fb44c9fa30b9..0a34d79a206b57093fdbaaa2c41438a240df187a 100644 (file)
@@ -58,6 +58,7 @@
 #include "ac_gpu_info.h"
 #include "ac_surface.h"
 #include "ac_llvm_build.h"
+#include "ac_llvm_util.h"
 #include "radv_descriptor_set.h"
 #include "radv_extensions.h"
 #include "radv_cs.h"
@@ -1795,16 +1796,14 @@ struct radv_fence {
 struct radv_shader_variant_info;
 struct radv_nir_compiler_options;
 
-void radv_compile_gs_copy_shader(LLVMTargetMachineRef tm,
-                                LLVMPassManagerRef passmgr,
+void radv_compile_gs_copy_shader(struct ac_llvm_compiler *ac_llvm,
                                 struct nir_shader *geom_shader,
                                 struct ac_shader_binary *binary,
                                 struct ac_shader_config *config,
                                 struct radv_shader_variant_info *shader_info,
                                 const struct radv_nir_compiler_options *option);
 
-void radv_compile_nir_shader(LLVMTargetMachineRef tm,
-                            LLVMPassManagerRef passmgr,
+void radv_compile_nir_shader(struct ac_llvm_compiler *ac_llvm,
                             struct ac_shader_binary *binary,
                             struct ac_shader_config *config,
                             struct radv_shader_variant_info *shader_info,
index d115f21cf327eb3c95802ba95b4dd2379c31a0d5..5cca761d89eebbbc075345b2084572e48f7f3a4d 100644 (file)
@@ -541,8 +541,7 @@ shader_variant_create(struct radv_device *device,
        enum ac_target_machine_options tm_options = 0;
        struct radv_shader_variant *variant;
        struct ac_shader_binary binary;
-       LLVMTargetMachineRef tm;
-       LLVMPassManagerRef passmgr;
+       struct ac_llvm_compiler ac_llvm;
 
        variant = calloc(1, sizeof(struct radv_shader_variant));
        if (!variant)
@@ -566,21 +565,19 @@ shader_variant_create(struct radv_device *device,
                tm_options |= AC_TM_CHECK_IR;
 
        radv_init_llvm_once();
-       tm = ac_create_target_machine(chip_family, tm_options, NULL);
-       passmgr = ac_create_passmgr(NULL, tm_options & AC_TM_CHECK_IR);
+       ac_init_llvm_compiler(&ac_llvm, false, chip_family, tm_options);
        if (gs_copy_shader) {
                assert(shader_count == 1);
-               radv_compile_gs_copy_shader(tm, passmgr, *shaders, &binary,
+               radv_compile_gs_copy_shader(&ac_llvm, *shaders, &binary,
                                            &variant->config, &variant->info,
                                            options);
        } else {
-               radv_compile_nir_shader(tm, passmgr, &binary, &variant->config,
+               radv_compile_nir_shader(&ac_llvm, &binary, &variant->config,
                                        &variant->info, shaders, shader_count,
                                        options);
        }
 
-       LLVMDisposePassManager(passmgr);
-       LLVMDisposeTargetMachine(tm);
+       ac_destroy_llvm_compiler(&ac_llvm);
 
        radv_fill_shader_variant(device, variant, &binary, stage);
 
index ad0ca7c6169f2db3be9a1e2cb7f762bc88575c90..d047807dd20ff1cb4fb5417aac0c805eb196dee9 100644 (file)
@@ -114,7 +114,7 @@ static void si_init_compiler(struct si_screen *sscreen,
                (!sscreen->llvm_has_working_vgpr_indexing ? AC_TM_PROMOTE_ALLOCA_TO_SCRATCH : 0) |
                (sscreen->debug_flags & DBG(CHECK_IR) ? AC_TM_CHECK_IR : 0);
 
-       ac_init_llvm_compiler(compiler, sscreen->info.family, tm_options);
+       ac_init_llvm_compiler(compiler, true, sscreen->info.family, tm_options);
 }
 
 static void si_destroy_compiler(struct ac_llvm_compiler *compiler)