radeonsi/gfx10: add si_context::emit_cache_flush
[mesa.git] / src / gallium / drivers / radeonsi / si_shader.c
index ab97cd872738f59d71c8b3237345ec88642641d2..934639822d0b011d21c5ea447a0066c2b17240ed 100644 (file)
@@ -28,6 +28,7 @@
 #include "tgsi/tgsi_strings.h"
 #include "tgsi/tgsi_util.h"
 #include "tgsi/tgsi_dump.h"
+#include "tgsi/tgsi_from_mesa.h"
 
 #include "ac_binary.h"
 #include "ac_exp_param.h"
@@ -46,14 +47,6 @@ static const char scratch_rsrc_dword0_symbol[] =
 static const char scratch_rsrc_dword1_symbol[] =
        "SCRATCH_RSRC_DWORD1";
 
-struct si_shader_output_values
-{
-       LLVMValueRef values[4];
-       unsigned semantic_name;
-       unsigned semantic_index;
-       ubyte vertex_stream[4];
-};
-
 static void si_init_shader_ctx(struct si_shader_context *ctx,
                               struct si_screen *sscreen,
                               struct ac_llvm_compiler *compiler);
@@ -646,8 +639,8 @@ static void declare_input_vs(
        si_llvm_load_input_vs(ctx, input_index, out);
 }
 
