radeonsi: inline si_llvm_shader_type into si_llvm_create_func
[mesa.git] / src / gallium / drivers / radeonsi / si_shader_tgsi_setup.c
index 3e38f0d29acc7590f6c2c2c6106418296316fb30..de671ef80d6ed6f38f95d0c024d4beea28f7d1ae 100644 (file)
@@ -50,20 +50,6 @@ struct si_llvm_flow {
        LLVMBasicBlockRef loop_entry_block;
 };
 
-#define CPU_STRING_LEN 30
-#define FS_STRING_LEN 30
-#define TRIPLE_STRING_LEN 7
-
-/**
- * Shader types for the LLVM backend.
- */
-enum si_llvm_shader_type {
-       RADEON_LLVM_SHADER_PS = 0,
-       RADEON_LLVM_SHADER_VS = 1,
-       RADEON_LLVM_SHADER_GS = 2,
-       RADEON_LLVM_SHADER_CS = 3,
-};
-
 enum si_llvm_calling_convention {
        RADEON_LLVM_AMDGPU_VS = 87,
        RADEON_LLVM_AMDGPU_GS = 88,
@@ -79,45 +65,6 @@ void si_llvm_add_attribute(LLVMValueRef F, const char *name, int value)
        LLVMAddTargetDependentFunctionAttr(F, name, str);
 }
 
