swr/rast: Add autogen of helper llvm intrinsics.
[mesa.git] / src / gallium / drivers / radeonsi / si_shader_tgsi_mem.c
index 0863876ed89687de443bad883e67bbe573a175c9..6a307c4ddba99f8c0a7483f68893a7427a8d9a00 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright 2017 Advanced Micro Devices, Inc.
+ * All Rights Reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
@@ -46,13 +47,12 @@ static LLVMValueRef get_buffer_size(
        LLVMValueRef descriptor)
 {
        struct si_shader_context *ctx = si_shader_context(bld_base);
-       struct gallivm_state *gallivm = &ctx->gallivm;
-       LLVMBuilderRef builder = gallivm->builder;
+       LLVMBuilderRef builder = ctx->ac.builder;
        LLVMValueRef size =
                LLVMBuildExtractElement(builder, descriptor,
                                        LLVMConstInt(ctx->i32, 2, 0), "");
 
-       if (ctx->screen->b.chip_class == VI) {
+       if (ctx->screen->info.chip_class == VI) {
                /* On VI, 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.
@@ -82,7 +82,7 @@ shader_buffer_fetch_rsrc(struct si_shader_context *ctx,
                index = LLVMConstInt(ctx->i32, reg->Register.Index, false);
        } else {
                index = si_get_indirect_index(ctx, &reg->Indirect,
-                                             reg->Register.Index);
+                                             1, reg->Register.Index);
        }
 
        if (ubo)
@@ -91,17 +91,6 @@ shader_buffer_fetch_rsrc(struct si_shader_context *ctx,
                return ctx->abi.load_ssbo(&ctx->abi, index, false);
 }
 
-static bool tgsi_is_array_sampler(unsigned target)
-{
-       return target == TGSI_TEXTURE_1D_ARRAY ||
-              target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
-              target == TGSI_TEXTURE_2D_ARRAY ||
-              target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
-              target == TGSI_TEXTURE_CUBE_ARRAY ||
-              target == TGSI_TEXTURE_SHADOWCUBE_ARRAY ||
-              target == TGSI_TEXTURE_2D_ARRAY_MSAA;
-}
-
 static bool tgsi_is_array_image(unsigned target)
 {
        return target == TGSI_TEXTURE_3D ||
@@ -126,17 +115,16 @@ static bool tgsi_is_array_image(unsigned target)
 static LLVMValueRef force_dcc_off(struct si_shader_context *ctx,
                                  LLVMValueRef rsrc)
 {
-       if (ctx->screen->b.chip_class <= CIK) {
+       if (ctx->screen->info.chip_class <= CIK) {
                return rsrc;
        } else {
-               LLVMBuilderRef builder = ctx->gallivm.builder;
                LLVMValueRef i32_6 = LLVMConstInt(ctx->i32, 6, 0);
                LLVMValueRef i32_C = LLVMConstInt(ctx->i32, C_008F28_COMPRESSION_EN, 0);
                LLVMValueRef tmp;
 
-               tmp = LLVMBuildExtractElement(builder, rsrc, i32_6, "");
-               tmp = LLVMBuildAnd(builder, tmp, i32_C, "");
-               return LLVMBuildInsertElement(builder, rsrc, tmp, i32_6, "");
+               tmp = LLVMBuildExtractElement(ctx->ac.builder, rsrc, i32_6, "");
+               tmp = LLVMBuildAnd(ctx->ac.builder, tmp, i32_C, "");
+               return LLVMBuildInsertElement(ctx->ac.builder, rsrc, tmp, i32_6, "");
        }
 }
 
@@ -144,7 +132,7 @@ LLVMValueRef si_load_image_desc(struct si_shader_context *ctx,
                                LLVMValueRef list, LLVMValueRef index,
                                enum ac_descriptor_type desc_type, bool dcc_off)
 {
-       LLVMBuilderRef builder = ctx->gallivm.builder;
+       LLVMBuilderRef builder = ctx->ac.builder;
        LLVMValueRef rsrc;
 
        if (desc_type == AC_DESC_BUFFER) {
@@ -153,13 +141,13 @@ LLVMValueRef si_load_image_desc(struct si_shader_context *ctx,
                index = LLVMBuildAdd(builder, index,
                                     ctx->i32_1, "");
                list = LLVMBuildPointerCast(builder, list,
-                                           si_const_array(ctx->v4i32, 0), "");
+                                           ac_array_in_const32_addr_space(ctx->v4i32), "");
        } else {
                assert(desc_type == AC_DESC_IMAGE);
        }
 
-       rsrc = ac_build_indexed_load_const(&ctx->ac, list, index);
-       if (dcc_off)
+       rsrc = ac_build_load_to_sgpr(&ctx->ac, list, index);
+       if (desc_type == AC_DESC_IMAGE && dcc_off)
                rsrc = force_dcc_off(ctx, rsrc);
        return rsrc;
 }
@@ -203,7 +191,7 @@ image_fetch_rsrc(
                index = si_get_bounded_indirect_index(ctx, &image->Indirect,
                                                      image->Register.Index,
                                                      ctx->num_images);
-               index = LLVMBuildSub(ctx->gallivm.builder,
+               index = LLVMBuildSub(ctx->ac.builder,
                                     LLVMConstInt(ctx->i32, SI_NUM_IMAGES - 1, 0),
                                     index, "");
        }
@@ -212,9 +200,6 @@ image_fetch_rsrc(
                /* Bindless descriptors are accessible from a different pair of
                 * user SGPR indices.
                 */
-               struct gallivm_state *gallivm = &ctx->gallivm;
-               LLVMBuilderRef builder = gallivm->builder;
-
                rsrc_ptr = LLVMGetParam(ctx->main_fn,
                                        ctx->param_bindless_samplers_and_images);
                index = lp_build_emit_fetch_src(bld_base, image,
@@ -223,7 +208,7 @@ image_fetch_rsrc(
                /* For simplicity, bindless image descriptors use fixed
                 * 16-dword slots for now.
                 */
-               index = LLVMBuildMul(builder, index,
+               index = LLVMBuildMul(ctx->ac.builder, index,
                                     LLVMConstInt(ctx->i32, 2, 0), "");
        }
 
@@ -238,8 +223,7 @@ static LLVMValueRef image_fetch_coords(
                unsigned src, LLVMValueRef desc)
 {
        struct si_shader_context *ctx = si_shader_context(bld_base);
-       struct gallivm_state *gallivm = &ctx->gallivm;
-       LLVMBuilderRef builder = gallivm->builder;
+       LLVMBuilderRef builder = ctx->ac.builder;
        unsigned target = inst->Memory.Texture;
        unsigned num_coords = tgsi_util_get_texture_coord_dim(target);
        LLVMValueRef coords[4];
@@ -248,11 +232,11 @@ static LLVMValueRef image_fetch_coords(
 
        for (chan = 0; chan < num_coords; ++chan) {
                tmp = lp_build_emit_fetch(bld_base, inst, src, chan);
-               tmp = LLVMBuildBitCast(builder, tmp, ctx->i32, "");
+               tmp = ac_to_integer(&ctx->ac, tmp);
                coords[chan] = tmp;
        }
 
-       if (ctx->screen->b.chip_class >= GFX9) {
+       if (ctx->screen->info.chip_class >= GFX9) {
                /* 1D textures are allocated and used as 2D on GFX9. */
                if (target == TGSI_TEXTURE_1D) {
                        coords[1] = ctx->i32_0;
@@ -288,7 +272,7 @@ static LLVMValueRef image_fetch_coords(
                num_coords = 4;
        }
 
-       return lp_build_gather_values(gallivm, coords, num_coords);
+       return lp_build_gather_values(&ctx->gallivm, coords, num_coords);
 }
 
 /**
@@ -313,7 +297,7 @@ static void image_append_args(
        LLVMValueRef slc = i1false;
        LLVMValueRef lwe = i1false;
 
-       if (atomic || (HAVE_LLVM <= 0x0309)) {
+       if (atomic) {
                emit_data->args[emit_data->arg_count++] = r128;
                emit_data->args[emit_data->arg_count++] = da;
                if (!atomic) {
@@ -323,7 +307,6 @@ static void image_append_args(
                return;
        }
 
-       /* HAVE_LLVM >= 0x0400 */
        emit_data->args[emit_data->arg_count++] = glc;
        emit_data->args[emit_data->arg_count++] = slc;
        emit_data->args[emit_data->arg_count++] = lwe;
@@ -367,7 +350,6 @@ static void load_fetch_args(
                struct lp_build_emit_data * emit_data)
 {
        struct si_shader_context *ctx = si_shader_context(bld_base);
-       struct gallivm_state *gallivm = &ctx->gallivm;
        const struct tgsi_full_instruction * inst = emit_data->inst;
        unsigned target = inst->Memory.Texture;
        LLVMValueRef rsrc;
@@ -376,7 +358,6 @@ static void load_fetch_args(
 
        if (inst->Src[0].Register.File == TGSI_FILE_BUFFER ||
                   inst->Src[0].Register.File == TGSI_FILE_CONSTBUF) {
-               LLVMBuilderRef builder = gallivm->builder;
                LLVMValueRef offset;
                LLVMValueRef tmp;
 
@@ -384,7 +365,7 @@ static void load_fetch_args(
                rsrc = shader_buffer_fetch_rsrc(ctx, &inst->Src[0], ubo);
 
                tmp = lp_build_emit_fetch(bld_base, inst, 1, 0);
-               offset = LLVMBuildBitCast(builder, tmp, ctx->i32, "");
+               offset = ac_to_integer(&ctx->ac, tmp);
 
                buffer_append_args(ctx, emit_data, rsrc, ctx->i32_0,
                                   offset, false, false);
@@ -409,22 +390,6 @@ static void load_fetch_args(
        }
 }
 
-static unsigned get_load_intr_attribs(bool can_speculate)
-{
-       /* READNONE means writes can't affect it, while READONLY means that
-        * writes can affect it. */
-       return can_speculate && HAVE_LLVM >= 0x0400 ?
-                                LP_FUNC_ATTR_READNONE :
-                                LP_FUNC_ATTR_READONLY;
-}
-
-static unsigned get_store_intr_attribs(bool writeonly_memory)
-{
-       return writeonly_memory && HAVE_LLVM >= 0x0400 ?
-                                 LP_FUNC_ATTR_INACCESSIBLE_MEM_ONLY :
-                                 LP_FUNC_ATTR_WRITEONLY;
-}
-
 static void load_emit_buffer(struct si_shader_context *ctx,
                             struct lp_build_emit_data *emit_data,
                             bool can_speculate, bool allow_smem)
@@ -461,15 +426,14 @@ static LLVMValueRef get_memory_ptr(struct si_shader_context *ctx,
                                    const struct tgsi_full_instruction *inst,
                                    LLVMTypeRef type, int arg)
 {
-       struct gallivm_state *gallivm = &ctx->gallivm;
-       LLVMBuilderRef builder = gallivm->builder;
+       LLVMBuilderRef builder = ctx->ac.builder;
        LLVMValueRef offset, ptr;
        int addr_space;
 
        offset = lp_build_emit_fetch(&ctx->bld_base, inst, arg, 0);
-       offset = LLVMBuildBitCast(builder, offset, ctx->i32, "");
+       offset = ac_to_integer(&ctx->ac, offset);
 
-       ptr = ctx->shared_memory;
+       ptr = ctx->ac.lds;
        ptr = LLVMBuildGEP(builder, ptr, &offset, 1, "");
        addr_space = LLVMGetPointerAddressSpace(LLVMTypeOf(ptr));
        ptr = LLVMBuildBitCast(builder, ptr, LLVMPointerType(type, addr_space), "");
@@ -482,8 +446,6 @@ static void load_emit_memory(
                struct lp_build_emit_data *emit_data)
 {
        const struct tgsi_full_instruction *inst = emit_data->inst;
-       struct gallivm_state *gallivm = &ctx->gallivm;
-       LLVMBuilderRef builder = gallivm->builder;
        unsigned writemask = inst->Dst[0].Register.WriteMask;
        LLVMValueRef channels[4], ptr, derived_ptr, index;
        int chan;
@@ -497,10 +459,10 @@ static void load_emit_memory(
                }
 
                index = LLVMConstInt(ctx->i32, chan, 0);
-               derived_ptr = LLVMBuildGEP(builder, ptr, &index, 1, "");
-               channels[chan] = LLVMBuildLoad(builder, derived_ptr, "");
+               derived_ptr = LLVMBuildGEP(ctx->ac.builder, ptr, &index, 1, "");
+               channels[chan] = LLVMBuildLoad(ctx->ac.builder, derived_ptr, "");
        }
-       emit_data->output[emit_data->chan] = lp_build_gather_values(gallivm, channels, 4);
+       emit_data->output[emit_data->chan] = lp_build_gather_values(&ctx->gallivm, channels, 4);
 }
 
 /**
@@ -572,8 +534,7 @@ static void load_emit(
                struct lp_build_emit_data *emit_data)
 {
        struct si_shader_context *ctx = si_shader_context(bld_base);
-       struct gallivm_state *gallivm = &ctx->gallivm;
-       LLVMBuilderRef builder = gallivm->builder;
+       LLVMBuilderRef builder = ctx->ac.builder;
        const struct tgsi_full_instruction * inst = emit_data->inst;
        const struct tgsi_shader_info *info = &ctx->shader->selector->info;
        char intrinsic_name[64];
@@ -590,7 +551,7 @@ static void load_emit(
        }
 
        if (inst->Memory.Qualifier & TGSI_MEMORY_VOLATILE)
-               si_emit_waitcnt(ctx, VM_CNT);
+               ac_build_waitcnt(&ctx->ac, VM_CNT);
 
        can_speculate = !(inst->Memory.Qualifier & TGSI_MEMORY_VOLATILE) &&
                          is_oneway_access_only(inst, info,
@@ -605,11 +566,17 @@ static void load_emit(
        }
 
        if (inst->Memory.Texture == TGSI_TEXTURE_BUFFER) {
+               unsigned num_channels = util_last_bit(inst->Dst[0].Register.WriteMask);
+               LLVMValueRef result =
+                       ac_build_buffer_load_format(&ctx->ac,
+                                                   emit_data->args[0],
+                                                   emit_data->args[1],
+                                                   emit_data->args[2],
+                                                   num_channels,
+                                                   LLVMConstIntGetZExtValue(emit_data->args[3]),
+                                                   can_speculate);
                emit_data->output[emit_data->chan] =
-                       lp_build_intrinsic(
-                               builder, "llvm.amdgcn.buffer.load.format.v4f32", emit_data->dst_type,
-                               emit_data->args, emit_data->arg_count,
-                               get_load_intr_attribs(can_speculate));
+                       ac_build_expand_to_vec4(&ctx->ac, result, num_channels);
        } else {
                ac_get_image_intr_name("llvm.amdgcn.image.load",
                                       emit_data->dst_type,             /* vdata */
@@ -621,7 +588,7 @@ static void load_emit(
                        lp_build_intrinsic(
                                builder, intrinsic_name, emit_data->dst_type,
                                emit_data->args, emit_data->arg_count,
-                               get_load_intr_attribs(can_speculate));
+                               ac_get_load_intr_attribs(can_speculate));
        }
 }
 
@@ -630,8 +597,6 @@ static void store_fetch_args(
                struct lp_build_emit_data * emit_data)
 {
        struct si_shader_context *ctx = si_shader_context(bld_base);
-       struct gallivm_state *gallivm = &ctx->gallivm;
-       LLVMBuilderRef builder = gallivm->builder;
        const struct tgsi_full_instruction * inst = emit_data->inst;
        struct tgsi_full_src_register memory;
        LLVMValueRef chans[4];
@@ -639,12 +604,12 @@ static void store_fetch_args(
        LLVMValueRef rsrc;
        unsigned chan;
 
-       emit_data->dst_type = LLVMVoidTypeInContext(gallivm->context);
+       emit_data->dst_type = ctx->voidt;
 
        for (chan = 0; chan < 4; ++chan) {
                chans[chan] = lp_build_emit_fetch(bld_base, inst, 1, chan);
        }
-       data = lp_build_gather_values(gallivm, chans, 4);
+       data = lp_build_gather_values(&ctx->gallivm, chans, 4);
 
        emit_data->args[emit_data->arg_count++] = data;
 
@@ -657,7 +622,7 @@ static void store_fetch_args(
                rsrc = shader_buffer_fetch_rsrc(ctx, &memory, false);
 
                tmp = lp_build_emit_fetch(bld_base, inst, 0, 0);
-               offset = LLVMBuildBitCast(builder, tmp, ctx->i32, "");
+               offset = ac_to_integer(&ctx->ac, tmp);
 
                buffer_append_args(ctx, emit_data, rsrc, ctx->i32_0,
                                   offset, false, false);
@@ -672,7 +637,7 @@ static void store_fetch_args(
                 * The only way to get unaligned stores in radeonsi is through
                 * shader images.
                 */
-               bool force_glc = ctx->screen->b.chip_class == SI;
+               bool force_glc = ctx->screen->info.chip_class == SI;
 
                image_fetch_rsrc(bld_base, &memory, true, target, &rsrc);
                coords = image_fetch_coords(bld_base, inst, 0, rsrc);
@@ -697,8 +662,7 @@ static void store_emit_buffer(
                bool writeonly_memory)
 {
        const struct tgsi_full_instruction *inst = emit_data->inst;
-       struct gallivm_state *gallivm = &ctx->gallivm;
-       LLVMBuilderRef builder = gallivm->builder;
+       LLVMBuilderRef builder = ctx->ac.builder;
        LLVMValueRef base_data = emit_data->args[0];
        LLVMValueRef base_offset = emit_data->args[3];
        unsigned writemask = inst->Dst[0].Register.WriteMask;
@@ -760,7 +724,7 @@ static void store_emit_buffer(
                lp_build_intrinsic(
                        builder, intrinsic_name, emit_data->dst_type,
                        emit_data->args, emit_data->arg_count,
-                       get_store_intr_attribs(writeonly_memory));
+                       ac_get_store_intr_attribs(writeonly_memory));
        }
 }
 
@@ -769,8 +733,7 @@ static void store_emit_memory(
                struct lp_build_emit_data *emit_data)
 {
        const struct tgsi_full_instruction *inst = emit_data->inst;
-       struct gallivm_state *gallivm = &ctx->gallivm;
-       LLVMBuilderRef builder = gallivm->builder;
+       LLVMBuilderRef builder = ctx->ac.builder;
        unsigned writemask = inst->Dst[0].Register.WriteMask;
        LLVMValueRef ptr, derived_ptr, data, index;
        int chan;
@@ -794,8 +757,7 @@ static void store_emit(
                struct lp_build_emit_data *emit_data)
 {
        struct si_shader_context *ctx = si_shader_context(bld_base);
-       struct gallivm_state *gallivm = &ctx->gallivm;
-       LLVMBuilderRef builder = gallivm->builder;
+       LLVMBuilderRef builder = ctx->ac.builder;
        const struct tgsi_full_instruction * inst = emit_data->inst;
        const struct tgsi_shader_info *info = &ctx->shader->selector->info;
        unsigned target = inst->Memory.Texture;
@@ -808,7 +770,7 @@ static void store_emit(
        }
 
        if (inst->Memory.Qualifier & TGSI_MEMORY_VOLATILE)
-               si_emit_waitcnt(ctx, VM_CNT);
+               ac_build_waitcnt(&ctx->ac, VM_CNT);
 
        writeonly_memory = is_oneway_access_only(inst, info,
                                                 info->shader_buffers_load |
@@ -826,7 +788,7 @@ static void store_emit(
                        builder, "llvm.amdgcn.buffer.store.format.v4f32",
                        emit_data->dst_type, emit_data->args,
                        emit_data->arg_count,
-                       get_store_intr_attribs(writeonly_memory));
+                       ac_get_store_intr_attribs(writeonly_memory));
        } else {
                ac_get_image_intr_name("llvm.amdgcn.image.store",
                                       LLVMTypeOf(emit_data->args[0]), /* vdata */
@@ -838,7 +800,7 @@ static void store_emit(
                        lp_build_intrinsic(
                                builder, intrinsic_name, emit_data->dst_type,
                                emit_data->args, emit_data->arg_count,
-                               get_store_intr_attribs(writeonly_memory));
+                               ac_get_store_intr_attribs(writeonly_memory));
        }
 }
 
@@ -847,8 +809,6 @@ static void atomic_fetch_args(
                struct lp_build_emit_data * emit_data)
 {
        struct si_shader_context *ctx = si_shader_context(bld_base);
-       struct gallivm_state *gallivm = &ctx->gallivm;
-       LLVMBuilderRef builder = gallivm->builder;
        const struct tgsi_full_instruction * inst = emit_data->inst;
        LLVMValueRef data1, data2;
        LLVMValueRef rsrc;
@@ -857,11 +817,11 @@ static void atomic_fetch_args(
        emit_data->dst_type = ctx->f32;
 
        tmp = lp_build_emit_fetch(bld_base, inst, 2, 0);
-       data1 = LLVMBuildBitCast(builder, tmp, ctx->i32, "");
+       data1 = ac_to_integer(&ctx->ac, tmp);
 
        if (inst->Instruction.Opcode == TGSI_OPCODE_ATOMCAS) {
                tmp = lp_build_emit_fetch(bld_base, inst, 3, 0);
-               data2 = LLVMBuildBitCast(builder, tmp, ctx->i32, "");
+               data2 = ac_to_integer(&ctx->ac, tmp);
        }
 
        /* llvm.amdgcn.image/buffer.atomic.cmpswap reflect the hardware order
@@ -877,7 +837,7 @@ static void atomic_fetch_args(
                rsrc = shader_buffer_fetch_rsrc(ctx, &inst->Src[0], false);
 
                tmp = lp_build_emit_fetch(bld_base, inst, 1, 0);
-               offset = LLVMBuildBitCast(builder, tmp, ctx->i32, "");
+               offset = ac_to_integer(&ctx->ac, tmp);
 
                buffer_append_args(ctx, emit_data, rsrc, ctx->i32_0,
                                   offset, true, false);
@@ -903,22 +863,21 @@ static void atomic_fetch_args(
 
 static void atomic_emit_memory(struct si_shader_context *ctx,
                                struct lp_build_emit_data *emit_data) {
-       struct gallivm_state *gallivm = &ctx->gallivm;
-       LLVMBuilderRef builder = gallivm->builder;
+       LLVMBuilderRef builder = ctx->ac.builder;
        const struct tgsi_full_instruction * inst = emit_data->inst;
        LLVMValueRef ptr, result, arg;
 
        ptr = get_memory_ptr(ctx, inst, ctx->i32, 1);
 
        arg = lp_build_emit_fetch(&ctx->bld_base, inst, 2, 0);
-       arg = LLVMBuildBitCast(builder, arg, ctx->i32, "");
+       arg = ac_to_integer(&ctx->ac, arg);
 
        if (inst->Instruction.Opcode == TGSI_OPCODE_ATOMCAS) {
                LLVMValueRef new_data;
                new_data = lp_build_emit_fetch(&ctx->bld_base,
                                               inst, 3, 0);
 
-               new_data = LLVMBuildBitCast(builder, new_data, ctx->i32, "");
+               new_data = ac_to_integer(&ctx->ac, new_data);
 
                result = LLVMBuildAtomicCmpXchg(builder, ptr, arg, new_data,
                                       LLVMAtomicOrderingSequentiallyConsistent,
@@ -974,8 +933,7 @@ static void atomic_emit(
                struct lp_build_emit_data *emit_data)
 {
        struct si_shader_context *ctx = si_shader_context(bld_base);
-       struct gallivm_state *gallivm = &ctx->gallivm;
-       LLVMBuilderRef builder = gallivm->builder;
+       LLVMBuilderRef builder = ctx->ac.builder;
        const struct tgsi_full_instruction * inst = emit_data->inst;
        char intrinsic_name[40];
        LLVMValueRef tmp;
@@ -1007,8 +965,7 @@ static void atomic_emit(
        tmp = lp_build_intrinsic(
                builder, intrinsic_name, ctx->i32,
                emit_data->args, emit_data->arg_count, 0);
-       emit_data->output[emit_data->chan] =
-               LLVMBuildBitCast(builder, tmp, ctx->f32, "");
+       emit_data->output[emit_data->chan] = ac_to_float(&ctx->ac, tmp);
 }
 
 static void set_tex_fetch_args(struct si_shader_context *ctx,
@@ -1018,7 +975,6 @@ static void set_tex_fetch_args(struct si_shader_context *ctx,
                               LLVMValueRef *param, unsigned count,
                               unsigned dmask)
 {
-       struct gallivm_state *gallivm = &ctx->gallivm;
        struct ac_image_args args = {};
 
        /* Pad to power of two vector */
@@ -1026,7 +982,7 @@ static void set_tex_fetch_args(struct si_shader_context *ctx,
                param[count++] = LLVMGetUndef(ctx->i32);
 
        if (count > 1)
-               args.addr = lp_build_gather_values(gallivm, param, count);
+               args.addr = lp_build_gather_values(&ctx->gallivm, param, count);
        else
                args.addr = param[0];
 
@@ -1045,10 +1001,10 @@ static void set_tex_fetch_args(struct si_shader_context *ctx,
 static LLVMValueRef fix_resinfo(struct si_shader_context *ctx,
                                unsigned target, LLVMValueRef out)
 {
-       LLVMBuilderRef builder = ctx->gallivm.builder;
+       LLVMBuilderRef builder = ctx->ac.builder;
 
        /* 1D textures are allocated and used as 2D on GFX9. */
-        if (ctx->screen->b.chip_class >= GFX9 &&
+        if (ctx->screen->info.chip_class >= GFX9 &&
            (target == TGSI_TEXTURE_1D_ARRAY ||
             target == TGSI_TEXTURE_SHADOW1D_ARRAY)) {
                LLVMValueRef layers =
@@ -1111,8 +1067,7 @@ static void resq_emit(
                struct lp_build_emit_data *emit_data)
 {
        struct si_shader_context *ctx = si_shader_context(bld_base);
-       struct gallivm_state *gallivm = &ctx->gallivm;
-       LLVMBuilderRef builder = gallivm->builder;
+       LLVMBuilderRef builder = ctx->ac.builder;
        const struct tgsi_full_instruction *inst = emit_data->inst;
        LLVMValueRef out;
 
@@ -1141,8 +1096,7 @@ LLVMValueRef si_load_sampler_desc(struct si_shader_context *ctx,
                                  LLVMValueRef list, LLVMValueRef index,
                                  enum ac_descriptor_type type)
 {
-       struct gallivm_state *gallivm = &ctx->gallivm;
-       LLVMBuilderRef builder = gallivm->builder;
+       LLVMBuilderRef builder = ctx->ac.builder;
 
        switch (type) {
        case AC_DESC_IMAGE:
@@ -1154,7 +1108,7 @@ LLVMValueRef si_load_sampler_desc(struct si_shader_context *ctx,
                index = LLVMBuildMul(builder, index, LLVMConstInt(ctx->i32, 4, 0), "");
                index = LLVMBuildAdd(builder, index, ctx->i32_1, "");
                list = LLVMBuildPointerCast(builder, list,
-                                           si_const_array(ctx->v4i32, 0), "");
+                                           ac_array_in_const32_addr_space(ctx->v4i32), "");
                break;
        case AC_DESC_FMASK:
                /* The FMASK is at [8:15]. */
@@ -1166,11 +1120,11 @@ LLVMValueRef si_load_sampler_desc(struct si_shader_context *ctx,
                index = LLVMBuildMul(builder, index, LLVMConstInt(ctx->i32, 4, 0), "");
                index = LLVMBuildAdd(builder, index, LLVMConstInt(ctx->i32, 3, 0), "");
                list = LLVMBuildPointerCast(builder, list,
-                                           si_const_array(ctx->v4i32, 0), "");
+                                           ac_array_in_const32_addr_space(ctx->v4i32), "");
                break;
        }
 
-       return ac_build_indexed_load_const(&ctx->ac, list, index);
+       return ac_build_load_to_sgpr(&ctx->ac, list, index);
 }
 
 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
@@ -1187,18 +1141,17 @@ LLVMValueRef si_load_sampler_desc(struct si_shader_context *ctx,
 static LLVMValueRef sici_fix_sampler_aniso(struct si_shader_context *ctx,
                                           LLVMValueRef res, LLVMValueRef samp)
 {
-       LLVMBuilderRef builder = ctx->gallivm.builder;
        LLVMValueRef img7, samp0;
 
-       if (ctx->screen->b.chip_class >= VI)
+       if (ctx->screen->info.chip_class >= VI)
                return samp;
 
-       img7 = LLVMBuildExtractElement(builder, res,
+       img7 = LLVMBuildExtractElement(ctx->ac.builder, res,
                                       LLVMConstInt(ctx->i32, 7, 0), "");
-       samp0 = LLVMBuildExtractElement(builder, samp,
+       samp0 = LLVMBuildExtractElement(ctx->ac.builder, samp,
                                        ctx->i32_0, "");
-       samp0 = LLVMBuildAnd(builder, samp0, img7, "");
-       return LLVMBuildInsertElement(builder, samp, samp0,
+       samp0 = LLVMBuildAnd(ctx->ac.builder, samp0, img7, "");
+       return LLVMBuildInsertElement(ctx->ac.builder, samp, samp0,
                                      ctx->i32_0, "");
 }
 
@@ -1223,7 +1176,7 @@ static void tex_fetch_ptrs(
                                                      &reg->Indirect,
                                                      reg->Register.Index,
                                                      ctx->num_samplers);
-               index = LLVMBuildAdd(ctx->gallivm.builder, index,
+               index = LLVMBuildAdd(ctx->ac.builder, index,
                                     LLVMConstInt(ctx->i32, SI_NUM_IMAGES / 2, 0), "");
        } else {
                index = LLVMConstInt(ctx->i32,
@@ -1316,7 +1269,6 @@ static void tex_fetch_args(
        struct lp_build_emit_data *emit_data)
 {
        struct si_shader_context *ctx = si_shader_context(bld_base);
-       struct gallivm_state *gallivm = &ctx->gallivm;
        const struct tgsi_full_instruction *inst = emit_data->inst;
        unsigned opcode = inst->Instruction.Opcode;
        unsigned target = inst->Texture.Texture;
@@ -1344,7 +1296,7 @@ static void tex_fetch_args(
 
        /* Fetch and project texture coordinates */
        coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
-       for (chan = 0; chan < 3; chan++ ) {
+       for (chan = 0; chan < 3; chan++) {
                coords[chan] = lp_build_emit_fetch(bld_base,
                                                   emit_data->inst, 0,
                                                   chan);
@@ -1356,7 +1308,7 @@ static void tex_fetch_args(
        }
 
        if (opcode == TGSI_OPCODE_TXP)
-               coords[3] = bld_base->base.one;
+               coords[3] = ctx->ac.f32_1;
 
        /* Pack offsets. */
        if (has_offset &&
@@ -1372,15 +1324,15 @@ static void tex_fetch_args(
                for (chan = 0; chan < 3; chan++) {
                        offset[chan] = lp_build_emit_fetch_texoffset(bld_base,
                                                                     emit_data->inst, 0, chan);
-                       offset[chan] = LLVMBuildAnd(gallivm->builder, offset[chan],
+                       offset[chan] = LLVMBuildAnd(ctx->ac.builder, offset[chan],
                                                    LLVMConstInt(ctx->i32, 0x3f, 0), "");
                        if (chan)
-                               offset[chan] = LLVMBuildShl(gallivm->builder, offset[chan],
+                               offset[chan] = LLVMBuildShl(ctx->ac.builder, offset[chan],
                                                            LLVMConstInt(ctx->i32, chan*8, 0), "");
                }
 
-               pack = LLVMBuildOr(gallivm->builder, offset[0], offset[1], "");
-               pack = LLVMBuildOr(gallivm->builder, pack, offset[2], "");
+               pack = LLVMBuildOr(ctx->ac.builder, offset[0], offset[1], "");
+               pack = LLVMBuildOr(ctx->ac.builder, pack, offset[2], "");
                address[count++] = pack;
        }
 
@@ -1412,16 +1364,16 @@ static void tex_fetch_args(
                 * so the depth comparison value isn't clamped for Z16 and
                 * Z24 anymore. Do it manually here.
                 */
-               if (ctx->screen->b.chip_class >= VI) {
+               if (ctx->screen->info.chip_class >= VI) {
                        LLVMValueRef upgraded;
                        LLVMValueRef clamped;
-                       upgraded = LLVMBuildExtractElement(gallivm->builder, samp_ptr,
+                       upgraded = LLVMBuildExtractElement(ctx->ac.builder, samp_ptr,
                                                           LLVMConstInt(ctx->i32, 3, false), "");
-                       upgraded = LLVMBuildLShr(gallivm->builder, upgraded,
+                       upgraded = LLVMBuildLShr(ctx->ac.builder, upgraded,
                                                 LLVMConstInt(ctx->i32, 29, false), "");
-                       upgraded = LLVMBuildTrunc(gallivm->builder, upgraded, ctx->i1, "");
+                       upgraded = LLVMBuildTrunc(ctx->ac.builder, upgraded, ctx->i1, "");
                        clamped = ac_build_clamp(&ctx->ac, z);
-                       z = LLVMBuildSelect(gallivm->builder, upgraded, clamped, z, "");
+                       z = LLVMBuildSelect(ctx->ac.builder, upgraded, clamped, z, "");
                }
 
                address[count++] = z;
@@ -1463,7 +1415,7 @@ static void tex_fetch_args(
                        num_src_deriv_channels = 1;
 
                        /* 1D textures are allocated and used as 2D on GFX9. */
-                       if (ctx->screen->b.chip_class >= GFX9) {
+                       if (ctx->screen->info.chip_class >= GFX9) {
                                num_dst_deriv_channels = 2;
                                num_deriv_channels = 2;
                        } else {
@@ -1484,7 +1436,7 @@ static void tex_fetch_args(
                        for (chan = num_src_deriv_channels;
                             chan < num_dst_deriv_channels; chan++)
                                derivs[param * num_dst_deriv_channels + chan] =
-                                       bld_base->base.zero;
+                                       ctx->ac.f32_0;
                }
        }
 
@@ -1501,7 +1453,7 @@ static void tex_fetch_args(
        } else if (tgsi_is_array_sampler(target) &&
                   opcode != TGSI_OPCODE_TXF &&
                   opcode != TGSI_OPCODE_TXF_LZ &&
-                  ctx->screen->b.chip_class <= VI) {
+                  ctx->screen->info.chip_class <= VI) {
                unsigned array_coord = target == TGSI_TEXTURE_1D_ARRAY ? 1 : 2;
                coords[array_coord] =
                        ac_build_intrinsic(&ctx->ac, "llvm.rint.f32", ctx->f32,
@@ -1520,7 +1472,7 @@ static void tex_fetch_args(
                address[count++] = coords[2];
 
        /* 1D textures are allocated and used as 2D on GFX9. */
-       if (ctx->screen->b.chip_class >= GFX9) {
+       if (ctx->screen->info.chip_class >= GFX9) {
                LLVMValueRef filler;
 
                /* Use 0.5, so that we don't sample the border color. */
@@ -1552,85 +1504,13 @@ static void tex_fetch_args(
                count = 16;
        }
 
-       for (chan = 0; chan < count; chan++ ) {
-               address[chan] = LLVMBuildBitCast(gallivm->builder,
-                                                address[chan], ctx->i32, "");
-       }
+       for (chan = 0; chan < count; chan++)
+               address[chan] = ac_to_integer(&ctx->ac, address[chan]);
 
-       /* 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;
-        */
        if (target == TGSI_TEXTURE_2D_MSAA ||
            target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
-               struct lp_build_emit_data txf_emit_data = *emit_data;
-               LLVMValueRef txf_address[4];
-               /* We only need .xy for non-arrays, and .xyz for arrays. */
-               unsigned txf_count = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
-               struct tgsi_full_instruction inst = {};
-
-               memcpy(txf_address, address, sizeof(txf_address));
-
-               /* Read FMASK using TXF_LZ. */
-               inst.Instruction.Opcode = TGSI_OPCODE_TXF_LZ;
-               inst.Texture.Texture = target;
-               txf_emit_data.inst = &inst;
-               txf_emit_data.chan = 0;
-               set_tex_fetch_args(ctx, &txf_emit_data,
-                                  target, fmask_ptr, NULL,
-                                  txf_address, txf_count, 0xf);
-               build_tex_intrinsic(&tex_action, bld_base, &txf_emit_data);
-
-               /* Initialize some constants. */
-               LLVMValueRef four = LLVMConstInt(ctx->i32, 4, 0);
-               LLVMValueRef F = LLVMConstInt(ctx->i32, 0xF, 0);
-
-               /* Apply the formula. */
-               LLVMValueRef fmask =
-                       LLVMBuildExtractElement(gallivm->builder,
-                                               txf_emit_data.output[0],
-                                               ctx->i32_0, "");
-
-               unsigned sample_chan = txf_count; /* the sample index is last */
-
-               LLVMValueRef sample_index4 =
-                       LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
-
-               LLVMValueRef shifted_fmask =
-                       LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
-
-               LLVMValueRef final_sample =
-                       LLVMBuildAnd(gallivm->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(gallivm->builder, fmask_ptr,
-                                        ctx->v8i32, "");
-
-               LLVMValueRef fmask_word1 =
-                       LLVMBuildExtractElement(gallivm->builder, fmask_desc,
-                                               ctx->i32_1, "");
-
-               LLVMValueRef word1_is_nonzero =
-                       LLVMBuildICmp(gallivm->builder, LLVMIntNE,
-                                     fmask_word1, ctx->i32_0, "");
-
-               /* Replace the MSAA sample index. */
-               address[sample_chan] =
-                       LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
-                                       final_sample, address[sample_chan], "");
+               ac_apply_fmask_to_sample(&ctx->ac, fmask_ptr, address,
+                                        target == TGSI_TEXTURE_2D_ARRAY_MSAA);
        }
 
        if (opcode == TGSI_OPCODE_TXF ||
@@ -1721,7 +1601,7 @@ si_lower_gather4_integer(struct si_shader_context *ctx,
                         unsigned target,
                         enum tgsi_return_type return_type)
 {
-       LLVMBuilderRef builder = ctx->gallivm.builder;
+       LLVMBuilderRef builder = ctx->ac.builder;
        LLVMValueRef wa_8888 = NULL;
        LLVMValueRef coord = args->addr;
        LLVMValueRef half_texel[2];
@@ -1821,9 +1701,9 @@ si_lower_gather4_integer(struct si_shader_context *ctx,
                LLVMValueRef index = LLVMConstInt(ctx->i32, coord_vgpr_index + c, 0);
 
                tmp = LLVMBuildExtractElement(builder, coord, index, "");
-               tmp = LLVMBuildBitCast(builder, tmp, ctx->f32, "");
+               tmp = ac_to_float(&ctx->ac, tmp);
                tmp = LLVMBuildFAdd(builder, tmp, half_texel[c], "");
-               tmp = LLVMBuildBitCast(builder, tmp, ctx->i32, "");
+               tmp = ac_to_integer(&ctx->ac, tmp);
                coord = LLVMBuildInsertElement(builder, coord, tmp, index, "");
        }
 
@@ -1841,7 +1721,7 @@ si_fix_gather4_integer_result(struct si_shader_context *ctx,
                           enum tgsi_return_type return_type,
                           LLVMValueRef wa)
 {
-       LLVMBuilderRef builder = ctx->gallivm.builder;
+       LLVMBuilderRef builder = ctx->ac.builder;
 
        assert(return_type == TGSI_RETURN_TYPE_SINT ||
               return_type == TGSI_RETURN_TYPE_UINT);
@@ -1857,7 +1737,7 @@ si_fix_gather4_integer_result(struct si_shader_context *ctx,
                        wa_value = LLVMBuildFPToUI(builder, value, ctx->i32, "");
                else
                        wa_value = LLVMBuildFPToSI(builder, value, ctx->i32, "");
-               wa_value = LLVMBuildBitCast(builder, wa_value, ctx->f32, "");
+               wa_value = ac_to_float(&ctx->ac, wa_value);
                value = LLVMBuildSelect(builder, wa, wa_value, value, "");
 
                result = LLVMBuildInsertElement(builder, result, value, chanv, "");
@@ -1877,12 +1757,16 @@ static void build_tex_intrinsic(const struct lp_build_tgsi_action *action,
        unsigned target = inst->Texture.Texture;
 
        if (target == TGSI_TEXTURE_BUFFER) {
-               emit_data->output[emit_data->chan] =
+               unsigned num_channels =
+                       util_last_bit(inst->Dst[0].Register.WriteMask);
+               LLVMValueRef result =
                        ac_build_buffer_load_format(&ctx->ac,
                                                    emit_data->args[0],
                                                    emit_data->args[2],
                                                    emit_data->args[1],
-                                                   true);
+                                                   num_channels, false, true);
+               emit_data->output[emit_data->chan] =
+                       ac_build_expand_to_vec4(&ctx->ac, result, num_channels);
                return;
        }
 
@@ -1940,7 +1824,7 @@ static void build_tex_intrinsic(const struct lp_build_tgsi_action *action,
        /* The hardware needs special lowering for Gather4 with integer formats. */
        LLVMValueRef gather4_int_result_workaround = NULL;
 
-       if (ctx->screen->b.chip_class <= VI &&
+       if (ctx->screen->info.chip_class <= VI &&
            opcode == TGSI_OPCODE_TG4) {
                assert(inst->Texture.ReturnType != TGSI_RETURN_TYPE_UNKNOWN);
 
@@ -1970,8 +1854,6 @@ static void si_llvm_emit_txqs(
        struct lp_build_emit_data *emit_data)
 {
        struct si_shader_context *ctx = si_shader_context(bld_base);
-       struct gallivm_state *gallivm = &ctx->gallivm;
-       LLVMBuilderRef builder = gallivm->builder;
        LLVMValueRef res, samples;
        LLVMValueRef res_ptr, samp_ptr, fmask_ptr = NULL;
 
@@ -1979,20 +1861,76 @@ static void si_llvm_emit_txqs(
 
 
        /* Read the samples from the descriptor directly. */
-       res = LLVMBuildBitCast(builder, res_ptr, ctx->v8i32, "");
-       samples = LLVMBuildExtractElement(
-               builder, res,
-               LLVMConstInt(ctx->i32, 3, 0), "");
-       samples = LLVMBuildLShr(builder, samples,
+       res = LLVMBuildBitCast(ctx->ac.builder, res_ptr, ctx->v8i32, "");
+       samples = LLVMBuildExtractElement(ctx->ac.builder, res,
+                                         LLVMConstInt(ctx->i32, 3, 0), "");
+       samples = LLVMBuildLShr(ctx->ac.builder, samples,
                                LLVMConstInt(ctx->i32, 16, 0), "");
-       samples = LLVMBuildAnd(builder, samples,
+       samples = LLVMBuildAnd(ctx->ac.builder, samples,
                               LLVMConstInt(ctx->i32, 0xf, 0), "");
-       samples = LLVMBuildShl(builder, ctx->i32_1,
+       samples = LLVMBuildShl(ctx->ac.builder, ctx->i32_1,
                               samples, "");
 
        emit_data->output[emit_data->chan] = samples;
 }
 
+static void si_llvm_emit_fbfetch(const struct lp_build_tgsi_action *action,
+                                struct lp_build_tgsi_context *bld_base,
+                                struct lp_build_emit_data *emit_data)
+{
+       struct si_shader_context *ctx = si_shader_context(bld_base);
+       struct ac_image_args args = {};
+       LLVMValueRef ptr, image, fmask, addr_vec;
+
+       /* Ignore src0, because KHR_blend_func_extended disallows multiple render
+        * targets.
+        */
+
+       /* Load the image descriptor. */
+       STATIC_ASSERT(SI_PS_IMAGE_COLORBUF0 % 2 == 0);
+       ptr = LLVMGetParam(ctx->main_fn, ctx->param_rw_buffers);
+       ptr = LLVMBuildPointerCast(ctx->ac.builder, ptr,
+                                  ac_array_in_const32_addr_space(ctx->v8i32), "");
+       image = ac_build_load_to_sgpr(&ctx->ac, ptr,
+                       LLVMConstInt(ctx->i32, SI_PS_IMAGE_COLORBUF0 / 2, 0));
+
+       LLVMValueRef addr[4];
+       unsigned chan = 0;
+
+       addr[chan++] = si_unpack_param(ctx, SI_PARAM_POS_FIXED_PT, 0, 16);
+
+       if (!ctx->shader->key.mono.u.ps.fbfetch_is_1D)
+               addr[chan++] = si_unpack_param(ctx, SI_PARAM_POS_FIXED_PT, 16, 16);
+
+       /* Get the current render target layer index. */
+       if (ctx->shader->key.mono.u.ps.fbfetch_layered)
+               addr[chan++] = si_unpack_param(ctx, SI_PARAM_ANCILLARY, 16, 11);
+
+       if (ctx->shader->key.mono.u.ps.fbfetch_msaa)
+               addr[chan++] = si_get_sample_id(ctx);
+
+       while (chan < 4)
+               addr[chan++] = LLVMGetUndef(ctx->i32);
+
+       if (ctx->shader->key.mono.u.ps.fbfetch_msaa) {
+               fmask = ac_build_load_to_sgpr(&ctx->ac, ptr,
+                       LLVMConstInt(ctx->i32, SI_PS_IMAGE_COLORBUF0_FMASK / 2, 0));
+
+               ac_apply_fmask_to_sample(&ctx->ac, fmask, addr, false);
+       }
+
+       addr_vec = ac_build_gather_values(&ctx->ac, addr, ARRAY_SIZE(addr));
+
+       args.opcode = ac_image_load;
+       args.resource = image;
+       args.addr = addr_vec;
+       args.dmask = 0xf;
+       args.da = ctx->shader->key.mono.u.ps.fbfetch_layered;
+
+       emit_data->output[emit_data->chan] =
+               ac_build_image_opcode(&ctx->ac, &args);
+}
+
 static const struct lp_build_tgsi_action tex_action = {
        .fetch_args = tex_fetch_args,
        .emit = build_tex_intrinsic,
@@ -2025,6 +1963,8 @@ void si_shader_context_init_mem(struct si_shader_context *ctx)
        bld_base->op_actions[TGSI_OPCODE_LODQ] = tex_action;
        bld_base->op_actions[TGSI_OPCODE_TXQS].emit = si_llvm_emit_txqs;
 
+       bld_base->op_actions[TGSI_OPCODE_FBFETCH].emit = si_llvm_emit_fbfetch;
+
        bld_base->op_actions[TGSI_OPCODE_LOAD].fetch_args = load_fetch_args;
        bld_base->op_actions[TGSI_OPCODE_LOAD].emit = load_emit;
        bld_base->op_actions[TGSI_OPCODE_STORE].fetch_args = store_fetch_args;