-static LLVMValueRef get_primitive_id(struct si_shader_context *ctx,
-                                    unsigned swizzle)
+LLVMValueRef si_get_primitive_id(struct si_shader_context *ctx,
+                                unsigned swizzle)
 {
        if (swizzle > 0)
                return ctx->i32_0;
@@ -991,13 +984,13 @@ static LLVMValueRef buffer_load(struct lp_build_tgsi_context *bld_base,
 }
 
 /**
- * Load from LDS.
+ * Load from LSHS LDS storage.
  *
  * \param type         output value type
  * \param swizzle      offset (typically 0..3); it can be ~0, which loads a vec4
  * \param dw_addr      address in dwords
  */
-static LLVMValueRef lds_load(struct lp_build_tgsi_context *bld_base,
+static LLVMValueRef lshs_lds_load(struct lp_build_tgsi_context *bld_base,
                             LLVMTypeRef type, unsigned swizzle,
                             LLVMValueRef dw_addr)
 {
@@ -1008,7 +1001,7 @@ static LLVMValueRef lds_load(struct lp_build_tgsi_context *bld_base,
                LLVMValueRef values[TGSI_NUM_CHANNELS];
 
                for (unsigned chan = 0; chan < TGSI_NUM_CHANNELS; chan++)
-                       values[chan] = lds_load(bld_base, type, chan, dw_addr);
+                       values[chan] = lshs_lds_load(bld_base, type, chan, dw_addr);
 
                return ac_build_gather_values(&ctx->ac, values,
                                              TGSI_NUM_CHANNELS);
@@ -1018,8 +1011,8 @@ static LLVMValueRef lds_load(struct lp_build_tgsi_context *bld_base,
        if (llvm_type_is_64bit(ctx, type)) {
                LLVMValueRef lo, hi;
 
-               lo = lds_load(bld_base, ctx->i32, swizzle, dw_addr);
-               hi = lds_load(bld_base, ctx->i32, swizzle + 1, dw_addr);
+               lo = lshs_lds_load(bld_base, ctx->i32, swizzle, dw_addr);
+               hi = lshs_lds_load(bld_base, ctx->i32, swizzle + 1, dw_addr);
                return si_llvm_emit_fetch_64bit(bld_base, type, lo, hi);
        }
 
@@ -1032,13 +1025,13 @@ static LLVMValueRef lds_load(struct lp_build_tgsi_context *bld_base,
 }
 
 /**
- * Store to LDS.
+ * Store to LSHS LDS storage.
  *
  * \param swizzle      offset (typically 0..3)
  * \param dw_addr      address in dwords
  * \param value                value to store
  */
-static void lds_store(struct si_shader_context *ctx,
+static void lshs_lds_store(struct si_shader_context *ctx,
                      unsigned dw_offset_imm, LLVMValueRef dw_addr,
                      LLVMValueRef value)
 {
@@ -1102,7 +1095,7 @@ static LLVMValueRef fetch_input_tcs(
        dw_addr = get_tcs_in_current_patch_offset(ctx);
        dw_addr = get_dw_address(ctx, NULL, reg, stride, dw_addr);
 
-       return lds_load(bld_base, tgsi2llvmtype(bld_base, type), swizzle, dw_addr);
+       return lshs_lds_load(bld_base, tgsi2llvmtype(bld_base, type), swizzle, dw_addr);
 }
 
 static LLVMValueRef si_nir_load_tcs_varyings(struct ac_shader_abi *abi,
@@ -1169,7 +1162,7 @@ static LLVMValueRef si_nir_load_tcs_varyings(struct ac_shader_abi *abi,
                        offset *= 2;
 
                offset += component;
-               value[i + component] = lds_load(bld_base, type, offset, dw_addr);
+               value[i + component] = lshs_lds_load(bld_base, type, offset, dw_addr);
        }
 
        return ac_build_varying_gather_values(&ctx->ac, value, num_components, component);
@@ -1193,7 +1186,7 @@ static LLVMValueRef fetch_output_tcs(
                dw_addr = get_dw_address(ctx, NULL, reg, NULL, dw_addr);
        }
 
-       return lds_load(bld_base, tgsi2llvmtype(bld_base, type), swizzle, dw_addr);
+       return lshs_lds_load(bld_base, tgsi2llvmtype(bld_base, type), swizzle, dw_addr);
 }
 
 static LLVMValueRef fetch_input_tes(
@@ -1343,7 +1336,7 @@ static void store_output_tcs(struct lp_build_tgsi_context *bld_base,
 
                /* Skip LDS stores if there is no LDS read of this output. */
                if (!skip_lds_store)
-                       lds_store(ctx, chan_index, dw_addr, value);
+                       lshs_lds_store(ctx, chan_index, dw_addr, value);
 
                value = ac_to_integer(&ctx->ac, value);
                values[chan_index] = value;
@@ -1351,7 +1344,7 @@ static void store_output_tcs(struct lp_build_tgsi_context *bld_base,
                if (reg->Register.WriteMask != 0xF && !is_tess_factor) {
                        ac_build_buffer_store_dword(&ctx->ac, buffer, value, 1,
                                                    buf_addr, base,
-                                                   4 * chan_index, 1, 0, true, false);
+                                                   4 * chan_index, 1, 0, false);
                }
 
                /* Write tess factors into VGPRs for the epilog. */
@@ -1371,7 +1364,7 @@ static void store_output_tcs(struct lp_build_tgsi_context *bld_base,
                LLVMValueRef value = ac_build_gather_values(&ctx->ac,
                                                            values, 4);
                ac_build_buffer_store_dword(&ctx->ac, buffer, value, 4, buf_addr,
-                                           base, 0, 1, 0, true, false);
+                                           base, 0, 1, 0, false);
        }
 }
 
@@ -1470,7 +1463,7 @@ static void si_nir_store_output_tcs(struct ac_shader_abi *abi,
 
                /* Skip LDS stores if there is no LDS read of this output. */
                if (!skip_lds_store)
-                       lds_store(ctx, chan, dw_addr, value);
+                       lshs_lds_store(ctx, chan, dw_addr, value);
 
                value = ac_to_integer(&ctx->ac, value);
                values[chan] = value;
@@ -1479,7 +1472,7 @@ static void si_nir_store_output_tcs(struct ac_shader_abi *abi,
                        ac_build_buffer_store_dword(&ctx->ac, buffer, value, 1,
                                                    addr, base,
                                                    4 * buffer_store_offset,
-                                                    1, 0, true, false);
+                                                    1, 0, false);
                }
 
                /* Write tess factors into VGPRs for the epilog. */
@@ -1499,7 +1492,7 @@ static void si_nir_store_output_tcs(struct ac_shader_abi *abi,
                LLVMValueRef value = ac_build_gather_values(&ctx->ac,
                                                            values, 4);
                ac_build_buffer_store_dword(&ctx->ac, buffer, value, 4, addr,
-                                           base, 0, 1, 0, true, false);
+                                           base, 0, 1, 0, false);
        }
 }
 
@@ -1543,9 +1536,22 @@ LLVMValueRef si_llvm_load_input_gs(struct ac_shader_abi *abi,
                        return NULL;
                }
 
+               unsigned offset = param * 4 + swizzle;
                vtx_offset = LLVMBuildAdd(ctx->ac.builder, vtx_offset,
-                                         LLVMConstInt(ctx->i32, param * 4, 0), "");
-               return lds_load(bld_base, type, swizzle, vtx_offset);
+                                         LLVMConstInt(ctx->i32, offset, false), "");
+
+               LLVMValueRef ptr = ac_build_gep0(&ctx->ac, ctx->esgs_ring, vtx_offset);
+               LLVMValueRef value = LLVMBuildLoad(ctx->ac.builder, ptr, "");
+               if (llvm_type_is_64bit(ctx, type)) {
+                       ptr = LLVMBuildGEP(ctx->ac.builder, ptr,
+                                          &ctx->ac.i32_1, 1, "");
+                       LLVMValueRef values[2] = {
+                               value,
+                               LLVMBuildLoad(ctx->ac.builder, ptr, "")
+                       };
+                       value = ac_build_gather_values(&ctx->ac, values, 2);
+               }
+               return LLVMBuildBitCast(ctx->ac.builder, value, type, "");
        }
 
        /* GFX6: input load from the ESGS ring in memory. */
@@ -1619,7 +1625,7 @@ static LLVMValueRef fetch_input_gs(
 
        unsigned semantic_name = info->input_semantic_name[reg->Register.Index];
        if (swizzle != ~0 && semantic_name == TGSI_SEMANTIC_PRIMID)
-               return get_primitive_id(ctx, swizzle);
+               return si_get_primitive_id(ctx, swizzle);
 
        if (!reg->Register.Dimension)
                return NULL;
@@ -2105,7 +2111,7 @@ void si_load_system_value(struct si_shader_context *ctx,
        }
 
        case TGSI_SEMANTIC_PRIMID:
-               value = get_primitive_id(ctx, 0);
+               value = si_get_primitive_id(ctx, 0);
                break;
 
        case TGSI_SEMANTIC_GRID_SIZE:
@@ -2204,7 +2210,7 @@ void si_declare_compute_memory(struct si_shader_context *ctx)
                                          LLVMArrayType(ctx->i8, lds_size),
                                          "compute_lds",
                                          AC_ADDR_SPACE_LDS);
-       LLVMSetAlignment(var, 4);
+       LLVMSetAlignment(var, 64 * 1024);
 
        ctx->ac.lds = LLVMBuildBitCast(ctx->ac.builder, var, i8p, "");
 }
@@ -2600,11 +2606,11 @@ static void si_dump_streamout(struct pipe_stream_output_info *so)
        }
 }
 