-/**
- * Set the shader type we want to compile
- *
- * @param type shader type to set
- */
-void si_llvm_shader_type(LLVMValueRef F, unsigned type)
-{
-       enum si_llvm_shader_type llvm_type;
-       enum si_llvm_calling_convention calling_conv;
-
-       switch (type) {
-       case PIPE_SHADER_VERTEX:
-       case PIPE_SHADER_TESS_CTRL:
-       case PIPE_SHADER_TESS_EVAL:
-               llvm_type = RADEON_LLVM_SHADER_VS;
-               calling_conv = RADEON_LLVM_AMDGPU_VS;
-               break;
-       case PIPE_SHADER_GEOMETRY:
-               llvm_type = RADEON_LLVM_SHADER_GS;
-               calling_conv = RADEON_LLVM_AMDGPU_GS;
-               break;
-       case PIPE_SHADER_FRAGMENT:
-               llvm_type = RADEON_LLVM_SHADER_PS;
-               calling_conv = RADEON_LLVM_AMDGPU_PS;
-               break;
-       case PIPE_SHADER_COMPUTE:
-               llvm_type = RADEON_LLVM_SHADER_CS;
-               calling_conv = RADEON_LLVM_AMDGPU_CS;
-               break;
-       default:
-               unreachable("Unhandle shader type");
-       }
-
-       if (HAVE_LLVM >= 0x309)
-               LLVMSetFunctionCallConv(F, calling_conv);
-       else
-               si_llvm_add_attribute(F, "ShaderType", llvm_type);
-}
-
 static void init_amdgpu_target()
 {
        gallivm_init_llvm_targets();
@@ -126,6 +73,9 @@ static void init_amdgpu_target()
        LLVMInitializeAMDGPUTargetMC();
        LLVMInitializeAMDGPUAsmPrinter();
 
+       /* For inline assembly. */
+       LLVMInitializeAMDGPUAsmParser();
+
        if (HAVE_LLVM >= 0x0400) {
                /*
                 * Workaround for bug in llvm 4.0 that causes image intrinsics
@@ -762,13 +712,29 @@ static LLVMValueRef fetch_system_value(struct lp_build_tgsi_context *bld_base,
                                       unsigned swizzle)
 {
        struct si_shader_context *ctx = si_shader_context(bld_base);
-       struct gallivm_state *gallivm = &ctx->gallivm;
-
+       LLVMBuilderRef builder = ctx->gallivm.builder;
        LLVMValueRef cval = ctx->system_values[reg->Register.Index];
+
+       if (tgsi_type_is_64bit(type)) {
+               LLVMValueRef lo, hi;
+
+               assert(swizzle == 0 || swizzle == 2);
+
+               lo = LLVMBuildExtractElement(
+                       builder, cval, LLVMConstInt(ctx->i32, swizzle, 0), "");
+               hi = LLVMBuildExtractElement(
+                       builder, cval, LLVMConstInt(ctx->i32, swizzle + 1, 0), "");
+
+               return si_llvm_emit_fetch_64bit(bld_base, type, lo, hi);
+       }
+
        if (LLVMGetTypeKind(LLVMTypeOf(cval)) == LLVMVectorTypeKind) {
-               cval = LLVMBuildExtractElement(gallivm->builder, cval,
-                                              LLVMConstInt(ctx->i32, swizzle, 0), "");
+               cval = LLVMBuildExtractElement(
+                       builder, cval, LLVMConstInt(ctx->i32, swizzle, 0), "");
+       } else {
+               assert(swizzle == 0);
        }
+
        return bitcast(bld_base, type, cval);
 }
 
@@ -829,10 +795,8 @@ static void emit_declaration(struct lp_build_tgsi_context *bld_base,
                         * FIXME: We shouldn't need to have the non-alloca
                         * code path for arrays. LLVM should be smart enough to
                         * promote allocas into registers when profitable.
-                        *
-                        * LLVM 3.8 crashes with this.
                         */
-                       if ((HAVE_LLVM >= 0x0309 && array_size > 16) ||
+                       if (array_size > 16 ||
                            /* TODO: VGPR indexing is buggy on GFX9. */
                            ctx->screen->b.chip_class == GFX9) {
                                array_alloca = LLVMBuildAlloca(builder,
@@ -1237,10 +1201,7 @@ static void emit_immediate(struct lp_build_tgsi_context *bld_base,
 
 void si_llvm_context_init(struct si_shader_context *ctx,
                          struct si_screen *sscreen,
-                         struct si_shader *shader,
-                         LLVMTargetMachineRef tm,
-                         const struct tgsi_shader_info *info,
-                         const struct tgsi_token *tokens)
+                         LLVMTargetMachineRef tm)
 {
        struct lp_type type;
 
@@ -1250,23 +1211,19 @@ void si_llvm_context_init(struct si_shader_context *ctx,
         * helper functions in the gallivm module.
         */
        memset(ctx, 0, sizeof(*ctx));
-       ctx->shader = shader;
        ctx->screen = sscreen;
        ctx->tm = tm;
-       ctx->type = info ? info->processor : -1;
 
        ctx->gallivm.context = LLVMContextCreate();
        ctx->gallivm.module = LLVMModuleCreateWithNameInContext("tgsi",
                                                ctx->gallivm.context);
        LLVMSetTarget(ctx->gallivm.module, "amdgcn--");
 
-#if HAVE_LLVM >= 0x0309
        LLVMTargetDataRef data_layout = LLVMCreateTargetDataLayout(tm);
        char *data_layout_str = LLVMCopyStringRepOfTargetData(data_layout);
        LLVMSetDataLayout(ctx->gallivm.module, data_layout_str);
        LLVMDisposeTargetData(data_layout);
        LLVMDisposeMessage(data_layout_str);
-#endif
 
        bool unsafe_fpmath = (sscreen->b.debug_flags & DBG_UNSAFE_MATH) != 0;
        enum lp_float_mode float_mode =
@@ -1282,24 +1239,6 @@ void si_llvm_context_init(struct si_shader_context *ctx,
 
        struct lp_build_tgsi_context *bld_base = &ctx->bld_base;
 
-       bld_base->info = info;
-
-       if (info && info->array_max[TGSI_FILE_TEMPORARY] > 0) {
-               int size = info->array_max[TGSI_FILE_TEMPORARY];
-
-               ctx->temp_arrays = CALLOC(size, sizeof(ctx->temp_arrays[0]));
-               ctx->temp_array_allocas = CALLOC(size, sizeof(ctx->temp_array_allocas[0]));
-
-               if (tokens)
-                       tgsi_scan_arrays(tokens, TGSI_FILE_TEMPORARY, size,
-                                        ctx->temp_arrays);
-       }
-
-       if (info && info->file_max[TGSI_FILE_IMMEDIATE] >= 0) {
-               int size = info->file_max[TGSI_FILE_IMMEDIATE] + 1;
-               ctx->imms = MALLOC(size * TGSI_NUM_CHANNELS * sizeof(LLVMValueRef));
-       }
-
        type.floating = true;
        type.fixed = false;
        type.sign = true;
@@ -1316,17 +1255,10 @@ void si_llvm_context_init(struct si_shader_context *ctx,
        lp_build_context_init(&ctx->bld_base.int64_bld, &ctx->gallivm, lp_int_type(type));
 
        bld_base->soa = 1;
-       bld_base->emit_store = si_llvm_emit_store;
        bld_base->emit_swizzle = emit_swizzle;
        bld_base->emit_declaration = emit_declaration;
        bld_base->emit_immediate = emit_immediate;
 
-       bld_base->emit_fetch_funcs[TGSI_FILE_IMMEDIATE] = si_llvm_emit_fetch;
-       bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = si_llvm_emit_fetch;
-       bld_base->emit_fetch_funcs[TGSI_FILE_TEMPORARY] = si_llvm_emit_fetch;
-       bld_base->emit_fetch_funcs[TGSI_FILE_OUTPUT] = si_llvm_emit_fetch;
-       bld_base->emit_fetch_funcs[TGSI_FILE_SYSTEM_VALUE] = fetch_system_value;
-
        /* metadata allowing 2.5 ULP */
        ctx->fpmath_md_kind = LLVMGetMDKindIDInContext(ctx->gallivm.context,
                                                       "fpmath", 6);
@@ -1352,7 +1284,6 @@ void si_llvm_context_init(struct si_shader_context *ctx,
        ctx->i64 = LLVMInt64TypeInContext(ctx->gallivm.context);
        ctx->i128 = LLVMIntTypeInContext(ctx->gallivm.context, 128);
        ctx->f32 = LLVMFloatTypeInContext(ctx->gallivm.context);
-       ctx->v16i8 = LLVMVectorType(ctx->i8, 16);
        ctx->v2i32 = LLVMVectorType(ctx->i32, 2);
        ctx->v4i32 = LLVMVectorType(ctx->i32, 4);
        ctx->v4f32 = LLVMVectorType(ctx->f32, 4);
@@ -1362,6 +1293,67 @@ void si_llvm_context_init(struct si_shader_context *ctx,
        ctx->i32_1 = LLVMConstInt(ctx->i32, 1, 0);
 }
 
+/* Set the context to a certain TGSI shader. Can be called repeatedly
+ * to change the shader. */
+void si_llvm_context_set_tgsi(struct si_shader_context *ctx,
+                             struct si_shader *shader)
+{
+       const struct tgsi_shader_info *info = NULL;
+       const struct tgsi_token *tokens = NULL;
+
+       if (shader && shader->selector) {
+               info = &shader->selector->info;
+               tokens = shader->selector->tokens;
+       }
+
+       ctx->shader = shader;
+       ctx->type = info ? info->processor : -1;
+       ctx->bld_base.info = info;
+
+       /* Clean up the old contents. */
+       FREE(ctx->temp_arrays);
+       ctx->temp_arrays = NULL;
+       FREE(ctx->temp_array_allocas);
+       ctx->temp_array_allocas = NULL;
+
+       FREE(ctx->imms);
+       ctx->imms = NULL;
+       ctx->imms_num = 0;
+
+       FREE(ctx->temps);
+       ctx->temps = NULL;
+       ctx->temps_count = 0;
+
+       if (!info || !tokens)
+               return;
+
+       if (info->array_max[TGSI_FILE_TEMPORARY] > 0) {
+               int size = info->array_max[TGSI_FILE_TEMPORARY];
+
+               ctx->temp_arrays = CALLOC(size, sizeof(ctx->temp_arrays[0]));
+               ctx->temp_array_allocas = CALLOC(size, sizeof(ctx->temp_array_allocas[0]));
+
+               tgsi_scan_arrays(tokens, TGSI_FILE_TEMPORARY, size,
+                                ctx->temp_arrays);
+       }
+       if (info->file_max[TGSI_FILE_IMMEDIATE] >= 0) {
+               int size = info->file_max[TGSI_FILE_IMMEDIATE] + 1;
+               ctx->imms = MALLOC(size * TGSI_NUM_CHANNELS * sizeof(LLVMValueRef));
+       }
+
+       /* Re-set these to start with a clean slate. */
+       ctx->bld_base.num_instructions = 0;
+       ctx->bld_base.pc = 0;
+       memset(ctx->outputs, 0, sizeof(ctx->outputs));
+
+       ctx->bld_base.emit_store = si_llvm_emit_store;
+       ctx->bld_base.emit_fetch_funcs[TGSI_FILE_IMMEDIATE] = si_llvm_emit_fetch;
+       ctx->bld_base.emit_fetch_funcs[TGSI_FILE_INPUT] = si_llvm_emit_fetch;
+       ctx->bld_base.emit_fetch_funcs[TGSI_FILE_TEMPORARY] = si_llvm_emit_fetch;
+       ctx->bld_base.emit_fetch_funcs[TGSI_FILE_OUTPUT] = si_llvm_emit_fetch;
+       ctx->bld_base.emit_fetch_funcs[TGSI_FILE_SYSTEM_VALUE] = fetch_system_value;
+}
+
 void si_llvm_create_func(struct si_shader_context *ctx,
                         const char *name,
                         LLVMTypeRef *return_types, unsigned num_return_elems,
@@ -1369,6 +1361,7 @@ void si_llvm_create_func(struct si_shader_context *ctx,
 {
        LLVMTypeRef main_fn_type, ret_type;
        LLVMBasicBlockRef main_fn_body;
+       enum si_llvm_calling_convention call_conv;
 
        if (num_return_elems)
                ret_type = LLVMStructTypeInContext(ctx->gallivm.context,
@@ -1384,22 +1377,47 @@ void si_llvm_create_func(struct si_shader_context *ctx,
        main_fn_body = LLVMAppendBasicBlockInContext(ctx->gallivm.context,
                        ctx->main_fn, "main_body");
        LLVMPositionBuilderAtEnd(ctx->gallivm.builder, main_fn_body);
+
+       switch (ctx->type) {
+       case PIPE_SHADER_VERTEX:
+       case PIPE_SHADER_TESS_CTRL:
+       case PIPE_SHADER_TESS_EVAL:
+               call_conv = RADEON_LLVM_AMDGPU_VS;
+               break;
+       case PIPE_SHADER_GEOMETRY:
+               call_conv = RADEON_LLVM_AMDGPU_GS;
+               break;
+       case PIPE_SHADER_FRAGMENT:
+               call_conv = RADEON_LLVM_AMDGPU_PS;
+               break;
+       case PIPE_SHADER_COMPUTE:
+               call_conv = RADEON_LLVM_AMDGPU_CS;
+               break;
+       default:
+               unreachable("Unhandle shader type");
+       }
+
+       LLVMSetFunctionCallConv(ctx->main_fn, call_conv);
 }
 
-void si_llvm_finalize_module(struct si_shader_context *ctx,
-                            bool run_verifier)
+void si_llvm_optimize_module(struct si_shader_context *ctx)
 {
        struct gallivm_state *gallivm = &ctx->gallivm;
        const char *triple = LLVMGetTarget(gallivm->module);
        LLVMTargetLibraryInfoRef target_library_info;
 
+       /* Dump LLVM IR before any optimization passes */
+       if (ctx->screen->b.debug_flags & DBG_PREOPT_IR &&
+           r600_can_dump_shader(&ctx->screen->b, ctx->type))
+               LLVMDumpModule(ctx->gallivm.module);
+
        /* Create the pass manager */
        gallivm->passmgr = LLVMCreatePassManager();
 
        target_library_info = gallivm_create_target_library_info(triple);
        LLVMAddTargetLibraryInfo(target_library_info, gallivm->passmgr);
 
-       if (run_verifier)
+       if (r600_extra_shader_checks(&ctx->screen->b, ctx->type))
                LLVMAddVerifierPass(gallivm->passmgr);
 
        LLVMAddAlwaysInlinerPass(gallivm->passmgr);