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
2 changes: 1 addition & 1 deletion tools/shader-compiler/src/hlsl/driver/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ fn translate_outputs_once_impl(ex: &ShaderExtract, feeds_ps: Option<&InputInterp
if !wave_cull && !page_waves { {
// Complete-wave compute groups can exchange unproven dynamic SGPR lane reads
// through group memory with wave-width masking (`& 31u` or `& 63u`).
if ex.stage == Stage::Pixel && pixel_heap_base(ex, 5).is_some() { lower_first_lane_reads(&mut insns); }
if ex.stage == Stage::Pixel { lower_first_lane_reads(&mut insns); }
lower_lane_spills(&mut insns, resolve_wave32, has_group_read_fallback)?;
if let Some(width) = read_plan.group_read_width {
for ins in insns.iter_mut().filter(|i| i.mnemonic == GROUP_READ) {
Expand Down
4 changes: 4 additions & 0 deletions tools/shader-compiler/src/hlsl/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,11 @@ fn validate_launch(body: &[Insn], group_shift: i64, lane_index: Opnd) -> Result<
_ if ins.mnemonic.ends_with("_b64") => 2,
_ => 1,
};
// `v_readlane_b32` writes an SGPR, though the generic VOP3 decoder puts it in `Vgpr`.
let scalar_dst = ins.mnemonic == "v_readlane_b32";
ins.dst.iter().any(|dst| match (*dst, reg) {
(V(first), S(n)) if scalar_dst => n == first,
(V(_), V(_)) if scalar_dst => false,
(S(first), S(n)) | (V(first), V(n)) => n >= first && u16::from(n - first) < width,
_ => false,
})
Expand Down
17 changes: 16 additions & 1 deletion tools/shader-compiler/src/hlsl/rewrite/lanes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,21 @@ fn dead_after(
true
}

/// Active source operands of `ins`, excluding unused `Sgpr(0)` padding slots
/// that the generic VOP3 decoder emits for 1- and 2-operand VOP1/VOP2/VOPC forms.
fn active_srcs(ins: &Insn) -> &[Opnd] {
if ins.enc != Enc::Vop3 {
return &ins.src;
}
if (0x180..0x200).contains(&ins.op) {
&ins.src[..ins.src.len().min(1)]
} else if ins.op < 0x180 && !matches!(ins.op, 0x101 | 0x128 | 0x129 | 0x12a) {
&ins.src[..ins.src.len().min(2)]
} else {
&ins.src
}
}

/// A pixel is a wave of its own, so the first lane of a mask that covers it is its own.
/// `s_ff1` names that lane for `v_readlane`; anything else that uses the lane is left alone.
pub(in crate::hlsl) fn lower_first_lane_reads(insns: &mut [Insn]) {
Expand All @@ -194,7 +209,7 @@ pub(in crate::hlsl) fn lower_first_lane_reads(insns: &mut [Insn]) {
if !insns[first].mnemonic.starts_with("s_ff1_i32_b") { continue; }
// The destination field names an SGPR (decoded generically as a VGPR).
let Some(&(Opnd::Vgpr(result) | Opnd::Sgpr(result))) = insns[i].dst.first() else { continue };
let reads = |j: usize| j != i && insns[j].src.contains(&Opnd::Sgpr(lane));
let reads = |j: usize| j != i && active_srcs(&insns[j]).contains(&Opnd::Sgpr(lane));
if (first + 1..i).any(reads) { continue; }
if result != lane {
let end = (i + 1..insns.len()).find(|&j| insns[j].dst.contains(&Opnd::Sgpr(lane)) || insns[j].mnemonic.contains("branch"));
Expand Down
25 changes: 25 additions & 0 deletions tools/shader-compiler/src/tests/hlsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,31 @@ fn sgpr_lane_readlane_refused_on_spill_area() {
assert!(err.contains("non-constant lane operand"));
}

#[test]
fn lower_first_lane_reads_ignores_unused_vop3_s0_padding() {
let ff1 = Insn {
offset: 0, enc: Enc::Sop1, op: 0x14, mnemonic: "s_ff1_i32_b64".into(),
dst: vec![Opnd::Sgpr(0)], src: vec![Opnd::Sgpr(0)], aux: Default::default(), dwords: 1, raw: vec![0],
};
// VOP3-encoded VOP1 (op 0x1ab = v_rcp_iflag_f32) pads unused src[1]/src[2] with Sgpr(0).
let rcp = Insn {
offset: 4, enc: Enc::Vop3, op: 0x1ab, mnemonic: "v_rcp_iflag_f32".into(),
dst: vec![Opnd::Vgpr(40)], src: vec![Opnd::Vgpr(40), Opnd::Sgpr(0), Opnd::Sgpr(0)],
aux: Default::default(), dwords: 2, raw: vec![0, 0],
};
let read = sgpr_lane_readlane(39, 0, 4);
let clobber = Insn {
offset: 16, enc: Enc::Sop2, op: 0x0e, mnemonic: "s_and_b64".into(),
dst: vec![Opnd::Sgpr(0)], src: vec![Opnd::Sgpr(72), Opnd::ExecLo],
aux: Default::default(), dwords: 1, raw: vec![0],
};
let mut insns = vec![ff1, rcp, read, clobber];
lower_first_lane_reads(&mut insns);
assert_eq!(insns[2].mnemonic, "v_readfirstlane_b32");
assert_eq!(insns[2].dst, vec![Opnd::Sgpr(4)]);
assert_eq!(insns[2].src, vec![Opnd::Vgpr(39)]);
}

#[test]
fn const_lane_readlane_still_group_read_when_group_reads() {
let mut insns = vec![const_lane_readlane(4, 3, 7)];
Expand Down
16 changes: 16 additions & 0 deletions tools/shader-compiler/src/tests/hlsl/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,3 +388,19 @@ fn sampler_heap_binding_uses_the_reaching_pointer() {
let reason = emit(clobbered).unwrap_err();
assert!(reason.contains("base s24 is not a root table pointer"));
}

#[test]
fn validate_launch_ignores_readlane_sgpr_destination_matching_lane_vgpr() {
let words = [
0xd765_0002u32, 0x0001_00c1, // v_mbcnt_lo_u32_b32 v2, -1, 0
0x7e00_0505, // v_readfirstlane_b32 s0, v5
0x9381_ff03, 0x0004_0018, // s_bfe_u32 s1, s3, 0x00040018
0xd766_0002, 0x0002_04c1, // v_mbcnt_hi_u32_b32 v2, -1, v2
0x9016_8700, // s_lshr_b32 s22, s0, 7
0xd746_0002, 0x0409_0c01, // v_lshl_add_u32 v2, s1, 6, v2
0xd760_0002, 0x0001_0100, // v_readlane_b32 s2, v0, 0 (writes s2, not v2)
];
let isa: Vec<u8> = words.into_iter().flat_map(u32::to_le_bytes).collect();
let body = disassemble(&isa);
super::validate_launch(&body, 7, Opnd::Vgpr(2)).expect("v_readlane_b32 writes SGPR, not lane_index VGPR");
}
Loading