-static void emit_streamout_output(struct si_shader_context *ctx,
-                                 LLVMValueRef const *so_buffers,
-                                 LLVMValueRef const *so_write_offsets,
-                                 struct pipe_stream_output *stream_out,
-                                 struct si_shader_output_values *shader_out)
+void si_emit_streamout_output(struct si_shader_context *ctx,
+                             LLVMValueRef const *so_buffers,
+                             LLVMValueRef const *so_write_offsets,
+                             struct pipe_stream_output *stream_out,
+                             struct si_shader_output_values *shader_out)
 {
        unsigned buf_idx = stream_out->output_buffer;
        unsigned start = stream_out->start_component;
@@ -2647,7 +2653,7 @@ static void emit_streamout_output(struct si_shader_context *ctx,
                                    vdata, num_comps,
                                    so_write_offsets[buf_idx],
                                    ctx->i32_0,
-                                   stream_out->dst_offset * 4, 1, 1, true, false);
+                                   stream_out->dst_offset * 4, 1, 1, false);
 }
 
 /**
@@ -2727,8 +2733,8 @@ static void si_llvm_emit_streamout(struct si_shader_context *ctx,
                        if (stream != so->output[i].stream)
                                continue;
 
-                       emit_streamout_output(ctx, so_buffers, so_write_offset,
-                                             &so->output[i], &outputs[reg]);
+                       si_emit_streamout_output(ctx, so_buffers, so_write_offset,
+                                                &so->output[i], &outputs[reg]);
                }
        }
        lp_build_endif(&if_ctx);
@@ -2856,10 +2862,12 @@ static void si_vertex_color_clamping(struct si_shader_context *ctx,
        }
 }
 
-/* Generate export instructions for hardware VS shader stage */
-static void si_llvm_export_vs(struct si_shader_context *ctx,
-                             struct si_shader_output_values *outputs,
-                             unsigned noutput)
+/* Generate export instructions for hardware VS shader stage or NGG GS stage
+ * (position and parameter data only).
+ */
+void si_llvm_export_vs(struct si_shader_context *ctx,
+                      struct si_shader_output_values *outputs,
+                      unsigned noutput)
 {
        struct si_shader *shader = ctx->shader;
        struct ac_export_args pos_args[4] = {};
@@ -3038,11 +3046,10 @@ static void si_copy_tcs_inputs(struct lp_build_tgsi_context *bld_base)
                                              invocation_id,
                                              LLVMConstInt(ctx->i32, i, 0));
 
