radeonsi: get rid of get_interp_param
[mesa.git] / src / gallium / drivers / radeonsi / si_shader.c
index b15c60d29e83b3d18f06df8783af8c79f549fe48..f3b94d7ff76fc3b8243251695a52cd3b32700012 100644 (file)
@@ -71,6 +71,14 @@ static void si_llvm_emit_barrier(const struct lp_build_tgsi_action *action,
 static void si_dump_shader_key(unsigned shader, union si_shader_key *key,
                               FILE *f);
 
+static void si_build_vs_prolog_function(struct si_shader_context *ctx,
+                                       union si_shader_part_key *key);
+static void si_build_vs_epilog_function(struct si_shader_context *ctx,
+                                       union si_shader_part_key *key);
+static void si_build_tcs_epilog_function(struct si_shader_context *ctx,
+                                        union si_shader_part_key *key);
+static void si_build_ps_prolog_function(struct si_shader_context *ctx,
+                                       union si_shader_part_key *key);
 static void si_build_ps_epilog_function(struct si_shader_context *ctx,
                                        union si_shader_part_key *key);
 
@@ -1175,45 +1183,6 @@ static int lookup_interp_param_index(unsigned interpolate, unsigned location)
        }
 }
 
-/* This shouldn't be used by explicit INTERP opcodes. */
-static unsigned select_interp_param(struct si_shader_context *ctx,
-                                   unsigned param)
-{
-       if (!ctx->no_prolog)
-               return param;
-
-       if (ctx->shader->key.ps.prolog.force_persp_sample_interp) {
-               switch (param) {
-               case SI_PARAM_PERSP_CENTROID:
-               case SI_PARAM_PERSP_CENTER:
-                       return SI_PARAM_PERSP_SAMPLE;
-               }
-       }
-       if (ctx->shader->key.ps.prolog.force_linear_sample_interp) {
-               switch (param) {
-               case SI_PARAM_LINEAR_CENTROID:
-               case SI_PARAM_LINEAR_CENTER:
-                       return SI_PARAM_LINEAR_SAMPLE;
-               }
-       }
-       if (ctx->shader->key.ps.prolog.force_persp_center_interp) {
-               switch (param) {
-               case SI_PARAM_PERSP_CENTROID:
-               case SI_PARAM_PERSP_SAMPLE:
-                       return SI_PARAM_PERSP_CENTER;
-               }
-       }
-       if (ctx->shader->key.ps.prolog.force_linear_center_interp) {
-               switch (param) {
-               case SI_PARAM_LINEAR_CENTROID:
-               case SI_PARAM_LINEAR_SAMPLE:
-                       return SI_PARAM_LINEAR_CENTER;
-               }
-       }
-
-       return param;
-}
-
 /**
  * Interpolate a fragment shader input.
  *
@@ -1331,56 +1300,6 @@ static void interp_fs_input(struct si_shader_context *ctx,
        }
 }
 
-/* LLVMGetParam with bc_optimize resolved. */
-static LLVMValueRef get_interp_param(struct si_shader_context *ctx,
-                                    int interp_param_idx)
-{
-       LLVMBuilderRef builder = ctx->gallivm.builder;
-       LLVMValueRef main_fn = ctx->main_fn;
-       LLVMValueRef param = NULL;
-
-       /* Handle PRIM_MASK[31] (bc_optimize). */
-       if (ctx->no_prolog &&
-           ((ctx->shader->key.ps.prolog.bc_optimize_for_persp &&
-             interp_param_idx == SI_PARAM_PERSP_CENTROID) ||
-            (ctx->shader->key.ps.prolog.bc_optimize_for_linear &&
-             interp_param_idx == SI_PARAM_LINEAR_CENTROID))) {
-               /* The shader should do: if (PRIM_MASK[31]) CENTROID = CENTER;
-                * The hw doesn't compute CENTROID if the whole wave only
-                * contains fully-covered quads.
-                */
-               LLVMValueRef bc_optimize =
-                       LLVMGetParam(main_fn, SI_PARAM_PRIM_MASK);
-               bc_optimize = LLVMBuildLShr(builder,
-                                           bc_optimize,
-                                           LLVMConstInt(ctx->i32, 31, 0), "");
-               bc_optimize = LLVMBuildTrunc(builder, bc_optimize, ctx->i1, "");
-
-               if (ctx->shader->key.ps.prolog.bc_optimize_for_persp &&
-                   interp_param_idx == SI_PARAM_PERSP_CENTROID) {
-                       param = LLVMBuildSelect(builder, bc_optimize,
-                                               LLVMGetParam(main_fn,
-                                                            SI_PARAM_PERSP_CENTER),
-                                               LLVMGetParam(main_fn,
-                                                            SI_PARAM_PERSP_CENTROID),
-                                               "");
-               }
-               if (ctx->shader->key.ps.prolog.bc_optimize_for_linear &&
-                   interp_param_idx == SI_PARAM_LINEAR_CENTROID) {
-                       param = LLVMBuildSelect(builder, bc_optimize,
-                                               LLVMGetParam(main_fn,
-                                                            SI_PARAM_LINEAR_CENTER),
-                                               LLVMGetParam(main_fn,
-                                                            SI_PARAM_LINEAR_CENTROID),
-                                               "");
-               }
-       }
-
-       if (!param)
-               param = LLVMGetParam(main_fn, interp_param_idx);
-       return param;
-}
-
 static void declare_input_fs(
        struct si_shader_context *radeon_bld,
        unsigned input_index,
@@ -1416,9 +1335,7 @@ static void declare_input_fs(
        if (interp_param_idx == -1)
                return;
        else if (interp_param_idx) {
-               interp_param_idx = select_interp_param(ctx,
-                                                      interp_param_idx);
-               interp_param = get_interp_param(ctx, interp_param_idx);
+               interp_param = LLVMGetParam(ctx->main_fn, interp_param_idx);
        }
 
        if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
@@ -2477,6 +2394,10 @@ handle_semantic:
        }
 }
 
