Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
59 changes: 59 additions & 0 deletions emulation/rocjitsu/lib/python/amdisa/codegen/_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10393,6 +10393,65 @@ def gen_insts(self) -> None:
f'<< "{inst.name} requires its GLOBAL segment";'
)

# Public LLVM defines gfx1251's F64 WMMA as a wave32
# v16x16 destination/accumulator tuple and v4 A/B tuples.
# The low OPSEL bits and NEG_HI A/B bits are reserved for
# this profile; OPSEL[2]/OPSEL_HI_2 are the only matrix
# reuse hints. VISrc_512_f64 admits the standard integer
# and FP64 inline-selector ranges.
if inst.name == 'V_WMMA_F64_16X16X4_F64':
raw_inst = (
f'reinterpret_cast<const {factory_op_encoding}*>(inst)'
)
factory_validation_parts.append(
f'if (({raw_inst}->opsel & 0x3u) != 0u || '
f'{raw_inst}->opsel_hi != 3u) '
f'[[unlikely]] return emit_error.emit() << "{inst.name} has an invalid '
'F64 WMMA element layout";'
)
factory_validation_parts.append(
f'if ({raw_inst}->clamp != 0u || '
f'({raw_inst}->neg_hi & 0x3u) != 0u) '
f'[[unlikely]] return emit_error.emit() << "{inst.name} has unsupported '
'modifier bits";'
)
factory_validation_parts.append(
f'if ({raw_inst}->vdst > 240u) '
f'[[unlikely]] return emit_error.emit() << "{inst.name} has a vdst '
'register tuple that exceeds the selector range";'
)
factory_validation_parts.append(
f'if (({raw_inst}->vdst & 1u) != 0u) '
f'[[unlikely]] return emit_error.emit() << "{inst.name} has an invalid '
'vdst register tuple alignment";'
)
for src_name in ('src0', 'src1'):
factory_validation_parts.append(
f'if ({raw_inst}->{src_name} > 508u) '
f'[[unlikely]] return emit_error.emit() << "{inst.name} has a '
f'{src_name} register tuple that exceeds the selector range";'
)
factory_validation_parts.append(
f'if (({raw_inst}->{src_name} & 1u) != 0u) '
f'[[unlikely]] return emit_error.emit() << "{inst.name} has an invalid '
f'{src_name} register tuple alignment";'
)
valid_inline = (
f'({raw_inst}->src2 >= 128u && {raw_inst}->src2 <= 208u) || '
f'({raw_inst}->src2 >= 240u && {raw_inst}->src2 <= 248u)'
)
factory_validation_parts.append(
f'if (!({valid_inline}) && '
f'({raw_inst}->src2 < 256u || {raw_inst}->src2 > 496u)) '
f'[[unlikely]] return emit_error.emit() << "{inst.name} requires a '
'legal inline accumulator or 16-register VGPR tuple";'
)
factory_validation_parts.append(
f'if (!({valid_inline}) && ({raw_inst}->src2 & 1u) != 0u) '
f'[[unlikely]] return emit_error.emit() << "{inst.name} has an invalid '
'src2 register tuple alignment";'
)