-               LLVMValueRef value = lds_load(bld_base, ctx->ac.i32, ~0,
-                                             lds_ptr);
+               LLVMValueRef value = lshs_lds_load(bld_base, ctx->ac.i32, ~0, lds_ptr);
 
                ac_build_buffer_store_dword(&ctx->ac, buffer, value, 4, buffer_addr,
-                                           buffer_offset, 0, 1, 0, true, false);
+                                           buffer_offset, 0, 1, 0, false);
        }
 }
 
@@ -3125,11 +3132,11 @@ static void si_write_tess_factors(struct lp_build_tgsi_context *bld_base,
 
                for (i = 0; i < outer_comps; i++) {
                        outer[i] = out[i] =
-                               lds_load(bld_base, ctx->ac.i32, i, lds_outer);
+                               lshs_lds_load(bld_base, ctx->ac.i32, i, lds_outer);
                }
                for (i = 0; i < inner_comps; i++) {
                        inner[i] = out[outer_comps+i] =
-                               lds_load(bld_base, ctx->ac.i32, i, lds_inner);
+                               lshs_lds_load(bld_base, ctx->ac.i32, i, lds_inner);
                }
        }
 
@@ -3168,7 +3175,7 @@ static void si_write_tess_factors(struct lp_build_tgsi_context *bld_base,
                ac_build_buffer_store_dword(&ctx->ac, buffer,
                                            LLVMConstInt(ctx->i32, 0x80000000, 0),
                                            1, ctx->i32_0, tf_base,
-                                           offset, 1, 0, true, false);
+                                           offset, 1, 0, false);
                offset += 4;
        }
 
