Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
2470f72
[rocjitsu] Add EXEC state tracking for liveness (PR #7470)
theSK2005 Jul 22, 2026
2c5125b
adjusted pytests that were failing after merge
theSK2005 Jul 23, 2026
293c1d7
fixed exec state tracking distinguishing between exec_lo vs exec_hi
theSK2005 Jul 23, 2026
073ed47
addressed Jakub's comments, exec_hi badly narrowing on Wave32 and som…
theSK2005 Jul 29, 2026
ec96c3f
pre-commit fix
theSK2005 Jul 29, 2026
4d297fa
fixing liveness_test after rebase
theSK2005 Jul 29, 2026
d73c1b3
addressed Tony's comments, allow kernel entry to be an explicit param…
theSK2005 Jul 30, 2026
947a147
handing kernel entries to BasicBlock::build so that kernel entries al…
theSK2005 Jul 31, 2026
6d51887
passing kernarg_preload_firmware_entry_text_offset as a kernel entry …
theSK2005 Jul 31, 2026
18f9271
Merge remote-tracking branch 'origin/develop' into users/skushwah/exe…
theSK2005 Jul 31, 2026
01ceed3
addrsesed Jakub's comments, regression test fails instead of skips, e…
theSK2005 Aug 3, 2026
0c4d924
add translate() preload fixture that shows firmware-entry vector def …
theSK2005 Aug 3, 2026
9831e5b
Merge remote-tracking branch 'origin/develop' into users/skushwah/exe…
theSK2005 Aug 3, 2026
4846f99
pre-commit fix
theSK2005 Aug 3, 2026
8a28f62
Merge branch 'develop' into users/skushwah/exec-def-kill
theSK2005 Aug 4, 2026
513c4f8
Merge branch 'develop' into users/skushwah/exec-def-kill
theSK2005 Aug 4, 2026
df118ed
made EXEC state tracking toggleable
theSK2005 Aug 5, 2026
206d791
added EXEC state optimizations and clarified what is preventing DBT f…
theSK2005 Aug 5, 2026
6db4ad7
added reference to github issue
theSK2005 Aug 5, 2026
f7d6397
pre-commit fixes
theSK2005 Aug 5, 2026
4cc3f37
added Jakub's perf improvements
theSK2005 Aug 6, 2026
fb2e77a
Merge branch 'develop' into users/skushwah/exec-def-kill
theSK2005 Aug 6, 2026
0370672
Merge branch 'develop' into users/skushwah/exec-def-kill
theSK2005 Aug 6, 2026
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
471 changes: 183 additions & 288 deletions emulation/rocjitsu/lib/python/amdisa/codegen/_generator.py

Large diffs are not rendered by default.

180 changes: 180 additions & 0 deletions emulation/rocjitsu/lib/python/amdisa/tests/test_exec_mask_flags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
# Copyright (c) 2025-2026 Advanced Micro Devices, Inc.
# SPDX-License-Identifier: MIT

"""Tests for EXEC-mask instruction flag emission."""

import pytest

from amdisa.codegen._generator import (
_exec_mask_flag_stmts,
_result_combinator_flag_stmts,
)
from amdisa.semantics import InstructionSemantics

# Flag names emitted for EXEC tracking.
ALL = {'IGNORES_EXEC', 'WRITES_EXEC'}


def _flags(sem):
"""Run the generator helper and return the set of emitted flag names."""
return {s[len('flags_ |= ') : -1] for s in _exec_mask_flag_stmts(sem)}


# (InstructionSemantics, expected-present flags, id). Anything in ALL but not in
# the expected set must be ABSENT — see the test below.
_CASES = [
# Scalar EXEC writers: write EXEC, but are not themselves EXEC-masked.
pytest.param(
InstructionSemantics(
'S_AND_SAVEEXEC_B64',
'scalar_saveexec',
operation='and',
data_type='b64',
sets_scc='nonzero',
),
{'WRITES_EXEC'},
id='scalar_saveexec',
),
pytest.param(
InstructionSemantics(
'S_ANDN2_WREXEC_B64',
'scalar_wrexec',
operation='andn2',
data_type='b64',
sets_scc='nonzero',
),
{'WRITES_EXEC'},
id='scalar_wrexec',
),
# Vector ALU: no EXEC interaction flags.
pytest.param(
InstructionSemantics(
'V_ADD_F32', 'vector_binop', operation='add', data_type='f32'
),
set(),
id='vector_binop',
),
pytest.param(
InstructionSemantics('V_MOV_B32', 'vector_mov', data_type='b32'),
set(),
id='vector_mov',
),
pytest.param(
InstructionSemantics('V_CNDMASK_B32', 'vector_cndmask', data_type='b32'),
set(),
id='vector_cndmask',
),
# Vector compare-and-set-exec: writes EXEC.
pytest.param(
InstructionSemantics(
'V_CMPX_LT_F32', 'vector_cmpx', operation='lt', data_type='f32'
),
{'WRITES_EXEC'},
id='vector_cmpx',
),
# Branches: ignore EXEC, never EXEC-masked.
pytest.param(
InstructionSemantics('S_BRANCH', 'branch'),
{'IGNORES_EXEC'},
id='branch',
),
pytest.param(
InstructionSemantics('S_CBRANCH_SCC1', 'cbranch', branch_condition='scc1'),
{'IGNORES_EXEC'},
id='cbranch',
),
# Plain scalar op: no EXEC interaction at all.
pytest.param(
InstructionSemantics('S_MOV_B32', 'scalar_mov', data_type='b32'),
set(),
id='scalar_mov',
),
]


class TestExecMaskFlagStmts:
def test_none_semantics_yields_no_flags(self):
assert _exec_mask_flag_stmts(None) == []

def test_unsupported_class_yields_no_flags(self):
# A semantic class with no registered deriver -> derive_sema_block
# returns None -> conservatively no flags.
sem = InstructionSemantics('NOT_A_REAL_INST', 'no_such_class')
assert _exec_mask_flag_stmts(sem) == []

@pytest.mark.parametrize('sem, expected', _CASES)
def test_flags_per_instruction_kind(self, sem, expected):
flags = _flags(sem)
assert expected <= flags, f'{sem.name}: missing {expected - flags}'
unexpected = (ALL - expected) & flags
assert not unexpected, f'{sem.name}: unexpected {unexpected}'

def test_flag_statement_format(self):
sem = InstructionSemantics(
'S_OR_SAVEEXEC_B64',
'scalar_saveexec',
operation='or',
data_type='b64',
sets_scc='nonzero',
)
stmts = _exec_mask_flag_stmts(sem)
assert stmts
for s in stmts:
assert s.startswith('flags_ |= ')
assert s.endswith(';')


class TestResultCombinatorFlagStmts:
Comment thread
accauble marked this conversation as resolved.
Resolved
"""RESULT_COPY / RESULT_OR drive EXEC-state all-ones reasoning."""

def _combinator(self, sem):
return {s[len('flags_ |= ') : -1] for s in _result_combinator_flag_stmts(sem)}

def test_scalar_mov_is_copy(self):
sem = InstructionSemantics('S_MOV_B64', 'scalar_mov', data_type='b64')
assert self._combinator(sem) == {'RESULT_COPY'}

def test_binop_or_is_or(self):
sem = InstructionSemantics(
'S_OR_B64', 'scalar_binop', operation='or', data_type='b64'
)
assert self._combinator(sem) == {'RESULT_OR'}

def test_saveexec_or_is_or(self):
sem = InstructionSemantics(
'S_OR_SAVEEXEC_B64', 'scalar_saveexec', operation='or', data_type='b64'
)
assert self._combinator(sem) == {'RESULT_OR'}

def test_saveexec_and_is_other(self):
# exec = exec & src -> not provably all-ones; no combinator flag.
sem = InstructionSemantics(
'S_AND_SAVEEXEC_B64', 'scalar_saveexec', operation='and', data_type='b64'
)
assert _result_combinator_flag_stmts(sem) == []

def test_binop_and_is_other(self):
sem = InstructionSemantics(
'S_AND_B64', 'scalar_binop', operation='and', data_type='b64'
)
assert _result_combinator_flag_stmts(sem) == []

def test_vector_mov_is_not_copy(self):
# RESULT_COPY is scalar-only; a vector mov must not emit it.
sem = InstructionSemantics('V_MOV_B32', 'vector_mov', data_type='b32')
assert _result_combinator_flag_stmts(sem) == []

def test_vector_binop_or_is_not_or(self):
# RESULT_OR is scalar-only; a vector bitwise-or must not emit it.
sem = InstructionSemantics(
'V_OR_B32', 'vector_binop', operation='or', data_type='b32'
)
assert _result_combinator_flag_stmts(sem) == []

def test_vector_cndmask_is_not_copy(self):
# A non-scalar select is not a plain copy.
sem = InstructionSemantics('V_CNDMASK_B32', 'vector_cndmask', data_type='b32')
assert _result_combinator_flag_stmts(sem) == []

def test_none_is_empty(self):
assert _result_combinator_flag_stmts(None) == []
Original file line number Diff line number Diff line change
Expand Up @@ -2974,21 +2974,42 @@ def test_gfx1250_buffer_u64_atomic_payload_width_uses_two_dwords():


def test_ev124_125_arch_gating_in_generated_operand(amdgpu_generated_root: Path):
rdna4_op = (amdgpu_generated_root / 'rdna4' / 'operand.cpp').read_text()
assert 'if (ev == 124)\n return 0u; // NULL' in rdna4_op
assert 'if (ev == 125)\n return wf.m0()' in rdna4_op

rdna3_op = (amdgpu_generated_root / 'rdna3' / 'operand.cpp').read_text()
assert 'if (ev == 124)\n return 0u; // NULL' in rdna3_op
assert 'if (ev == 125)\n return wf.m0()' in rdna3_op

rdna3_5_op = (amdgpu_generated_root / 'rdna3_5' / 'operand.cpp').read_text()
assert 'if (ev == 124)\n return 0u; // NULL' in rdna3_5_op
assert 'if (ev == 125)\n return wf.m0()' in rdna3_5_op

cdna4_op = (amdgpu_generated_root / 'cdna4' / 'operand.cpp').read_text()
assert 'if (ev == 124)\n return wf.m0()' in cdna4_op
assert 'ev == 125' not in cdna4_op.split('can_resolve_src_scalar')[1].split('}')[0]
# M0 is encoded as 125 on RDNA3+ (and gfx1250) and as 124 on the
# older RDNA1/2 and all CDNA arches. Verify the generated operand.cpp
# carries the correct kM0EncodingValue constant for each ISA.
expected_m0_encoding = {
'cdna1': 124,
'cdna2': 124,
'cdna3': 124,
'cdna4': 124,
'rdna1': 124,
'rdna2': 124,
'rdna3': 125,
'rdna3_5': 125,
'rdna4': 125,
'gfx1250': 125,
}
for arch, encoding in expected_m0_encoding.items():
# The kM0EncodingValue constant lives with the scalar-resolve code, which
# is emitted into operand.cpp for unsplit arches and into operand_exec.cpp
# for arches that split model/execution sources (e.g. gfx1250).
op = (amdgpu_generated_root / arch / 'operand.cpp').read_text()
exec_path = amdgpu_generated_root / arch / 'operand_exec.cpp'
if exec_path.exists():
op += exec_path.read_text()
assert (
f'constexpr int kM0EncodingValue = {encoding};' in op
), f'{arch}: expected kM0EncodingValue = {encoding}'

# The M0 resolution logic is shared (parameterized by m0_ev) in
# scalar_operand_resolve.h rather than emitted per-arch, so verify the gating
# there: encoding value 124 is the NULL slot when M0 is 125, and the operand
# matching the arch's M0 encoding reads M0.
shared_resolve = (
amdgpu_generated_root / 'shared' / 'scalar_operand_resolve.h'
).read_text()
assert 'if (m0_ev == 125 && ev == 124)\n return 0u; // NULL' in shared_resolve
assert 'if (ev == m0_ev)\n return wf.m0();' in shared_resolve


def test_cdna4_mfma_f8f6f4_accepts_standalone_and_prefixed_encodings(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,39 +167,6 @@ def test_isa_properties_codegen_uses_profile_values(tmp_path):
) in output


@pytest.mark.parametrize(
('arch', 'profile', 'raw_exec'),
[
('cdna3', CdnaProfile(), False),
('rdna4', Rdna4Profile(), True),
],
)
def test_operand_exec_register_access_is_wave32_gated(
tmp_path, arch, profile, raw_exec
):
generator = CodeGenerator(
SimpleNamespace(
arch_name=arch,
opnd_selectors=[],
operand_types=['OPR_SIMM16', 'OPR_SIMM32', 'OPR_VGPR'],
profile=profile,
),
str(tmp_path),
)

generator.gen_operand()
operand_cpp = (tmp_path / arch / 'operand.cpp').read_text()

assert 'return static_cast<uint32_t>(wf.exec());' in operand_cpp
assert 'wf.set_exec((wf.exec() & 0xFFFFFFFF00000000ULL) | val);' in operand_cpp
if raw_exec:
assert 'return static_cast<uint32_t>(wf.exec_raw() >> 32);' in operand_cpp
assert 'wf.set_exec_raw((wf.exec_raw() & 0x00000000FFFFFFFFULL)' in operand_cpp
else:
assert 'exec_raw' not in operand_cpp
assert 'set_exec_raw' not in operand_cpp


def test_gfx1250_operand_execution_backend_uses_separate_source(tmp_path):
generator = CodeGenerator(
SimpleNamespace(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

rj_add_object_library(rocjitsu_analysis
def_use_chain.cpp
exec_state.cpp
gfx1250_vgpr_msb.cpp
indirect_branch_discovery.cpp
liveness.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ namespace rocjitsu {

namespace {

[[nodiscard]] bool is_exec_masked_def(RegisterRef ref) {
// Checks if a RegisterRef class is a vector (VGPR/AccVGPR).
[[nodiscard]] bool is_vector_def(RegisterRef ref) {
return ref.cls == RegClass::VGPR || ref.cls == RegClass::ACC_VGPR;
}

Expand Down Expand Up @@ -55,37 +56,31 @@ void expand_operand_register(RegisterSet &set, const Instruction &inst, const Op
}

// Must-write with an unknown bank: expanding to all four tuples would falsely
// kill three the instruction does not write, so record NOTHING in the def set.
// This is only sound because such a def never contributes a liveness kill:
// control reaches here only for a VGPR ref (see the early return above), every
// VGPR def is exec-masked (is_exec_masked_def), and kill_defs() drops all VGPR
// kills once has_exec_masked_vector_def is set. If any of those change (a
// non-exec-masked VGPR def, or kill_defs no longer suppressing VGPR kills), an
// unknown-bank def would start over-killing and this must record the precise
// physical tuple instead. (An assert(is_exec_masked_def(ref)) here would be
// tautological — ref is already known to be a VGPR — so the invariant is
// documented rather than checked.)
}

void add_def(InstDefUse &du, const Instruction &inst, const Operand &operand, RegisterRef ref,
const Gfx1250VgprMsbAnalysis *vgpr_msb) {
expand_operand_register(du.defs, inst, operand, ref, vgpr_msb, OperandExpansionKind::Def);
if (is_exec_masked_def(ref))
du.has_exec_masked_vector_def = true;
// kill three tuples the instruction does not write (an over-kill, which is
// unsound for liveness), so record NOTHING in the def set instead. An
// unrecorded def can only cause an under-kill, which merely keeps a value live
// longer than necessary and is always a sound (conservative) approximation.
// Known-bank defs above record the precise physical tuple and kill as usual,
// subject to the EXEC-masking rules in kill_defs().
}

} // namespace

InstDefUse::InstDefUse(const Instruction &inst, const Gfx1250VgprMsbAnalysis *vgpr_msb) {
has_predicated_def = inst.flags() & PREDICATED_DEF;
const bool ignores_exec = inst.flags() & IGNORES_EXEC;
bool has_vector_def = false;

for (int i = 0; i < inst.num_dst_operands(); ++i) {
const auto *op = inst.dst_operand(i);
if (op == nullptr)
continue;
if (auto ref = op->to_register_ref())
add_def(*this, inst, *op, *ref, vgpr_msb);
if (auto ref = op->to_register_ref()) {
expand_operand_register(defs, inst, *op, *ref, vgpr_msb, OperandExpansionKind::Def);
has_vector_def |= is_vector_def(*ref);
}
}
has_exec_masked_vector_def = has_vector_def && !ignores_exec;
inst.implicit_defs(defs);

for (int i = 0; i < inst.num_src_operands(); ++i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ class InstDefUse {
/// @param inst Decoded instruction whose operands have stable lifetimes.
InstDefUse(const Instruction &inst, const Gfx1250VgprMsbAnalysis *vgpr_msb = nullptr);

RegisterSet defs; ///< Registers overwritten by the instruction.
Comment thread
theSK2005 marked this conversation as resolved.
Resolved
RegisterSet uses; ///< Registers read before the instruction writes defs.
bool has_exec_masked_vector_def = false; ///< True if any vector def is predicated by EXEC.
bool has_predicated_def = false; ///< True if defs preserve old values on some paths.
RegisterSet defs; ///< Registers overwritten by the instruction.
RegisterSet uses; ///< Registers read before the instruction writes defs.
bool has_exec_masked_vector_def =
false; ///< True if the instruction doesn't ignore EXEC and has a vector def.
bool has_predicated_def = false; ///< True if defs preserve old values on some paths.
};

} // namespace rocjitsu
Loading
Loading