# LLVM's public gfx1251 profiles define the packed U64 and
# F64 arithmetic operations without op_sel. The profile and
# corresponding MC vectors are permanently linked here:
Expand Down
20 changes: 17 additions & 3 deletions emulation/rocjitsu/lib/python/amdisa/codegen/execute/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,13 @@ def gen_mfma(ctx: ExecuteContext) -> str:
and ctx.profile.wave_size < ctx.profile.wave_size_max
and is_dense_wmma
)
is_gfx1251_f64_wmma = (
arch == 'cdna5'
and is_dense_wmma
and result_type == 'F64'
and input_type == 'F64'
and (M, N, K) == (16, 16, 4)
)
swmmac_index_entries = 32 if is_swmmac and K >= 128 and in_bits <= 8 else 16
if ctx.profile.uses_vgpr_msb_indexing:
L.append(
Expand Down Expand Up @@ -358,7 +365,8 @@ def gen_mfma(ctx: ExecuteContext) -> str:
index_base_expr = 'index_base'
index_key_expr = 'index_key'
else:
L.append(f' uint32_t const_acc;')
const_acc_type = 'uint64_t' if is_gfx1251_f64_wmma else 'uint32_t'
L.append(f' {const_acc_type} const_acc;')
L.append(
f' auto src2_off = Isa::resolved_vgpr_offset(wf, {s2}.opr_type_, '
f'{s2}.encoding_value_, {s2}.vgpr_msb_role());'
Expand All @@ -368,7 +376,8 @@ def gen_mfma(ctx: ExecuteContext) -> str:
L.append(f' const_acc = amdgpu::ACC_FROM_VGPR;')
L.append(f' s2 = vb + *src2_off;')
L.append(f' }} else {{')
L.append(f' const_acc = amdgpu::RegisterAccess(wf).read_scalar({s2});')
read_acc = 'read_scalar64' if is_gfx1251_f64_wmma else 'read_scalar'
L.append(f' const_acc = amdgpu::RegisterAccess(wf).{read_acc}({s2});')
L.append(f' }}')
else:
# ACC_CD selects VGPRs or AccVGPRs for the C and D matrices. Encodings
Expand Down Expand Up @@ -414,7 +423,12 @@ def gen_mfma(ctx: ExecuteContext) -> str:
'wf, vb, s2, amdgpu::VgprMsbRole::Src2);'
)

if result_type == 'F64':
if is_gfx1251_f64_wmma:
L.append(f' amdgpu::exec_wmma_f64_16x16x4_f64(cu, dst,')
L.append(
f' src0_base, src1_base, s2, const_acc, inst_.neg, inst_.neg_hi, wf.exec());'
)
elif result_type == 'F64':
L.append(f' amdgpu::exec_f64(cu, {M}, {N}, {K}, {B}, dst,')
L.append(f' {src0_base_expr},')
L.append(f' {src1_base_expr},')
Expand Down
4 changes: 2 additions & 2 deletions emulation/rocjitsu/lib/python/amdisa/semantics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1707,8 +1707,8 @@ def _derive_vop3p(name: str) -> InstructionSemantics | None:
import re

m = re.match(
r'V_(?:S?WMMA[C]?)_(F32|F16|BF16|BF16F32|I32|FP8|BF8)_'
r'(\d+)X(\d+)X(\d+)_?(F32|F16|BF16|IU8|IU4|FP8|BF8'
r'V_(?:S?WMMA[C]?)_(F32|F64|F16|BF16|BF16F32|I32|FP8|BF8)_'
r'(\d+)X(\d+)X(\d+)_?(F32|F64|F16|BF16|IU8|IU4|FP8|BF8'
r'|FP8_FP8|FP8_BF8|BF8_FP8|BF8_BF8'
r'|F16_FP8|F16_BF8|BF16_FP8|BF16_BF8|F8F6F4|F4)?$',
name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2888,6 +2888,26 @@ def test_cdna_f64_mfma_uses_blgp_as_neg_immediate():
assert 's2, const_acc, 0u);' in body


def test_gfx1251_f64_wmma_uses_wave32_mode_independent_executor():
operands = [
Operand('vdst', 512, 'OPR_VGPR', False, True, False, False, 0),
Operand('src0', 128, 'OPR_SRC_VGPR', True, False, False, False, 1),
Operand('src1', 128, 'OPR_SRC_VGPR', True, False, False, False, 2),
Operand('src2', 512, 'OPR_SRC_VGPR_OR_INLINE', True, False, False, False, 3),
]
inst = Instruction('V_WMMA_F64_16X16X4_F64', 'ENC_VOP3P', 0, operands)

body = gen_mfma(inst, ['vdst'], ['src0', 'src1', 'src2'], 'cdna5')

assert 'amdgpu::exec_wmma_f64_16x16x4_f64(cu, dst,' in body
assert 'inst_.neg, inst_.neg_hi' in body
assert 'amdgpu::RegisterAccess(wf).read_scalar64(src2)' in body
assert 'wf.fp_round_mode_f16_f64()' not in body
assert 'wf.fp_denorm_mode_f16_f64()' not in body
assert 'wf.exec()' in body
assert 'amdgpu::exec_f64' not in body


@pytest.mark.parametrize('dtype, mode', [('f32', 'f32'), ('f64', 'f16_f64')])
def test_div_scale_delegates_classification_and_preserves_explicit_mask(dtype, mode):
body = gen_vector_div_scale(
Expand Down Expand Up @@ -5339,6 +5359,29 @@ def test_gfx1251_packed_f64_fma_rejects_undefined_layouts_and_register_tuples(
assert f'{operand_name} <= 510u' in body


def test_gfx1251_f64_wmma_validates_fields_sources_and_register_tuples(
gfx1250_generated_root: Path,
):
source = (gfx1250_generated_root / 'vop3p.cpp').read_text()
body = _generated_decode_body(source, 'VWmmaF6416x16x4F64Vop3p')

assert 'has an invalid F64 WMMA element layout' in body
assert 'has unsupported modifier bits' in body
assert 'vdst register tuple that exceeds the selector range' in body
assert 'invalid vdst register tuple alignment' in body
assert 'src0 register tuple that exceeds the selector range' in body
assert 'invalid src0 register tuple alignment' in body
assert 'src1 register tuple that exceeds the selector range' in body
assert 'invalid src1 register tuple alignment' in body
assert 'requires a legal inline accumulator or ' in body
assert '16-register VGPR tuple' in body
assert 'src2 >= 128u &&' in body
assert 'src2 <= 208u' in body
assert 'src2 >= 240u &&' in body
assert 'src2 <= 248u' in body
assert 'invalid src2 register tuple alignment' in body


def test_gfx1251_packed_f64_literals_use_f64_high_bits_widening(
gfx1250_generated_root: Path,
):
Expand Down Expand Up @@ -5441,24 +5484,12 @@ def test_split_execution_ids_name_and_match_callbacks(
def test_cdna5_variant_execution_callback_inventory(
gfx1250_generated_root: Path,
) -> None:
model_only_classes = ('VWmmaF6416x16x4F64Vop3p',)
header = (gfx1250_generated_root / 'vop3p.h').read_text()
model = (gfx1250_generated_root / 'vop3p.cpp').read_text()
backend_header = (gfx1250_generated_root / 'execution_backend.h').read_text()
backend_source = (gfx1250_generated_root / 'execution_backend_exec.cpp').read_text()
execution_source = (gfx1250_generated_root / 'vop3p_exec.cpp').read_text()

for class_name in model_only_classes:
class_body = header.split(f'class {class_name} ', 1)[1].split('\n};', 1)[0]
assert 'execute_impl' not in class_body
constructor = model.split(f'{class_name}::{class_name}(', 1)[1].split('\n}', 1)[
0
]
assert 'nullptr' in constructor
assert class_name not in backend_header
assert class_name not in backend_source
assert class_name not in execution_source

executable_classes = (
'VPkFmaF64Vop3p',
'VPkMulF64Vop3p',
Expand All @@ -5468,6 +5499,7 @@ def test_cdna5_variant_execution_callback_inventory(
'VPkMaxNumF64Vop3p',
'VPkMinNumF64Vop3p',
'VPkLshlAddU64Vop3p',
'VWmmaF6416x16x4F64Vop3p',
)
for executable_class in executable_classes:
class_body = header.split(f'class {executable_class} ', 1)[1].split('\n};', 1)[
Expand Down Expand Up @@ -5506,6 +5538,8 @@ def test_cdna5_variant_execution_callback_inventory(
assert 'if (reg && reg->cls == RegClass::SGPR)' in u32_read_helper
assert 'read_lane(operand, lane)' in u32_read_helper
assert 'read_lane_pair32(operand, lane)' in u32_read_helper
assert 'is_general_register' not in u64_read_helper
assert 'exec_wmma_f64_16x16x4_f64' in execution_source


def test_generated_vop_execution_has_no_instruction_storage_bypass(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2774,13 +2774,14 @@ def test_mfma(self):
@pytest.mark.parametrize(
'name',
[
'V_WMMA_F64_16X16X4_F64',
'V_WMMA_BF16F32_16X16X32_BF16',
'V_WMMA_F32_16X16X128_F8F6F4',
'V_WMMA_F32_32X16X128_F4',
'V_SWMMAC_BF16F32_16X16X64_BF16',
],
)
def test_gfx1250_low_precision_wmma_derives_mfma(self, name):
def test_cdna5_wmma_profiles_derive_mfma(self, name):
sem = derive_semantics(name, 'ENC_VOP3P')
assert sem is not None
assert sem.semantic_class == 'mfma'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,7 @@ enum class InstructionExecutionId : size_t {
VPkMaxNumF64Vop3p,
VPkMinNumF64Vop3p,
VPkLshlAddU64Vop3p,
VWmmaF6416x16x4F64Vop3p,
VWmmaScaleF32Vop3px2,
DsAddU32Vds,
DsSubU32Vds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1284,6 +1284,7 @@ constexpr InstructionCallbackTable kInstructionCallbacks{{
&execute_with_backend<VPkMaxNumF64Vop3p>,
&execute_with_backend<VPkMinNumF64Vop3p>,
&execute_with_backend<VPkLshlAddU64Vop3p>,
&execute_with_backend<VWmmaF6416x16x4F64Vop3p>,
&execute_with_backend<VWmmaScaleF32Vop3px2>,
&execute_with_backend<DsAddU32Vds>,
&execute_with_backend<DsSubU32Vds>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5131,7 +5131,8 @@ DecodeResult decodeVPkLshlAddU64Vop3p(const MachineInst *opcode,
} // namespace detail

VWmmaF6416x16x4F64Vop3p::VWmmaF6416x16x4F64Vop3p(const MachineInst *inst)
: Vop3p("v_wmma_f64_16x16x4_f64", reinterpret_cast<const OpEncoding *>(inst), nullptr),
: Vop3p("v_wmma_f64_16x16x4_f64", reinterpret_cast<const OpEncoding *>(inst),
selected_exec_fn(InstructionExecutionId::VWmmaF6416x16x4F64Vop3p)),
vdst(512, OperandType::OPR_VGPR, reinterpret_cast<const OpEncoding *>(inst)->vdst),
src0(128, OperandType::OPR_SRC_VGPR, reinterpret_cast<const OpEncoding *>(inst)->src0),
src1(128, OperandType::OPR_SRC_VGPR, reinterpret_cast<const OpEncoding *>(inst)->src1),
Expand Down Expand Up @@ -5159,6 +5160,45 @@ DecodeResult decodeVWmmaF6416x16x4F64Vop3p(const MachineInst *opcode,
emit_error, LiteralSupport::None);
if (validation.failed()) [[unlikely]]
return Result::failure();
if ((reinterpret_cast<const Vop3p::OpEncoding *>(inst)->opsel & 0x3u) != 0u ||
reinterpret_cast<const Vop3p::OpEncoding *>(inst)->opsel_hi != 3u) [[unlikely]]
return emit_error.emit() << "V_WMMA_F64_16X16X4_F64 has an invalid F64 WMMA element layout";
if (reinterpret_cast<const Vop3p::OpEncoding *>(inst)->clamp != 0u ||
(reinterpret_cast<const Vop3p::OpEncoding *>(inst)->neg_hi & 0x3u) != 0u) [[unlikely]]
return emit_error.emit() << "V_WMMA_F64_16X16X4_F64 has unsupported modifier bits";
if (reinterpret_cast<const Vop3p::OpEncoding *>(inst)->vdst > 240u) [[unlikely]]
return emit_error.emit()
<< "V_WMMA_F64_16X16X4_F64 has a vdst register tuple that exceeds the selector range";
if ((reinterpret_cast<const Vop3p::OpEncoding *>(inst)->vdst & 1u) != 0u) [[unlikely]]
return emit_error.emit()
<< "V_WMMA_F64_16X16X4_F64 has an invalid vdst register tuple alignment";
if (reinterpret_cast<const Vop3p::OpEncoding *>(inst)->src0 > 508u) [[unlikely]]
return emit_error.emit()
<< "V_WMMA_F64_16X16X4_F64 has a src0 register tuple that exceeds the selector range";
if ((reinterpret_cast<const Vop3p::OpEncoding *>(inst)->src0 & 1u) != 0u) [[unlikely]]
return emit_error.emit()
<< "V_WMMA_F64_16X16X4_F64 has an invalid src0 register tuple alignment";
if (reinterpret_cast<const Vop3p::OpEncoding *>(inst)->src1 > 508u) [[unlikely]]
return emit_error.emit()
<< "V_WMMA_F64_16X16X4_F64 has a src1 register tuple that exceeds the selector range";
if ((reinterpret_cast<const Vop3p::OpEncoding *>(inst)->src1 & 1u) != 0u) [[unlikely]]
return emit_error.emit()
<< "V_WMMA_F64_16X16X4_F64 has an invalid src1 register tuple alignment";
if (!((reinterpret_cast<const Vop3p::OpEncoding *>(inst)->src2 >= 128u &&
reinterpret_cast<const Vop3p::OpEncoding *>(inst)->src2 <= 208u) ||
(reinterpret_cast<const Vop3p::OpEncoding *>(inst)->src2 >= 240u &&
reinterpret_cast<const Vop3p::OpEncoding *>(inst)->src2 <= 248u)) &&
(reinterpret_cast<const Vop3p::OpEncoding *>(inst)->src2 < 256u ||
reinterpret_cast<const Vop3p::OpEncoding *>(inst)->src2 > 496u)) [[unlikely]]
return emit_error.emit() << "V_WMMA_F64_16X16X4_F64 requires a legal inline accumulator or "
"16-register VGPR tuple";
if (!((reinterpret_cast<const Vop3p::OpEncoding *>(inst)->src2 >= 128u &&
reinterpret_cast<const Vop3p::OpEncoding *>(inst)->src2 <= 208u) ||
(reinterpret_cast<const Vop3p::OpEncoding *>(inst)->src2 >= 240u &&
reinterpret_cast<const Vop3p::OpEncoding *>(inst)->src2 <= 248u)) &&
(reinterpret_cast<const Vop3p::OpEncoding *>(inst)->src2 & 1u) != 0u) [[unlikely]]
return emit_error.emit()
<< "V_WMMA_F64_16X16X4_F64 has an invalid src2 register tuple alignment";
if (reinterpret_cast<const Vop3p::OpEncoding *>(inst)->src0 == amdgpu::SRC_DPP ||
amdgpu::dpp::is_src_dpp8(reinterpret_cast<const Vop3p::OpEncoding *>(inst)->src0))
[[unlikely]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,7 @@ class VPkLshlAddU64Vop3p : public Vop3p {
class VWmmaF6416x16x4F64Vop3p : public Vop3p {
public:
VWmmaF6416x16x4F64Vop3p(const MachineInst *inst);
void execute_impl(amdgpu::Wavefront &wf);
Operand vdst;
Operand src0;
Operand src1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3150,6 +3150,29 @@ void VPkLshlAddU64Vop3p::execute_impl(amdgpu::Wavefront &wf) {
}
}

void VWmmaF6416x16x4F64Vop3p::execute_impl(amdgpu::Wavefront &wf) {
auto &cu = wf.cu();
uint32_t vb = wf.vgpr_alloc().base;
uint32_t dst = vb + *Isa::resolved_vgpr_offset(wf, vdst.opr_type_, vdst.encoding_value_,
vdst.vgpr_msb_role());
uint32_t src0_base = vb + *Isa::resolved_vgpr_offset(wf, src0.opr_type_, src0.encoding_value_,
src0.vgpr_msb_role());
uint32_t src1_base = vb + *Isa::resolved_vgpr_offset(wf, src1.opr_type_, src1.encoding_value_,
src1.vgpr_msb_role());
uint64_t const_acc;
auto src2_off =
Isa::resolved_vgpr_offset(wf, src2.opr_type_, src2.encoding_value_, src2.vgpr_msb_role());
uint32_t s2 = dst;
if (src2_off) {
const_acc = amdgpu::ACC_FROM_VGPR;
s2 = vb + *src2_off;
} else {
const_acc = amdgpu::RegisterAccess(wf).read_scalar64(src2);
}
amdgpu::exec_wmma_f64_16x16x4_f64(cu, dst, src0_base, src1_base, s2, const_acc, inst_.neg,
inst_.neg_hi, wf.exec());
}

void VWmmaScaleF32Vop3px2::execute_impl(amdgpu::Wavefront &wf) {
auto &cu = wf.cu();
uint32_t vb = wf.vgpr_alloc().base;
Expand Down
Loading
Loading