@@ -3177,12 +3184,12 @@ static void si_write_tess_factors(struct lp_build_tgsi_context *bld_base,
        /* Store the tessellation factors. */
        ac_build_buffer_store_dword(&ctx->ac, buffer, vec0,
                                    MIN2(stride, 4), byteoffset, tf_base,
-                                   offset, 1, 0, true, false);
+                                   offset, 1, 0, false);
        offset += 16;
        if (vec1)
                ac_build_buffer_store_dword(&ctx->ac, buffer, vec1,
                                            stride - 4, byteoffset, tf_base,
-                                           offset, 1, 0, true, false);
+                                           offset, 1, 0, false);
 
        /* Store the tess factors into the offchip buffer if TES reads them. */
        if (shader->key.part.tcs.epilog.tes_reads_tess_factors) {
@@ -3205,7 +3212,7 @@ static void si_write_tess_factors(struct lp_build_tgsi_context *bld_base,
 
                ac_build_buffer_store_dword(&ctx->ac, buf, outer_vec,
                                            outer_comps, tf_outer_offset,
-                                           base, 0, 1, 0, true, false);
+                                           base, 0, 1, 0, false);
                if (inner_comps) {
                        param_inner = si_shader_io_get_unique_index_patch(
                                              TGSI_SEMANTIC_TESSINNER, 0);
@@ -3216,7 +3223,7 @@ static void si_write_tess_factors(struct lp_build_tgsi_context *bld_base,
                                    ac_build_gather_values(&ctx->ac, inner, inner_comps);
                        ac_build_buffer_store_dword(&ctx->ac, buf, inner_vec,
                                                    inner_comps, tf_inner_offset,
-                                                   base, 0, 1, 0, true, false);
+                                                   base, 0, 1, 0, false);
                }
        }
 
@@ -3461,7 +3468,7 @@ static void si_llvm_emit_ls_epilogue(struct ac_shader_abi *abi,
                        if (!(info->output_usagemask[i] & (1 << chan)))
                                continue;
 
-                       lds_store(ctx, chan, dw_addr,
+                       lshs_lds_store(ctx, chan, dw_addr,
                                  LLVMBuildLoad(ctx->ac.builder, addrs[4 * i + chan], ""));
                }
        }
@@ -3513,7 +3520,9 @@ static void si_llvm_emit_es_epilogue(struct ac_shader_abi *abi,
 
                        /* GFX9 has the ESGS ring in LDS. */
                        if (ctx->screen->info.chip_class >= GFX9) {
-                               lds_store(ctx, param * 4 + chan, lds_base, out_val);
+                               LLVMValueRef idx = LLVMConstInt(ctx->i32, param * 4 + chan, false);
+                               idx = LLVMBuildAdd(ctx->ac.builder, lds_base, idx, "");
+                               ac_build_indexed_store(&ctx->ac, ctx->esgs_ring, idx, out_val);
                                continue;
                        }
 
@@ -3521,7 +3530,7 @@ static void si_llvm_emit_es_epilogue(struct ac_shader_abi *abi,
                                                    ctx->esgs_ring,
                                                    out_val, 1, NULL, soffset,
                                                    (4 * param + chan) * 4,
-                                                   1, 1, true, true);
+                                                   1, 1, true);
                }
        }
 
@@ -3599,7 +3608,7 @@ static void si_llvm_emit_vs_epilogue(struct ac_shader_abi *abi,
        if (ctx->shader->key.mono.u.vs_export_prim_id) {
                outputs[i].semantic_name = TGSI_SEMANTIC_PRIMID;
                outputs[i].semantic_index = 0;
-               outputs[i].values[0] = ac_to_float(&ctx->ac, get_primitive_id(ctx, 0));
+               outputs[i].values[0] = ac_to_float(&ctx->ac, si_get_primitive_id(ctx, 0));
                for (j = 1; j < 4; j++)
                        outputs[i].values[j] = LLVMConstReal(ctx->f32, 0);
 
@@ -4233,7 +4242,7 @@ static void si_llvm_emit_vertex(struct ac_shader_abi *abi,
                                                    ctx->gsvs_ring[stream],
                                                    out_val, 1,
                                                    voffset, soffset, 0,
-                                                   1, 1, true, true);
+                                                   1, 1, true);
                }
        }
 
@@ -4911,11 +4920,21 @@ static void create_function(struct si_shader_context *ctx)
        assert(shader->info.num_input_vgprs >= num_prolog_vgprs);
        shader->info.num_input_vgprs -= num_prolog_vgprs;
 
