ac/registers: use better names for disambiguated definitions
[mesa.git] / src / amd / common / ac_nir_to_llvm.c
index d983ac77ebdf050ffa2af01bef02b46b36199f65..f26e74cb63ec81af32e27518c5efd82e109ec558 100644 (file)
@@ -38,6 +38,7 @@ struct ac_nir_context {
        struct ac_shader_abi *abi;
 
        gl_shader_stage stage;
+       shader_info *info;
 
        LLVMValueRef *ssa_defs;
 
@@ -112,7 +113,7 @@ get_ac_image_dim(const struct ac_llvm_context *ctx, enum glsl_sampler_dim sdim,
        enum ac_image_dim dim = get_ac_sampler_dim(ctx, sdim, is_array);
 
        if (dim == ac_image_cube ||
-           (ctx->chip_class <= VI && dim == ac_image_3d))
+           (ctx->chip_class <= GFX8 && dim == ac_image_3d))
                dim = ac_image_2darray;
 
        return dim;
@@ -371,7 +372,7 @@ static LLVMValueRef emit_f2f16(struct ac_llvm_context *ctx,
        src0 = ac_to_float(ctx, src0);
        result = LLVMBuildFPTrunc(ctx->builder, src0, ctx->f16, "");
 
-       if (ctx->chip_class >= VI) {
+       if (ctx->chip_class >= GFX8) {
                LLVMValueRef args[2];
                /* Check if the result is a denormal - and flush to 0 if so. */
                args[0] = result;
@@ -382,10 +383,10 @@ static LLVMValueRef emit_f2f16(struct ac_llvm_context *ctx,
        /* need to convert back up to f32 */
        result = LLVMBuildFPExt(ctx->builder, result, ctx->f32, "");
 
-       if (ctx->chip_class >= VI)
+       if (ctx->chip_class >= GFX8)
                result = LLVMBuildSelect(ctx->builder, cond, ctx->f32_0, result, "");
        else {
-               /* for SI/CIK */
+               /* for GFX6-GFX7 */
                /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
                 * so compare the result and flush to 0 if it's smaller.
                 */
@@ -540,27 +541,6 @@ static LLVMValueRef emit_ddxy(struct ac_nir_context *ctx,
        return result;
 }
 
-/*
- * this takes an I,J coordinate pair,
- * and works out the X and Y derivatives.
- * it returns DDX(I), DDX(J), DDY(I), DDY(J).
- */
-static LLVMValueRef emit_ddxy_interp(
-       struct ac_nir_context *ctx,
-       LLVMValueRef interp_ij)
-{
-       LLVMValueRef result[4], a;
-       unsigned i;
-
-       for (i = 0; i < 2; i++) {
-               a = LLVMBuildExtractElement(ctx->ac.builder, interp_ij,
-                                           LLVMConstInt(ctx->ac.i32, i, false), "");
-               result[i] = emit_ddxy(ctx, nir_op_fddx, a);
-               result[2+i] = emit_ddxy(ctx, nir_op_fddy, a);
-       }
-       return ac_build_gather_values(&ctx->ac, result, 4);
-}
-
 static void visit_alu(struct ac_nir_context *ctx, const nir_alu_instr *instr)
 {
        LLVMValueRef src[4], result = NULL;
@@ -593,8 +573,7 @@ static void visit_alu(struct ac_nir_context *ctx, const nir_alu_instr *instr)
                src[i] = get_alu_src(ctx, instr->src[i], src_components);
 
        switch (instr->op) {
-       case nir_op_fmov:
-       case nir_op_imov:
+       case nir_op_mov:
                result = src[0];
                break;
        case nir_op_fneg:
@@ -895,13 +874,11 @@ static void visit_alu(struct ac_nir_context *ctx, const nir_alu_instr *instr)
        case nir_op_i2f16:
        case nir_op_i2f32:
        case nir_op_i2f64:
-               src[0] = ac_to_integer(&ctx->ac, src[0]);
                result = LLVMBuildSIToFP(ctx->ac.builder, src[0], ac_to_float_type(&ctx->ac, def_type), "");
                break;
        case nir_op_u2f16:
        case nir_op_u2f32:
        case nir_op_u2f64:
-               src[0] = ac_to_integer(&ctx->ac, src[0]);
                result = LLVMBuildUIToFP(ctx->ac.builder, src[0], ac_to_float_type(&ctx->ac, def_type), "");
                break;
        case nir_op_f2f16_rtz:
@@ -926,7 +903,6 @@ static void visit_alu(struct ac_nir_context *ctx, const nir_alu_instr *instr)
        case nir_op_u2u16:
        case nir_op_u2u32:
        case nir_op_u2u64:
-               src[0] = ac_to_integer(&ctx->ac, src[0]);
                if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])) < ac_get_elem_bits(&ctx->ac, def_type))
                        result = LLVMBuildZExt(ctx->ac.builder, src[0], def_type, "");
                else
@@ -936,7 +912,6 @@ static void visit_alu(struct ac_nir_context *ctx, const nir_alu_instr *instr)
        case nir_op_i2i16:
        case nir_op_i2i32:
        case nir_op_i2i64:
-               src[0] = ac_to_integer(&ctx->ac, src[0]);
                if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])) < ac_get_elem_bits(&ctx->ac, def_type))
                        result = LLVMBuildSExt(ctx->ac.builder, src[0], def_type, "");
                else
