radeonsi: move si_shader_binary_upload out of si_compile_llvm
[mesa.git] / src / gallium / drivers / radeonsi / si_shader.c
index b453d97fa7e5e0f17147404aa8d0229631a81e7c..2a206dd62908f03ec07d16d6d3e1dcd4f087d9cb 100644 (file)
@@ -1302,18 +1302,8 @@ static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
        if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
                int cbuf = target - V_008DFC_SQ_EXP_MRT;
 
-               if (cbuf >= 0 && cbuf < 8) {
+               if (cbuf >= 0 && cbuf < 8)
                        compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
-
-                       if (compressed)
-                               si_shader_ctx->shader->spi_shader_col_format |=
-                                       V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
-                       else
-                               si_shader_ctx->shader->spi_shader_col_format |=
-                                       V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
-
-                       si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
-               }
        }
 
        /* Set COMPR flag */
@@ -1333,34 +1323,19 @@ static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
                                                    LLVMInt32TypeInContext(base->gallivm->context),
                                                    pack_args, 2,
                                                    LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
-                       args[chan + 7] = args[chan + 5] =
+                       args[chan + 5] =
                                LLVMBuildBitCast(base->gallivm->builder,
                                                 packed,
                                                 LLVMFloatTypeInContext(base->gallivm->context),
                                                 "");
+                       args[chan + 7] = base->undef;
                }
        } else
                memcpy(&args[5], values, sizeof(values[0]) * 4);
 }
 
-/* Load from output pointers and initialize arguments for the shader export intrinsic */
-static void si_llvm_init_export_args_load(struct lp_build_tgsi_context *bld_base,
-                                         LLVMValueRef *out_ptr,
-                                         unsigned target,
-                                         LLVMValueRef *args)
-{
-       struct gallivm_state *gallivm = bld_base->base.gallivm;
-       LLVMValueRef values[4];
-       int i;
-
-       for (i = 0; i < 4; i++)
-               values[i] = LLVMBuildLoad(gallivm->builder, out_ptr[i], "");
-
-       si_llvm_init_export_args(bld_base, values, target, args);
-}
-
 static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
-                         LLVMValueRef alpha_ptr)
+                         LLVMValueRef alpha)
 {
        struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
        struct gallivm_state *gallivm = bld_base->base.gallivm;
@@ -1372,8 +1347,7 @@ static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
                LLVMValueRef alpha_pass =
                        lp_build_cmp(&bld_base->base,
                                     si_shader_ctx->shader->key.ps.alpha_func,
-                                    LLVMBuildLoad(gallivm->builder, alpha_ptr, ""),
-                                    alpha_ref);
+                                    alpha, alpha_ref);
                LLVMValueRef arg =
                        lp_build_select(&bld_base->base,
                                        alpha_pass,
@@ -1390,16 +1364,14 @@ static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
                                LLVMVoidTypeInContext(gallivm->context),
                                NULL, 0, 0);
        }
-
-       si_shader_ctx->shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
 }
 
-static void si_scale_alpha_by_sample_mask(struct lp_build_tgsi_context *bld_base,
-                                         LLVMValueRef alpha_ptr)
+static LLVMValueRef si_scale_alpha_by_sample_mask(struct lp_build_tgsi_context *bld_base,
+                                                 LLVMValueRef alpha)
 {
        struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
        struct gallivm_state *gallivm = bld_base->base.gallivm;
-       LLVMValueRef coverage, alpha;
+       LLVMValueRef coverage;
 
        /* alpha = alpha * popcount(coverage) / SI_NUM_SMOOTH_AA_SAMPLES */
        coverage = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
@@ -1417,9 +1389,7 @@ static void si_scale_alpha_by_sample_mask(struct lp_build_tgsi_context *bld_base
                                 lp_build_const_float(gallivm,
                                        1.0 / SI_NUM_SMOOTH_AA_SAMPLES), "");
 
-       alpha = LLVMBuildLoad(gallivm->builder, alpha_ptr, "");
-       alpha = LLVMBuildFMul(gallivm->builder, alpha, coverage, "");
-       LLVMBuildStore(gallivm->builder, alpha, alpha_ptr);
+       return LLVMBuildFMul(gallivm->builder, alpha, coverage, "");
 }
 
 static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
@@ -2112,197 +2082,193 @@ static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context * bld_base)
        FREE(outputs);
 }
 
