Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
38 changes: 35 additions & 3 deletions src/asahi/compiler/agx3_matrix_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,44 @@ agx3_matrix_encode_gemm16(enum agx_isa_encoding encoding,
const struct agx3_matrix_gemm16_desc *d,
uint8_t out[10], uint8_t *length)
{
if (!d)
if (!d || encoding != AGX_ISA_ENCODING_AGX3C || !out || !length ||
(d->dest & 15) || d->dest > 240)
return false;
enum agx3_matrix_input_type b_type = d->b_input_type ? d->b_input_type : d->input_type;
if (encoding != AGX_ISA_ENCODING_AGX3C || !out || !length ||
if (agx3_matrix_input_is_int8(d->input_type) ||
agx3_matrix_input_is_int8(b_type)) {
if (!agx3_matrix_input_is_int8(d->input_type) ||
!agx3_matrix_input_is_int8(b_type) ||
(d->a & 3) || d->a > 252 || (d->b & 3) || d->b > 252)
return false;

const struct agx3_field_kv base[] = {
{1, 2}, {2, 2}, {9, 2}, {10, 0}, {14, 1}, {20, 2}, {21, 0},
{25, 1}, {30, 3}, {31, 83},
};
struct agx3_field_kv fields[24];
unsigned count = 0;
fields[count++] = (struct agx3_field_kv){0, d->dest};
fields[count++] = (struct agx3_field_kv){3, d->dest_hint};
fields[count++] = (struct agx3_field_kv){7, d->zero_accumulator};
fields[count++] = (struct agx3_field_kv){8, d->a};
fields[count++] = (struct agx3_field_kv){12, d->input_type == AGX3_MATRIX_INPUT_S8};
fields[count++] = (struct agx3_field_kv){14, !d->preserve_a};
fields[count++] = (struct agx3_field_kv){15, d->transpose_a};
fields[count++] = (struct agx3_field_kv){19, d->b};
fields[count++] = (struct agx3_field_kv){23, b_type == AGX3_MATRIX_INPUT_S8};
fields[count++] = (struct agx3_field_kv){25, !d->preserve_b};
fields[count++] = (struct agx3_field_kv){26, d->transpose_b};
fields[count++] = (struct agx3_field_kv){40, d->saturate};
for (unsigned i = 0; i < 8; ++i)
if (d->wait_mask & (1u << i))
fields[count++] = (struct agx3_field_kv){32 + i, 1};
return agx3_bind_compose(encoding, AGX3_MN_iGeMM16x16x16, 0, base,
sizeof(base) / sizeof(base[0]), fields, count,
out, length);
}
if (d->saturate ||
(b_type != AGX3_MATRIX_INPUT_F16 && b_type != AGX3_MATRIX_INPUT_BF16) ||
(d->dest & 15) || d->dest > 240 ||
(d->a & 7) || d->a > 248 || (d->b & 7) || d->b > 248 ||
(d->input_type != AGX3_MATRIX_INPUT_F16 &&
d->input_type != AGX3_MATRIX_INPUT_BF16))
Expand Down
11 changes: 11 additions & 0 deletions src/asahi/compiler/agx3_matrix_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,20 @@ enum agx3_matrix_input_type {
AGX3_MATRIX_INPUT_F16 = 1,
AGX3_MATRIX_INPUT_F32 = 2,
AGX3_MATRIX_INPUT_BF16 = 4,
AGX3_MATRIX_INPUT_U8 = 8,
AGX3_MATRIX_INPUT_S8 = 9,
};

static inline bool
agx3_matrix_input_is_int8(unsigned type)
{
return type == AGX3_MATRIX_INPUT_U8 || type == AGX3_MATRIX_INPUT_S8;
}

/* matrix_type bit of an 8x8 matmad with an f16 accumulator and result */
#define AGX3_MATRIX_ACCUM_F16 (1u << 8)
/* matrix_type bit of a 16x16 integer GEMM with signed 32-bit saturation */
#define AGX3_MATRIX_SATURATE (1u << 9)

struct agx3_matrix_gemm16_desc {
uint16_t dest;
Expand All @@ -32,6 +42,7 @@ struct agx3_matrix_gemm16_desc {
bool preserve_b;
uint8_t wait_mask;
enum agx3_matrix_input_type b_input_type;
bool saturate;
};

bool agx3_matrix_encode_gemm16(enum agx_isa_encoding encoding,
Expand Down
9 changes: 7 additions & 2 deletions src/asahi/compiler/agx3_pack_binary.c
Original file line number Diff line number Diff line change
Expand Up @@ -14995,11 +14995,15 @@ agx3_pack_binary(agx_context *ctx, struct util_dynarray *emission)
agx_channels(I->dest[0]) != 8 ||
I->dest[0].value != I->src[2].value)
AGX3_REFUSE(I);
const bool is_int8 = agx3_matrix_input_is_int8(I->matrix_type & 15);
for (unsigned s = 0; s < 3; ++s) {
const agx_index src = I->src[s];
const unsigned exp_ch = (s < 2 && is_int8) ? 2 : 8;
const enum agx_size exp_sz =
(s == 2 || is_int8) ? AGX_SIZE_32 : AGX_SIZE_16;
if (!agx3_src_is_register(src) || src.memory || src.abs ||
src.neg || agx_channels(src) != 8 ||
src.size != (s == 2 ? AGX_SIZE_32 : AGX_SIZE_16))
src.neg || agx_channels(src) != exp_ch ||
src.size != exp_sz)
AGX3_REFUSE(I);
}
struct agx3_matrix_gemm16_desc d = {
Expand All @@ -15011,6 +15015,7 @@ agx3_pack_binary(agx_context *ctx, struct util_dynarray *emission)
.wait_mask = wait,
.preserve_a = !I->src[0].discard,
.preserve_b = !I->src[1].discard,
.saturate = (I->matrix_type & AGX3_MATRIX_SATURATE) != 0,
};
uint8_t bytes[10], length;
if (!agx3_matrix_encode_gemm16(enc, &d, bytes, &length) ||
Expand Down
20 changes: 14 additions & 6 deletions src/asahi/compiler/agx_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -3720,15 +3720,23 @@ agx_emit_intrinsic(agx_builder *b, nir_intrinsic_instr *instr)
return NULL;
}

unsigned type = nir_intrinsic_src_type(instr) == nir_type_uint16
? AGX3_MATRIX_INPUT_BF16 : nir_intrinsic_src_type(instr) == nir_type_float32
? AGX3_MATRIX_INPUT_F32 : AGX3_MATRIX_INPUT_F16;
unsigned type_b = nir_intrinsic_src_type2(instr) == nir_type_uint16
? AGX3_MATRIX_INPUT_BF16 : nir_intrinsic_src_type2(instr) == nir_type_float32
? AGX3_MATRIX_INPUT_F32 : AGX3_MATRIX_INPUT_F16;
const nir_alu_type ta = nir_intrinsic_src_type(instr);
const nir_alu_type tb = nir_intrinsic_src_type2(instr);
unsigned type =
ta == nir_type_int8 ? AGX3_MATRIX_INPUT_S8 :
ta == nir_type_uint8 ? AGX3_MATRIX_INPUT_U8 :
ta == nir_type_uint16 ? AGX3_MATRIX_INPUT_BF16 :
ta == nir_type_float32 ? AGX3_MATRIX_INPUT_F32 : AGX3_MATRIX_INPUT_F16;
unsigned type_b =
tb == nir_type_int8 ? AGX3_MATRIX_INPUT_S8 :
tb == nir_type_uint8 ? AGX3_MATRIX_INPUT_U8 :
tb == nir_type_uint16 ? AGX3_MATRIX_INPUT_BF16 :
tb == nir_type_float32 ? AGX3_MATRIX_INPUT_F32 : AGX3_MATRIX_INPUT_F16;
type |= type_b << 4;
if (instr->def.bit_size == 16)
type |= AGX3_MATRIX_ACCUM_F16;
if (nir_intrinsic_saturate(instr))
type |= AGX3_MATRIX_SATURATE;
/* The 16x16 GEMM form is M5 (AGX3C) only; the 8x8 matmad also exists on
* the AGX3B encoding (G15/G16/G17P), so the 2-component path is allowed
* there too. */
Expand Down
80 changes: 67 additions & 13 deletions src/asahi/compiler/agx_nir_lower_cmat.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,24 @@ shuffle_xor_values(nir_builder *b, nir_def **in, nir_def **out, unsigned count,
for (unsigned i = 0; i < count; ++i) {
if (in[i]->bit_size == 32) {
out[i] = nir_shuffle_xor(b, in[i], m);
} else if (i + 1 < count) {
nir_def *pair = nir_shuffle_xor(b, nir_pack_32_2x16_split(b, in[i], in[i + 1]), m);
out[i] = nir_unpack_32_2x16_split_x(b, pair);
out[++i] = nir_unpack_32_2x16_split_y(b, pair);
} else if (in[i]->bit_size == 16) {
if (i + 1 < count) {
nir_def *pair = nir_shuffle_xor(b, nir_pack_32_2x16_split(b, in[i], in[i + 1]), m);
out[i] = nir_unpack_32_2x16_split_x(b, pair);
out[++i] = nir_unpack_32_2x16_split_y(b, pair);
} else {
out[i] = nir_u2u16(b, nir_shuffle_xor(b, nir_u2u32(b, in[i]), m));
}
} else {
out[i] = nir_u2u16(b, nir_shuffle_xor(b, nir_u2u32(b, in[i]), m));
assert(in[i]->bit_size == 8);
unsigned n = MIN2(4u, count - i);
nir_def *packed = nir_u2u32(b, in[i]);
for (unsigned k = 1; k < n; ++k)
packed = nir_ior(b, packed, nir_ishl_imm(b, nir_u2u32(b, in[i + k]), 8 * k));
nir_def *shuf = nir_shuffle_xor(b, packed, m);
for (unsigned k = 0; k < n; ++k)
out[i + k] = nir_u2u8(b, k ? nir_ushr_imm(b, shuf, 8 * k) : shuf);
i += n - 1;
}
}
}
Expand Down Expand Up @@ -350,7 +362,7 @@ convert_matrix_value(nir_builder *b, nir_def *value,
}
if (dst == GLSL_TYPE_BFLOAT16) {
if (src != GLSL_TYPE_FLOAT)
value = nir_f2f32(b, value);
value = convert_matrix_value(b, value, src, GLSL_TYPE_FLOAT);
return nir_f2bf(b, value);
}
nir_op op = nir_type_conversion_op(nir_get_nir_type_for_glsl_base_type(src),
Expand All @@ -359,6 +371,21 @@ convert_matrix_value(nir_builder *b, nir_def *value,
return nir_build_alu1(b, op, value);
}

static nir_def *
pack_8x8_to_2x32(nir_builder *b, nir_def *v)
{
assert(v->bit_size == 8 && v->num_components == 8);
nir_def *w[2];
for (unsigned i = 0; i < 2; ++i) {
nir_def *b0 = nir_u2u32(b, nir_channel(b, v, 4 * i + 0));
nir_def *b1 = nir_ishl_imm(b, nir_u2u32(b, nir_channel(b, v, 4 * i + 1)), 8);
nir_def *b2 = nir_ishl_imm(b, nir_u2u32(b, nir_channel(b, v, 4 * i + 2)), 16);
nir_def *b3 = nir_ishl_imm(b, nir_u2u32(b, nir_channel(b, v, 4 * i + 3)), 24);
w[i] = nir_ior(b, nir_ior(b, b0, b1), nir_ior(b, b2, b3));
}
return nir_vec2(b, w[0], w[1]);
}

static bool
lower_matrix_instr(nir_builder *b, nir_instr *instr, struct hash_table *types,
bool native_f16_accum8, bool widen_bf16_inputs)
Expand Down Expand Up @@ -446,12 +473,34 @@ lower_matrix_instr(nir_builder *b, nir_instr *instr, struct hash_table *types,
return true;
}
case nir_intrinsic_cmat_muladd: {
assert(!nir_intrinsic_saturate(intr));
enum glsl_base_type input = matrix_desc(intr->src[1]).element_type;
enum glsl_base_type input_b = matrix_desc(intr->src[2]).element_type;
enum glsl_base_type accum = matrix_desc(intr->src[3]).element_type;
enum glsl_base_type result = matrix_desc(intr->src[0]).element_type;
const nir_cmat_signed signed_mask = nir_intrinsic_cmat_signed_mask(intr);
enum glsl_base_type input = glsl_apply_signedness_to_base_type(
matrix_desc(intr->src[1]).element_type, signed_mask & NIR_CMAT_A_SIGNED);
enum glsl_base_type input_b = glsl_apply_signedness_to_base_type(
matrix_desc(intr->src[2]).element_type, signed_mask & NIR_CMAT_B_SIGNED);
enum glsl_base_type accum = glsl_apply_signedness_to_base_type(
matrix_desc(intr->src[3]).element_type, signed_mask & NIR_CMAT_C_SIGNED);
enum glsl_base_type result = glsl_apply_signedness_to_base_type(
matrix_desc(intr->src[0]).element_type, signed_mask & NIR_CMAT_RESULT_SIGNED);
const unsigned rows = matrix_desc(intr->src[1]).rows;
if (input == GLSL_TYPE_INT8 || input == GLSL_TYPE_UINT8) {
assert(rows == 16 &&
(input_b == GLSL_TYPE_INT8 || input_b == GLSL_TYPE_UINT8) &&
(accum == GLSL_TYPE_INT || accum == GLSL_TYPE_UINT) &&
result == accum &&
(!nir_intrinsic_saturate(intr) || accum == GLSL_TYPE_INT));
nir_def *a = pack_8x8_to_2x32(b, load_matrix(b, intr->src[1]));
nir_def *bb = pack_8x8_to_2x32(b, load_matrix(b, intr->src[2]));
nir_def *c = load_matrix(b, intr->src[3]);
nir_def *value = nir_cmat_muladd_agx(
b, a, bb, c,
.src_type = input == GLSL_TYPE_INT8 ? nir_type_int8 : nir_type_uint8,
.src_type2 = input_b == GLSL_TYPE_INT8 ? nir_type_int8 : nir_type_uint8,
.saturate = nir_intrinsic_saturate(intr));
store_matrix(b, intr->src[0], value);
break;
}
assert(!nir_intrinsic_saturate(intr));
/* 8x8 matmad has a single input-type field, so mixed A/B types
* cannot be encoded. f16 and bf16 both widen to f32 exactly;
* promote both operands to f32 and use that form. 16x16 gemm
Expand Down Expand Up @@ -545,14 +594,19 @@ lower_matrix_instr(nir_builder *b, nir_instr *instr, struct hash_table *types,
/* Convert where the elements are narrower */
bool convert_first = glsl_base_type_bit_size(dst.element_type) <
glsl_base_type_bit_size(src.element_type);
const nir_cmat_signed signed_mask = nir_intrinsic_cmat_signed_mask(intr);
enum glsl_base_type src_type = glsl_apply_signedness_to_base_type(
src.element_type, signed_mask & NIR_CMAT_A_SIGNED);
enum glsl_base_type dst_type = glsl_apply_signedness_to_base_type(
dst.element_type, signed_mask & NIR_CMAT_RESULT_SIGNED);
b->fp_math_ctrl = nir_intrinsic_fp_math_ctrl(intr);
nir_def *value = load_matrix(b, intr->src[1]);
if (convert_first)
value = convert_matrix_value(b, value, src.element_type, dst.element_type);
value = convert_matrix_value(b, value, src_type, dst_type);
if (move)
value = permute_matrix(b, value, from, to);
if (!convert_first)
value = convert_matrix_value(b, value, src.element_type, dst.element_type);
value = convert_matrix_value(b, value, src_type, dst_type);
store_matrix(b, intr->src[0], value);
b->fp_math_ctrl = nir_fp_fast_math;
break;
Expand Down
14 changes: 10 additions & 4 deletions src/asahi/compiler/agx_register_allocate.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "asahi/lib/agx_chip_caps.h"
#include "agx3_compute_program.h"
#include "agx3_copy_core.h"
#include "agx3_matrix_core.h"
#include "agx_builder.h"
#include "agx_compile.h"
#include "agx_compiler.h"
Expand Down Expand Up @@ -4516,19 +4517,24 @@ agx_ra(agx_context *ctx)
continue;

agx_foreach_ssa_src(I, s) {
const unsigned src_comps =
(I->op == AGX_OPCODE_MATMAD16_AGX3 && s < 2 &&
agx3_matrix_input_is_int8(I->matrix_type & 15))
? 2
: mat_comps;
/* The accumulator is a copy (agx_lower_agx3_matrix_sources) */
agx_instr *def = defs[I->src[s].value];
if (def && def->op == AGX_OPCODE_MOV &&
def->src[0].type == AGX_INDEX_NORMAL)
def = defs[def->src[0].value];

if (!def || def->op != AGX_OPCODE_COLLECT ||
def->nr_srcs != mat_comps)
def->nr_srcs != src_comps)
continue;

struct matrix_group group = {0};
bool valid = true;
for (unsigned k = 0; k < mat_comps && valid; ++k) {
for (unsigned k = 0; k < src_comps && valid; ++k) {
const agx_index src = def->src[k];
valid = src.type == AGX_INDEX_NORMAL && !src.memory &&
agx_channels(src) == 1 &&
Expand All @@ -4546,15 +4552,15 @@ agx_ra(agx_context *ctx)
continue;

group.size = agx_size_align_16(def->src[0].size);
group.count = mat_comps;
group.count = src_comps;
util_dynarray_append(&matrix_groups, group);
const unsigned id = util_dynarray_num_elements(
&matrix_groups, struct matrix_group);
if (id > UINT16_MAX) {
(void)util_dynarray_pop(&matrix_groups, struct matrix_group);
continue;
}
for (unsigned k = 0; k < mat_comps; ++k)
for (unsigned k = 0; k < src_comps; ++k)
web_matrix_group[group.root[k]] = id;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/asahi/compiler/agx_validate.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ agx_read_registers(const agx_context *ctx, const agx_instr *I, unsigned s)
return 2 * size;

case AGX_OPCODE_MATMAD16_AGX3:
return s == 2 ? 16 : 8;
return s == 2 ? 16 : (agx3_matrix_input_is_int8(I->matrix_type & 15) ? 4 : 8);
case AGX_OPCODE_MATMAD8_AGX3:
if (I->matrix_type & AGX3_MATRIX_ACCUM_F16)
return 2;
Expand Down
36 changes: 36 additions & 0 deletions src/asahi/compiler/test/test-native-form-regressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5634,6 +5634,42 @@ TEST(NativeForms, M5GemmMixedOperandExecutionWitnesses)
}
}

TEST(NativeForms, M5IGemm16x16x16EncodingAndAlignment)
{
struct {
agx3_matrix_input_type a, b;
bool sat;
uint8_t expected[10];
} cases[] = {
{AGX3_MATRIX_INPUT_U8, AGX3_MATRIX_INPUT_U8, false, {0xa7,0x30,0x25,0x01,0x22,0x02,0xa1,0x02,0x00,0x02}},
{AGX3_MATRIX_INPUT_S8, AGX3_MATRIX_INPUT_U8, false, {0xa7,0x30,0x25,0x01,0x22,0x02,0xa1,0x02,0x40,0x02}},
{AGX3_MATRIX_INPUT_U8, AGX3_MATRIX_INPUT_S8, false, {0xa7,0x30,0x25,0x01,0x22,0x0a,0xa1,0x02,0x00,0x02}},
{AGX3_MATRIX_INPUT_S8, AGX3_MATRIX_INPUT_S8, false, {0xa7,0x30,0x25,0x01,0x22,0x0a,0xa1,0x02,0x40,0x02}},
{AGX3_MATRIX_INPUT_U8, AGX3_MATRIX_INPUT_U8, true, {0xa7,0x30,0x25,0x01,0x22,0x02,0xa1,0x02,0x10,0x02}},
{AGX3_MATRIX_INPUT_S8, AGX3_MATRIX_INPUT_S8, true, {0xa7,0x30,0x25,0x01,0x22,0x0a,0xa1,0x02,0x50,0x02}},
};
for (const auto &c : cases) {
agx3_matrix_gemm16_desc d = {};
d.dest = 16; d.a = 32; d.b = 8; d.wait_mask = 0xc;
d.input_type = c.a; d.b_input_type = c.b; d.saturate = c.sat;
uint8_t bytes[10], length;
ASSERT_TRUE(agx3_matrix_encode_gemm16(AGX_ISA_ENCODING_AGX3C, &d, bytes, &length));
ASSERT_EQ(length, 10u);
EXPECT_EQ(std::vector<uint8_t>(bytes, bytes + length), std::vector<uint8_t>(c.expected, c.expected + 10));
}

/* 4-half-reg aligned A/B accepted; 2-half-reg aligned or float/int mix rejected */
agx3_matrix_gemm16_desc d = {};
d.dest = 16; d.a = 36; d.b = 12; d.input_type = AGX3_MATRIX_INPUT_S8; d.b_input_type = AGX3_MATRIX_INPUT_S8;
uint8_t bytes[10], length;
EXPECT_TRUE(agx3_matrix_encode_gemm16(AGX_ISA_ENCODING_AGX3C, &d, bytes, &length));
EXPECT_FALSE(agx3_matrix_encode_gemm16(AGX_ISA_ENCODING_AGX3B, &d, bytes, &length));
d.a = 34;
EXPECT_FALSE(agx3_matrix_encode_gemm16(AGX_ISA_ENCODING_AGX3C, &d, bytes, &length));
d.a = 36; d.b_input_type = AGX3_MATRIX_INPUT_F16;
EXPECT_FALSE(agx3_matrix_encode_gemm16(AGX_ISA_ENCODING_AGX3C, &d, bytes, &length));
}

TEST(NativeForms, M5VectorAccumulatorCopiesEveryComponent)
{
for (unsigned channels : {2u, 4u, 8u}) {
Expand Down
Loading