Skip to content
Merged
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
75 changes: 75 additions & 0 deletions src/asahi/compiler/agx_nir_lower_cmat.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,38 @@ matrix_memory_at(nir_builder *b, nir_intrinsic_instr *intr, unsigned element,
b, ptr, nir_u2uN(b, nir_iadd_imm(b, col, skip), bits));
}

/* Whether every row of the matrix memory of a load starts `bytes` aligned.
* Rows are a whole number of elements of the pointer type apart
* (matrix_memory_at), so this holds when that type is a multiple of `bytes`
* and so are the strides of the arrays it is in, up to a pointer cast that
* is as aligned, or up to a shared variable, which this then aligns. Other
* variables, and members of structures, may not be.
*/
static bool
matrix_rows_aligned(nir_shader *nir, nir_intrinsic_instr *intr, unsigned bytes)
{
nir_deref_instr *ptr = nir_src_as_deref(intr->src[1]);
if (!glsl_type_is_vector_or_scalar(ptr->type) ||
(glsl_get_vector_elements(ptr->type) * glsl_get_bit_size(ptr->type) / 8) %
bytes)
return false;
while (ptr->deref_type == nir_deref_type_array ||
ptr->deref_type == nir_deref_type_ptr_as_array) {
if (nir_deref_instr_array_stride(ptr) % bytes)
return false;
ptr = nir_deref_instr_parent(ptr);
}
if (ptr->deref_type == nir_deref_type_cast)
return ptr->cast.align_mul >= bytes && ptr->cast.align_offset % bytes == 0;
if (ptr->deref_type != nir_deref_type_var ||
ptr->var->data.mode != nir_var_mem_shared ||
nir->info.shared_memory_explicit_layout ||
glsl_type_is_struct_or_ifc(glsl_without_array(ptr->var->type)))
return false;
ptr->var->data.alignment = MAX2(ptr->var->data.alignment, bytes);
return true;
}

static nir_deref_instr *
matrix_memory(nir_builder *b, nir_intrinsic_instr *intr, unsigned element)
{
Expand Down Expand Up @@ -421,6 +453,49 @@ lower_matrix_instr(nir_builder *b, nir_instr *instr, struct hash_table *types,
: (!row_major ? 2 : desc.use == GLSL_CMAT_USE_A ? 1 : 4);
unsigned component_step = desc.rows == 16 && !row_major ? 4 : 1;
unsigned component_mask = (width - 1) * component_step;
/* Where rows start 4-byte aligned, the seven columns a lane reads of a
* row (below) are among the eight elements of four 32-bit words that
* start at its first column rounded down to even: one load, which reads
* no column outside the lane's eight-column half of the row
* (matrix_col). The lane's elements are then the low halves of the
* words, or the high halves where its first column is odd, already in
* the order of the operand registers: half the loads of the 16-bit
* loads below.
*/
if (!store && desc.rows == 16 && row_major &&
desc.use == GLSL_CMAT_USE_A &&
glsl_base_type_bit_size(desc.element_type) == 16 &&
matrix_rows_aligned(b->shader, intr, 4)) {
nir_def *lane = nir_load_subgroup_invocation(b);
for (unsigned i = 0; i < count; i += 4) {
nir_deref_instr *first = matrix_memory(b, intr, i);
nir_def *col = matrix_col(b, lane, desc, i);
/* Index the row in words, so the byte offset is a multiple of
* the word size term by term: the shared offset then divides
* into a word index exactly, and the constant offsets of the
* other rows and steps stay foldable into the load.
*/
nir_deref_instr *row = nir_deref_instr_parent(first);
nir_deref_instr *ptr = nir_build_deref_cast_with_alignment(
b, &row->def, row->modes, glsl_uint_type(), 4, 4, 0);
ptr = nir_build_deref_ptr_as_array(
b, ptr, nir_u2uN(b, nir_ushr_imm(b, col, 1), row->def.bit_size));
ptr = nir_build_deref_cast_with_alignment(
b, &ptr->def, ptr->modes, glsl_vector_type(GLSL_TYPE_UINT, 4),
0, 4, 0);
nir_def *words = nir_load_deref(b, ptr);
/* A select of a half per element, never a shift of the word. */
nir_def *odd = nir_ine_imm(b, nir_iand_imm(b, col, 1), 0);
for (unsigned e = 0; e < 4; ++e) {
nir_def *word = nir_channel(b, words, e);
values[i + e] = nir_bcsel(b, odd,
nir_unpack_32_2x16_split_y(b, word),
nir_unpack_32_2x16_split_x(b, word));
}
}
store_matrix(b, intr->src[0], nir_vec(b, values, count));
break;
}
/* A lane's four elements of a row of a 16x16 A operand are every other
* one of seven consecutive columns (matrix_col), so they are in two
* four-element loads that start at the first and at the fourth of the
Expand Down