Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
357 changes: 339 additions & 18 deletions src/asahi/compiler/agx_compile.c

Large diffs are not rendered by default.

20 changes: 18 additions & 2 deletions src/asahi/compiler/agx_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#pragma once

#include "asahi/genxml/agx_g11.h"
#include "compiler/nir/nir.h"
#include "util/shader_stats.h"
#include "util/u_dynarray.h"
Expand Down Expand Up @@ -77,6 +78,11 @@ struct agx_rodata {
uint16_t size_16;
};

/* Top of the G11 spill area: the 32 KB window less the 16 bytes that
* agx_usc_shader_g11 adds to the threadgroup memory size.
*/
#define AGX_G11_SPILL_TOP (32768 - 16)

struct agx_shader_info {
mesa_shader_stage stage;
uint32_t binary_size;
Expand All @@ -95,6 +101,11 @@ struct agx_shader_info {
/* Scratch memory allocation in bytes for main/preamble respectively */
unsigned scratch_size, preamble_scratch_size;

/* G11: bytes of threadgroup memory per thread for register spills, at the
* top of the window, see agx_lower_spill.c.
*/
unsigned spill_stride;

/* Size in bytes of the main sahder */
unsigned main_size;

Expand Down Expand Up @@ -368,6 +379,8 @@ static const nir_shader_compiler_options agx_nir_options = {
.lower_fminmax_signed_zero = true,
.lower_fdph = true,
.lower_ffract = true,
/* G11 has floor but no ceil; trunc and roundeven are in lower_g11_alu. */
.lower_fceil = AGX_G11,
.lower_pack_half_2x16 = true,
.lower_pack_unorm_2x16 = true,
.lower_pack_snorm_2x16 = true,
Expand Down Expand Up @@ -395,10 +408,13 @@ static const nir_shader_compiler_options agx_nir_options = {
.lower_int64_options =
(nir_lower_int64_options) ~(nir_lower_iadd64 | nir_lower_imul_2x32_64),
.lower_doubles_options = (nir_lower_doubles_options)(~0),
/* G11's iter and st_vary take immediate indices only */
.support_indirect_inputs = BITFIELD_BIT(MESA_SHADER_TESS_CTRL) |
BITFIELD_BIT(MESA_SHADER_TESS_EVAL) |
BITFIELD_BIT(MESA_SHADER_FRAGMENT),
.support_indirect_outputs = (uint8_t)BITFIELD_MASK(MESA_SHADER_STAGES),
(AGX_G11 ? 0 : BITFIELD_BIT(MESA_SHADER_FRAGMENT)),
.support_indirect_outputs =
(uint8_t)(BITFIELD_MASK(MESA_SHADER_STAGES) &
~(AGX_G11 ? BITFIELD_BIT(MESA_SHADER_VERTEX) : 0)),
.max_samples = 4,
.lower_fquantize2f16 = true,
.compact_arrays = true,
Expand Down
4 changes: 3 additions & 1 deletion src/asahi/compiler/agx_compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#pragma once

#include "asahi/genxml/agx_g11.h"
#include "asahi/isa/agx_minifloat.h"
#include "compiler/nir/nir.h"
#include "util/bitset.h"
Expand All @@ -24,7 +25,8 @@ extern "C" {
#define AGX_NUM_REGS (256)

/* u0-u255 inclusive, as pairs of 16-bits */
#define AGX_NUM_UNIFORMS (512)
/* G11 uniforms past 255 need an encoding we do not know */
#define AGX_NUM_UNIFORMS (AGX_G11 ? 256 : 512)

/* Semi-arbitrary limit for spill slot allocation */
#define AGX_NUM_MODELED_REGS_LOG2 (11)
Expand Down
5 changes: 5 additions & 0 deletions src/asahi/compiler/agx_insert_waits.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
static bool
instr_is_async(agx_instr *I)
{
/* G11 threadgroup loads and atomics use the device memory encodings. */
if (AGX_G11 &&
(I->op == AGX_OPCODE_LOCAL_LOAD || I->op == AGX_OPCODE_LOCAL_ATOMIC))
return true;

return agx_opcodes_info[I->op].immediates & AGX_IMMEDIATE_SCOREBOARD;
}

Expand Down
49 changes: 48 additions & 1 deletion src/asahi/compiler/agx_lower_spill.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,63 @@ spill_fill(agx_builder *b, agx_instr *I, enum agx_size size, unsigned channels,
"RA allocates enough scratch");

/* Emit the spill/fill */
if (I->dest[0].memory) {
if (AGX_G11) {
/* r3l holds this thread's spill base, r3h the address of the access */
agx_index addr = agx_register(7, AGX_SIZE_16);
agx_mov_imm_to(b, addr, stack_offs_B);
agx_iadd_to(b, addr, agx_register(6, AGX_SIZE_16), addr, 0);

if (I->dest[0].memory)
agx_local_store(b, reg, addr, agx_zero(), format, mask);
else
agx_local_load_to(b, reg, addr, agx_zero(), format, mask);
} else if (I->dest[0].memory) {
agx_stack_store(b, reg, agx_immediate(stack_offs_B), format, mask);
} else {
agx_stack_load_to(b, reg, agx_immediate(stack_offs_B), format, mask);
}
}

/*
* G11 kills the thread on stack_load/stack_store, so spill to threadgroup
* memory instead: each thread gets spill_stride bytes, stacked down
* from AGX_G11_SPILL_TOP. Nothing is live at entry, so r1-r3 (reserved by RA
* when spilling) are free to compute the base,
* r3l = TOP - (local_index + 1) * stride.
*/
static void
g11_spill_base(agx_context *ctx)
{
assert(ctx->stage == MESA_SHADER_COMPUTE && !ctx->is_preamble);
assert(ctx->has_spill_pcopy_reserved);

unsigned stride = align(ctx->scratch_size_B, 16);
agx_index r1 = agx_register(2, AGX_SIZE_32);
agx_index r2 = agx_register(4, AGX_SIZE_32);
agx_index r3 = agx_register(6, AGX_SIZE_32);
agx_builder b = agx_init_builder(ctx, agx_before_block(agx_start_block(ctx)));

agx_get_sr_to(&b, r1, AGX_SR_THREAD_POSITION_IN_THREADGROUP_X + 2);
agx_get_sr_to(&b, r2, AGX_SR_THREADS_PER_THREADGROUP_X + 1);
agx_get_sr_to(&b, r3, AGX_SR_THREAD_POSITION_IN_THREADGROUP_X + 1);
agx_imad_to(&b, r1, r1, r2, r3, 0);
agx_get_sr_to(&b, r2, AGX_SR_THREADS_PER_THREADGROUP_X);
agx_get_sr_to(&b, r3, AGX_SR_THREAD_POSITION_IN_THREADGROUP_X);
agx_imad_to(&b, r1, r1, r2, r3, 0);
agx_mov_imm_to(&b, r2, (uint32_t)-stride);
agx_mov_imm_to(&b, r3, AGX_G11_SPILL_TOP - stride);
agx_imad_to(&b, r3, r1, r2, r3, 0);

ctx->max_reg = MAX2(ctx->max_reg, 7);
ctx->out->spill_stride = stride;
}

void
agx_lower_spill(agx_context *ctx)
{
if (AGX_G11)
g11_spill_base(ctx);

agx_foreach_instr_global_safe(ctx, I) {
if (I->op != AGX_OPCODE_MOV || (!I->dest[0].memory && !I->src[0].memory))
continue;
Expand Down
4 changes: 2 additions & 2 deletions src/asahi/compiler/agx_lower_uniform_sources.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ agx_instr_accepts_uniform(enum agx_opcode op, unsigned src_index,
case AGX_OPCODE_ATOMIC:
return src_index == 1 && !high;
case AGX_OPCODE_LOCAL_LOAD:
return src_index == 0;
return !AGX_G11 && src_index == 0;
case AGX_OPCODE_LOCAL_STORE:
return src_index == 1;
return !AGX_G11 && src_index == 1;
case AGX_OPCODE_IMAGE_WRITE:
return src_index == 3;
case AGX_OPCODE_BLOCK_IMAGE_STORE:
Expand Down
1 change: 1 addition & 0 deletions src/asahi/compiler/agx_nir.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ bool agx_nir_lower_fminmax(struct nir_shader *s);
bool agx_nir_lower_texture_early(struct nir_shader *s, bool support_lod_bias);
bool agx_nir_lower_texture(struct nir_shader *s);
bool agx_nir_lower_multisampled_image_store(struct nir_shader *s);
bool agx_nir_lower_g11_layered_image_store(struct nir_shader *s);
bool agx_nir_needs_texture_crawl(struct nir_instr *instr);
4 changes: 4 additions & 0 deletions src/asahi/compiler/agx_nir_lower_discard_zs_emit.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ lower_zs_emit(nir_block *block, bool force_early_z)
*/
if (z)
value = nir_f2f32(&b, value);

/* G11 does not clamp the depth a shader writes to [0, 1] */
if (z && AGX_G11)
value = nir_fsat(&b, value);
else
value = nir_u2u16(&b, value);

Expand Down
Loading
Loading