ac/nir: extract shader_info->fs.can_discard from NIR shader info
[mesa.git] / src / amd / common / ac_nir_to_llvm.c
index adef36d6eac811251cb3dc62b080aa29ae368efc..ecddb5e9b9b10923e4223db8c0df6b5f72f74605 100644 (file)
@@ -47,13 +47,6 @@ enum radeon_llvm_calling_convention {
 #define RADEON_LLVM_MAX_INPUTS (VARYING_SLOT_VAR31 + 1)
 #define RADEON_LLVM_MAX_OUTPUTS (VARYING_SLOT_VAR31 + 1)
 
-enum desc_type {
-       DESC_IMAGE,
-       DESC_FMASK,
-       DESC_SAMPLER,
-       DESC_BUFFER,
-};
-
 struct nir_to_llvm_context;
 
 struct ac_nir_context {
@@ -196,9 +189,11 @@ nir_to_llvm_context_from_abi(struct ac_shader_abi *abi)
        return container_of(abi, ctx, abi);
 }
 
-static LLVMValueRef get_sampler_desc(struct nir_to_llvm_context *ctx,
+static LLVMValueRef get_sampler_desc(struct ac_nir_context *ctx,
                                     const nir_deref_var *deref,
-                                    enum desc_type desc_type);
+                                    enum ac_descriptor_type desc_type,
+                                    bool image, bool write);
+
 static unsigned radeon_llvm_reg_index_soa(unsigned index, unsigned chan)
 {
        return (index * 4) + chan;
@@ -3282,21 +3277,21 @@ static LLVMValueRef adjust_sample_index_using_fmask(struct ac_llvm_context *ctx,
        return sample_index;
 }
 
-static LLVMValueRef get_image_coords(struct nir_to_llvm_context *ctx,
+static LLVMValueRef get_image_coords(struct ac_nir_context *ctx,
                                     const nir_intrinsic_instr *instr)
 {
        const struct glsl_type *type = instr->variables[0]->var->type;
        if(instr->variables[0]->deref.child)
                type = instr->variables[0]->deref.child->type;
 
-       LLVMValueRef src0 = get_src(ctx->nir, instr->src[0]);
+       LLVMValueRef src0 = get_src(ctx, instr->src[0]);
        LLVMValueRef coords[4];
        LLVMValueRef masks[] = {
-               LLVMConstInt(ctx->i32, 0, false), LLVMConstInt(ctx->i32, 1, false),
-               LLVMConstInt(ctx->i32, 2, false), LLVMConstInt(ctx->i32, 3, false),
+               LLVMConstInt(ctx->ac.i32, 0, false), LLVMConstInt(ctx->ac.i32, 1, false),
+               LLVMConstInt(ctx->ac.i32, 2, false), LLVMConstInt(ctx->ac.i32, 3, false),
        };
        LLVMValueRef res;
-       LLVMValueRef sample_index = llvm_extract_elem(&ctx->ac, get_src(ctx->nir, instr->src[1]), 0);
+       LLVMValueRef sample_index = llvm_extract_elem(&ctx->ac, get_src(ctx, instr->src[1]), 0);
 
        int count;
        enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
@@ -3312,26 +3307,26 @@ static LLVMValueRef get_image_coords(struct nir_to_llvm_context *ctx,
                LLVMValueRef fmask_load_address[3];
                int chan;
 
-               fmask_load_address[0] = LLVMBuildExtractElement(ctx->builder, src0, masks[0], "");
-               fmask_load_address[1] = LLVMBuildExtractElement(ctx->builder, src0, masks[1], "");
+               fmask_load_address[0] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[0], "");
+               fmask_load_address[1] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[1], "");
                if (glsl_sampler_type_is_array(type))
-                       fmask_load_address[2] = LLVMBuildExtractElement(ctx->builder, src0, masks[2], "");
+                       fmask_load_address[2] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[2], "");
                else
                        fmask_load_address[2] = NULL;
                if (add_frag_pos) {
                        for (chan = 0; chan < 2; ++chan)
-                               fmask_load_address[chan] = LLVMBuildAdd(ctx->builder, fmask_load_address[chan], LLVMBuildFPToUI(ctx->builder, ctx->frag_pos[chan], ctx->i32, ""), "");
+                               fmask_load_address[chan] = LLVMBuildAdd(ctx->ac.builder, fmask_load_address[chan], LLVMBuildFPToUI(ctx->ac.builder, ctx->nctx->frag_pos[chan], ctx->ac.i32, ""), "");
                }
                sample_index = adjust_sample_index_using_fmask(&ctx->ac,
                                                               fmask_load_address[0],
                                                               fmask_load_address[1],
                                                               fmask_load_address[2],
                                                               sample_index,
-                                                              get_sampler_desc(ctx, instr->variables[0], DESC_FMASK));
+                                                              get_sampler_desc(ctx, instr->variables[0], AC_DESC_FMASK, true, false));
        }
        if (count == 1) {
                if (instr->src[0].ssa->num_components)
-                       res = LLVMBuildExtractElement(ctx->builder, src0, masks[0], "");
+                       res = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[0], "");
                else
                        res = src0;
        } else {
@@ -3339,12 +3334,12 @@ static LLVMValueRef get_image_coords(struct nir_to_llvm_context *ctx,
                if (is_ms)
                        count--;
                for (chan = 0; chan < count; ++chan) {
-                       coords[chan] = LLVMBuildExtractElement(ctx->builder, src0, masks[chan], "");
+                       coords[chan] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[chan], "");
                }
 
                if (add_frag_pos) {
                        for (chan = 0; chan < count; ++chan)
-                               coords[chan] = LLVMBuildAdd(ctx->builder, coords[chan], LLVMBuildFPToUI(ctx->builder, ctx->frag_pos[chan], ctx->i32, ""), "");
+                               coords[chan] = LLVMBuildAdd(ctx->ac.builder, coords[chan], LLVMBuildFPToUI(ctx->ac.builder, ctx->nctx->frag_pos[chan], ctx->ac.i32, ""), "");
                }
                if (is_ms) {
                        coords[count] = sample_index;
@@ -3352,7 +3347,7 @@ static LLVMValueRef get_image_coords(struct nir_to_llvm_context *ctx,
                }
 
                if (count == 3) {
-                       coords[3] = LLVMGetUndef(ctx->i32);
+                       coords[3] = LLVMGetUndef(ctx->ac.i32);
                        count = 4;
                }
                res = ac_build_gather_values(&ctx->ac, coords, count);
@@ -3360,7 +3355,7 @@ static LLVMValueRef get_image_coords(struct nir_to_llvm_context *ctx,
        return res;
 }
 
-static LLVMValueRef visit_image_load(struct nir_to_llvm_context *ctx,
+static LLVMValueRef visit_image_load(struct ac_nir_context *ctx,
                                     const nir_intrinsic_instr *instr)
 {
        LLVMValueRef params[7];
@@ -3368,18 +3363,21 @@ static LLVMValueRef visit_image_load(struct nir_to_llvm_context *ctx,
        char intrinsic_name[64];
        const nir_variable *var = instr->variables[0]->var;
        const struct glsl_type *type = var->type;
+       LLVMValueRef i1false = LLVMConstInt(ctx->ac.i1, 0, false);
+       LLVMValueRef i1true = LLVMConstInt(ctx->ac.i1, 1, false);
+
        if(instr->variables[0]->deref.child)
                type = instr->variables[0]->deref.child->type;
 
        type = glsl_without_array(type);
        if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF) {
-               params[0] = get_sampler_desc(ctx, instr->variables[0], DESC_BUFFER);
-               params[1] = LLVMBuildExtractElement(ctx->builder, get_src(ctx->nir, instr->src[0]),
-                                                   LLVMConstInt(ctx->i32, 0, false), ""); /* vindex */
-               params[2] = LLVMConstInt(ctx->i32, 0, false); /* voffset */
-               params[3] = ctx->i1false;  /* glc */
-               params[4] = ctx->i1false;  /* slc */
-               res = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.buffer.load.format.v4f32", ctx->v4f32,
+               params[0] = get_sampler_desc(ctx, instr->variables[0], AC_DESC_BUFFER, true, false);
+               params[1] = LLVMBuildExtractElement(ctx->ac.builder, get_src(ctx, instr->src[0]),
+                                                   ctx->ac.i32_0, ""); /* vindex */
+               params[2] = ctx->ac.i32_0; /* voffset */
+               params[3] = i1false;  /* glc */
+               params[4] = i1false;  /* slc */
+               res = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.buffer.load.format.v4f32", ctx->ac.v4f32,
                                         params, 5, 0);
 
                res = trim_vector(&ctx->ac, res, instr->dest.ssa.num_components);
@@ -3387,20 +3385,20 @@ static LLVMValueRef visit_image_load(struct nir_to_llvm_context *ctx,
        } else {
                bool is_da = glsl_sampler_type_is_array(type) ||
                             glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE;
-               LLVMValueRef da = is_da ? ctx->i1true : ctx->i1false;
-               LLVMValueRef glc = ctx->i1false;
-               LLVMValueRef slc = ctx->i1false;
+               LLVMValueRef da = is_da ? i1true : i1false;
+               LLVMValueRef glc = i1false;
+               LLVMValueRef slc = i1false;
 
                params[0] = get_image_coords(ctx, instr);
-               params[1] = get_sampler_desc(ctx, instr->variables[0], DESC_IMAGE);
-               params[2] = LLVMConstInt(ctx->i32, 15, false); /* dmask */
+               params[1] = get_sampler_desc(ctx, instr->variables[0], AC_DESC_IMAGE, true, false);
+               params[2] = LLVMConstInt(ctx->ac.i32, 15, false); /* dmask */
                if (HAVE_LLVM <= 0x0309) {
-                       params[3] = ctx->i1false;  /* r128 */
+                       params[3] = i1false;  /* r128 */
                        params[4] = da;
                        params[5] = glc;
                        params[6] = slc;
                } else {
-                       LLVMValueRef lwe = ctx->i1false;
+                       LLVMValueRef lwe = i1false;
                        params[3] = glc;
                        params[4] = slc;
                        params[5] = lwe;
@@ -3408,58 +3406,58 @@ static LLVMValueRef visit_image_load(struct nir_to_llvm_context *ctx,
                }
 
                ac_get_image_intr_name("llvm.amdgcn.image.load",
-                                      ctx->v4f32, /* vdata */
+                                      ctx->ac.v4f32, /* vdata */
                                       LLVMTypeOf(params[0]), /* coords */
                                       LLVMTypeOf(params[1]), /* rsrc */
                                       intrinsic_name, sizeof(intrinsic_name));
 
-               res = ac_build_intrinsic(&ctx->ac, intrinsic_name, ctx->v4f32,
+               res = ac_build_intrinsic(&ctx->ac, intrinsic_name, ctx->ac.v4f32,
                                         params, 7, AC_FUNC_ATTR_READONLY);
        }
        return to_integer(&ctx->ac, res);
 }
 
-static void visit_image_store(struct nir_to_llvm_context *ctx,
+static void visit_image_store(struct ac_nir_context *ctx,
                              nir_intrinsic_instr *instr)
 {
        LLVMValueRef params[8];
        char intrinsic_name[64];
        const nir_variable *var = instr->variables[0]->var;
        const struct glsl_type *type = glsl_without_array(var->type);
-       LLVMValueRef glc = ctx->i1false;
-       bool force_glc = ctx->options->chip_class == SI;
+       LLVMValueRef i1false = LLVMConstInt(ctx->ac.i1, 0, false);
+       LLVMValueRef i1true = LLVMConstInt(ctx->ac.i1, 1, false);
+       LLVMValueRef glc = i1false;
+       bool force_glc = ctx->abi->chip_class == SI;
        if (force_glc)
-               glc = ctx->i1true;
-       if (ctx->stage == MESA_SHADER_FRAGMENT)
-               ctx->shader_info->fs.writes_memory = true;
+               glc = i1true;
 
        if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF) {
-               params[0] = to_float(&ctx->ac, get_src(ctx->nir, instr->src[2])); /* data */
-               params[1] = get_sampler_desc(ctx, instr->variables[0], DESC_BUFFER);
-               params[2] = LLVMBuildExtractElement(ctx->builder, get_src(ctx->nir, instr->src[0]),
-                                                   LLVMConstInt(ctx->i32, 0, false), ""); /* vindex */
-               params[3] = LLVMConstInt(ctx->i32, 0, false); /* voffset */
+               params[0] = to_float(&ctx->ac, get_src(ctx, instr->src[2])); /* data */
+               params[1] = get_sampler_desc(ctx, instr->variables[0], AC_DESC_BUFFER, true, true);
+               params[2] = LLVMBuildExtractElement(ctx->ac.builder, get_src(ctx, instr->src[0]),
+                                                   ctx->ac.i32_0, ""); /* vindex */
+               params[3] = ctx->ac.i32_0; /* voffset */
                params[4] = glc;  /* glc */
-               params[5] = ctx->i1false;  /* slc */
-               ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.buffer.store.format.v4f32", ctx->voidt,
+               params[5] = i1false;  /* slc */
+               ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.buffer.store.format.v4f32", ctx->ac.voidt,
                                   params, 6, 0);
        } else {
                bool is_da = glsl_sampler_type_is_array(type) ||
                             glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE;
-               LLVMValueRef da = is_da ? ctx->i1true : ctx->i1false;
-               LLVMValueRef slc = ctx->i1false;
+               LLVMValueRef da = is_da ? i1true : i1false;
+               LLVMValueRef slc = i1false;
 
-               params[0] = to_float(&ctx->ac, get_src(ctx->nir, instr->src[2]));
+               params[0] = to_float(&ctx->ac, get_src(ctx, instr->src[2]));
                params[1] = get_image_coords(ctx, instr); /* coords */
-               params[2] = get_sampler_desc(ctx, instr->variables[0], DESC_IMAGE);
-               params[3] = LLVMConstInt(ctx->i32, 15, false); /* dmask */
+               params[2] = get_sampler_desc(ctx, instr->variables[0], AC_DESC_IMAGE, true, true);
+               params[3] = LLVMConstInt(ctx->ac.i32, 15, false); /* dmask */
                if (HAVE_LLVM <= 0x0309) {
-                       params[4] = ctx->i1false;  /* r128 */
+                       params[4] = i1false;  /* r128 */
                        params[5] = da;
                        params[6] = glc;
                        params[7] = slc;
                } else {
-                       LLVMValueRef lwe = ctx->i1false;
+                       LLVMValueRef lwe = i1false;
                        params[4] = glc;
                        params[5] = slc;
                        params[6] = lwe;
@@ -3472,13 +3470,13 @@ static void visit_image_store(struct nir_to_llvm_context *ctx,
                                       LLVMTypeOf(params[2]), /* rsrc */
                                       intrinsic_name, sizeof(intrinsic_name));
 
-               ac_build_intrinsic(&ctx->ac, intrinsic_name, ctx->voidt,
+               ac_build_intrinsic(&ctx->ac, intrinsic_name, ctx->ac.voidt,
                                   params, 8, 0);
        }
 
 }
 
-static LLVMValueRef visit_image_atomic(struct nir_to_llvm_context *ctx,
+static LLVMValueRef visit_image_atomic(struct ac_nir_context *ctx,
                                        const nir_intrinsic_instr *instr)
 {
        LLVMValueRef params[6];
@@ -3488,11 +3486,10 @@ static LLVMValueRef visit_image_atomic(struct nir_to_llvm_context *ctx,
        const char *atomic_name;
        char intrinsic_name[41];
        const struct glsl_type *type = glsl_without_array(var->type);
+       LLVMValueRef i1false = LLVMConstInt(ctx->ac.i1, 0, false);
+       LLVMValueRef i1true = LLVMConstInt(ctx->ac.i1, 1, false);
        MAYBE_UNUSED int length;
 
-       if (ctx->stage == MESA_SHADER_FRAGMENT)
-               ctx->shader_info->fs.writes_memory = true;
-
        switch (instr->intrinsic) {
        case nir_intrinsic_image_atomic_add:
                atomic_name = "add";
@@ -3523,15 +3520,16 @@ static LLVMValueRef visit_image_atomic(struct nir_to_llvm_context *ctx,
        }
 
        if (instr->intrinsic == nir_intrinsic_image_atomic_comp_swap)
-               params[param_count++] = get_src(ctx->nir, instr->src[3]);
-       params[param_count++] = get_src(ctx->nir, instr->src[2]);
+               params[param_count++] = get_src(ctx, instr->src[3]);
+       params[param_count++] = get_src(ctx, instr->src[2]);
 
        if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF) {
-               params[param_count++] = get_sampler_desc(ctx, instr->variables[0], DESC_BUFFER);
-               params[param_count++] = LLVMBuildExtractElement(ctx->builder, get_src(ctx->nir, instr->src[0]),
-                                                               LLVMConstInt(ctx->i32, 0, false), ""); /* vindex */
-               params[param_count++] = ctx->i32zero; /* voffset */
-               params[param_count++] = ctx->i1false;  /* slc */
+               params[param_count++] = get_sampler_desc(ctx, instr->variables[0], AC_DESC_BUFFER,
+                                                        true, true);
+               params[param_count++] = LLVMBuildExtractElement(ctx->ac.builder, get_src(ctx, instr->src[0]),
+                                                               ctx->ac.i32_0, ""); /* vindex */
+               params[param_count++] = ctx->ac.i32_0; /* voffset */
+               params[param_count++] = i1false;  /* slc */
 
                length = snprintf(intrinsic_name, sizeof(intrinsic_name),
                                  "llvm.amdgcn.buffer.atomic.%s", atomic_name);
@@ -3542,10 +3540,11 @@ static LLVMValueRef visit_image_atomic(struct nir_to_llvm_context *ctx,
                          glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE;
 
                LLVMValueRef coords = params[param_count++] = get_image_coords(ctx, instr);
-               params[param_count++] = get_sampler_desc(ctx, instr->variables[0], DESC_IMAGE);
-               params[param_count++] = ctx->i1false; /* r128 */
-               params[param_count++] = da ? ctx->i1true : ctx->i1false;      /* da */
-               params[param_count++] = ctx->i1false;  /* slc */
+               params[param_count++] = get_sampler_desc(ctx, instr->variables[0], AC_DESC_IMAGE,
+                                                        true, true);
+               params[param_count++] = i1false; /* r128 */
+               params[param_count++] = da ? i1true : i1false;      /* da */
+               params[param_count++] = i1false;  /* slc */
 
                build_int_type_name(LLVMTypeOf(coords),
                                    coords_type, sizeof(coords_type));
@@ -3555,7 +3554,7 @@ static LLVMValueRef visit_image_atomic(struct nir_to_llvm_context *ctx,
        }
 
        assert(length < sizeof(intrinsic_name));
-       return ac_build_intrinsic(&ctx->ac, intrinsic_name, ctx->i32, params, param_count, 0);
+       return ac_build_intrinsic(&ctx->ac, intrinsic_name, ctx->ac.i32, params, param_count, 0);
 }
 
 static LLVMValueRef visit_image_size(struct ac_nir_context *ctx,
@@ -3570,13 +3569,15 @@ static LLVMValueRef visit_image_size(struct ac_nir_context *ctx,
                type = instr->variables[0]->deref.child->type;
 
        if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF)
-               return get_buffer_size(ctx, get_sampler_desc(ctx->nctx, instr->variables[0], DESC_BUFFER), true);
+               return get_buffer_size(ctx,
+                       get_sampler_desc(ctx, instr->variables[0],
+                                        AC_DESC_BUFFER, true, false), true);
 
        struct ac_image_args args = { 0 };
 
        args.da = da;
        args.dmask = 0xf;
-       args.resource = get_sampler_desc(ctx->nctx, instr->variables[0], DESC_IMAGE);
+       args.resource = get_sampler_desc(ctx, instr->variables[0], AC_DESC_IMAGE, true, false);
        args.opcode = ac_image_get_resinfo;
        args.addr = ctx->ac.i32_0;
 
@@ -3626,7 +3627,6 @@ static void emit_discard_if(struct nir_to_llvm_context *ctx,
                            const nir_intrinsic_instr *instr)
 {
        LLVMValueRef cond;
-       ctx->shader_info->fs.can_discard = true;
 
        cond = LLVMBuildICmp(ctx->builder, LLVMIntNE,
                             get_src(ctx->nir, instr->src[0]),
@@ -4022,8 +4022,6 @@ static void visit_intrinsic(struct ac_nir_context *ctx,
                break;
        case nir_intrinsic_load_instance_id:
                result = ctx->abi->instance_id;
-               ctx->nctx->shader_info->vs.vgpr_comp_cnt = MAX2(3,
-                                           ctx->nctx->shader_info->vs.vgpr_comp_cnt);
                break;
        case nir_intrinsic_load_num_work_groups:
                result = ctx->nctx->num_work_groups;
@@ -4068,10 +4066,10 @@ static void visit_intrinsic(struct ac_nir_context *ctx,
                visit_store_var(ctx, instr);
                break;
        case nir_intrinsic_image_load:
-               result = visit_image_load(ctx->nctx, instr);
+               result = visit_image_load(ctx, instr);
                break;
        case nir_intrinsic_image_store:
-               visit_image_store(ctx->nctx, instr);
+               visit_image_store(ctx, instr);
                break;
        case nir_intrinsic_image_atomic_add:
        case nir_intrinsic_image_atomic_min:
@@ -4081,13 +4079,12 @@ static void visit_intrinsic(struct ac_nir_context *ctx,
        case nir_intrinsic_image_atomic_xor:
        case nir_intrinsic_image_atomic_exchange:
        case nir_intrinsic_image_atomic_comp_swap:
-               result = visit_image_atomic(ctx->nctx, instr);
+               result = visit_image_atomic(ctx, instr);
                break;
        case nir_intrinsic_image_size:
                result = visit_image_size(ctx, instr);
                break;
        case nir_intrinsic_discard:
-               ctx->nctx->shader_info->fs.can_discard = true;
                ac_build_intrinsic(&ctx->ac, "llvm.AMDGPU.kilp",
                                   LLVMVoidTypeInContext(ctx->ac.context),
                                   NULL, 0, AC_FUNC_ATTR_LEGACY);
@@ -4141,42 +4138,47 @@ static void visit_intrinsic(struct ac_nir_context *ctx,
        }
 }
 
-static LLVMValueRef get_sampler_desc(struct nir_to_llvm_context *ctx,
-                                    const nir_deref_var *deref,
-                                    enum desc_type desc_type)
+static LLVMValueRef radv_get_sampler_desc(struct ac_shader_abi *abi,
+                                         unsigned descriptor_set,
+                                         unsigned base_index,
+                                         unsigned constant_index,
+                                         LLVMValueRef index,
+                                         enum ac_descriptor_type desc_type,
+                                         bool image, bool write)
 {
-       unsigned desc_set = deref->var->data.descriptor_set;
-       LLVMValueRef list = ctx->descriptor_sets[desc_set];
-       struct radv_descriptor_set_layout *layout = ctx->options->layout->set[desc_set].layout;
-       struct radv_descriptor_set_binding_layout *binding = layout->binding + deref->var->data.binding;
+       struct nir_to_llvm_context *ctx = nir_to_llvm_context_from_abi(abi);
+       LLVMValueRef list = ctx->descriptor_sets[descriptor_set];
+       struct radv_descriptor_set_layout *layout = ctx->options->layout->set[descriptor_set].layout;
+       struct radv_descriptor_set_binding_layout *binding = layout->binding + base_index;
        unsigned offset = binding->offset;
        unsigned stride = binding->size;
        unsigned type_size;
        LLVMBuilderRef builder = ctx->builder;
        LLVMTypeRef type;
-       LLVMValueRef index = NULL;
-       unsigned constant_index = 0;
 
-       assert(deref->var->data.binding < layout->binding_count);
+       assert(base_index < layout->binding_count);
+
+       if (write && ctx->stage == MESA_SHADER_FRAGMENT)
+               ctx->shader_info->fs.writes_memory = true;
 
        switch (desc_type) {
-       case DESC_IMAGE:
+       case AC_DESC_IMAGE:
                type = ctx->v8i32;
                type_size = 32;
                break;
-       case DESC_FMASK:
+       case AC_DESC_FMASK:
                type = ctx->v8i32;
                offset += 32;
                type_size = 32;
                break;
-       case DESC_SAMPLER:
+       case AC_DESC_SAMPLER:
                type = ctx->v4i32;
                if (binding->type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
                        offset += 64;
 
                type_size = 16;
                break;
-       case DESC_BUFFER:
+       case AC_DESC_BUFFER:
                type = ctx->v4i32;
                type_size = 16;
                break;
@@ -4184,19 +4186,9 @@ static LLVMValueRef get_sampler_desc(struct nir_to_llvm_context *ctx,
                unreachable("invalid desc_type\n");
        }
 
-       if (deref->deref.child) {
-               const nir_deref_array *child =
-                       (const nir_deref_array *)deref->deref.child;
+       offset += constant_index * stride;
 
-               assert(child->deref_array_type != nir_deref_array_type_wildcard);
-               offset += child->base_offset * stride;
-               if (child->deref_array_type == nir_deref_array_type_indirect) {
-                       index = get_src(ctx->nir, child->indirect);
-               }
-
-               constant_index = child->base_offset;
-       }
-       if (desc_type == DESC_SAMPLER && binding->immutable_samplers_offset &&
+       if (desc_type == AC_DESC_SAMPLER && binding->immutable_samplers_offset &&
            (!index || binding->immutable_samplers_equal)) {
                if (binding->immutable_samplers_equal)
                        constant_index = 0;
@@ -4225,6 +4217,48 @@ static LLVMValueRef get_sampler_desc(struct nir_to_llvm_context *ctx,
        return ac_build_indexed_load_const(&ctx->ac, list, index);
 }
 
+static LLVMValueRef get_sampler_desc(struct ac_nir_context *ctx,
+                                    const nir_deref_var *deref,
+                                    enum ac_descriptor_type desc_type,
+                                    bool image, bool write)
+{
+       LLVMValueRef index = NULL;
+       unsigned constant_index = 0;
+       const nir_deref *tail = &deref->deref;
+
+       while (tail->child) {
+               const nir_deref_array *child = nir_deref_as_array(tail->child);
+               unsigned array_size = glsl_get_aoa_size(tail->child->type);
+
+               if (!array_size)
+                       array_size = 1;
+
+               assert(child->deref_array_type != nir_deref_array_type_wildcard);
+
+               if (child->deref_array_type == nir_deref_array_type_indirect) {
+                       LLVMValueRef indirect = get_src(ctx, child->indirect);
+
+                       indirect = LLVMBuildMul(ctx->ac.builder, indirect,
+                               LLVMConstInt(ctx->ac.i32, array_size, false), "");
+
+                       if (!index)
+                               index = indirect;
+                       else
+                               index = LLVMBuildAdd(ctx->ac.builder, index, indirect, "");
+               }
+
+               constant_index += child->base_offset * array_size;
+
+               tail = &child->deref;
+       }
+
+       return ctx->abi->load_sampler_desc(ctx->abi,
+                                         deref->var->data.descriptor_set,
+                                         deref->var->data.binding,
+                                         constant_index, index,
+                                         desc_type, image, write);
+}
+
 static void set_tex_fetch_args(struct ac_llvm_context *ctx,
                               struct ac_image_args *args,
                               const nir_tex_instr *instr,
@@ -4271,44 +4305,44 @@ static void set_tex_fetch_args(struct ac_llvm_context *ctx,
  * VI:
  *   The ANISO_OVERRIDE sampler field enables this fix in TA.
  */
-static LLVMValueRef sici_fix_sampler_aniso(struct nir_to_llvm_context *ctx,
+static LLVMValueRef sici_fix_sampler_aniso(struct ac_nir_context *ctx,
                                            LLVMValueRef res, LLVMValueRef samp)
 {
-       LLVMBuilderRef builder = ctx->builder;
+       LLVMBuilderRef builder = ctx->ac.builder;
        LLVMValueRef img7, samp0;
 
-       if (ctx->options->chip_class >= VI)
+       if (ctx->abi->chip_class >= VI)
                return samp;
 
        img7 = LLVMBuildExtractElement(builder, res,
-                                      LLVMConstInt(ctx->i32, 7, 0), "");
+                                      LLVMConstInt(ctx->ac.i32, 7, 0), "");
        samp0 = LLVMBuildExtractElement(builder, samp,
-                                       LLVMConstInt(ctx->i32, 0, 0), "");
+                                       LLVMConstInt(ctx->ac.i32, 0, 0), "");
        samp0 = LLVMBuildAnd(builder, samp0, img7, "");
        return LLVMBuildInsertElement(builder, samp, samp0,
-                                     LLVMConstInt(ctx->i32, 0, 0), "");
+                                     LLVMConstInt(ctx->ac.i32, 0, 0), "");
 }
 
-static void tex_fetch_ptrs(struct nir_to_llvm_context *ctx,
+static void tex_fetch_ptrs(struct ac_nir_context *ctx,
                           nir_tex_instr *instr,
                           LLVMValueRef *res_ptr, LLVMValueRef *samp_ptr,
                           LLVMValueRef *fmask_ptr)
 {
        if (instr->sampler_dim  == GLSL_SAMPLER_DIM_BUF)
-               *res_ptr = get_sampler_desc(ctx, instr->texture, DESC_BUFFER);
+               *res_ptr = get_sampler_desc(ctx, instr->texture, AC_DESC_BUFFER, false, false);
        else
-               *res_ptr = get_sampler_desc(ctx, instr->texture, DESC_IMAGE);
+               *res_ptr = get_sampler_desc(ctx, instr->texture, AC_DESC_IMAGE, false, false);
        if (samp_ptr) {
                if (instr->sampler)
-                       *samp_ptr = get_sampler_desc(ctx, instr->sampler, DESC_SAMPLER);
+                       *samp_ptr = get_sampler_desc(ctx, instr->sampler, AC_DESC_SAMPLER, false, false);
                else
-                       *samp_ptr = get_sampler_desc(ctx, instr->texture, DESC_SAMPLER);
+                       *samp_ptr = get_sampler_desc(ctx, instr->texture, AC_DESC_SAMPLER, false, false);
                if (instr->sampler_dim < GLSL_SAMPLER_DIM_RECT)
                        *samp_ptr = sici_fix_sampler_aniso(ctx, *res_ptr, *samp_ptr);
        }
        if (fmask_ptr && !instr->sampler && (instr->op == nir_texop_txf_ms ||
                                             instr->op == nir_texop_samples_identical))
-               *fmask_ptr = get_sampler_desc(ctx, instr->texture, DESC_FMASK);
+               *fmask_ptr = get_sampler_desc(ctx, instr->texture, AC_DESC_FMASK, false, false);
 }
 
 static LLVMValueRef apply_round_slice(struct ac_llvm_context *ctx,
@@ -4336,7 +4370,7 @@ static void visit_tex(struct ac_nir_context *ctx, nir_tex_instr *instr)
        unsigned const_src = 0, num_deriv_comp = 0;
        bool lod_is_zero = false;
 
-       tex_fetch_ptrs(ctx->nctx, instr, &res_ptr, &samp_ptr, &fmask_ptr);
+       tex_fetch_ptrs(ctx, instr, &res_ptr, &samp_ptr, &fmask_ptr);
 
        for (unsigned i = 0; i < instr->num_srcs; i++) {
                switch (instr->src[i].src_type) {
@@ -4588,7 +4622,9 @@ static void visit_tex(struct ac_nir_context *ctx, nir_tex_instr *instr)
 
        if (instr->op == nir_texop_query_levels)
                result = LLVMBuildExtractElement(ctx->ac.builder, result, LLVMConstInt(ctx->ac.i32, 3, false), "");
-       else if (instr->is_shadow && instr->op != nir_texop_txs && instr->op != nir_texop_lod && instr->op != nir_texop_tg4)
+       else if (instr->is_shadow && instr->is_new_style_shadow &&
+                instr->op != nir_texop_txs && instr->op != nir_texop_lod &&
+                instr->op != nir_texop_tg4)
                result = LLVMBuildExtractElement(ctx->ac.builder, result, ctx->ac.i32_0, "");
        else if (instr->op == nir_texop_txs &&
                 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
@@ -6162,7 +6198,7 @@ LLVMModuleRef ac_translate_nir_to_llvm(LLVMTargetMachineRef tm,
        memset(shader_info, 0, sizeof(*shader_info));
 
        ac_nir_shader_info_pass(nir, options, &shader_info->info);
-               
+
        LLVMSetTarget(ctx.module, options->supports_spill ? "amdgcn-mesa-mesa3d" : "amdgcn--");
 
        LLVMTargetDataRef data_layout = LLVMCreateTargetDataLayout(tm);
@@ -6191,6 +6227,13 @@ LLVMModuleRef ac_translate_nir_to_llvm(LLVMTargetMachineRef tm,
                ctx.gs_max_out_vertices = nir->info.gs.vertices_out;
        } else if (nir->stage == MESA_SHADER_TESS_EVAL) {
                ctx.tes_primitive_mode = nir->info.tess.primitive_mode;
+       } else if (nir->stage == MESA_SHADER_VERTEX) {
+               if (shader_info->info.vs.needs_instance_id) {
+                       ctx.shader_info->vs.vgpr_comp_cnt =
+                               MAX2(3, ctx.shader_info->vs.vgpr_comp_cnt);
+               }
+       } else if (nir->stage == MESA_SHADER_FRAGMENT) {
+               shader_info->fs.can_discard = nir->info.fs.uses_discard;
        }
 
        ac_setup_rings(&ctx);
@@ -6207,6 +6250,7 @@ LLVMModuleRef ac_translate_nir_to_llvm(LLVMTargetMachineRef tm,
        ctx.abi.chip_class = options->chip_class;
        ctx.abi.inputs = &ctx.inputs[0];
        ctx.abi.emit_outputs = handle_shader_outputs_post;
+       ctx.abi.load_sampler_desc = radv_get_sampler_desc;
 
        nir_foreach_variable(variable, &nir->outputs)
                scan_shader_output_decl(&ctx, variable);