Skip to content
Merged
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
37 changes: 34 additions & 3 deletions crates/xcl/src/gpu/dxil/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub enum Term {
}

pub struct CfgBlock {
/// byte offset of the block's first instruction
pub pc: u64,
/// instruction indices (the terminating branch excluded for `Cond`/`Jump` by branch)
pub insts: Vec<usize>,
pub term: Term,
Expand All @@ -38,6 +40,16 @@ fn is_end(name: &str) -> bool {

impl Cfg {
pub fn build(code: &[u8]) -> Result<Cfg, String> {
Self::build_with(code, None)
}

/// Geometry mode: the ES's `s_setpc_b64` continues at `gs_start` (the GS code) when
/// that is past the start, else ends the program (`recomp::Mode::Geometry`).
pub fn build_geometry(code: &[u8], gs_start: Option<u64>) -> Result<Cfg, String> {
Self::build_with(code, Some(gs_start.filter(|&s| s > 0)))
}

fn build_with(code: &[u8], setpc: Option<Option<u64>>) -> Result<Cfg, String> {
let mut insts: Vec<Decoded> = Vec::new();
let mut off = 0usize;
while off + 4 <= code.len() {
Expand All @@ -48,7 +60,19 @@ impl Cfg {
rdna2::resolve_far_jumps(&mut insts);
let index: BTreeMap<u64, usize> = insts.iter().enumerate().map(|(i, d)| (d.pc, i)).collect();
let mut leaders: BTreeSet<usize> = BTreeSet::from([0]);
let is_setpc = |d: &Decoded| setpc.is_some() && d.inst.name == "S_SETPC_B64" && d.far_target.is_none();
let setpc_target = match setpc {
Some(Some(t)) => Some(*index.get(&t).ok_or_else(|| format!("GS start {t:#x} is not the start of an instruction"))?),
_ => None,
};
if let Some(t) = setpc_target {
leaders.insert(t);
}
for (i, d) in insts.iter().enumerate() {
if is_setpc(d) {
leaders.insert(i + 1);
continue;
}
if let Some(t) = rdna2::branch_target(d) {
let ti = *index.get(&t).ok_or_else(|| format!("+{:#x}: branch to {t:#x}, which is not the start of an instruction", d.pc))?;
leaders.insert(ti);
Expand All @@ -70,6 +94,13 @@ impl Cfg {
term = Term::End;
break;
}
if is_setpc(d) {
term = match setpc_target {
Some(t) => Term::Jump(block_of[&t]),
None => Term::End,
};
break;
}
if let Some(t) = rdna2::branch_target(d) {
let tb = block_of[&index[&t]];
let name = d.inst.name;
Expand All @@ -86,7 +117,7 @@ impl Cfg {
}
list.push(i);
}
blocks.push(CfgBlock { insts: list, term, preds: Vec::new() });
blocks.push(CfgBlock { pc: insts[s].pc, insts: list, term, preds: Vec::new() });
}
// a conditional branch whose both edges meet is a jump
for b in &mut blocks {
Expand Down Expand Up @@ -172,7 +203,7 @@ fn make_reducible(blocks: &mut Vec<CfgBlock>) -> Result<(Vec<usize>, BTreeSet<us
let mut copy = vec![usize::MAX; n];
for b in (0..n).filter(|&b| region[b] && b != h) {
copy[b] = blocks.len();
blocks.push(CfgBlock { insts: blocks[b].insts.clone(), term: blocks[b].term.clone(), preds: Vec::new() });
blocks.push(CfgBlock { pc: blocks[b].pc, insts: blocks[b].insts.clone(), term: blocks[b].term.clone(), preds: Vec::new() });
}
let redirect = |term: &mut Term, to: &dyn Fn(usize) -> usize| match term {
Term::Jump(x) => *x = to(*x),
Expand Down Expand Up @@ -299,7 +330,7 @@ mod tests {
use super::*;

fn blk(term: Term) -> CfgBlock {
CfgBlock { insts: Vec::new(), term, preds: Vec::new() }
CfgBlock { pc: 0, insts: Vec::new(), term, preds: Vec::new() }
}

/// The console's loop entered in the middle (`62b584e061c05fc5`'s shape): 0 → {1 | 3},
Expand Down
54 changes: 54 additions & 0 deletions crates/xcl/src/gpu/dxil/core_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,60 @@ fn graphics_stages_with_a_branch_before_their_exports() {
assert_eq!(draw(&dvs, &dps, 1), kept, "branch taken");
}

/// Geometry mode (ES+GS, the compute pass of `recomp::Mode::Geometry`): an ES that writes
/// its vertex to the LDS ring, the GS that reads its triangle's vertices, writes them to
/// its output area and emits a strip; split and native forms validate (also the ES or GS
/// alone, as the coverage sweep translates them).
#[test]
fn geometry_mode_validates() {
use crate::gpu::recomp::GsInterface;
let e = |n: &str, enc: &str, f: &[(&str, u64)]| encode(n, enc, f, None).unwrap_or_else(|x| panic!("{n}: {x}"));
let mut es = Vec::new();
// v20 = lane * ES item (16 B), ring[v20] = v9 (a fetched input)
es.extend(e("V_MBCNT_LO_U32_B32", "ENC_VOP3", &[("VDST", 20), ("SRC0", 193), ("SRC1", 128)]));
es.extend(e("V_MBCNT_HI_U32_B32", "ENC_VOP3", &[("VDST", 20), ("SRC0", 193), ("SRC1", 256 + 20)]));
es.extend(e("V_LSHLREV_B32", "ENC_VOP2", &[("VDST", 20), ("SRC0", 132), ("VSRC1", 20)]));
es.extend(e("DS_WRITE_B32", "ENC_DS", &[("ADDR", 20), ("DATA0", 9)]));
es.extend(e("S_BARRIER", "ENC_SOPP", &[]));
es.extend(e("S_SETPC_B64", "ENC_SOP1", &[("SSRC0", 6)]));
let mut gs = Vec::new();
// v0 = vertex 0's dword offset: read it, write 3 output vertices at s2 + lane * 48
gs.extend(e("V_LSHLREV_B32", "ENC_VOP2", &[("VDST", 30), ("SRC0", 130), ("VSRC1", 0)]));
gs.extend(e("DS_READ_B32", "ENC_DS", &[("ADDR", 30), ("VDST", 31)]));
gs.extend(e("V_MBCNT_LO_U32_B32", "ENC_VOP3", &[("VDST", 32), ("SRC0", 193), ("SRC1", 128)]));
gs.extend(e("V_MUL_U32_U24", "ENC_VOP2", &[("VDST", 32), ("SRC0", 176), ("VSRC1", 32)]));
gs.extend(e("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 32), ("SRC0", 2), ("VSRC1", 32)]));
for k in 0..3u64 {
gs.extend(e("DS_WRITE_B32", "ENC_DS", &[("ADDR", 32), ("DATA0", 31), ("OFFSET0", 16 * k)]));
gs.extend(e("S_SENDMSG", "ENC_SOPP", &[("SIMM16", 0x22)]));
}
gs.extend(e("S_SENDMSG", "ENC_SOPP", &[("SIMM16", 0x12)]));
gs.extend(e("S_ENDPGM", "ENC_SOPP", &[]));
let sig = |name: &str, sv: u32, reg: u32| crate::gpu::dxbc::SigElement { name: name.into(), index: 0, system_value: sv, component_type: 3, register: reg, mask: 0xF, rw_mask: 0xF, stream: 0 };
let g = GsInterface {
es_inputs: vec![sig("POSITION", 0, 0)],
es_outputs: vec![sig("SV_Position", 1, 0)],
outputs: vec![sig("SV_Position", 1, 0)],
max_vertex_count: 3,
gs_start: Some(es.len() as u64),
};
let mut code = es.clone();
code.extend_from_slice(&gs);
let mut es_end = es;
es_end.extend(e("S_ENDPGM", "ENC_SOPP", &[]));
let cases = [
("ES+GS", code, g.clone()),
("ES alone", es_end, GsInterface { gs_start: None, ..g.clone() }),
("GS alone", gs, GsInterface { gs_start: Some(0), ..g }),
];
for (name, code, g) in cases {
for split in [true, false] {
let o = KernelOptions { wave64: true, split, mem_windows: 1, sampler_base: 0 };
super::kernel::translate_geometry(&code, o, &g).unwrap_or_else(|e| panic!("{name} split {split}: {}", &e[..e.len().min(3000)]));
}
}
}

/// Exhaustive bit-exact check of the IEEE-754 bit-manipulation formula used by
/// `lower_mem::uf_small_bits` for all 10-bit (mb=5) and 11-bit (mb=6) float inputs.
#[test]
Expand Down
5 changes: 2 additions & 3 deletions crates/xcl/src/gpu/dxil/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ pub fn translate_mode(code: &[u8], mode: &Mode, o: KernelOptions, validate: bool
Mode::Vertex(vs) => kernel::translate_vertex(code, KernelOptions { split: false, ..o }, vs),
Mode::Pixel(ps) => kernel::translate_pixel(code, KernelOptions { split: false, ..o }, ps),
Mode::Mesh(ms) => kernel::translate_mesh(code, o, ms),
Mode::Geometry(_) => Err("geometry mode (ES+GS): not supported by the direct translator".into()),
Mode::Geometry(g) => kernel::translate_geometry(code, o, g),
}));
let k = match r {
Ok(r) => r?,
Expand Down Expand Up @@ -409,8 +409,7 @@ mod tests {
r.add(name, &Sample { stage: stage_name(&mode), result: res, time: t0.elapsed(), hlsl_size: None });
}
assert_eq!(r.stages["kernel"].translated, 1, "{}", r.text(5));
assert_eq!(r.stages["GS"].unsupported, 1);
assert!(r.ranked()[0].0.starts_with("geometry mode"));
assert_eq!(r.stages["GS"].translated, 1, "{}", r.text(5));
}

#[test]
Expand Down
Loading
Loading