@@ -946,25 +921,18 @@ static void visit_alu(struct ac_nir_context *ctx, const nir_alu_instr *instr)
                result = emit_bcsel(&ctx->ac, src[0], src[1], src[2]);
                break;
        case nir_op_find_lsb:
-               src[0] = ac_to_integer(&ctx->ac, src[0]);
                result = ac_find_lsb(&ctx->ac, ctx->ac.i32, src[0]);
                break;
        case nir_op_ufind_msb:
-               src[0] = ac_to_integer(&ctx->ac, src[0]);
                result = ac_build_umsb(&ctx->ac, src[0], ctx->ac.i32);
                break;
        case nir_op_ifind_msb:
-               src[0] = ac_to_integer(&ctx->ac, src[0]);
                result = ac_build_imsb(&ctx->ac, src[0], ctx->ac.i32);
                break;
        case nir_op_uadd_carry:
-               src[0] = ac_to_integer(&ctx->ac, src[0]);
-               src[1] = ac_to_integer(&ctx->ac, src[1]);
                result = emit_uint_carry(&ctx->ac, "llvm.uadd.with.overflow.i32", src[0], src[1]);
                break;
        case nir_op_usub_borrow:
-               src[0] = ac_to_integer(&ctx->ac, src[0]);
-               src[1] = ac_to_integer(&ctx->ac, src[1]);
                result = emit_uint_carry(&ctx->ac, "llvm.usub.with.overflow.i32", src[0], src[1]);
                break;
        case nir_op_b2f16:
@@ -982,20 +950,15 @@ static void visit_alu(struct ac_nir_context *ctx, const nir_alu_instr *instr)
                result = emit_b2i(&ctx->ac, src[0], instr->dest.dest.ssa.bit_size);
                break;
        case nir_op_i2b32:
-               src[0] = ac_to_integer(&ctx->ac, src[0]);
                result = emit_i2b(&ctx->ac, src[0]);
                break;
        case nir_op_fquantize2f16:
                result = emit_f2f16(&ctx->ac, src[0]);
                break;
        case nir_op_umul_high:
-               src[0] = ac_to_integer(&ctx->ac, src[0]);
-               src[1] = ac_to_integer(&ctx->ac, src[1]);
                result = emit_umul_high(&ctx->ac, src[0], src[1]);
                break;
        case nir_op_imul_high:
-               src[0] = ac_to_integer(&ctx->ac, src[0]);
-               src[1] = ac_to_integer(&ctx->ac, src[1]);
                result = emit_imul_high(&ctx->ac, src[0], src[1]);
                break;
        case nir_op_pack_half_2x16:
@@ -1034,8 +997,7 @@ static void visit_alu(struct ac_nir_context *ctx, const nir_alu_instr *instr)
        }
 
        case nir_op_pack_64_2x32_split: {
-               LLVMValueRef tmp = LLVMGetUndef(ctx->ac.v2i32);
-               tmp = ac_build_gather_values(&ctx->ac, src, 2);
+               LLVMValueRef tmp = ac_build_gather_values(&ctx->ac, src, 2);
                result = LLVMBuildBitCast(ctx->ac.builder, tmp, ctx->ac.i64, "");
                break;
        }
@@ -1070,10 +1032,17 @@ static void visit_alu(struct ac_nir_context *ctx, const nir_alu_instr *instr)
                LLVMValueRef in[3];
                for (unsigned chan = 0; chan < 3; chan++)
                        in[chan] = ac_llvm_extract_elem(&ctx->ac, src[0], chan);
-               results[0] = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.cubetc",
+               results[0] = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.cubesc",
                                                ctx->ac.f32, in, 3, AC_FUNC_ATTR_READNONE);
-               results[1] = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.cubesc",
+               results[1] = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.cubetc",
                                                ctx->ac.f32, in, 3, AC_FUNC_ATTR_READNONE);
+               LLVMValueRef ma = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.cubema",
+                                                    ctx->ac.f32, in, 3, AC_FUNC_ATTR_READNONE);
+               results[0] = ac_build_fdiv(&ctx->ac, results[0], ma);
+               results[1] = ac_build_fdiv(&ctx->ac, results[1], ma);
+               LLVMValueRef offset = LLVMConstReal(ctx->ac.f32, 0.5);
+               results[0] = LLVMBuildFAdd(ctx->ac.builder, results[0], offset, "");
+               results[1] = LLVMBuildFAdd(ctx->ac.builder, results[1], offset, "");
                result = ac_build_gather_values(&ctx->ac, results, 2);
                break;
        }
