Skip to content
Closed
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
26 changes: 24 additions & 2 deletions tools/check_paired_wave64.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@
}


# Dependent, in-place integer operations under sparse EXEC, including
# scalar inputs, wrapping arithmetic and a masked row exchange.
PROGRAMS['direct_alu_chain'] = [
'v_mov_b32_e32 v10, v0', 'v_mov_b32_e32 v11, v1',
's_mov_b32 s30, 0x12345678',
] + [
'v_add_nc_u32_e32 v10, s30, v10',
'v_sub_nc_u32_e32 v11, v10, v11',
'v_subrev_nc_u32_e32 v10, v11, v10',
'v_not_b32_e32 v11, v11',
'v_xor_b32_e32 v10, v10, v11',
'v_lshlrev_b32_e32 v10, 5, v10',
'v_lshrrev_b32_e32 v11, v1, v11',
'v_or_b32_e32 v11, v10, v11',
'v_and_b32_e32 v10, 0x7fffffff, v10',
'v_add_nc_u32 v10, v10, v11 dpp8:[1,0,3,2,5,4,7,6]',
'v_mov_b32_dpp v11, v10 row_shr:1 row_mask:0xa bank_mask:0x5 bound_ctrl:1',
] * 8


def assemble(llvm_mc, lines):
result = subprocess.run([str(llvm_mc), "-triple=amdgcn-amd-amdhsa", "-mcpu=gfx1030",
"-mattr=-wavefrontsize32,+wavefrontsize64", "-show-encoding"],
Expand Down Expand Up @@ -266,7 +286,9 @@ def step(w, text):
if not (rows >> (l >> 4)) & (banks >> (l & 3)) & 1 or not (valid or zero): continue
a = first[source] if valid else 0
x = [o[l] for o in others]
results[l] = {'v_mov_b32': lambda: a, 'v_add_nc_u32': lambda: a + x[0], 'v_and_b32': lambda: a & x[0],
results[l] = {'v_mov_b32': lambda: a, 'v_add_nc_u32': lambda: a + x[0],
'v_sub_nc_u32': lambda: a - x[0], 'v_subrev_nc_u32': lambda: x[0] - a,
'v_not_b32': lambda: ~a, 'v_and_b32': lambda: a & x[0],
'v_or_b32': lambda: a | x[0], 'v_xor_b32': lambda: a ^ x[0], 'v_max_u32': lambda: max(a, x[0]),
'v_lshlrev_b32': lambda: x[0] << (a & 31), 'v_lshrrev_b32': lambda: x[0] >> (a & 31),
'v_cndmask_b32': lambda: x[0] if (select >> l) & 1 else a,
Expand Down Expand Up @@ -324,7 +346,7 @@ def inputs(rng):
def selected():
"""Each program in one console wave per group, and some in four."""
single = [(name, body, 64) for name, body in PROGRAMS.items()]
return single + [(f"{name}_x4", PROGRAMS[name], 256) for name in ("saveexec", "waterfall", "permlane", "dispatch")]
return single + [(f"{name}_x4", PROGRAMS[name], 256) for name in ("saveexec", "waterfall", "permlane", "dispatch", "direct_alu_chain", "row_masks")]


def main(argv=None):
Expand Down
92 changes: 92 additions & 0 deletions tools/shader-compiler/src/hlsl/paired_wave64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,91 @@ fn ds_swizzle(em: &mut Emitter<'_>, ins: &Insn) -> Result<(), String> {
Ok(())
}


/// Inline only the small integer ALU subset whose emitted body is a single
/// predicated VGPR assignment. Both lanes have distinct storage, and no
/// scalar or mask is written. Anything outside this closed expression grammar
/// keeps the generic shadow-register path.
fn direct_alu_expr(rhs: &str, lane: u32, moved: bool) -> Option<String> {
if !rhs.is_ascii() || rhs.contains("++") || rhs.contains("--") {
return None;
}
let bytes = rhs.as_bytes();
let mut out = String::new();
let mut at = 0;
while at < bytes.len() {
let start = at;
if bytes[at].is_ascii_alphanumeric() || bytes[at] == b'_' {
while at < bytes.len() && (bytes[at].is_ascii_alphanumeric() || bytes[at] == b'_') {
at += 1;
}
let word = &rhs[start..at];
if is_vector_register(word) {
let _ = write!(out, "{word}__{lane}");
} else if moved && word == "tb_s60000" {
let _ = write!(out, "tbd_{lane}");
} else if word.strip_prefix('s').is_some_and(|n| !n.is_empty() && n.bytes().all(|c| c.is_ascii_digit())) {
out.push_str(word);
} else {
let literal = word.strip_suffix('u').unwrap_or(word);
let valid = if let Some(hex) = literal.strip_prefix("0x") {
!hex.is_empty() && hex.bytes().all(|c| c.is_ascii_hexdigit())
} else {
!literal.is_empty() && literal.bytes().all(|c| c.is_ascii_digit())
};
if !valid {
return None;
}
out.push_str(word);
}
} else {
if !matches!(bytes[at], b' ' | b'\t' | b'(' | b')' | b'+' | b'-'
| b'*' | b'&' | b'|' | b'^' | b'~' | b'<' | b'>') {
return None;
}
out.push(bytes[at] as char);
at += 1;
}
}
Some(out)
}

fn direct_alu(em: &mut Emitter<'_>, ins: &Insn, code: &str, moved: Option<&[String]>, gated: bool) -> bool {
if !matches!(ins.mnemonic.as_str(), "v_mov_b32" | "v_add_nc_u32" | "v_sub_nc_u32"
| "v_subrev_nc_u32" | "v_and_b32" | "v_or_b32" | "v_xor_b32" | "v_not_b32"
| "v_lshlrev_b32" | "v_lshrrev_b32") {
return false;
}
let [Opnd::Vgpr(dst)] = ins.dst.as_slice() else { return false };
let mut lines = code.lines().map(str::trim).filter(|line| !line.is_empty() && !line.starts_with("//"));
let Some(statement) = lines.next() else { return false };
if lines.next().is_some() { return false; }
let prefix = format!("if (exec_lo & 1u) v{dst} = ");
let Some(rhs) = statement.strip_prefix(&prefix).and_then(|s| s.strip_suffix(';')) else { return false };
let Some(first) = direct_alu_expr(rhs, 0, moved.is_some()) else { return false };
let Some(second) = direct_alu_expr(rhs, 1, moved.is_some()) else { return false };
for (word, _) in identifiers(code) {
if is_vector_register(word) {
em.regs.insert(word.to_string());
}
}
if let Some(reads) = moved {
em.line("{");
for line in reads {
em.line(line);
}
}
for (lane, rhs) in [first, second].iter().enumerate() {
let active = lane_bit("exec_lo", "exec_hi", lane as u32);
let gate = if gated { format!(" & tbw_{lane}") } else { String::new() };
em.line(&format!("if ({active}{gate}) v{dst}__{lane} = {rhs};"));
}
if moved.is_some() {
em.line("}");
}
true
}

pub(super) fn emit(em: &mut Emitter<'_>, idx: usize, ins: &Insn, is_ps: bool, br: Option<usize>) -> Result<(), String> {
let m = ins.mnemonic.as_str();
if !per_lane(ins.enc) {
Expand Down Expand Up @@ -441,6 +526,9 @@ pub(super) fn emit(em: &mut Emitter<'_>, idx: usize, ins: &Insn, is_ps: bool, br
return Err(format!("paired wave64: {m} writes {word} outside a known mask"));
}
}
if written.is_empty() && direct_alu(em, ins, &code, moved.as_deref(), gated) {
return Ok(());
}
let vectors: BTreeSet<&str> = identifiers(&code).map(|(word, _)| word).filter(|w| is_vector_register(w)).collect();
let mut shadowed = read.clone();
for words in &written {
Expand Down Expand Up @@ -497,3 +585,7 @@ pub(super) fn emit(em: &mut Emitter<'_>, idx: usize, ins: &Insn, is_ps: bool, br
em.line("}");
Ok(())
}

#[cfg(test)]
#[path = "../tests/hlsl/paired_direct_alu.rs"]
mod direct_alu_tests;
10 changes: 10 additions & 0 deletions tools/shader-compiler/src/tests/hlsl/host_profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,13 @@ fn group_shared_accesses_are_checked_against_the_declared_size() {
}
assert!(checked > 0, "no host profile translated the shared-memory program");
}

#[test]
fn paired_simple_alu_writes_lane_registers_directly() {
// v_mov_b32_e32 v10, v0; v_add_nc_u32 v10, v10, v11; s_endpgm.
let words = [0x7e140300, 0x4a14170a, 0xbf810000];
let hlsl = translate_paired_wave64(&program(64, &words)).unwrap().hlsl;
assert!(hlsl.contains("v10__0 = v0__0;"), "{hlsl}");
assert!(hlsl.contains("v10__1 = v0__1;"));
assert!(!hlsl.contains("uint tbx_exec_lo"));
}
34 changes: 17 additions & 17 deletions tools/shader-compiler/src/tests/hlsl/host_profiles.txt
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
compare any-host cs_5_0 waves=any 00ed2a2e5e807d85
compare wave32-host cs_5_0 waves=any 00ed2a2e5e807d85
compare two-lanes cs_6_6 waves=32 9e68c336f8d6010e
compare two-lanes cs_6_6 waves=32 d1ab76a5a83c1fc0
saveexec any-host cs_5_0 waves=any d39d73f14c0262a8
saveexec wave32-host cs_5_0 waves=any d39d73f14c0262a8
saveexec two-lanes cs_6_6 waves=32 7e752b8501d90a84
saveexec two-lanes cs_6_6 waves=32 e7b9ead0539639e6
mbcnt any-host cs_5_0 waves=any f9b64857bd2ae39b
mbcnt wave32-host cs_5_0 waves=any f9b64857bd2ae39b
mbcnt two-lanes cs_6_6 waves=32 d7be408280f250a4
mbcnt two-lanes cs_6_6 waves=32 8af8ad78b09de42e
waterfall any-host cs_5_0 waves=any cf4390217f7ee376
waterfall wave32-host cs_5_0 waves=any cf4390217f7ee376
waterfall two-lanes cs_6_6 waves=32 1072bf42d6cc3002
waterfall two-lanes cs_6_6 waves=32 900c5b70c2358087
cmpx_carry any-host cs_5_0 waves=any bc5f5813277397b3
cmpx_carry wave32-host cs_5_0 waves=any bc5f5813277397b3
cmpx_carry two-lanes cs_6_6 waves=32 b156ed63e584b218
cmpx_carry two-lanes cs_6_6 waves=32 f58dd288ea619862
lanes any-host cs_5_0 waves=any 4dfb64f0c32887f2
lanes wave32-host cs_5_0 waves=any 4dfb64f0c32887f2
lanes two-lanes cs_6_6 waves=32 ba7897692f781b7b
lanes two-lanes cs_6_6 waves=32 a1cc78f44c70fb07
dpp8 any-host cs_5_0 waves=any 1bfe3fd91e4804f0
dpp8 wave32-host cs_5_0 waves=any 1bfe3fd91e4804f0
dpp8 two-lanes cs_6_6 waves=32 c54178f1098ec8f2
dpp8 two-lanes cs_6_6 waves=32 cedaf3e9f9f43850
row_shift any-host cs_5_0 waves=32 cc1e201b4a9ff628
row_shift wave32-host cs_5_0 waves=32 cc1e201b4a9ff628
row_shift two-lanes cs_6_6 waves=32 db5b85908612433b
row_shift two-lanes cs_6_6 waves=32 7893d42ef3eb06fb
row_masks any-host refused
row_masks wave32-host refused
row_masks two-lanes cs_6_6 waves=32 ac3a73b8bc6bce4c
row_masks two-lanes cs_6_6 waves=32 2c56333165b7435a
permlane any-host cs_5_0 waves=any 189cd822efaab806
permlane wave32-host cs_5_0 waves=any 189cd822efaab806
permlane two-lanes cs_6_6 waves=32 ca0116ae4cce249d
permlane two-lanes cs_6_6 waves=32 7f9b076ecef05fdb
empty any-host cs_5_0 waves=any d308d92ea83f4e32
empty wave32-host cs_5_0 waves=any d308d92ea83f4e32
empty two-lanes cs_6_6 waves=32 49940f6d68fca5ed
empty two-lanes cs_6_6 waves=32 10dfd3f086e8ee7e
dispatch any-host cs_5_0 waves=any a09feae2667e9d78
dispatch wave32-host cs_5_0 waves=any a09feae2667e9d78
dispatch two-lanes cs_6_6 waves=32 48b23403731e9be4
dispatch two-lanes cs_6_6 waves=32 50b097b51bbbae67
shared any-host cs_5_0 waves=any d80843365be5c48d
shared wave32-host cs_5_0 waves=any d80843365be5c48d
shared two-lanes cs_6_6 waves=32 39003b0cc1e22abe
shared two-lanes cs_6_6 waves=32 ae5d29d0bbebded4
masks any-host cs_5_0 waves=any 103f015433e974a1
masks wave32-host cs_5_0 waves=any 103f015433e974a1
masks two-lanes cs_6_6 waves=32 ca68eb3457bb35b3
masks two-lanes cs_6_6 waves=32 e88070de9b4f42a1
lane-sum any-host cs_5_0 waves=any 621ed1c48da4ec86
lane-sum wave32-host cs_5_0 waves=any 621ed1c48da4ec86
lane-sum two-lanes cs_6_6 waves=32 d3db670e6ab5c712
lane-sum two-lanes cs_6_6 waves=32 c320caa91155769f
lane-sum-two-waves any-host cs_5_0 waves=64 0cfd9e050c2caba1
lane-sum-two-waves wave32-host cs_6_6 waves=32 b62a8aa3cc1c1ad1
lane-sum-two-waves two-lanes cs_6_6 waves=32 b62a8aa3cc1c1ad1
lane-sum-two-waves wave32-host cs_6_6 waves=32 e5b3da156dd85658
lane-sum-two-waves two-lanes cs_6_6 waves=32 e5b3da156dd85658
11 changes: 11 additions & 0 deletions tools/shader-compiler/src/tests/hlsl/paired_direct_alu.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use super::direct_alu_expr;

#[test]
fn expression_grammar_does_not_rewrite_masks_or_effectful_forms() {
assert_eq!(direct_alu_expr("(v10 + s1)", 1, false).as_deref(), Some("(v10__1 + s1)"));
assert_eq!(direct_alu_expr("tb_s60000 ^ 0xffu", 0, true).as_deref(), Some("tbd_0 ^ 0xffu"));
for code in ["exec_lo", "vcc_lo", "tb_s60000", "asuint(v0)", "v0++", "v1 = 1u",
"v0[1]", "v0.x", "read(v0)", "v10; v11", "scc"] {
assert!(direct_alu_expr(code, 1, false).is_none(), "{code}");
}
}
Loading