-static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context * bld_base)
+static void si_export_mrt_z(struct lp_build_tgsi_context *bld_base,
+                          LLVMValueRef depth, LLVMValueRef stencil,
+                          LLVMValueRef samplemask)
 {
-       struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
-       struct si_shader * shader = si_shader_ctx->shader;
-       struct lp_build_context * base = &bld_base->base;
-       struct lp_build_context * uint = &bld_base->uint_bld;
-       struct tgsi_shader_info *info = &shader->selector->info;
-       LLVMBuilderRef builder = base->gallivm->builder;
+       struct si_screen *sscreen = si_shader_context(bld_base)->screen;
+       struct lp_build_context *base = &bld_base->base;
+       struct lp_build_context *uint = &bld_base->uint_bld;
        LLVMValueRef args[9];
-       LLVMValueRef last_args[9] = { 0 };
-       int depth_index = -1, stencil_index = -1, samplemask_index = -1;
-       int i;
+       unsigned mask = 0;
 
-       for (i = 0; i < info->num_outputs; i++) {
-               unsigned semantic_name = info->output_semantic_name[i];
-               unsigned semantic_index = info->output_semantic_index[i];
-               unsigned target;
-               LLVMValueRef alpha_ptr;
+       assert(depth || stencil || samplemask);
 
-               /* Select the correct target */
-               switch (semantic_name) {
-               case TGSI_SEMANTIC_POSITION:
-                       depth_index = i;
-                       continue;
-               case TGSI_SEMANTIC_STENCIL:
-                       stencil_index = i;
-                       continue;
-               case TGSI_SEMANTIC_SAMPLEMASK:
-                       samplemask_index = i;
-                       continue;
-               case TGSI_SEMANTIC_COLOR:
-                       target = V_008DFC_SQ_EXP_MRT + semantic_index;
-                       alpha_ptr = si_shader_ctx->radeon_bld.soa.outputs[i][3];
+       args[1] = uint->one; /* whether the EXEC mask is valid */
+       args[2] = uint->one; /* DONE bit */
+
+       /* Specify the target we are exporting */
+       args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
 
-                       if (si_shader_ctx->shader->key.ps.clamp_color) {
-                               for (int j = 0; j < 4; j++) {
-                                       LLVMValueRef ptr = si_shader_ctx->radeon_bld.soa.outputs[i][j];
-                                       LLVMValueRef result = LLVMBuildLoad(builder, ptr, "");
+       args[4] = uint->zero; /* COMP flag */
+       args[5] = base->undef; /* R, depth */
+       args[6] = base->undef; /* G, stencil test value[0:7], stencil op value[8:15] */
+       args[7] = base->undef; /* B, sample mask */
+       args[8] = base->undef; /* A, alpha to mask */
 
-                                       result = radeon_llvm_saturate(bld_base, result);
-                                       LLVMBuildStore(builder, result, ptr);
-                               }
-                       }
+       if (depth) {
+               args[5] = depth;
+               mask |= 0x1;
+       }
 
-                       if (si_shader_ctx->shader->key.ps.alpha_to_one)
-                               LLVMBuildStore(base->gallivm->builder,
-                                              base->one, alpha_ptr);
+       if (stencil) {
+               args[6] = stencil;
+               mask |= 0x2;
+       }
 
-                       if (semantic_index == 0 &&
-                           si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
-                               si_alpha_test(bld_base, alpha_ptr);
+       if (samplemask) {
+               args[7] = samplemask;
+               mask |= 0x4;
+       }
 
-                       if (si_shader_ctx->shader->key.ps.poly_line_smoothing)
-                               si_scale_alpha_by_sample_mask(bld_base, alpha_ptr);
+       /* SI (except OLAND) has a bug that it only looks
+        * at the X writemask component. */
+       if (sscreen->b.chip_class == SI &&
+           sscreen->b.family != CHIP_OLAND)
+               mask |= 0x1;
 
-                       break;
-               default:
-                       target = 0;
-                       fprintf(stderr,
-                               "Warning: SI unhandled fs output type:%d\n",
-                               semantic_name);
-               }
+       /* Specify which components to enable */
+       args[0] = lp_build_const_int32(base->gallivm, mask);
 
-               si_llvm_init_export_args_load(bld_base,
-                                             si_shader_ctx->radeon_bld.soa.outputs[i],
-                                             target, args);
-
-               if (semantic_name == TGSI_SEMANTIC_COLOR) {
-                       /* If there is an export instruction waiting to be emitted, do so now. */
-                       if (last_args[0]) {
-                               lp_build_intrinsic(base->gallivm->builder,
-                                                  "llvm.SI.export",
-                                                  LLVMVoidTypeInContext(base->gallivm->context),
-                                                  last_args, 9, 0);
-                       }
+       lp_build_intrinsic(base->gallivm->builder, "llvm.SI.export",
+                          LLVMVoidTypeInContext(base->gallivm->context),
+                          args, 9, 0);
+}
 
-                       /* This instruction will be emitted at the end of the shader. */
-                       memcpy(last_args, args, sizeof(args));
-
-                       /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
-                       if (shader->selector->info.properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS] &&
-                           semantic_index == 0 &&
-                           si_shader_ctx->shader->key.ps.last_cbuf > 0) {
-                               for (int c = 1; c <= si_shader_ctx->shader->key.ps.last_cbuf; c++) {
-                                       si_llvm_init_export_args_load(bld_base,
-                                                                     si_shader_ctx->radeon_bld.soa.outputs[i],
-                                                                     V_008DFC_SQ_EXP_MRT + c, args);
-                                       lp_build_intrinsic(base->gallivm->builder,
-                                                          "llvm.SI.export",
-                                                          LLVMVoidTypeInContext(base->gallivm->context),
-                                                          args, 9, 0);
-                               }
-                       }
-               } else {
-                       lp_build_intrinsic(base->gallivm->builder,
-                                          "llvm.SI.export",
+static void si_export_mrt_color(struct lp_build_tgsi_context *bld_base,
+                               LLVMValueRef *color, unsigned index,
+                               bool is_last)
+{
+       struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
+       struct lp_build_context *base = &bld_base->base;
+       LLVMValueRef args[9];
+       int i;
+
+       /* Clamp color */
+       if (si_shader_ctx->shader->key.ps.clamp_color)
+               for (i = 0; i < 4; i++)
+                       color[i] = radeon_llvm_saturate(bld_base, color[i]);
+
+       /* Alpha to one */
+       if (si_shader_ctx->shader->key.ps.alpha_to_one)
+               color[3] = base->one;
+
+       /* Alpha test */
+       if (index == 0 &&
+           si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
+               si_alpha_test(bld_base, color[3]);
+
+       /* Line & polygon smoothing */
+       if (si_shader_ctx->shader->key.ps.poly_line_smoothing)
+               color[3] = si_scale_alpha_by_sample_mask(bld_base, color[3]);
+
+       /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
+       if (index == 0 &&
+           si_shader_ctx->shader->key.ps.last_cbuf > 0) {
+               for (int c = 1; c <= si_shader_ctx->shader->key.ps.last_cbuf; c++) {
+                       si_llvm_init_export_args(bld_base, color,
+                                                V_008DFC_SQ_EXP_MRT + c, args);
+                       lp_build_intrinsic(base->gallivm->builder, "llvm.SI.export",
                                           LLVMVoidTypeInContext(base->gallivm->context),
                                           args, 9, 0);
                }
        }
 
-       if (depth_index >= 0 || stencil_index >= 0 || samplemask_index >= 0) {
-               LLVMValueRef out_ptr;
-               unsigned mask = 0;
-
-               /* Specify the target we are exporting */
-               args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
-
-               args[5] = base->zero; /* R, depth */
-               args[6] = base->zero; /* G, stencil test value[0:7], stencil op value[8:15] */
-               args[7] = base->zero; /* B, sample mask */
-               args[8] = base->zero; /* A, alpha to mask */
-
-               if (depth_index >= 0) {
-                       out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
-                       args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
-                       mask |= 0x1;
-                       si_shader_ctx->shader->db_shader_control |= S_02880C_Z_EXPORT_ENABLE(1);
-               }
-
-               if (stencil_index >= 0) {
-                       out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
-                       args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
-                       mask |= 0x2;
-                       si_shader_ctx->shader->db_shader_control |=
-                               S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(1);
-               }
-
-               if (samplemask_index >= 0) {
-                       out_ptr = si_shader_ctx->radeon_bld.soa.outputs[samplemask_index][0];
-                       args[7] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
-                       mask |= 0x4;
-                       si_shader_ctx->shader->db_shader_control |= S_02880C_MASK_EXPORT_ENABLE(1);
-               }
-
-               /* SI (except OLAND) has a bug that it only looks
-                * at the X writemask component. */
-               if (si_shader_ctx->screen->b.chip_class == SI &&
-                   si_shader_ctx->screen->b.family != CHIP_OLAND)
-                       mask |= 0x1;
+       /* Export */
+       si_llvm_init_export_args(bld_base, color, V_008DFC_SQ_EXP_MRT + index,
+                                args);
+       if (is_last) {
+               args[1] = bld_base->uint_bld.one; /* whether the EXEC mask is valid */
+               args[2] = bld_base->uint_bld.one; /* DONE bit */
+       }
+       lp_build_intrinsic(base->gallivm->builder, "llvm.SI.export",
+                          LLVMVoidTypeInContext(base->gallivm->context),
+                          args, 9, 0);
+}
 
-               if (samplemask_index >= 0)
-                       si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_ABGR;
-               else if (stencil_index >= 0)
-                       si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_GR;
-               else
-                       si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_R;
+static void si_export_null(struct lp_build_tgsi_context *bld_base)
+{
+       struct lp_build_context *base = &bld_base->base;
+       struct lp_build_context *uint = &bld_base->uint_bld;
+       LLVMValueRef args[9];
 
-               /* Specify which components to enable */
-               args[0] = lp_build_const_int32(base->gallivm, mask);
+       args[0] = lp_build_const_int32(base->gallivm, 0x0); /* enabled channels */
+       args[1] = uint->one; /* whether the EXEC mask is valid */
+       args[2] = uint->one; /* DONE bit */
+       args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_NULL);
+       args[4] = uint->zero; /* COMPR flag (0 = 32-bit export) */
+       args[5] = uint->undef; /* R */
+       args[6] = uint->undef; /* G */
+       args[7] = uint->undef; /* B */
+       args[8] = uint->undef; /* A */
+
+       lp_build_intrinsic(base->gallivm->builder, "llvm.SI.export",
+                          LLVMVoidTypeInContext(base->gallivm->context),
+                          args, 9, 0);
+}
 
-               args[1] =
-               args[2] =
-               args[4] = uint->zero;
+static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context * bld_base)
+{
+       struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
+       struct si_shader * shader = si_shader_ctx->shader;
+       struct lp_build_context * base = &bld_base->base;
+       struct tgsi_shader_info *info = &shader->selector->info;
+       LLVMBuilderRef builder = base->gallivm->builder;
+       LLVMValueRef depth = NULL, stencil = NULL, samplemask = NULL;
+       int last_color_export = -1;
+       int i;
 
-               if (last_args[0])
-                       lp_build_intrinsic(base->gallivm->builder,
-                                          "llvm.SI.export",
-                                          LLVMVoidTypeInContext(base->gallivm->context),
-                                          args, 9, 0);
-               else
-                       memcpy(last_args, args, sizeof(args));
+       /* If there are no outputs, add a dummy export. */
+       if (!info->num_outputs) {
+               si_export_null(bld_base);
+               return;
        }
 
-       if (!last_args[0]) {
-               /* Specify which components to enable */
-               last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
+       /* Determine the last export. If MRTZ is present, it's always last.
+        * Otherwise, find the last color export.
+        */
+       if (!info->writes_z && !info->writes_stencil && !info->writes_samplemask)
+               for (i = 0; i < info->num_outputs; i++)
+                       if (info->output_semantic_name[i] == TGSI_SEMANTIC_COLOR)
+                               last_color_export = i;
 
-               /* Specify the target we are exporting */
-               last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
+       for (i = 0; i < info->num_outputs; i++) {
+               unsigned semantic_name = info->output_semantic_name[i];
+               unsigned semantic_index = info->output_semantic_index[i];
+               unsigned j;
+               LLVMValueRef color[4] = {};
 
-               /* Set COMPR flag to zero to export data as 32-bit */
-               last_args[4] = uint->zero;
+               /* Select the correct target */
+               switch (semantic_name) {
+               case TGSI_SEMANTIC_POSITION:
+                       depth = LLVMBuildLoad(builder,
+                                             si_shader_ctx->radeon_bld.soa.outputs[i][2], "");
+                       break;
+               case TGSI_SEMANTIC_STENCIL:
+                       stencil = LLVMBuildLoad(builder,
+                                               si_shader_ctx->radeon_bld.soa.outputs[i][1], "");
+                       break;
+               case TGSI_SEMANTIC_SAMPLEMASK:
+                       samplemask = LLVMBuildLoad(builder,
+                                                  si_shader_ctx->radeon_bld.soa.outputs[i][0], "");
+                       break;
+               case TGSI_SEMANTIC_COLOR:
+                       for (j = 0; j < 4; j++)
+                               color[j] = LLVMBuildLoad(builder,
+                                                        si_shader_ctx->radeon_bld.soa.outputs[i][j], "");
 
-               /* dummy bits */
-               last_args[5]= uint->zero;
-               last_args[6]= uint->zero;
-               last_args[7]= uint->zero;
-               last_args[8]= uint->zero;
+                       si_export_mrt_color(bld_base, color, semantic_index,
+                                           last_color_export == i);
+                       break;
+               default:
+                       fprintf(stderr,
+                               "Warning: SI unhandled fs output type:%d\n",
+                               semantic_name);
+               }
        }
 
-       /* Specify whether the EXEC mask represents the valid mask */
-       last_args[1] = uint->one;
-
-       /* Specify that this is the last export */
-       last_args[2] = lp_build_const_int32(base->gallivm, 1);
-
-       lp_build_intrinsic(base->gallivm->builder,
-                          "llvm.SI.export",
-                          LLVMVoidTypeInContext(base->gallivm->context),
-                          last_args, 9, 0);
+       if (depth || stencil || samplemask)
+               si_export_mrt_z(bld_base, depth, stencil, samplemask);
 }
 
 static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
@@ -3741,20 +3707,19 @@ static void preload_ring_buffers(struct si_shader_context *si_shader_ctx)
        }
 }
 
-void si_shader_binary_read_config(const struct si_screen *sscreen,
-                               struct si_shader *shader,
-                               unsigned symbol_offset)
+void si_shader_binary_read_config(struct radeon_shader_binary *binary,
+                                 struct si_shader_config *conf,
+                                 unsigned symbol_offset)
 {
        unsigned i;
        const unsigned char *config =
-               radeon_shader_binary_config_start(&shader->binary,
-                                               symbol_offset);
+               radeon_shader_binary_config_start(binary, symbol_offset);
 
        /* XXX: We may be able to emit some of these values directly rather than
         * extracting fields to be emitted later.
         */
 
-       for (i = 0; i < shader->binary.config_size_per_symbol; i+= 8) {
+       for (i = 0; i < binary->config_size_per_symbol; i+= 8) {
                unsigned reg = util_le32_to_cpu(*(uint32_t*)(config + i));
                unsigned value = util_le32_to_cpu(*(uint32_t*)(config + i + 4));
                switch (reg) {
@@ -3762,25 +3727,25 @@ void si_shader_binary_read_config(const struct si_screen *sscreen,
                case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
                case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
                case R_00B848_COMPUTE_PGM_RSRC1:
-                       shader->num_sgprs = MAX2(shader->num_sgprs, (G_00B028_SGPRS(value) + 1) * 8);
-                       shader->num_vgprs = MAX2(shader->num_vgprs, (G_00B028_VGPRS(value) + 1) * 4);
-                       shader->float_mode =  G_00B028_FLOAT_MODE(value);
-                       shader->rsrc1 = value;
+                       conf->num_sgprs = MAX2(conf->num_sgprs, (G_00B028_SGPRS(value) + 1) * 8);
+                       conf->num_vgprs = MAX2(conf->num_vgprs, (G_00B028_VGPRS(value) + 1) * 4);
+                       conf->float_mode =  G_00B028_FLOAT_MODE(value);
+                       conf->rsrc1 = value;
                        break;
                case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
-                       shader->lds_size = MAX2(shader->lds_size, G_00B02C_EXTRA_LDS_SIZE(value));
+                       conf->lds_size = MAX2(conf->lds_size, G_00B02C_EXTRA_LDS_SIZE(value));
                        break;
                case R_00B84C_COMPUTE_PGM_RSRC2:
-                       shader->lds_size = MAX2(shader->lds_size, G_00B84C_LDS_SIZE(value));
-                       shader->rsrc2 = value;
+                       conf->lds_size = MAX2(conf->lds_size, G_00B84C_LDS_SIZE(value));
+                       conf->rsrc2 = value;
                        break;
                case R_0286CC_SPI_PS_INPUT_ENA:
-                       shader->spi_ps_input_ena = value;
+                       conf->spi_ps_input_ena = value;
                        break;
                case R_0286E8_SPI_TMPRING_SIZE:
                case R_00B860_COMPUTE_TMPRING_SIZE:
                        /* WAVESIZE is in units of 256 dwords. */
-                       shader->scratch_bytes_per_wave =
+                       conf->scratch_bytes_per_wave =
                                G_00B860_WAVESIZE(value) * 256 * 4 * 1;
                        break;
                default:
@@ -3799,7 +3764,7 @@ void si_shader_apply_scratch_relocs(struct si_context *sctx,
        uint32_t scratch_rsrc_dword0 = scratch_va;
        uint32_t scratch_rsrc_dword1 =
                S_008F04_BASE_ADDRESS_HI(scratch_va >> 32)
-               |  S_008F04_STRIDE(shader->scratch_bytes_per_wave / 64);
+               |  S_008F04_STRIDE(shader->config.scratch_bytes_per_wave / 64);
 
        for (i = 0 ; i < shader->binary.reloc_count; i++) {
                const struct radeon_shader_reloc *reloc =
@@ -3887,70 +3852,60 @@ static void si_shader_dump_disassembly(const struct radeon_shader_binary *binary
        }
 }
 
-int si_shader_binary_read(struct si_screen *sscreen, struct si_shader *shader,
-                         struct pipe_debug_callback *debug)
+void si_shader_binary_read(struct si_screen *sscreen,
+                          struct radeon_shader_binary *binary,
+                          struct si_shader_config *conf,
+                          struct pipe_debug_callback *debug,
+                          unsigned processor)
 {
-       const struct radeon_shader_binary *binary = &shader->binary;
-       int r;
-       bool dump  = r600_can_dump_shader(&sscreen->b,
-               shader->selector ? shader->selector->tokens : NULL);
-
-       si_shader_binary_read_config(sscreen, shader, 0);
-       r = si_shader_binary_upload(sscreen, shader);
-       if (r)
-               return r;
+       si_shader_binary_read_config(binary, conf, 0);
 
-       if (dump) {
+       if (r600_can_dump_shader(&sscreen->b, processor)) {
                if (!(sscreen->b.debug_flags & DBG_NO_ASM))
                        si_shader_dump_disassembly(binary, debug);
 
                fprintf(stderr, "*** SHADER STATS ***\n"
                        "SGPRS: %d\nVGPRS: %d\nCode Size: %d bytes\nLDS: %d blocks\n"
                        "Scratch: %d bytes per wave\n********************\n",
-                       shader->num_sgprs, shader->num_vgprs, binary->code_size,
-                       shader->lds_size, shader->scratch_bytes_per_wave);
+                       conf->num_sgprs, conf->num_vgprs, binary->code_size,
+                       conf->lds_size, conf->scratch_bytes_per_wave);
        }
 
        pipe_debug_message(debug, SHADER_INFO,
                           "Shader Stats: SGPRS: %d VGPRS: %d Code Size: %d LDS: %d Scratch: %d",
-                          shader->num_sgprs, shader->num_vgprs, binary->code_size,
-                          shader->lds_size, shader->scratch_bytes_per_wave);
-
-       return 0;
+                          conf->num_sgprs, conf->num_vgprs, binary->code_size,
+                          conf->lds_size, conf->scratch_bytes_per_wave);
 }
 
 int si_compile_llvm(struct si_screen *sscreen, struct si_shader *shader,
                    LLVMTargetMachineRef tm, LLVMModuleRef mod,
-                   struct pipe_debug_callback *debug)
+                   struct pipe_debug_callback *debug, unsigned processor)
 {
        int r = 0;
-       bool dump_asm = r600_can_dump_shader(&sscreen->b,
-                               shader->selector ? shader->selector->tokens : NULL);
-       bool dump_ir = dump_asm && !(sscreen->b.debug_flags & DBG_NO_IR);
        unsigned count = p_atomic_inc_return(&sscreen->b.num_compilations);
 
-       if (dump_ir || dump_asm)
+       if (r600_can_dump_shader(&sscreen->b, processor)) {
                fprintf(stderr, "radeonsi: Compiling shader %d\n", count);
 
+               if (!(sscreen->b.debug_flags & DBG_NO_IR))
+                       LLVMDumpModule(mod);
+       }
+
        if (!si_replace_shader(count, &shader->binary)) {
                r = radeon_llvm_compile(mod, &shader->binary,
-                       r600_get_llvm_processor_name(sscreen->b.family), dump_ir, dump_asm, tm,
+                       r600_get_llvm_processor_name(sscreen->b.family), tm,
                        debug);
                if (r)
                        return r;
        }
 
-       r = si_shader_binary_read(sscreen, shader, debug);
+       si_shader_binary_read(sscreen, &shader->binary, &shader->config,
+                             debug, processor);
 
        FREE(shader->binary.config);
-       FREE(shader->binary.rodata);
        FREE(shader->binary.global_symbol_offsets);
-       if (shader->scratch_bytes_per_wave == 0) {
-               FREE(shader->binary.code);
-               FREE(shader->binary.relocs);
-               memset(&shader->binary, 0,
-                      offsetof(struct radeon_shader_binary, disasm_string));
-       }
+       shader->binary.config = NULL;
+       shader->binary.global_symbol_offsets = NULL;
        return r;
 }
 
@@ -4026,7 +3981,9 @@ static int si_generate_gs_copy_shader(struct si_screen *sscreen,
 
        r = si_compile_llvm(sscreen, si_shader_ctx->shader,
                            si_shader_ctx->tm, bld_base->base.gallivm->module,
-                           debug);
+                           debug, TGSI_PROCESSOR_GEOMETRY);
+       if (!r)
+               r = si_shader_binary_upload(sscreen, si_shader_ctx->shader);
 
        radeon_llvm_dispose(&si_shader_ctx->radeon_bld);
 
@@ -4092,7 +4049,7 @@ int si_shader_create(struct si_screen *sscreen, LLVMTargetMachineRef tm,
        int r = 0;
        bool poly_stipple = sel->type == PIPE_SHADER_FRAGMENT &&
                            shader->key.ps.poly_stipple;
-       bool dump = r600_can_dump_shader(&sscreen->b, sel->tokens);
+       bool dump = r600_can_dump_shader(&sscreen->b, sel->info.processor);
 
        if (poly_stipple) {
                tokens = util_pstipple_create_fragment_shader(tokens, NULL,
@@ -4117,9 +4074,6 @@ int si_shader_create(struct si_screen *sscreen, LLVMTargetMachineRef tm,
        if (sel->type != PIPE_SHADER_COMPUTE)
                shader->dx10_clamp_mode = true;
 
-       if (sel->info.uses_kill)
-               shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
-
        shader->uses_instanceid = sel->info.uses_instanceid;
        bld_base->info = poly_stipple ? &stipple_shader_info : &sel->info;
        bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
@@ -4194,17 +4148,6 @@ int si_shader_create(struct si_screen *sscreen, LLVMTargetMachineRef tm,
        case TGSI_PROCESSOR_FRAGMENT:
                si_shader_ctx.radeon_bld.load_input = declare_input_fs;
                bld_base->emit_epilogue = si_llvm_emit_fs_epilogue;
-
-               switch (sel->info.properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT]) {
-               case TGSI_FS_DEPTH_LAYOUT_GREATER:
-                       shader->db_shader_control |=
-                               S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_GREATER_THAN_Z);
-                       break;
-               case TGSI_FS_DEPTH_LAYOUT_LESS:
-                       shader->db_shader_control |=
-                               S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_LESS_THAN_Z);
-                       break;
-               }
                break;
        default:
                assert(!"Unsupported shader type");
@@ -4235,12 +4178,18 @@ int si_shader_create(struct si_screen *sscreen, LLVMTargetMachineRef tm,
        radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
 
        mod = bld_base->base.gallivm->module;
-       r = si_compile_llvm(sscreen, shader, tm, mod, debug);
+       r = si_compile_llvm(sscreen, shader, tm, mod, debug, si_shader_ctx.type);
        if (r) {
                fprintf(stderr, "LLVM failed to compile shader\n");
                goto out;
        }
 
+       r = si_shader_binary_upload(sscreen, shader);
+       if (r) {
+               fprintf(stderr, "LLVM failed to upload shader\n");
+               goto out;
+       }
+
        radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
 
        if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
@@ -4277,6 +4226,7 @@ void si_shader_destroy(struct si_shader *shader)
        r600_resource_reference(&shader->bo, NULL);
 
        FREE(shader->binary.code);
+       FREE(shader->binary.rodata);
        FREE(shader->binary.relocs);
        FREE(shader->binary.disasm_string);
 }