@@ -1164,19 +1133,19 @@ static void visit_load_const(struct ac_nir_context *ctx,
                switch (instr->def.bit_size) {
                case 8:
                        values[i] = LLVMConstInt(element_type,
-                                                instr->value.u8[i], false);
+                                                instr->value[i].u8, false);
                        break;
                case 16:
                        values[i] = LLVMConstInt(element_type,
-                                                instr->value.u16[i], false);
+                                                instr->value[i].u16, false);
                        break;
                case 32:
                        values[i] = LLVMConstInt(element_type,
-                                                instr->value.u32[i], false);
+                                                instr->value[i].u32, false);
                        break;
                case 64:
                        values[i] = LLVMConstInt(element_type,
-                                                instr->value.u64[i], false);
+                                                instr->value[i].u64, false);
                        break;
                default:
                        fprintf(stderr,
@@ -1200,9 +1169,9 @@ get_buffer_size(struct ac_nir_context *ctx, LLVMValueRef descriptor, bool in_ele
                LLVMBuildExtractElement(ctx->ac.builder, descriptor,
                                        LLVMConstInt(ctx->ac.i32, 2, false), "");
 
-       /* VI only */
-       if (ctx->ac.chip_class == VI && in_elements) {
-               /* On VI, the descriptor contains the size in bytes,
+       /* GFX8 only */
+       if (ctx->ac.chip_class == GFX8 && in_elements) {
+               /* On GFX8, the descriptor contains the size in bytes,
                 * but TXQ must return the size in elements.
                 * The stride is always non-zero for resources using TXQ.
                 */
@@ -1297,7 +1266,7 @@ static LLVMValueRef lower_gather4_integer(struct ac_llvm_context *ctx,
                                              LLVMConstInt(ctx->i32, 0x14000000, false), "");
 
                /* replace the NUM FORMAT in the descriptor */
-               tmp2 = LLVMBuildAnd(ctx->builder, tmp2, LLVMConstInt(ctx->i32, C_008F14_NUM_FORMAT_GFX6, false), "");
+               tmp2 = LLVMBuildAnd(ctx->builder, tmp2, LLVMConstInt(ctx->i32, C_008F14_NUM_FORMAT, false), "");
                tmp2 = LLVMBuildOr(ctx->builder, tmp2, tmp, "");
 
                args->resource = LLVMBuildInsertElement(ctx->builder, args->resource, tmp2, ctx->i32_1, "");
@@ -1407,7 +1376,7 @@ static LLVMValueRef build_tex_intrinsic(struct ac_nir_context *ctx,
                break;
        }
 
-       if (instr->op == nir_texop_tg4 && ctx->ac.chip_class <= VI) {
+       if (instr->op == nir_texop_tg4 && ctx->ac.chip_class <= GFX8) {
                nir_deref_instr *texture_deref_instr = get_tex_texture_deref(instr);
                nir_variable *var = nir_deref_instr_get_variable(texture_deref_instr);
                const struct glsl_type *type = glsl_without_array(var->type);
@@ -1426,6 +1395,22 @@ static LLVMValueRef build_tex_intrinsic(struct ac_nir_context *ctx,
        }
 
        args->attributes = AC_FUNC_ATTR_READNONE;
+       bool cs_derivs = ctx->stage == MESA_SHADER_COMPUTE &&
+                        ctx->info->cs.derivative_group != DERIVATIVE_GROUP_NONE;
+       if (ctx->stage == MESA_SHADER_FRAGMENT || cs_derivs) {
+               /* Prevent texture instructions with implicit derivatives from being
+                * sinked into branches. */
+               switch (instr->op) {
+               case nir_texop_tex:
+               case nir_texop_txb:
+               case nir_texop_lod:
+                       args->attributes |= AC_FUNC_ATTR_CONVERGENT;
+                       break;
+               default:
+                       break;
+               }
+       }
+
        return ac_build_image_opcode(&ctx->ac, args);
 }
 
@@ -1470,7 +1455,7 @@ static LLVMValueRef visit_load_push_constant(struct ac_nir_context *ctx,
                }
        }
 
-       ptr = ac_build_gep0(&ctx->ac, ctx->abi->push_constants, addr);
+       ptr = LLVMBuildGEP(ctx->ac.builder, ctx->abi->push_constants, &addr, 1, "");
 
        if (instr->dest.ssa.bit_size == 8) {
                unsigned load_dwords = instr->dest.ssa.num_components > 1 ? 2 : 1;
@@ -1566,11 +1551,11 @@ static unsigned get_cache_policy(struct ac_nir_context *ctx,
 {
        unsigned cache_policy = 0;
 
-       /* SI has a TC L1 bug causing corruption of 8bit/16bit stores.  All
+       /* GFX6 has a TC L1 bug causing corruption of 8bit/16bit stores.  All
         * store opcodes not aligned to a dword are affected. The only way to
         * get unaligned stores is through shader images.
         */
-       if (((may_store_unaligned && ctx->ac.chip_class == SI) ||
+       if (((may_store_unaligned && ctx->ac.chip_class == GFX6) ||
             /* If this is write-only, don't keep data in L1 to prevent
              * evicting L1 cache lines that may be needed by other
              * instructions.
@@ -1606,9 +1591,10 @@ static void visit_store_ssbo(struct ac_nir_context *ctx,
 
                u_bit_scan_consecutive_range(&writemask, &start, &count);
 
-               /* Due to an LLVM limitation, split 3-element writes
-                * into a 2-element and a 1-element write. */
-               if (count == 3) {
+               /* Due to an LLVM limitation with LLVM < 9, split 3-element
+                * writes into a 2-element and a 1-element write. */
+               if (count == 3 &&
+                   (elem_size_bytes != 4 || !ac_has_vec3_support(ctx->ac.chip_class, false))) {
                        writemask |= 1 << (start + 2);
                        count = 2;
                }
@@ -1650,6 +1636,9 @@ static void visit_store_ssbo(struct ac_nir_context *ctx,
                        case 16: /* v4f32 */
                                data_type = ctx->ac.v4f32;
                                break;
+                       case 12: /* v3f32 */
+                               data_type = ctx->ac.v3f32;
+                               break;
                        case 8: /* v2f32 */
                                data_type = ctx->ac.v2f32;
                                break;
@@ -1674,22 +1663,12 @@ static void visit_store_ssbo(struct ac_nir_context *ctx,
 static LLVMValueRef visit_atomic_ssbo(struct ac_nir_context *ctx,
                                       const nir_intrinsic_instr *instr)
 {
+       LLVMTypeRef return_type = LLVMTypeOf(get_src(ctx, instr->src[2]));
        const char *op;
-       char name[64];
+       char name[64], type[8];
        LLVMValueRef params[6];
        int arg_count = 0;
 
-       if (instr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap) {
-               params[arg_count++] = ac_llvm_extract_elem(&ctx->ac, get_src(ctx, instr->src[3]), 0);
-       }
-       params[arg_count++] = ac_llvm_extract_elem(&ctx->ac, get_src(ctx, instr->src[2]), 0);
-       params[arg_count++] = ctx->abi->load_ssbo(ctx->abi,
-                                                get_src(ctx, instr->src[0]),
-                                                true);
-       params[arg_count++] = ctx->ac.i32_0; /* vindex */
-       params[arg_count++] = get_src(ctx, instr->src[1]);      /* voffset */
-       params[arg_count++] = ctx->ac.i1false;  /* slc */
-
        switch (instr->intrinsic) {
        case nir_intrinsic_ssbo_atomic_add:
                op = "add";
@@ -1725,16 +1704,37 @@ static LLVMValueRef visit_atomic_ssbo(struct ac_nir_context *ctx,
                abort();
        }
 
-       if (HAVE_LLVM >= 0x900 &&
-           instr->intrinsic != nir_intrinsic_ssbo_atomic_comp_swap) {
+       if (instr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap) {
+               params[arg_count++] = ac_llvm_extract_elem(&ctx->ac, get_src(ctx, instr->src[3]), 0);
+       }
+       params[arg_count++] = ac_llvm_extract_elem(&ctx->ac, get_src(ctx, instr->src[2]), 0);
+       params[arg_count++] = ctx->abi->load_ssbo(ctx->abi,
+                                                 get_src(ctx, instr->src[0]),
+                                                 true);
+
+       if (HAVE_LLVM >= 0x900) {
+               /* XXX: The new raw/struct atomic intrinsics are buggy with
+                * LLVM 8, see r358579.
+                */
+               params[arg_count++] = get_src(ctx, instr->src[1]); /* voffset */
+               params[arg_count++] = ctx->ac.i32_0; /* soffset */
+               params[arg_count++] = ctx->ac.i32_0; /* slc */
+
+               ac_build_type_name_for_intr(return_type, type, sizeof(type));
                snprintf(name, sizeof(name),
-                        "llvm.amdgcn.buffer.atomic.%s.i32", op);
+                        "llvm.amdgcn.raw.buffer.atomic.%s.%s", op, type);
        } else {
+               params[arg_count++] = ctx->ac.i32_0; /* vindex */
+               params[arg_count++] = get_src(ctx, instr->src[1]); /* voffset */
+               params[arg_count++] = ctx->ac.i1false; /* slc */
+
+               assert(return_type == ctx->ac.i32);
                snprintf(name, sizeof(name),
                         "llvm.amdgcn.buffer.atomic.%s", op);
        }
 
-       return ac_build_intrinsic(&ctx->ac, name, ctx->ac.i32, params, arg_count, 0);
+       return ac_build_intrinsic(&ctx->ac, name, return_type, params,
+                                 arg_count, 0);
 }
 
 static LLVMValueRef visit_load_buffer(struct ac_nir_context *ctx,
@@ -1873,9 +1873,7 @@ get_deref_offset(struct ac_nir_context *ctx, nir_deref_instr *instr,
                        if (vertex_index_out)
                                *vertex_index_out = 0;
                } else {
-                       nir_const_value *v = nir_src_as_const_value(path.path[idx_lvl]->arr.index);
-                       assert(v);
-                       *vertex_index_out = v->u32[0];
+                       *vertex_index_out = nir_src_as_uint(path.path[idx_lvl]->arr.index);
                }
                ++idx_lvl;
        }
@@ -1885,9 +1883,7 @@ get_deref_offset(struct ac_nir_context *ctx, nir_deref_instr *instr,
 
        if (var->data.compact) {
                assert(instr->deref_type == nir_deref_type_array);
-               nir_const_value *v = nir_src_as_const_value(instr->arr.index);
-               assert(v);
-               const_offset = v->u32[0];
+               const_offset = nir_src_as_uint(instr->arr.index);
                goto out;
        }
 
@@ -2319,78 +2315,18 @@ static int image_type_to_components_count(enum glsl_sampler_dim dim, bool array)
        return 0;
 }
 
-
-/* Adjust the sample index according to FMASK.
- *
- * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
- * which is the identity mapping. Each nibble says which physical sample
- * should be fetched to get that sample.
- *
- * For example, 0x11111100 means there are only 2 samples stored and
- * the second sample covers 3/4 of the pixel. When reading samples 0
- * and 1, return physical sample 0 (determined by the first two 0s
- * in FMASK), otherwise return physical sample 1.
- *
- * The sample index should be adjusted as follows:
- *   sample_index = (fmask >> (sample_index * 4)) & 0xF;
- */
 static LLVMValueRef adjust_sample_index_using_fmask(struct ac_llvm_context *ctx,
                                                    LLVMValueRef coord_x, LLVMValueRef coord_y,
                                                    LLVMValueRef coord_z,
                                                    LLVMValueRef sample_index,
                                                    LLVMValueRef fmask_desc_ptr)
 {
-       struct ac_image_args args = {0};
-       LLVMValueRef res;
-
-       args.coords[0] = coord_x;
-       args.coords[1] = coord_y;
-       if (coord_z)
-               args.coords[2] = coord_z;
-
-       args.opcode = ac_image_load;
-       args.dim = coord_z ? ac_image_2darray : ac_image_2d;
-       args.resource = fmask_desc_ptr;
-       args.dmask = 0xf;
-       args.attributes = AC_FUNC_ATTR_READNONE;
-
-       res = ac_build_image_opcode(ctx, &args);
-
-       res = ac_to_integer(ctx, res);
-       LLVMValueRef four = LLVMConstInt(ctx->i32, 4, false);
-       LLVMValueRef F = LLVMConstInt(ctx->i32, 0xf, false);
+       unsigned sample_chan = coord_z ? 3 : 2;
+       LLVMValueRef addr[4] = {coord_x, coord_y, coord_z};
+       addr[sample_chan] = sample_index;
 
-       LLVMValueRef fmask = LLVMBuildExtractElement(ctx->builder,
-                                                    res,
-                                                    ctx->i32_0, "");
-
-       LLVMValueRef sample_index4 =
-               LLVMBuildMul(ctx->builder, sample_index, four, "");
-       LLVMValueRef shifted_fmask =
-               LLVMBuildLShr(ctx->builder, fmask, sample_index4, "");
-       LLVMValueRef final_sample =
-               LLVMBuildAnd(ctx->builder, shifted_fmask, F, "");
-
-       /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
-        * resource descriptor is 0 (invalid),
-        */
-       LLVMValueRef fmask_desc =
-               LLVMBuildBitCast(ctx->builder, fmask_desc_ptr,
-                                ctx->v8i32, "");
-
-       LLVMValueRef fmask_word1 =
-               LLVMBuildExtractElement(ctx->builder, fmask_desc,
-                                       ctx->i32_1, "");
-
-       LLVMValueRef word1_is_nonzero =
-               LLVMBuildICmp(ctx->builder, LLVMIntNE,
-                             fmask_word1, ctx->i32_0, "");
-
-       /* Replace the MSAA sample index. */
-       sample_index =
-               LLVMBuildSelect(ctx->builder, word1_is_nonzero,
-                               final_sample, sample_index, "");
-       return sample_index;
+       ac_apply_fmask_to_sample(ctx, fmask_desc_ptr, addr, coord_z != NULL);
+       return addr[sample_chan];
 }
 
 static nir_deref_instr *get_image_deref(const nir_intrinsic_instr *instr)
@@ -2501,10 +2437,12 @@ static void get_image_coords(struct ac_nir_context *ctx,
 }
 
 static LLVMValueRef get_image_buffer_descriptor(struct ac_nir_context *ctx,
-                                                const nir_intrinsic_instr *instr, bool write)
+                                                const nir_intrinsic_instr *instr,
+                                               bool write, bool atomic)
 {
        LLVMValueRef rsrc = get_image_descriptor(ctx, instr, AC_DESC_BUFFER, write);
-       if (ctx->abi->gfx9_stride_size_workaround) {
+       if (ctx->abi->gfx9_stride_size_workaround ||
+           (ctx->abi->gfx9_stride_size_workaround_for_atomic && atomic)) {
                LLVMValueRef elem_count = LLVMBuildExtractElement(ctx->ac.builder, rsrc, LLVMConstInt(ctx->ac.i32, 2, 0), "");
                LLVMValueRef stride = LLVMBuildExtractElement(ctx->ac.builder, rsrc, LLVMConstInt(ctx->ac.i32, 1, 0), "");
                stride = LLVMBuildLShr(ctx->ac.builder, stride, LLVMConstInt(ctx->ac.i32, 16, 0), "");
@@ -2550,7 +2488,7 @@ static LLVMValueRef visit_image_load(struct ac_nir_context *ctx,
                unsigned num_channels = util_last_bit(mask);
                LLVMValueRef rsrc, vindex;
 
-               rsrc = get_image_buffer_descriptor(ctx, instr, false);
+               rsrc = get_image_buffer_descriptor(ctx, instr, false, false);
                vindex = LLVMBuildExtractElement(ctx->ac.builder, get_src(ctx, instr->src[1]),
                                                 ctx->ac.i32_0, "");
 
@@ -2573,7 +2511,7 @@ static LLVMValueRef visit_image_load(struct ac_nir_context *ctx,
 
                res = ac_build_image_opcode(&ctx->ac, &args);
        }
-       return ac_to_integer(&ctx->ac, res);
+       return res;
 }
 
 static void visit_image_store(struct ac_nir_context *ctx,
@@ -2604,7 +2542,7 @@ static void visit_image_store(struct ac_nir_context *ctx,
        args.cache_policy = get_cache_policy(ctx, access, true, writeonly_memory);
 
        if (dim == GLSL_SAMPLER_DIM_BUF) {
-               LLVMValueRef rsrc = get_image_buffer_descriptor(ctx, instr, true);
+               LLVMValueRef rsrc = get_image_buffer_descriptor(ctx, instr, true, false);
                LLVMValueRef src = ac_to_float(&ctx->ac, get_src(ctx, instr->src[3]));
                unsigned src_channels = ac_get_llvm_num_components(src);
                LLVMValueRef vindex;
@@ -2618,7 +2556,7 @@ static void visit_image_store(struct ac_nir_context *ctx,
 
                ac_build_buffer_store_format(&ctx->ac, rsrc, src, vindex,
                                             ctx->ac.i32_0, src_channels,
-                                            args.cache_policy & ac_glc,
+                                            args.cache_policy & ac_glc, false,
                                             writeonly_memory);
        } else {
                args.opcode = ac_image_store;
@@ -2648,11 +2586,11 @@ static LLVMValueRef visit_image_atomic(struct ac_nir_context *ctx,
        MAYBE_UNUSED int length;
 
        enum glsl_sampler_dim dim;
-       bool is_unsigned;
+       bool is_unsigned = false;
        bool is_array;
        if (bindless) {
-               if (instr->intrinsic == nir_intrinsic_image_atomic_min ||
-                   instr->intrinsic == nir_intrinsic_image_atomic_max) {
+               if (instr->intrinsic == nir_intrinsic_bindless_image_atomic_min ||
+                   instr->intrinsic == nir_intrinsic_bindless_image_atomic_max) {
                        const GLenum format = nir_intrinsic_format(instr);
                        assert(format == GL_R32UI || format == GL_R32I);
                        is_unsigned = format == GL_R32UI;
@@ -2716,11 +2654,14 @@ static LLVMValueRef visit_image_atomic(struct ac_nir_context *ctx,
        params[param_count++] = get_src(ctx, instr->src[3]);
 
        if (dim == GLSL_SAMPLER_DIM_BUF) {
-               params[param_count++] = get_image_buffer_descriptor(ctx, instr, true);
+               params[param_count++] = get_image_buffer_descriptor(ctx, instr, true, true);
                params[param_count++] = LLVMBuildExtractElement(ctx->ac.builder, get_src(ctx, instr->src[1]),
                                                                ctx->ac.i32_0, ""); /* vindex */
                params[param_count++] = ctx->ac.i32_0; /* voffset */
-               if (HAVE_LLVM >= 0x800) {
+               if (HAVE_LLVM >= 0x900) {
+                       /* XXX: The new raw/struct atomic intrinsics are buggy
+                        * with LLVM 8, see r358579.
+                        */
                        params[param_count++] = ctx->ac.i32_0; /* soffset */
                        params[param_count++] = ctx->ac.i32_0;  /* slc */
 
@@ -2852,11 +2793,11 @@ static void emit_membar(struct ac_llvm_context *ac,
 
 void ac_emit_barrier(struct ac_llvm_context *ac, gl_shader_stage stage)
 {
-       /* SI only (thanks to a hw bug workaround):
+       /* GFX6 only (thanks to a hw bug workaround):
         * The real barrier instruction isn’t needed, because an entire patch
         * always fits into a single wave.
         */
-       if (ac->chip_class == SI && stage == MESA_SHADER_TESS_CTRL) {
+       if (ac->chip_class == GFX6 && stage == MESA_SHADER_TESS_CTRL) {
                ac_build_waitcnt(ac, LGKM_CNT & VM_CNT);
                return;
        }
@@ -2880,17 +2821,6 @@ static void emit_discard(struct ac_nir_context *ctx,
        ctx->abi->emit_kill(ctx->abi, cond);
 }
 
-static LLVMValueRef
-visit_load_helper_invocation(struct ac_nir_context *ctx)
-{
-       LLVMValueRef result = ac_build_intrinsic(&ctx->ac,
-                                                "llvm.amdgcn.ps.live",
-                                                ctx->ac.i1, NULL, 0,
-                                                AC_FUNC_ATTR_READNONE);
-       result = LLVMBuildNot(ctx->ac.builder, result, "");
-       return LLVMBuildSExt(ctx->ac.builder, result, ctx->ac.i32, "");
-}
-
 static LLVMValueRef
 visit_load_local_invocation_index(struct ac_nir_context *ctx)
 {
@@ -2989,14 +2919,12 @@ static LLVMValueRef visit_var_atomic(struct ac_nir_context *ctx,
        LLVMValueRef result;
        LLVMValueRef src = get_src(ctx, instr->src[src_idx]);
 
+       const char *sync_scope = HAVE_LLVM >= 0x0900 ? "workgroup-one-as" : "workgroup";
+
        if (instr->intrinsic == nir_intrinsic_shared_atomic_comp_swap ||
            instr->intrinsic == nir_intrinsic_deref_atomic_comp_swap) {
                LLVMValueRef src1 = get_src(ctx, instr->src[src_idx + 1]);
-               result = LLVMBuildAtomicCmpXchg(ctx->ac.builder,
-                                               ptr, src, src1,
-                                               LLVMAtomicOrderingSequentiallyConsistent,
-                                               LLVMAtomicOrderingSequentiallyConsistent,
-                                               false);
+               result = ac_build_atomic_cmp_xchg(&ctx->ac, ptr, src, src1, sync_scope);
                result = LLVMBuildExtractValue(ctx->ac.builder, result, 0, "");
        } else {
                LLVMAtomicRMWBinOp op;
@@ -3041,9 +2969,7 @@ static LLVMValueRef visit_var_atomic(struct ac_nir_context *ctx,
                        return NULL;
                }
 
-               result = LLVMBuildAtomicRMW(ctx->ac.builder, op, ptr, ac_to_integer(&ctx->ac, src),
-                                           LLVMAtomicOrderingSequentiallyConsistent,
-                                           false);
+               result = ac_build_atomic_rmw(&ctx->ac, op, ptr, ac_to_integer(&ctx->ac, src), sync_scope);
        }
        return result;
 }
@@ -3107,7 +3033,7 @@ static LLVMValueRef visit_interp(struct ac_nir_context *ctx,
 
        if (location == INTERP_CENTER) {
                LLVMValueRef ij_out[2];
-               LLVMValueRef ddxy_out = emit_ddxy_interp(ctx, interp_param);
+               LLVMValueRef ddxy_out = ac_build_ddxy_interp(&ctx->ac, interp_param);
 
                /*
                 * take the I then J parameters, and the DDX/Y for it, and
@@ -3147,9 +3073,8 @@ static LLVMValueRef visit_interp(struct ac_nir_context *ctx,
                        unsigned array_size = glsl_count_attribute_slots(deref_instr->type, false);
 
                        LLVMValueRef offset;
-                       nir_const_value *const_value = nir_src_as_const_value(deref_instr->arr.index);
-                       if (const_value) {
-                               offset = LLVMConstInt(ctx->ac.i32, array_size * const_value->u32[0], false);
+                       if (nir_src_is_const(deref_instr->arr.index)) {
+                               offset = LLVMConstInt(ctx->ac.i32, array_size * nir_src_as_uint(deref_instr->arr.index), false);
                        } else {
                                LLVMValueRef indirect = get_src(ctx, deref_instr->arr.index);
 
@@ -3304,7 +3229,7 @@ static void visit_intrinsic(struct ac_nir_context *ctx,
                result = ctx->abi->front_face;
                break;
        case nir_intrinsic_load_helper_invocation:
-               result = visit_load_helper_invocation(ctx);
+               result = ac_build_load_helper_invocation(&ctx->ac);
                break;
        case nir_intrinsic_load_instance_id:
                result = ctx->abi->instance_id;
@@ -3519,7 +3444,7 @@ static void visit_intrinsic(struct ac_nir_context *ctx,
                                instr->const_index[0]);
                break;
        case nir_intrinsic_quad_broadcast: {
-               unsigned lane = nir_src_as_const_value(instr->src[1])->u32[0];
+               unsigned lane = nir_src_as_uint(instr->src[1]);
                result = ac_build_quad_swizzle(&ctx->ac, get_src(ctx, instr->src[0]),
                                lane, lane, lane, lane);
                break;
@@ -3533,6 +3458,26 @@ static void visit_intrinsic(struct ac_nir_context *ctx,
        case nir_intrinsic_quad_swap_diagonal:
                result = ac_build_quad_swizzle(&ctx->ac, get_src(ctx, instr->src[0]), 3, 2, 1 ,0);
                break;
+       case nir_intrinsic_quad_swizzle_amd: {
+               uint32_t mask = nir_intrinsic_swizzle_mask(instr);
+               result = ac_build_quad_swizzle(&ctx->ac, get_src(ctx, instr->src[0]),
+                                              mask & 0x3, (mask >> 2) & 0x3,
+                                              (mask >> 4) & 0x3, (mask >> 6) & 0x3);
+               break;
+       }
+       case nir_intrinsic_masked_swizzle_amd: {
+               uint32_t mask = nir_intrinsic_swizzle_mask(instr);
+               result = ac_build_ds_swizzle(&ctx->ac, get_src(ctx, instr->src[0]), mask);
+               break;
+       }
+       case nir_intrinsic_write_invocation_amd:
+               result = ac_build_writelane(&ctx->ac, get_src(ctx, instr->src[0]),
+                                           get_src(ctx, instr->src[1]),
+                                           get_src(ctx, instr->src[2]));
+               break;
+       case nir_intrinsic_mbcnt_amd:
+               result = ac_build_mbcnt(&ctx->ac, get_src(ctx, instr->src[0]));
+               break;
        default:
                fprintf(stderr, "Unknown intrinsic: ");
                nir_print_instr(&instr->instr, stderr);
@@ -3604,9 +3549,8 @@ static LLVMValueRef get_sampler_desc(struct ac_nir_context *ctx,
                                if (!array_size)
                                        array_size = 1;
 
-                               nir_const_value *const_value = nir_src_as_const_value(deref_instr->arr.index);
-                               if (const_value) {
-                                       constant_index += array_size * const_value->u32[0];
+                               if (nir_src_is_const(deref_instr->arr.index)) {
+                                       constant_index += array_size * nir_src_as_uint(deref_instr->arr.index);
                                } else {
                                        LLVMValueRef indirect = get_src(ctx, deref_instr->arr.index);
 
@@ -3653,13 +3597,13 @@ static LLVMValueRef get_sampler_desc(struct ac_nir_context *ctx,
 
 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
  *
- * SI-CI:
+ * GFX6-GFX7:
  *   If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
  *   filtering manually. The driver sets img7 to a mask clearing
  *   MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
  *     s_and_b32 samp0, samp0, img7
  *
- * VI:
+ * GFX8:
  *   The ANISO_OVERRIDE sampler field enables this fix in TA.
  */
 static LLVMValueRef sici_fix_sampler_aniso(struct ac_nir_context *ctx,
@@ -3668,7 +3612,7 @@ static LLVMValueRef sici_fix_sampler_aniso(struct ac_nir_context *ctx,
        LLVMBuilderRef builder = ctx->ac.builder;
        LLVMValueRef img7, samp0;
 
-       if (ctx->ac.chip_class >= VI)
+       if (ctx->ac.chip_class >= GFX8)
                return samp;
 
        img7 = LLVMBuildExtractElement(builder, res,
@@ -3687,6 +3631,7 @@ static void tex_fetch_ptrs(struct ac_nir_context *ctx,
 {
        nir_deref_instr *texture_deref_instr = NULL;
        nir_deref_instr *sampler_deref_instr = NULL;
+       int plane = -1;
 
        for (unsigned i = 0; i < instr->num_srcs; i++) {
                switch (instr->src[i].src_type) {
@@ -3696,6 +3641,9 @@ static void tex_fetch_ptrs(struct ac_nir_context *ctx,
                case nir_tex_src_sampler_deref:
                        sampler_deref_instr = nir_src_as_deref(instr->src[i].src);
                        break;
+               case nir_tex_src_plane:
+                       plane = nir_src_as_int(instr->src[i].src);
+                       break;
                default:
                        break;
                }
@@ -3704,10 +3652,18 @@ static void tex_fetch_ptrs(struct ac_nir_context *ctx,
        if (!sampler_deref_instr)
                sampler_deref_instr = texture_deref_instr;
 
-       if (instr->sampler_dim  == GLSL_SAMPLER_DIM_BUF)
-               *res_ptr = get_sampler_desc(ctx, texture_deref_instr, AC_DESC_BUFFER, &instr->instr, false, false);
-       else
-               *res_ptr = get_sampler_desc(ctx, texture_deref_instr, AC_DESC_IMAGE, &instr->instr, false, false);
+       enum ac_descriptor_type main_descriptor = instr->sampler_dim  == GLSL_SAMPLER_DIM_BUF ? AC_DESC_BUFFER : AC_DESC_IMAGE;
+
+       if (plane >= 0) {
+               assert(instr->op != nir_texop_txf_ms &&
+                      instr->op != nir_texop_samples_identical);
+               assert(instr->sampler_dim  != GLSL_SAMPLER_DIM_BUF);
+
+               main_descriptor = AC_DESC_PLANE_0 + plane;
+       }
+
+       *res_ptr = get_sampler_desc(ctx, texture_deref_instr, main_descriptor, &instr->instr, false, false);
+
        if (samp_ptr) {
                *samp_ptr = get_sampler_desc(ctx, sampler_deref_instr, AC_DESC_SAMPLER, &instr->instr, false, false);
                if (instr->sampler_dim < GLSL_SAMPLER_DIM_RECT)
@@ -3760,9 +3716,7 @@ static void visit_tex(struct ac_nir_context *ctx, nir_tex_instr *instr)
                                args.bias = get_src(ctx, instr->src[i].src);
                        break;
                case nir_tex_src_lod: {
-                       nir_const_value *val = nir_src_as_const_value(instr->src[i].src);
-
-                       if (val && val->i32[0] == 0)
+                       if (nir_src_is_const(instr->src[i].src) && nir_src_as_uint(instr->src[i].src) == 0)
                                args.level_zero = true;
                        else
                                args.lod = get_src(ctx, instr->src[i].src);
@@ -3842,7 +3796,7 @@ static void visit_tex(struct ac_nir_context *ctx, nir_tex_instr *instr)
         * It's unnecessary if the original texture format was
         * Z32_FLOAT, but we don't know that here.
         */
-       if (args.compare && ctx->ac.chip_class >= VI && ctx->abi->clamp_shadow_reference)
+       if (args.compare && ctx->ac.chip_class >= GFX8 && ctx->abi->clamp_shadow_reference)
                args.compare = ac_build_clamp(&ctx->ac, ac_to_float(&ctx->ac, args.compare));
 
        /* pack derivatives */
@@ -3951,20 +3905,23 @@ static void visit_tex(struct ac_nir_context *ctx, nir_tex_instr *instr)
        }
 
        if (args.offset && instr->op == nir_texop_txf) {
-               nir_const_value *const_offset =
-                       nir_src_as_const_value(instr->src[offset_src].src);
                int num_offsets = instr->src[offset_src].src.ssa->num_components;
-               assert(const_offset);
                num_offsets = MIN2(num_offsets, instr->coord_components);
                for (unsigned i = 0; i < num_offsets; ++i) {
                        args.coords[i] = LLVMBuildAdd(
                                ctx->ac.builder, args.coords[i],
-                               LLVMConstInt(ctx->ac.i32, const_offset->i32[i], false), "");
+                               LLVMConstInt(ctx->ac.i32, nir_src_comp_as_uint(instr->src[offset_src].src, i), false), "");
                }
                args.offset = NULL;
        }
 
-       /* TODO TG4 support */
+       /* DMASK was repurposed for GATHER4. 4 components are always
+        * returned and DMASK works like a swizzle - it selects
+        * the component to fetch. The only valid DMASK values are
+        * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
+        * (red,red,red,red) etc.) The ISA document doesn't mention
+        * this.
+        */
        args.dmask = 0xf;
        if (instr->op == nir_texop_tg4) {
                if (instr->is_shadow)
@@ -4440,6 +4397,7 @@ void ac_nir_translate(struct ac_llvm_context *ac, struct ac_shader_abi *abi,
        ctx.abi = abi;
 
        ctx.stage = nir->info.stage;
+       ctx.info = &nir->info;
 
        ctx.main_function = LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx.ac.builder));
 
@@ -4485,7 +4443,7 @@ ac_lower_indirect_derefs(struct nir_shader *nir, enum chip_class chip_class)
         * by the reality that LLVM 5.0 doesn't have working VGPR indexing
         * on GFX9.
         */
-       bool llvm_has_working_vgpr_indexing = chip_class <= VI;
+       bool llvm_has_working_vgpr_indexing = chip_class <= GFX8;
 
        /* TODO: Indirect indexing of GS inputs is unimplemented.
         *