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
139 changes: 139 additions & 0 deletions crates/victory-shader/src/emit/halves_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,43 @@ fn reads_across_32_lane_halves_are_refused_with_their_reason() {
// first lane, in halves or not.
assert_eq!(refusal(&first, 64, 0), "translated");

// With console lanes the program runs in halves, where lane 32 is the
// second host wave's first lane: a value that differs between the
// halves (the thread id) would read 32 there, the console 0.
let first_varying = [
PROLOGUE.as_slice(),
&[
0x3420_0085, // v_lshlrev_b32 v16, 5, v0
0xD765_0001,
0x0001_00C1, // v_mbcnt_lo_u32_b32 v1, -1, 0
0x7E18_0500, // v_readfirstlane_b32 s12, v0
0x7E04_020C, // v_mov_b32 v2, s12
],
&store(1, 0),
&store(2, 4),
&[S_ENDPGM],
]
.concat();
let why = refusal(&first_varying, 64, 0);
assert!(
why.contains("a first-lane read of a value that may differ between 32-lane halves"),
"{why}"
);
// A first-lane read nothing uses costs nothing.
let unused_first = [
PROLOGUE.as_slice(),
&[
0x3420_0085, // v_lshlrev_b32 v16, 5, v0
0xD765_0001,
0x0001_00C1, // v_mbcnt_lo_u32_b32 v1, -1, 0
0x7E18_0500, // v_readfirstlane_b32 s12, v0
],
&store(1, 0),
&[S_ENDPGM],
]
.concat();
assert_eq!(refusal(&unused_first, 64, 0), "translated");

// A value read across halves that nothing uses costs nothing.
let unused = [
PROLOGUE.as_slice(),
Expand Down Expand Up @@ -374,3 +411,105 @@ fn wave64_programs_in_halves_match_the_console_on_every_gpu() {
)
);
}

/// Per thread `l` of a group of 128 (two console wave64s), two dwords at
/// `l * 32`: the first lane of `l >> 6` (one value per console wave, the
/// same in both its halves) and the lane `v_mbcnt` counts.
fn first_lane_alike() -> Vec<u32> {
let mut w = PROLOGUE.to_vec();
w.extend([
0x3420_0085, // v_lshlrev_b32 v16, 5, v0
0xD765_0001,
0x0001_00C1, // v_mbcnt_lo_u32_b32 v1, -1, 0
0xD766_0001,
0x0002_02C1, // v_mbcnt_hi_u32_b32 v1, -1, v1
0x2C04_0086, // v_lshrrev_b32 v2, 6, v0
0x7E18_0502, // v_readfirstlane_b32 s12, v2
0x7E06_020C, // v_mov_b32 v3, s12
]);
w.extend(store(3, 0));
w.extend(store(1, 4));
w.push(S_ENDPGM);
w
}

#[test]
fn the_first_lane_program_is_what_its_comments_say() {
let code: Vec<u8> = first_lane_alike()
.iter()
.flat_map(|w| w.to_le_bytes())
.collect();
let all = gfx10::decode(&code)
.expect("decodes")
.iter()
.map(|i| i.text(true))
.collect::<Vec<_>>()
.join("\n");
for want in [
"v_lshrrev_b32 v2, 6, v0",
"v_readfirstlane_b32 s12, v2",
"v_mov_b32 v3, s12",
"buffer_store_dword v1, v16, s[40:43], 0 offen offset:4",
] {
assert!(all.contains(want), "{want:?} not in:\n{all}");
}
}

/// A first-lane read of a value the same in every lane of the console wave
/// ([`first_lane_alike`]) translates in halves, and every hardware adapter
/// gives the console's results: each half reads its console wave's value.
#[test]
fn first_lanes_of_alike_values_match_the_console_on_every_gpu() {
let n = native(&first_lane_alike(), true, 128, 0);
let shader = shader(lowered(&n, true).expect("halves"));
let want: Vec<[u32; 2]> = (0..128).map(|l| [l >> 6, l & 63]).collect();
let views = [
View::Texture(1, 1, &[[0.0; 4]]),
View::Buffer {
data: &[0; 4],
stride: Stride::Structured(16),
},
View::RawUav(128 * 8),
];
let _one = super::valu_tests::ONE_AT_A_TIME
.lock()
.unwrap_or_else(|e| e.into_inner());
let hosts = Host::each_hardware();
if hosts.is_empty() {
eprintln!("no SM 6.6 hardware device; skipped");
}
let mut failed = Vec::new();
for (adapter, host) in &hosts {
let (root, pso) = host
.compute_pipeline(&shader)
.unwrap_or_else(|e| panic!("{adapter}: no pipeline: {e}"));
let ud = vec![0u32; shader.user_data_dwords().max(1) as usize];
let out = host
.dispatch(
&root,
&pso,
&Dispatch {
roots: vec![(0, Root::Constants(ud))],
views: &views,
samplers: &[],
groups: [1, 1, 1],
},
)
.unwrap_or_else(|e| panic!("{adapter}: dispatch failed: {e}"));
let wrong: Vec<String> = out[0]
.chunks(8)
.zip(&want)
.enumerate()
.filter(|(_, (got, want))| got[..2] != want[..])
.map(|(l, (got, want))| {
format!("{adapter}: lane {l}: {:?}, the console {want:?}", &got[..2])
})
.collect();
eprintln!(
"{adapter}: {} lanes of 128 differ from the console",
wrong.len()
);
failed.extend(wrong);
}
assert!(failed.is_empty(), "{}", failed.join("\n"));
}
24 changes: 20 additions & 4 deletions crates/victory-shader/src/emit/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ struct Lower<'a> {
halves_refused: Option<Why>,
/// [`Lower::relabelled_flat`], once made.
relabelled: Option<V>,
/// In halves: VGPR values the same in every lane of the console wave
/// ([`halves::lane_uniform`]), whose first lane either half reads.
alike: HashSet<Value>,
/// Pixel: an ordered section (POPS), whose UAVs the host makes
/// rasterizer-ordered views.
pops: bool,
Expand Down Expand Up @@ -691,6 +694,11 @@ impl<'a> Lower<'a> {
halves,
halves_refused,
relabelled: None,
alike: if halves {
halves::lane_uniform(x)
} else {
HashSet::new()
},
pops: matches!(x.native.io, StageIo::Ps { db_shader_control, .. }
if db_shader_control & 1 << 16 != 0),
root_memory,
Expand Down Expand Up @@ -6451,11 +6459,19 @@ impl<'a> Lower<'a> {
int(self, a)
}
"v_readfirstlane_b32" => {
// In halves the host wave's first lane is the console's only
// for a value the same in both halves.
if self.halves
&& matches!(v.src.first(), Some(&Operand::Vector { reg, .. })
if !inst.read(Reg::V(reg)).is_some_and(|x| self.alike.contains(&x)))
{
return Err(halves::FIRST_ACROSS);
}
let a = s(self, 0)?;
// shortcut: in halves, the first lane of the host wave, as in
// every program without console lanes (whose host waves are
// the driver's choice): the console's where the value is the
// same in every lane, what the compiler reads first lanes for.
// shortcut: without console lanes, the first lane of the host
// wave (whose waves are the driver's choice): the console's
// where the value is the same in every lane, what the compiler
// reads first lanes for.
self.s.flags |= flags::WAVE_OPS;
let r = self.s.op(
c,
Expand Down
Loading