-       if (shader->key.as_ls ||
-           ctx->type == PIPE_SHADER_TESS_CTRL ||
-           /* GFX9 has the ESGS ring buffer in LDS. */
-           type == SI_SHADER_MERGED_VERTEX_OR_TESSEVAL_GEOMETRY)
-               ac_declare_lds_as_pointer(&ctx->ac);
+       if (shader->key.as_ls || ctx->type == PIPE_SHADER_TESS_CTRL) {
+               if (USE_LDS_SYMBOLS && HAVE_LLVM >= 0x0900) {
+                       /* The LSHS size is not known until draw time, so we append it
+                        * at the end of whatever LDS use there may be in the rest of
+                        * the shader (currently none, unless LLVM decides to do its
+                        * own LDS-based lowering).
+                        */
+                       ctx->ac.lds = LLVMAddGlobalInAddressSpace(
+                               ctx->ac.module, LLVMArrayType(ctx->i32, 0),
+                               "__lds_end", AC_ADDR_SPACE_LDS);
+                       LLVMSetAlignment(ctx->ac.lds, 256);
+               } else {
+                       ac_declare_lds_as_pointer(&ctx->ac);
+               }
+       }
 }
 
 /**
@@ -4929,15 +4948,33 @@ static void preload_ring_buffers(struct si_shader_context *ctx)
        LLVMValueRef buf_ptr = LLVMGetParam(ctx->main_fn,
                                            ctx->param_rw_buffers);
 
-       if (ctx->screen->info.chip_class <= GFX8 &&
-           (ctx->shader->key.as_es || ctx->type == PIPE_SHADER_GEOMETRY)) {
-               unsigned ring =
-                       ctx->type == PIPE_SHADER_GEOMETRY ? SI_GS_RING_ESGS
-                                                            : SI_ES_RING_ESGS;
-               LLVMValueRef offset = LLVMConstInt(ctx->i32, ring, 0);
+       if (ctx->shader->key.as_es || ctx->type == PIPE_SHADER_GEOMETRY) {
+               if (ctx->screen->info.chip_class <= GFX8) {
+                       unsigned ring =
+                               ctx->type == PIPE_SHADER_GEOMETRY ? SI_GS_RING_ESGS
+                                                                 : SI_ES_RING_ESGS;
+                       LLVMValueRef offset = LLVMConstInt(ctx->i32, ring, 0);
 
-               ctx->esgs_ring =
-                       ac_build_load_to_sgpr(&ctx->ac, buf_ptr, offset);
+                       ctx->esgs_ring =
+                               ac_build_load_to_sgpr(&ctx->ac, buf_ptr, offset);
+               } else {
+                       if (USE_LDS_SYMBOLS && HAVE_LLVM >= 0x0900) {
+                               /* Declare the ESGS ring as an explicit LDS symbol.
+                                * For monolithic shaders, we declare the ring only once.
+                                *
+                                * We declare it with 64KB alignment as a hint that the
+                                * pointer value will always be 0.
+                                */
+                               ctx->esgs_ring = LLVMAddGlobalInAddressSpace(
+                                       ctx->ac.module, LLVMArrayType(ctx->i32, 0),
+                                       "esgs_ring",
+                                       AC_ADDR_SPACE_LDS);
+                               LLVMSetAlignment(ctx->esgs_ring, 64 * 1024);
+                       } else {
+                               ac_declare_lds_as_pointer(&ctx->ac);
+                               ctx->esgs_ring = ctx->ac.lds;
+                       }
+               }
        }
 
        if (ctx->shader->is_gs_copy_shader) {
@@ -5055,6 +5092,8 @@ static bool si_shader_binary_open(struct si_screen *screen,
                                  struct si_shader *shader,
                                  struct ac_rtld_binary *rtld)
 {
+       const struct si_shader_selector *sel = shader->selector;
+       enum pipe_shader_type shader_type = sel ? sel->type : PIPE_SHADER_COMPUTE;
        const char *part_elfs[5];
        size_t part_sizes[5];
        unsigned num_parts = 0;
@@ -5074,11 +5113,31 @@ static bool si_shader_binary_open(struct si_screen *screen,
 
 #undef add_part
 
+       struct ac_rtld_symbol lds_symbols[1];
+       unsigned num_lds_symbols = 0;
+
+       if (sel && screen->info.chip_class >= GFX9 &&
+           sel->type == PIPE_SHADER_GEOMETRY && !shader->is_gs_copy_shader) {
+               /* We add this symbol even on LLVM <= 8 to ensure that
+                * shader->config.lds_size is set correctly below.
+                */
+               struct ac_rtld_symbol *sym = &lds_symbols[num_lds_symbols++];
+               sym->name = "esgs_ring";
+               sym->size = shader->gs_info.esgs_ring_size;
+               sym->align = 64 * 1024;
+       }
+
        bool ok = ac_rtld_open(rtld, (struct ac_rtld_open_info){
                        .info = &screen->info,
+                       .options = {
+                               .halt_at_entry = screen->options.halt_shaders,
+                       },
+                       .shader_type = tgsi_processor_to_shader_stage(shader_type),
                        .num_parts = num_parts,
                        .elf_ptrs = part_elfs,
-                       .elf_sizes = part_sizes });
+                       .elf_sizes = part_sizes,
+                       .num_shared_lds_symbols = num_lds_symbols,
+                       .shared_lds_symbols = lds_symbols });
 
        if (rtld->lds_size > 0) {
                unsigned alloc_granularity = screen->info.chip_class >= GFX7 ? 512 : 256;
@@ -5178,13 +5237,8 @@ static void si_shader_dump_disassembly(struct si_screen *screen,
        if (!ac_rtld_get_section_by_name(&rtld_binary, ".AMDGPU.disasm", &disasm, &nbytes))
                goto out;
 
-       fprintf(file, "Shader %s disassembly:\n", name);
-       if (nbytes > INT_MAX) {
-               fprintf(file, "too long\n");
+       if (nbytes > INT_MAX)
                goto out;
-       }
-
-       fprintf(file, "%*s", (int)nbytes, disasm);
 
        if (debug && debug->debug_message) {
                /* Very long debug messages are cut off, so send the
@@ -5200,7 +5254,7 @@ static void si_shader_dump_disassembly(struct si_screen *screen,
                        int count = nbytes - line;
                        const char *nl = memchr(disasm + line, '\n', nbytes - line);
                        if (nl)
-                               count = nl - disasm;
+                               count = nl - (disasm + line);
 
                        if (count) {
                                pipe_debug_message(debug, SHADER_INFO,
@@ -5214,6 +5268,11 @@ static void si_shader_dump_disassembly(struct si_screen *screen,
                                   "Shader Disassembly End");
        }
 
+       if (file) {
+               fprintf(file, "Shader %s disassembly:\n", name);
+               fprintf(file, "%*s", (int)nbytes, disasm);
+       }
+
 out:
        ac_rtld_close(&rtld_binary);
 }
@@ -5279,6 +5338,9 @@ void si_shader_dump_stats_for_shader_db(struct si_screen *screen,
 {
        const struct ac_shader_config *conf = &shader->config;
 
+       if (screen->options.debug_disassembly)
+               si_shader_dump_disassembly(screen, &shader->binary, debug, "main", NULL);
+
        pipe_debug_message(debug, SHADER_INFO,
                           "Shader Stats: SGPRS: %d VGPRS: %d Code Size: %d "
                           "LDS: %d Scratch: %d Max Waves: %d Spilled SGPRs: %d "
@@ -6067,7 +6129,8 @@ static void si_get_ps_prolog_key(struct si_shader *shader,
                        /* BCOLORs are stored after the last input. */
                        key->ps_prolog.num_interp_inputs = info->num_inputs;
                        key->ps_prolog.face_vgpr_index = shader->info.face_vgpr_index;
-                       shader->config.spi_ps_input_ena |= S_0286CC_FRONT_FACE_ENA(1);
+                       if (separate_prolog)
+                               shader->config.spi_ps_input_ena |= S_0286CC_FRONT_FACE_ENA(1);
                }
 
                for (unsigned i = 0; i < 2; i++) {
@@ -6098,18 +6161,24 @@ static void si_get_ps_prolog_key(struct si_shader *shader,
                                switch (location) {
                                case TGSI_INTERPOLATE_LOC_SAMPLE:
                                        key->ps_prolog.color_interp_vgpr_index[i] = 0;
-                                       shader->config.spi_ps_input_ena |=
-                                               S_0286CC_PERSP_SAMPLE_ENA(1);
+                                       if (separate_prolog) {
+                                               shader->config.spi_ps_input_ena |=
+                                                       S_0286CC_PERSP_SAMPLE_ENA(1);
+                                       }
                                        break;
                                case TGSI_INTERPOLATE_LOC_CENTER:
                                        key->ps_prolog.color_interp_vgpr_index[i] = 2;
-                                       shader->config.spi_ps_input_ena |=
-                                               S_0286CC_PERSP_CENTER_ENA(1);
+                                       if (separate_prolog) {
+                                               shader->config.spi_ps_input_ena |=
+                                                       S_0286CC_PERSP_CENTER_ENA(1);
+                                       }
                                        break;
                                case TGSI_INTERPOLATE_LOC_CENTROID:
                                        key->ps_prolog.color_interp_vgpr_index[i] = 4;
-                                       shader->config.spi_ps_input_ena |=
-                                               S_0286CC_PERSP_CENTROID_ENA(1);
+                                       if (separate_prolog) {
+                                               shader->config.spi_ps_input_ena |=
+                                                       S_0286CC_PERSP_CENTROID_ENA(1);
+                                       }
                                        break;
                                default:
                                        assert(0);
@@ -6130,20 +6199,26 @@ static void si_get_ps_prolog_key(struct si_shader *shader,
                                case TGSI_INTERPOLATE_LOC_SAMPLE:
                                        key->ps_prolog.color_interp_vgpr_index[i] =
                                                separate_prolog ? 6 : 9;
-                                       shader->config.spi_ps_input_ena |=
-                                               S_0286CC_LINEAR_SAMPLE_ENA(1);
+                                       if (separate_prolog) {
+                                               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] =
                                                separate_prolog ? 8 : 11;
-                                       shader->config.spi_ps_input_ena |=
-                                               S_0286CC_LINEAR_CENTER_ENA(1);
+                                       if (separate_prolog) {
+                                               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] =
                                                separate_prolog ? 10 : 13;
-                                       shader->config.spi_ps_input_ena |=
-                                               S_0286CC_LINEAR_CENTROID_ENA(1);
+                                       if (separate_prolog) {
+                                               shader->config.spi_ps_input_ena |=
+                                                       S_0286CC_LINEAR_CENTROID_ENA(1);
+                                       }
                                        break;
                                default:
                                        assert(0);
@@ -6523,7 +6598,7 @@ static void si_build_wrapper_function(struct si_shader_context *ctx,
                        out_idx += param_size;
                }
 
-               ret = LLVMBuildCall(builder, parts[part], in, num_params, "");
+               ret = ac_build_call(&ctx->ac, parts[part], in, num_params);
 
                if (is_merged_shader(ctx) &&
                    part + 1 == next_shader_first_part) {
@@ -8013,6 +8088,9 @@ bool si_shader_create(struct si_screen *sscreen, struct ac_llvm_compiler *compil
                si_calculate_max_simd_waves(shader);
        }
 
+       if (sscreen->info.chip_class >= GFX9 && sel->type == PIPE_SHADER_GEOMETRY)
+               gfx9_get_gs_info(shader->previous_stage_sel, sel, &shader->gs_info);
+
        si_fix_resource_usage(sscreen, shader);
        si_shader_dump(sscreen, shader, debug, sel->info.processor,
                       stderr, true);