+/**
+ * Forward all outputs from the vertex shader to the TES. This is only used
+ * for the fixed function TCS.
+ */
 static void si_copy_tcs_inputs(struct lp_build_tgsi_context *bld_base)
 {
        struct si_shader_context *ctx = si_shader_context(bld_base);
@@ -2631,6 +2552,8 @@ static void si_llvm_emit_tcs_epilogue(struct lp_build_tgsi_context *bld_base)
        struct si_shader_context *ctx = si_shader_context(bld_base);
        LLVMValueRef rel_patch_id, invocation_id, tf_lds_offset;
 
+       si_copy_tcs_inputs(bld_base);
+
        rel_patch_id = get_rel_patch_id(ctx);
        invocation_id = unpack_param(ctx, SI_PARAM_REL_IDS, 8, 5);
        tf_lds_offset = get_tcs_out_current_patch_data_offset(ctx);
@@ -2673,7 +2596,6 @@ static void si_llvm_emit_tcs_epilogue(struct lp_build_tgsi_context *bld_base)
                return;
        }
 
-       si_copy_tcs_inputs(bld_base);
        si_write_tess_factors(bld_base, rel_patch_id, invocation_id, tf_lds_offset);
 }
 
@@ -5159,7 +5081,7 @@ static void build_interp_intrinsic(const struct lp_build_tgsi_action *action,
        if (interp_param_idx == -1)
                return;
        else if (interp_param_idx)
-               interp_param = get_interp_param(ctx, interp_param_idx);
+               interp_param = LLVMGetParam(ctx->main_fn, interp_param_idx);
        else
                interp_param = NULL;
 
@@ -6767,12 +6689,55 @@ static bool si_compile_tgsi_main(struct si_shader_context *ctx,
        return true;
 }
 
+/**
+ * Compute the VS prolog key, which contains all the information needed to
+ * build the VS prolog function, and set shader->info bits where needed.
+ */
+static void si_get_vs_prolog_key(struct si_shader *shader,
+                                union si_shader_part_key *key)
+{
+       struct tgsi_shader_info *info = &shader->selector->info;
+
+       memset(key, 0, sizeof(*key));
+       key->vs_prolog.states = shader->key.vs.prolog;
+       key->vs_prolog.num_input_sgprs = shader->info.num_input_sgprs;
+       key->vs_prolog.last_input = MAX2(1, info->num_inputs) - 1;
+
+       /* Set the instanceID flag. */
+       for (unsigned i = 0; i < info->num_inputs; i++)
+               if (key->vs_prolog.states.instance_divisors[i])
+                       shader->info.uses_instanceid = true;
+}
+
+/**
+ * Compute the VS epilog key, which contains all the information needed to
+ * build the VS epilog function, and set the PrimitiveID output offset.
+ */
+static void si_get_vs_epilog_key(struct si_shader *shader,
+                                struct si_vs_epilog_bits *states,
+                                union si_shader_part_key *key)
+{
+       memset(key, 0, sizeof(*key));
+       key->vs_epilog.states = *states;
+
+       /* Set up the PrimitiveID output. */
+       if (shader->key.vs.epilog.export_prim_id) {
+               unsigned index = shader->selector->info.num_outputs;
+               unsigned offset = shader->info.nr_param_exports++;
+
+               key->vs_epilog.prim_id_param_offset = offset;
+               assert(index < ARRAY_SIZE(shader->info.vs_output_param_offset));
+               shader->info.vs_output_param_offset[index] = offset;
+       }
+}
+
 /**
  * Compute the PS prolog key, which contains all the information needed to
  * build the PS prolog function, and set related bits in shader->config.
  */
 static void si_get_ps_prolog_key(struct si_shader *shader,
-                                union si_shader_part_key *key)
+                                union si_shader_part_key *key,
+                                bool separate_prolog)
 {
        struct tgsi_shader_info *info = &shader->selector->info;
 
@@ -6852,19 +6817,26 @@ static void si_get_ps_prolog_key(struct si_shader *shader,
                                if (shader->key.ps.prolog.force_linear_center_interp)
                                        location = TGSI_INTERPOLATE_LOC_CENTER;
 
+                               /* The VGPR assignment for non-monolithic shaders
+                                * works because InitialPSInputAddr is set on the
+                                * main shader and PERSP_PULL_MODEL is never used.
+                                */
                                switch (location) {
                                case TGSI_INTERPOLATE_LOC_SAMPLE:
-                                       key->ps_prolog.color_interp_vgpr_index[i] = 6;
+                                       key->ps_prolog.color_interp_vgpr_index[i] =
+                                               separate_prolog ? 6 : 9;
                                        shader->config.spi_ps_input_ena |=
                                                S_0286CC_LINEAR_SAMPLE_ENA(1);
                                        break;
                                case TGSI_INTERPOLATE_LOC_CENTER:
-                                       key->ps_prolog.color_interp_vgpr_index[i] = 8;
+                                       key->ps_prolog.color_interp_vgpr_index[i] =
+                                               separate_prolog ? 8 : 11;
                                        shader->config.spi_ps_input_ena |=
                                                S_0286CC_LINEAR_CENTER_ENA(1);
                                        break;
                                case TGSI_INTERPOLATE_LOC_CENTROID:
-                                       key->ps_prolog.color_interp_vgpr_index[i] = 10;
+                                       key->ps_prolog.color_interp_vgpr_index[i] =
+                                               separate_prolog ? 10 : 13;
                                        shader->config.spi_ps_input_ena |=
                                                S_0286CC_LINEAR_CENTROID_ENA(1);
                                        break;
@@ -7122,8 +7094,13 @@ int si_compile_tgsi_shader(struct si_screen *sscreen,
        ctx.no_epilog = is_monolithic;
        ctx.separate_prolog = !is_monolithic;
 
-       if (ctx.type == PIPE_SHADER_FRAGMENT)
+       if (ctx.type == PIPE_SHADER_VERTEX ||
+           ctx.type == PIPE_SHADER_TESS_CTRL ||
+           ctx.type == PIPE_SHADER_TESS_EVAL ||
+           ctx.type == PIPE_SHADER_FRAGMENT) {
+               ctx.no_prolog = false;
                ctx.no_epilog = false;
+       }
 
        memset(shader->info.vs_output_param_offset, 0xff,
               sizeof(shader->info.vs_output_param_offset));
@@ -7138,17 +7115,77 @@ int si_compile_tgsi_shader(struct si_screen *sscreen,
                return -1;
        }
 
-       if (is_monolithic && ctx.type == PIPE_SHADER_FRAGMENT) {
+       if (is_monolithic && ctx.type == PIPE_SHADER_VERTEX) {
+               LLVMValueRef parts[3];
+               bool need_prolog;
+               bool need_epilog;
+
+               need_prolog = sel->info.num_inputs;
+               need_epilog = !shader->key.vs.as_es && !shader->key.vs.as_ls;
+
+               parts[need_prolog ? 1 : 0] = ctx.main_fn;
+
+               if (need_prolog) {
+                       union si_shader_part_key prolog_key;
+                       si_get_vs_prolog_key(shader, &prolog_key);
+                       si_build_vs_prolog_function(&ctx, &prolog_key);
+                       parts[0] = ctx.main_fn;
+               }
+
+               if (need_epilog) {
+                       union si_shader_part_key epilog_key;
+                       si_get_vs_epilog_key(shader, &shader->key.vs.epilog, &epilog_key);
+                       si_build_vs_epilog_function(&ctx, &epilog_key);
+                       parts[need_prolog ? 2 : 1] = ctx.main_fn;
+               }
+
+               si_build_wrapper_function(&ctx, parts, 1 + need_prolog + need_epilog,
+                                         need_prolog ? 1 : 0);
+       } else if (is_monolithic && ctx.type == PIPE_SHADER_TESS_CTRL) {
                LLVMValueRef parts[2];
                union si_shader_part_key epilog_key;
 
                parts[0] = ctx.main_fn;
 
-               si_get_ps_epilog_key(shader, &epilog_key);
-               si_build_ps_epilog_function(&ctx, &epilog_key);
+               memset(&epilog_key, 0, sizeof(epilog_key));
+               epilog_key.tcs_epilog.states = shader->key.tcs.epilog;
+               si_build_tcs_epilog_function(&ctx, &epilog_key);
+               parts[1] = ctx.main_fn;
+
+               si_build_wrapper_function(&ctx, parts, 2, 0);
+       } else if (is_monolithic && ctx.type == PIPE_SHADER_TESS_EVAL &&
+                  !shader->key.tes.as_es) {
+               LLVMValueRef parts[2];
+               union si_shader_part_key epilog_key;
+
+               parts[0] = ctx.main_fn;
+
+               si_get_vs_epilog_key(shader, &shader->key.tes.epilog, &epilog_key);
+               si_build_vs_epilog_function(&ctx, &epilog_key);
                parts[1] = ctx.main_fn;
 
                si_build_wrapper_function(&ctx, parts, 2, 0);
+       } else if (is_monolithic && ctx.type == PIPE_SHADER_FRAGMENT) {
+               LLVMValueRef parts[3];
+               union si_shader_part_key prolog_key;
+               union si_shader_part_key epilog_key;
+               bool need_prolog;
+
+               si_get_ps_prolog_key(shader, &prolog_key, false);
+               need_prolog = si_need_ps_prolog(&prolog_key);
+
+               parts[need_prolog ? 1 : 0] = ctx.main_fn;
+
+               if (need_prolog) {
+                       si_build_ps_prolog_function(&ctx, &prolog_key);
+                       parts[0] = ctx.main_fn;
+               }
+
+               si_get_ps_epilog_key(shader, &epilog_key);
+               si_build_ps_epilog_function(&ctx, &epilog_key);
+               parts[need_prolog ? 2 : 1] = ctx.main_fn;
+
+               si_build_wrapper_function(&ctx, parts, need_prolog ? 3 : 2, need_prolog ? 1 : 0);
        }
 
        mod = bld_base->base.gallivm->module;
@@ -7324,11 +7361,11 @@ si_get_shader_part(struct si_screen *sscreen,
 }
 
 /**
- * Create a vertex shader prolog.
+ * Build the vertex shader prolog function.
  *
  * The inputs are the same as VS (a lot of SGPRs and 4 VGPR system values).
  * All inputs are returned unmodified. The vertex load indices are
- * stored after them, which will used by the API VS for fetching inputs.
+ * stored after them, which will be used by the API VS for fetching inputs.
  *
  * For example, the expected outputs for instance_divisors[] = {0, 1, 2} are:
  *   input_v0,
@@ -7339,24 +7376,16 @@ si_get_shader_part(struct si_screen *sscreen,
  *   (InstanceID + StartInstance),
  *   (InstanceID / 2 + StartInstance)
  */
-static bool si_compile_vs_prolog(struct si_screen *sscreen,
-                                LLVMTargetMachineRef tm,
-                                struct pipe_debug_callback *debug,
-                                struct si_shader_part *out)
+static void si_build_vs_prolog_function(struct si_shader_context *ctx,
+                                       union si_shader_part_key *key)
 {
-       union si_shader_part_key *key = &out->key;
-       struct si_shader shader = {};
-       struct si_shader_context ctx;
-       struct gallivm_state *gallivm = &ctx.gallivm;
+       struct gallivm_state *gallivm = &ctx->gallivm;
        LLVMTypeRef *params, *returns;
        LLVMValueRef ret, func;
        int last_sgpr, num_params, num_returns, i;
-       bool status = true;
 
-       si_init_shader_ctx(&ctx, sscreen, &shader, tm);
-       ctx.type = PIPE_SHADER_VERTEX;
-       ctx.param_vertex_id = key->vs_prolog.num_input_sgprs;
-       ctx.param_instance_id = key->vs_prolog.num_input_sgprs + 3;
+       ctx->param_vertex_id = key->vs_prolog.num_input_sgprs;
+       ctx->param_instance_id = key->vs_prolog.num_input_sgprs + 3;
 
        /* 4 preloaded VGPRs + vertex load indices as prolog outputs */
        params = alloca((key->vs_prolog.num_input_sgprs + 4) *
@@ -7370,37 +7399,37 @@ static bool si_compile_vs_prolog(struct si_screen *sscreen,
        /* Declare input and output SGPRs. */
        num_params = 0;
        for (i = 0; i < key->vs_prolog.num_input_sgprs; i++) {
-               params[num_params++] = ctx.i32;
-               returns[num_returns++] = ctx.i32;
+               params[num_params++] = ctx->i32;
+               returns[num_returns++] = ctx->i32;
        }
        last_sgpr = num_params - 1;
 
        /* 4 preloaded VGPRs (outputs must be floats) */
        for (i = 0; i < 4; i++) {
-               params[num_params++] = ctx.i32;
-               returns[num_returns++] = ctx.f32;
+               params[num_params++] = ctx->i32;
+               returns[num_returns++] = ctx->f32;
        }
 
        /* Vertex load indices. */
        for (i = 0; i <= key->vs_prolog.last_input; i++)
-               returns[num_returns++] = ctx.f32;
+               returns[num_returns++] = ctx->f32;
 
        /* Create the function. */
-       si_create_function(&ctx, "vs_prolog", returns, num_returns, params,
+       si_create_function(ctx, "vs_prolog", returns, num_returns, params,
                           num_params, last_sgpr);
-       func = ctx.main_fn;
+       func = ctx->main_fn;
 
        /* Copy inputs to outputs. This should be no-op, as the registers match,
         * but it will prevent the compiler from overwriting them unintentionally.
         */
-       ret = ctx.return_value;
+       ret = ctx->return_value;
        for (i = 0; i < key->vs_prolog.num_input_sgprs; i++) {
                LLVMValueRef p = LLVMGetParam(func, i);
                ret = LLVMBuildInsertValue(gallivm->builder, ret, p, i, "");
        }
        for (i = num_params - 4; i < num_params; i++) {
                LLVMValueRef p = LLVMGetParam(func, i);
-               p = LLVMBuildBitCast(gallivm->builder, p, ctx.f32, "");
+               p = LLVMBuildBitCast(gallivm->builder, p, ctx->f32, "");
                ret = LLVMBuildInsertValue(gallivm->builder, ret, p, i, "");
        }
 
@@ -7411,23 +7440,44 @@ static bool si_compile_vs_prolog(struct si_screen *sscreen,
 
                if (divisor) {
                        /* InstanceID / Divisor + StartInstance */
-                       index = get_instance_index_for_fetch(&ctx,
+                       index = get_instance_index_for_fetch(ctx,
                                                             SI_SGPR_START_INSTANCE,
                                                             divisor);
                } else {
                        /* VertexID + BaseVertex */
                        index = LLVMBuildAdd(gallivm->builder,
-                                            LLVMGetParam(func, ctx.param_vertex_id),
+                                            LLVMGetParam(func, ctx->param_vertex_id),
                                             LLVMGetParam(func, SI_SGPR_BASE_VERTEX), "");
                }
 
-               index = LLVMBuildBitCast(gallivm->builder, index, ctx.f32, "");
+               index = LLVMBuildBitCast(gallivm->builder, index, ctx->f32, "");
                ret = LLVMBuildInsertValue(gallivm->builder, ret, index,
                                           num_params++, "");
        }
 
+       si_llvm_build_ret(ctx, ret);
+}
+
+/**
+ * Create a vertex shader prolog.
+ */
+static bool si_compile_vs_prolog(struct si_screen *sscreen,
+                                LLVMTargetMachineRef tm,
+                                struct pipe_debug_callback *debug,
+                                struct si_shader_part *out)
+{
+       union si_shader_part_key *key = &out->key;
+       struct si_shader shader = {};
+       struct si_shader_context ctx;
+       struct gallivm_state *gallivm = &ctx.gallivm;
+       bool status = true;
+
+       si_init_shader_ctx(&ctx, sscreen, &shader, tm);
+       ctx.type = PIPE_SHADER_VERTEX;
+
+       si_build_vs_prolog_function(&ctx, key);
+
        /* Compile. */
-       si_llvm_build_ret(&ctx, ret);
        si_llvm_finalize_module(&ctx,
                r600_extra_shader_checks(&sscreen->b, PIPE_SHADER_VERTEX));
 
@@ -7441,7 +7491,7 @@ static bool si_compile_vs_prolog(struct si_screen *sscreen,
 }
 
 /**
- * Compile the vertex shader epilog. This is also used by the tessellation
+ * Build the vertex shader epilog function. This is also used by the tessellation
  * evaluation shader compiled as VS.
  *
  * The input is PrimitiveID.
@@ -7449,21 +7499,13 @@ static bool si_compile_vs_prolog(struct si_screen *sscreen,
  * If PrimitiveID is required by the pixel shader, export it.
  * Otherwise, do nothing.
  */
-static bool si_compile_vs_epilog(struct si_screen *sscreen,
-                                LLVMTargetMachineRef tm,
-                                struct pipe_debug_callback *debug,
-                                struct si_shader_part *out)
+static void si_build_vs_epilog_function(struct si_shader_context *ctx,
+                                       union si_shader_part_key *key)
 {
-       union si_shader_part_key *key = &out->key;
-       struct si_shader_context ctx;
-       struct gallivm_state *gallivm = &ctx.gallivm;
-       struct lp_build_tgsi_context *bld_base = &ctx.soa.bld_base;
+       struct gallivm_state *gallivm = &ctx->gallivm;
+       struct lp_build_tgsi_context *bld_base = &ctx->soa.bld_base;
        LLVMTypeRef params[5];
        int num_params, i;
-       bool status = true;
-
-       si_init_shader_ctx(&ctx, sscreen, NULL, tm);
-       ctx.type = PIPE_SHADER_VERTEX;
 
        /* Declare input VGPRs. */
        num_params = key->vs_epilog.states.export_prim_id ?
@@ -7471,10 +7513,10 @@ static bool si_compile_vs_epilog(struct si_screen *sscreen,
        assert(num_params <= ARRAY_SIZE(params));
 
        for (i = 0; i < num_params; i++)
-               params[i] = ctx.f32;
+               params[i] = ctx->f32;
 
        /* Create the function. */
-       si_create_function(&ctx, "vs_epilog", NULL, 0, params, num_params, -1);
+       si_create_function(ctx, "vs_epilog", NULL, 0, params, num_params, -1);
 
        /* Emit exports. */
        if (key->vs_epilog.states.export_prim_id) {
@@ -7488,7 +7530,7 @@ static bool si_compile_vs_epilog(struct si_screen *sscreen,
                args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_PARAM +
                                               key->vs_epilog.prim_id_param_offset);
                args[4] = uint->zero; /* COMPR flag (0 = 32-bit export) */
-               args[5] = LLVMGetParam(ctx.main_fn,
+               args[5] = LLVMGetParam(ctx->main_fn,
                                       VS_EPILOG_PRIMID_LOC); /* X */
                args[6] = base->undef; /* Y */
                args[7] = base->undef; /* Z */
@@ -7499,8 +7541,29 @@ static bool si_compile_vs_epilog(struct si_screen *sscreen,
                                   args, 9, 0);
        }
 
-       /* Compile. */
        LLVMBuildRetVoid(gallivm->builder);
+}
+
+/**
+ * Compile the vertex shader epilog. This is also used by the tessellation
+ * evaluation shader compiled as VS.
+ */
+static bool si_compile_vs_epilog(struct si_screen *sscreen,
+                                LLVMTargetMachineRef tm,
+                                struct pipe_debug_callback *debug,
+                                struct si_shader_part *out)
+{
+       union si_shader_part_key *key = &out->key;
+       struct si_shader_context ctx;
+       struct gallivm_state *gallivm = &ctx.gallivm;
+       bool status = true;
+
+       si_init_shader_ctx(&ctx, sscreen, NULL, tm);
+       ctx.type = PIPE_SHADER_VERTEX;
+
+       si_build_vs_epilog_function(&ctx, key);
+
+       /* Compile. */
        si_llvm_finalize_module(&ctx,
                r600_extra_shader_checks(&sscreen->b, PIPE_SHADER_VERTEX));
 
@@ -7524,18 +7587,7 @@ static bool si_get_vs_epilog(struct si_screen *sscreen,
 {
        union si_shader_part_key epilog_key;
 
-       memset(&epilog_key, 0, sizeof(epilog_key));
-       epilog_key.vs_epilog.states = *states;
-
-       /* Set up the PrimitiveID output. */
-       if (shader->key.vs.epilog.export_prim_id) {
-               unsigned index = shader->selector->info.num_outputs;
-               unsigned offset = shader->info.nr_param_exports++;
-
-               epilog_key.vs_epilog.prim_id_param_offset = offset;
-               assert(index < ARRAY_SIZE(shader->info.vs_output_param_offset));
-               shader->info.vs_output_param_offset[index] = offset;
-       }
+       si_get_vs_epilog_key(shader, states, &epilog_key);
 
        shader->epilog = si_get_shader_part(sscreen, &sscreen->vs_epilogs,
                                            &epilog_key, tm, debug,
@@ -7553,13 +7605,9 @@ static bool si_shader_select_vs_parts(struct si_screen *sscreen,
 {
        struct tgsi_shader_info *info = &shader->selector->info;
        union si_shader_part_key prolog_key;
-       unsigned i;
 
        /* Get the prolog. */
-       memset(&prolog_key, 0, sizeof(prolog_key));
-       prolog_key.vs_prolog.states = shader->key.vs.prolog;
-       prolog_key.vs_prolog.num_input_sgprs = shader->info.num_input_sgprs;
-       prolog_key.vs_prolog.last_input = MAX2(1, info->num_inputs) - 1;
+       si_get_vs_prolog_key(shader, &prolog_key);
 
        /* The prolog is a no-op if there are no inputs. */
        if (info->num_inputs) {
@@ -7577,11 +7625,6 @@ static bool si_shader_select_vs_parts(struct si_screen *sscreen,
                              &shader->key.vs.epilog))
                return false;
 
-       /* Set the instanceID flag. */
-       for (i = 0; i < info->num_inputs; i++)
-               if (prolog_key.vs_prolog.states.instance_divisors[i])
-                       shader->info.uses_instanceid = true;
-
        return true;
 }
 
@@ -7601,6 +7644,51 @@ static bool si_shader_select_tes_parts(struct si_screen *sscreen,
                                &shader->key.tes.epilog);
 }
 
+/**
+ * Compile the TCS epilog function. This writes tesselation factors to memory
+ * based on the output primitive type of the tesselator (determined by TES).
+ */
+static void si_build_tcs_epilog_function(struct si_shader_context *ctx,
+                                        union si_shader_part_key *key)
+{
+       struct gallivm_state *gallivm = &ctx->gallivm;
+       struct lp_build_tgsi_context *bld_base = &ctx->soa.bld_base;
+       LLVMTypeRef params[16];
+       LLVMValueRef func;
+       int last_sgpr, num_params;
+
+       /* Declare inputs. Only RW_BUFFERS and TESS_FACTOR_OFFSET are used. */
+       params[SI_PARAM_RW_BUFFERS] = const_array(ctx->v16i8, SI_NUM_RW_BUFFERS);
+       params[SI_PARAM_CONST_BUFFERS] = ctx->i64;
+       params[SI_PARAM_SAMPLERS] = ctx->i64;
+       params[SI_PARAM_IMAGES] = ctx->i64;
+       params[SI_PARAM_SHADER_BUFFERS] = ctx->i64;
+       params[SI_PARAM_TCS_OFFCHIP_LAYOUT] = ctx->i32;
+       params[SI_PARAM_TCS_OUT_OFFSETS] = ctx->i32;
+       params[SI_PARAM_TCS_OUT_LAYOUT] = ctx->i32;
+       params[SI_PARAM_TCS_IN_LAYOUT] = ctx->i32;
+       params[ctx->param_oc_lds = SI_PARAM_TCS_OC_LDS] = ctx->i32;
+       params[SI_PARAM_TESS_FACTOR_OFFSET] = ctx->i32;
+       last_sgpr = SI_PARAM_TESS_FACTOR_OFFSET;
+       num_params = last_sgpr + 1;
+
+       params[num_params++] = ctx->i32; /* patch index within the wave (REL_PATCH_ID) */
+       params[num_params++] = ctx->i32; /* invocation ID within the patch */
+       params[num_params++] = ctx->i32; /* LDS offset where tess factors should be loaded from */
+
+       /* Create the function. */
+       si_create_function(ctx, "tcs_epilog", NULL, 0, params, num_params, last_sgpr);
+       declare_tess_lds(ctx);
+       func = ctx->main_fn;
+
+       si_write_tess_factors(bld_base,
+                             LLVMGetParam(func, last_sgpr + 1),
+                             LLVMGetParam(func, last_sgpr + 2),
+                             LLVMGetParam(func, last_sgpr + 3));
+
+       LLVMBuildRetVoid(gallivm->builder);
+}
+
 /**
  * Compile the TCS epilog. This writes tesselation factors to memory based on
  * the output primitive type of the tesselator (determined by TES).
@@ -7614,47 +7702,15 @@ static bool si_compile_tcs_epilog(struct si_screen *sscreen,
        struct si_shader shader = {};
        struct si_shader_context ctx;
        struct gallivm_state *gallivm = &ctx.gallivm;
-       struct lp_build_tgsi_context *bld_base = &ctx.soa.bld_base;
-       LLVMTypeRef params[16];
-       LLVMValueRef func;
-       int last_sgpr, num_params;
        bool status = true;
 
        si_init_shader_ctx(&ctx, sscreen, &shader, tm);
        ctx.type = PIPE_SHADER_TESS_CTRL;
        shader.key.tcs.epilog = key->tcs_epilog.states;
 
-       /* Declare inputs. Only RW_BUFFERS and TESS_FACTOR_OFFSET are used. */
-       params[SI_PARAM_RW_BUFFERS] = const_array(ctx.v16i8, SI_NUM_RW_BUFFERS);
-       params[SI_PARAM_CONST_BUFFERS] = ctx.i64;
-       params[SI_PARAM_SAMPLERS] = ctx.i64;
-       params[SI_PARAM_IMAGES] = ctx.i64;
-       params[SI_PARAM_SHADER_BUFFERS] = ctx.i64;
-       params[SI_PARAM_TCS_OFFCHIP_LAYOUT] = ctx.i32;
-       params[SI_PARAM_TCS_OUT_OFFSETS] = ctx.i32;
-       params[SI_PARAM_TCS_OUT_LAYOUT] = ctx.i32;
-       params[SI_PARAM_TCS_IN_LAYOUT] = ctx.i32;
-       params[ctx.param_oc_lds = SI_PARAM_TCS_OC_LDS] = ctx.i32;
-       params[SI_PARAM_TESS_FACTOR_OFFSET] = ctx.i32;
-       last_sgpr = SI_PARAM_TESS_FACTOR_OFFSET;
-       num_params = last_sgpr + 1;
-
-       params[num_params++] = ctx.i32; /* patch index within the wave (REL_PATCH_ID) */
-       params[num_params++] = ctx.i32; /* invocation ID within the patch */
-       params[num_params++] = ctx.i32; /* LDS offset where tess factors should be loaded from */
-
-       /* Create the function. */
-       si_create_function(&ctx, "tcs_epilog", NULL, 0, params, num_params, last_sgpr);
-       declare_tess_lds(&ctx);
-       func = ctx.main_fn;
-
-       si_write_tess_factors(bld_base,
-                             LLVMGetParam(func, last_sgpr + 1),
-                             LLVMGetParam(func, last_sgpr + 2),
-                             LLVMGetParam(func, last_sgpr + 3));
+       si_build_tcs_epilog_function(&ctx, key);
 
        /* Compile. */
-       LLVMBuildRetVoid(gallivm->builder);
        si_llvm_finalize_module(&ctx,
                r600_extra_shader_checks(&sscreen->b, PIPE_SHADER_TESS_CTRL));
 
@@ -8113,7 +8169,7 @@ static bool si_shader_select_ps_parts(struct si_screen *sscreen,
        union si_shader_part_key epilog_key;
 
        /* Get the prolog. */
-       si_get_ps_prolog_key(shader, &prolog_key);
+       si_get_ps_prolog_key(shader, &prolog_key, true);
 
        /* The prolog is a no-op if these aren't set. */
        if (si_need_ps_prolog(&prolog_key)) {