Skip to content

Commit c1acabc

Browse files
committed
dxil: preserve GS entry block boundary during EXECZ collapse
1 parent 045500b commit c1acabc

2 files changed

Lines changed: 129 additions & 2 deletions

File tree

‎crates/xcl/src/gpu/dxil/cfg.rs‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,11 @@ impl Cfg {
167167
.iter()
168168
.filter_map(|b| b.ops.first().and_then(|op| match *op { CfgOp::Inst(i) => Some(insts[i].pc), _ => None }))
169169
.collect();
170+
// Geometry mode: preserve the GS entry block boundary (`pc == gs_start`) so
171+
// `kernel.rs` sees the ES -> GS phase switch in split wave64.
172+
let pinned = setpc.flatten().and_then(|s| (0..blocks.len()).find(|&b| blocks[b].pc == s));
170173
if post.len() >= min_blocks {
171-
if collapse_execz_regions(&insts, &mut blocks, &post, &headers, wave64, live_at_end) {
174+
if collapse_execz_regions(&insts, &mut blocks, &post, &headers, wave64, live_at_end, pinned) {
172175
(post, headers) = order(&blocks);
173176
}
174177
}
@@ -192,6 +195,7 @@ fn collapse_execz_regions(
192195
headers: &BTreeSet<usize>,
193196
wave64: bool,
194197
live_at_end: bool,
198+
pinned: Option<usize>,
195199
) -> bool {
196200
let n = blocks.len();
197201
let mut pos = vec![usize::MAX; n];
@@ -211,6 +215,9 @@ fn collapse_execz_regions(
211215
if let Some(&entry) = rpo.first() {
212216
pred_count[entry] = 1; // keep entry from being absorbed
213217
}
218+
if let Some(p) = pinned {
219+
pred_count[p] += 1; // keep GS entry boundary intact
220+
}
214221
for &b in rpo {
215222
for t in succ_of(&blocks[b]) {
216223
pred_count[t] += 1;
@@ -222,7 +229,7 @@ fn collapse_execz_regions(
222229
for &b in rpo.iter().rev() {
223230
loop {
224231
match blocks[b].term {
225-
Term::Jump(nxt) if nxt != b && pred_count[nxt] == 1 && !headers.contains(&nxt) && pos[nxt] > pos[b] => {
232+
Term::Jump(nxt) if nxt != b && pred_count[nxt] == 1 && !headers.contains(&nxt) && pinned != Some(nxt) && pos[nxt] > pos[b] => {
226233
let tail_ops = std::mem::take(&mut blocks[nxt].ops);
227234
let tail_term = std::mem::replace(&mut blocks[nxt].term, Term::End);
228235
blocks[b].ops.extend(tail_ops);
@@ -236,6 +243,7 @@ fn collapse_execz_regions(
236243
&& pos[fall] > pos[b]
237244
&& pred_count[fall] == 1
238245
&& !headers.contains(&fall)
246+
&& pinned != Some(fall)
239247
&& blocks[fall].term == Term::Jump(target) =>
240248
{
241249
let Some(written_scalar) = analyze_execz_region(&blocks[fall].ops, insts, wave64) else {

‎crates/xcl/src/gpu/dxil/geometry_tests.rs‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,122 @@ fn gpu_geometry_direct_records() {
166166
check(&format!("HLSL, split {split}"), &got);
167167
}
168168
}
169+
170+
/// `branchy_program` extended with 70 forward `S_CBRANCH_EXECZ +1` diamonds right after
171+
/// GS EXEC initialization so `Cfg::build_geometry` crosses the EXECZ collapse threshold.
172+
fn branchy_program_over_execz_threshold() -> (Vec<u8>, GsInterface) {
173+
let e = |n: &str, enc: &str, f: &[(&str, u64)], lit: Option<u32>| encode(n, enc, f, lit).unwrap_or_else(|x| panic!("{n}: {x}"));
174+
const V: u64 = 256;
175+
let es = e("S_SETPC_B64", "ENC_SOP1", &[("SSRC0", 6)], None);
176+
let mut gs: Vec<Vec<u8>> = Vec::new();
177+
gs.push(e("S_BFE_U32", "ENC_SOP2", &[("SDST", 0), ("SSRC0", 3), ("SSRC1", 255)], Some(0x80008)));
178+
gs.push(e("S_BFM_B64", "ENC_SOP2", &[("SDST", 126), ("SSRC0", 0), ("SSRC1", 128)], None));
179+
for _ in 0..70 {
180+
gs.push(e("S_CBRANCH_EXECZ", "ENC_SOPP", &[("SIMM16", 1)], None));
181+
gs.push(e("V_NOP", "ENC_VOP1", &[], None));
182+
}
183+
gs.push(e("V_MBCNT_LO_U32_B32", "ENC_VOP3", &[("VDST", 20), ("SRC0", 193), ("SRC1", 128)], None));
184+
gs.push(e("V_MBCNT_HI_U32_B32", "ENC_VOP3", &[("VDST", 20), ("SRC0", 193), ("SRC1", V + 20)], None));
185+
gs.push(e("V_AND_B32", "ENC_VOP2", &[("VDST", 21), ("SRC0", 131), ("VSRC1", 2)], None)); // prim & 3
186+
gs.push(e("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 21), ("SRC0", 130), ("VSRC1", 21)], None)); // + 2
187+
gs.push(e("V_MAD_U32_U24", "ENC_VOP3", &[("VDST", 50), ("SRC0", V + 20), ("SRC1", 255), ("SRC2", 2)], Some(160)));
188+
gs.push(e("V_MOV_B32", "ENC_VOP1", &[("VDST", 52), ("SRC0", 242)], None)); // 1.0
189+
gs.push(e("S_MOV_B32", "ENC_SOP1", &[("SDST", 4), ("SSRC0", 128)], None));
190+
let top = gs.len();
191+
gs.push(e("V_CMP_LT_U32", "ENC_VOPC", &[("SRC0", 4), ("VSRC1", 21)], None));
192+
gs.push(e("S_AND_B64", "ENC_SOP2", &[("SDST", 126), ("SSRC0", 126), ("SSRC1", 106)], None));
193+
let execz = gs.len();
194+
gs.push(Vec::new()); // s_cbranch_execz end
195+
gs.push(e("V_MOV_B32", "ENC_VOP1", &[("VDST", 51), ("SRC0", 4)], None));
196+
gs.push(e("DS_WRITE2_B32", "ENC_DS", &[("ADDR", 50), ("DATA0", 20), ("DATA1", 51), ("OFFSET0", 0), ("OFFSET1", 1)], None));
197+
gs.push(e("DS_WRITE2_B32", "ENC_DS", &[("ADDR", 50), ("DATA0", 2), ("DATA1", 52), ("OFFSET0", 2), ("OFFSET1", 3)], None));
198+
gs.push(e("DS_WRITE2_B32", "ENC_DS", &[("ADDR", 50), ("DATA0", 21), ("DATA1", 20), ("OFFSET0", 4), ("OFFSET1", 5)], None));
199+
gs.push(e("DS_WRITE2_B32", "ENC_DS", &[("ADDR", 50), ("DATA0", 20), ("DATA1", 20), ("OFFSET0", 6), ("OFFSET1", 7)], None));
200+
gs.push(e("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 50), ("SRC0", 160), ("VSRC1", 50)], None));
201+
gs.push(e("S_SENDMSG", "ENC_SOPP", &[("SIMM16", 0x22)], None)); // emit
202+
gs.push(e("S_CMP_EQ_U32", "ENC_SOPC", &[("SSRC0", 4), ("SSRC1", 129)], None));
203+
let skip = gs.len();
204+
gs.push(Vec::new()); // s_cbranch_scc0 past the cut
205+
gs.push(e("S_SENDMSG", "ENC_SOPP", &[("SIMM16", 0x12)], None)); // cut
206+
let after_cut = gs.len();
207+
gs.push(e("S_ADD_U32", "ENC_SOP2", &[("SDST", 4), ("SSRC0", 4), ("SSRC1", 129)], None));
208+
let back = gs.len();
209+
gs.push(Vec::new()); // s_branch top
210+
let end = gs.len();
211+
gs.push(e("S_SENDMSG", "ENC_SOPP", &[("SIMM16", 0x3)], None));
212+
gs.push(e("S_ENDPGM", "ENC_SOPP", &[], None));
213+
let pc = |gs: &Vec<Vec<u8>>, i: usize| gs[..i].iter().map(|p| if p.is_empty() { 4 } else { p.len() }).sum::<usize>() as i64;
214+
let br = |gs: &Vec<Vec<u8>>, name: &str, at: usize, to: usize| {
215+
let rel = (pc(gs, to) - pc(gs, at) - 4) / 4;
216+
e(name, "ENC_SOPP", &[("SIMM16", rel as i16 as u16 as u64)], None)
217+
};
218+
gs[execz] = br(&gs, "S_CBRANCH_EXECZ", execz, end);
219+
gs[skip] = br(&gs, "S_CBRANCH_SCC0", skip, after_cut);
220+
gs[back] = br(&gs, "S_BRANCH", back, top);
221+
let g = GsInterface {
222+
es_inputs: vec![],
223+
es_outputs: vec![sig("SV_Position", 1, 0, 3)],
224+
outputs: vec![sig("SV_Position", 1, 0, 3), sig("TEXCOORD", 0, 1, 1)],
225+
max_vertex_count: 5,
226+
gs_start: Some(es.len() as u64),
227+
};
228+
let mut code = es;
229+
code.extend(gs.concat());
230+
(code, g)
231+
}
232+
233+
/// Collapsing 70 forward `S_CBRANCH_EXECZ` regions inside GS must preserve the `pc == gs_start`
234+
/// block boundary so split-wave64 geometry shaders still emit the ES -> GS phase switch.
235+
#[test]
236+
fn geometry_execz_collapse_preserves_gs_entry_boundary() {
237+
let (code, g) = branchy_program_over_execz_threshold();
238+
let gs_start = g.gs_start.unwrap();
239+
let cfg = super::cfg::Cfg::build_geometry(&code, g.gs_start).unwrap();
240+
// All 70 forward S_CBRANCH_EXECZ diamonds collapsed, while ES (pc=0) and GS entry (pc=gs_start) stayed separate!
241+
assert!(cfg.rpo.len() < 16, "expected 70 EXECZ diamonds to collapse, got {} blocks", cfg.rpo.len());
242+
assert!(
243+
cfg.rpo.iter().any(|&b| cfg.blocks[b].pc == gs_start),
244+
"GS entry block at pc={gs_start:#x} was merged into ES"
245+
);
246+
assert_eq!(cfg.blocks[cfg.rpo[0]].pc, 0);
247+
for split in [false, true] {
248+
let k = translate_geometry(&code, opts(split, 1), &g).unwrap_or_else(|e| panic!("split {split}: {e}"));
249+
if let Err(err) = super::validate::validate(&k.dxil) {
250+
panic!("split {split} DXIL validation failed: {err}");
251+
}
252+
}
253+
}
254+
255+
/// GPU regression test for `branchy_program_over_execz_threshold` in split wave64 (72 vertices / 5 triangles for group 0).
256+
#[test]
257+
fn gpu_geometry_collapsed_execz_preserves_gs_entry() {
258+
if runner::adapter().is_err() || runner::wave64_programs_supported() != Ok(true) || dxc::location().is_err() || runner::mesh_shaders_supported() != Ok(true) {
259+
runner::skip_test("no GPU / DXC / mesh shaders");
260+
return;
261+
}
262+
let (code, g) = branchy_program_over_execz_threshold();
263+
let rec = g.record();
264+
let msb = dxc::compile(&mesh_passthrough(&rec), "main", "ms_6_6", &[]).unwrap();
265+
let ps_src = "struct V { float4 pos : SV_Position; nointerpolation uint4 p0 : TEXCOORD0; };\nfloat4 main(V v) : SV_Target0 { return float4(1.0, (float)v.p0.y, 0.0, 1.0); }\n";
266+
let ps = dxc::compile(ps_src, "main", "ps_6_6", &[]).unwrap();
267+
let prims = 25;
268+
let mut io = vec![0u32; 128];
269+
io[IO_GS_PRIMS] = prims as u32;
270+
let mem = vec![0u32; 64];
271+
let want = expected_records(&g, prims);
272+
assert_eq!((want[0], want[1]), (72, 5));
273+
let direct = translate_geometry(&code, opts(true, 1), &g).unwrap().dxil;
274+
let (_, got) = runner::mesh_draw(&direct, &msb, &ps, 2, rec.record_dwords(), &io, &mem, 32, [0.0; 4]).unwrap();
275+
let rd = rec.record_dwords();
276+
let (vd, maxv) = (rec.vert_dwords(), rec.max_vertices as usize);
277+
let diff: Vec<usize> = (0..want.len())
278+
.filter(|&i| {
279+
let (grp, o) = (i / rd, i % rd);
280+
let r = &want[grp * rd..];
281+
let used = o < MESH_HEADER || (o < MESH_HEADER + maxv * vd && (o - MESH_HEADER) / vd < r[0] as usize) || (o >= MESH_HEADER + maxv * vd && o - MESH_HEADER - maxv * vd < r[1] as usize);
282+
used && want[i] != got[i]
283+
})
284+
.take(16)
285+
.collect();
286+
assert!(diff.is_empty(), "split-wave64 collapsed-EXECZ geometry records differ at {diff:?}: got {:?}, want {:?}", diff.iter().map(|&i| got[i]).collect::<Vec<_>>(), diff.iter().map(|&i| want[i]).collect::<Vec<_>>());
287+
}

0 commit comments

Comments
 (0)