Merge pull request #423 from pedrofort/shader-perm-no-int64

Shaders: lower dynamic v_perm_b32 without requiring INT64
This commit is contained in:
Marvis authored and GitHub committed 2026-11-24 09:13:04 +01:00
commit 9f18eb4587
1 file changed
+94 -13
+94 -13
View File
@@ -4900,24 +4900,22 @@ impl<'a> Lower<'a> {
_ => {
// A computed selector: each byte picked at run time.
let (a, b, sv) = (s(self, 0)?, s(self, 1)?, s(self, 2)?);
let i64t = self.s.m.int(64);
self.s.flags |= flags::INT64;
let wa = self.s.m.cast(c, Cast::ZExt, a, i64t);
let wb = self.s.m.cast(c, Cast::ZExt, b, i64t);
let k32 = self.s.m.int_const(i64t, 32);
let wa = self.s.m.bin(c, Bin::Shl, wa, k32);
let both = self.s.m.bin(c, Bin::Or, wa, wb);
let mut r = k(self, 0);
for i in 0..4u32 {
let (sh, ff) = (k(self, 8 * i), k(self, 0xFF));
let sel = self.s.m.bin(c, Bin::LShr, sv, sh);
let sel = self.s.m.bin(c, Bin::And, sel, ff);
// Bytes 0-7 of {src0, src1}.
let k3 = k(self, 3);
let bits = self.s.m.bin(c, Bin::Shl, sel, k3);
let bits = self.s.m.cast(c, Cast::ZExt, bits, i64t);
let x = self.s.m.bin(c, Bin::LShr, both, bits);
let x = self.s.m.cast(c, Cast::Trunc, x, self.s.t.i32);
// Select a dword and a byte within it.
// Later selects handle codes 8 and above.
let four = k(self, 4);
let zero = k(self, 0);
let high = self.s.m.bin(c, Bin::And, sel, four);
let high = self.s.m.cmp(c, pred::NE, high, zero);
let word = self.s.m.select(c, high, a, b);
let three = k(self, 3);
let index = self.s.m.bin(c, Bin::And, sel, three);
let bits = self.s.m.bin(c, Bin::Shl, index, three);
let x = self.s.m.bin(c, Bin::LShr, word, bits);
let mut byte = self.s.m.bin(c, Bin::And, x, ff);
// 8-11: the sign of bytes 1, 3 of src1, then of src0.
for (n, src, bit) in [(8, b, 15), (9, b, 31), (10, a, 15), (11, a, 31)]
@@ -6782,6 +6780,89 @@ mod tests {
}
}
fn perm_features(selector: u32) -> u64 {
// Synthetic compute shader:
// v0 is the thread ID and can supply a dynamic selector.
// Store the result in LDS to keep the permutation live.
let words = [
0x7E02_02FFu32,
0x817F_34AB,
0x7E04_02FF,
0xFE01_56CD,
0xD744_0003,
257 | (258 << 9) | (selector << 18),
0x3408_0082,
0xD834_0000,
0x0000_0304,
S_ENDPGM,
];
let code: Vec<u8> = words.iter()
.flat_map(|w| w.to_le_bytes())
.collect();
let native = NativeProgram {
stage: HwStage::Cs,
wave64: false,
vgprs: 256,
layout: vec![],
used: vec![],
io: StageIo::Cs(ComputeIo {
rsrc2: 1 << 15,
threads: [64, 1, 1],
}),
code,
};
let p = ir::build(&native.code, false, 256).unwrap();
assert!(p.blocks.iter()
.flat_map(|b| &b.insts)
.any(|i| i.name() == "v_perm_b32"));
let res = resource::track(
&p, &native.layout, native.stage
).unwrap();
let io = io::raise(&p, &native).unwrap();
let st = structure::structure(&p).unwrap();
let emitted = lower(&Parts {
native: &native,
p: &p,
st: &st,
io: &io,
res: &res,
inputs: vec![],
outputs: vec![],
prims: vec![],
mesh_stride: 0,
}).unwrap();
let container =
crate::Container::parse(&emitted.dxil).unwrap();
let features =
container.part(crate::FourCC::SFI0).unwrap();
u64::from_le_bytes(features.data.try_into().unwrap())
}
#[test]
fn perm_constant_selector_needs_no_int64() {
assert_eq!(perm_features(128) & 0x8000, 0);
}
#[test]
fn perm_dynamic_selector_needs_no_int64() {
assert_eq!(
perm_features(256) & 0x8000,
0,
"dynamic v_perm_b32 must not require INT64"
);
}
#[test]
fn scalar_bit_tests_lower() {
// Each tests one bit of a constant into SCC and branches on it (only