Merge upstream main (42361b0) into shader/per-lane-phi
main moved under #307 and this branch. One conflict, in the test module of emit/lower.rs: the imports take both sides' names (#117's `half_shift` beside `Lowered`), and #117's `scalar_bit_tests_lower` sits after this branch's waterfall tests. No code outside the test module was touched by hand. A merge rather than a rebase so #307's commits keep their ids. Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
commit
89639f014c
24 files changed
+1363
-191
No files matched your search
@@ -473,6 +473,37 @@ impl Module {
|
||||
}
|
||||
|
||||
/// The module as bitcode.
|
||||
/// Which instructions are written: every body instruction, the branch
|
||||
/// conditions, and whatever they read, transitively (a phi nothing reads
|
||||
/// is dropped). A worklist visits each instruction once, so a long chain
|
||||
/// of merges costs one pass, not one pass per merge.
|
||||
fn live(&self) -> Vec<bool> {
|
||||
let mut live = vec![false; self.insts.len()];
|
||||
let mut work: Vec<u32> = Vec::new();
|
||||
let mark = |live: &mut [bool], work: &mut Vec<u32>, v: V| {
|
||||
if let V::Inst(i) = v
|
||||
&& !live[i as usize]
|
||||
{
|
||||
live[i as usize] = true;
|
||||
work.push(i);
|
||||
}
|
||||
};
|
||||
for b in &self.blocks {
|
||||
for &i in &b.body {
|
||||
mark(&mut live, &mut work, V::Inst(i));
|
||||
}
|
||||
if let Some(Term::CondBr(c, ..)) = b.term {
|
||||
mark(&mut live, &mut work, c);
|
||||
}
|
||||
}
|
||||
while let Some(i) = work.pop() {
|
||||
for_each_operand(&self.insts[i as usize].op, |v| {
|
||||
mark(&mut live, &mut work, v)
|
||||
});
|
||||
}
|
||||
live
|
||||
}
|
||||
|
||||
pub fn write(&self) -> Vec<u8> {
|
||||
let mut w = Writer::default();
|
||||
w.bytes(b"BC\xC0\xDE");
|
||||
@@ -606,56 +637,12 @@ impl Module {
|
||||
w.exit();
|
||||
|
||||
// Phis nothing reads are left out (one register per phi makes many).
|
||||
let mut live = vec![false; self.insts.len()];
|
||||
let mark = |live: &mut Vec<bool>, v: V| {
|
||||
if let V::Inst(i) = v
|
||||
&& !live[i as usize]
|
||||
{
|
||||
live[i as usize] = true;
|
||||
return true;
|
||||
}
|
||||
false
|
||||
};
|
||||
let operands = |op: &Op| -> Vec<V> {
|
||||
match op {
|
||||
Op::Bin(_, a, b) | Op::Cmp(_, a, b) | Op::Store(a, b) | Op::AtomicRmw(_, a, b) => {
|
||||
vec![*a, *b]
|
||||
}
|
||||
Op::Cast(_, v) | Op::Extract(v, _) | Op::Load(v) => vec![*v],
|
||||
Op::Select(a, b, c) | Op::CmpXchg(a, b, c) => vec![*a, *b, *c],
|
||||
Op::Call(_, args) => args.clone(),
|
||||
Op::Phi(inc) => inc.iter().map(|x| x.0).collect(),
|
||||
Op::Gep(_, p, idx) => std::iter::once(*p).chain(idx.iter().copied()).collect(),
|
||||
}
|
||||
};
|
||||
for b in &self.blocks {
|
||||
for &i in &b.body {
|
||||
live[i as usize] = true;
|
||||
}
|
||||
if let Some(Term::CondBr(c, ..)) = b.term {
|
||||
mark(&mut live, c);
|
||||
}
|
||||
}
|
||||
let mut changed = true;
|
||||
while changed {
|
||||
changed = false;
|
||||
for (i, inst) in self.insts.iter().enumerate() {
|
||||
if live[i] {
|
||||
for v in operands(&inst.op) {
|
||||
changed |= mark(&mut live, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let live = self.live();
|
||||
|
||||
// Blocks are laid out in reverse postorder, so every block follows
|
||||
// its forward predecessors (drivers take a branch to an earlier
|
||||
// block for a loop's back edge).
|
||||
let succs = |b: usize| match self.blocks[b].term {
|
||||
Some(Term::Br(t)) => vec![t],
|
||||
Some(Term::CondBr(_, t, f)) => vec![t, f],
|
||||
_ => vec![],
|
||||
};
|
||||
let succs = |b: usize| successors(self.blocks[b].term);
|
||||
let mut seen = vec![false; self.blocks.len()];
|
||||
let mut post = Vec::new();
|
||||
let mut stack = vec![(0usize, 0usize)];
|
||||
@@ -685,7 +672,7 @@ impl Module {
|
||||
// DXC's loops; the AMD driver hangs on divergent loops otherwise.
|
||||
let mut preds = vec![Vec::new(); self.blocks.len()];
|
||||
for &b in &post {
|
||||
for s in succs(b) {
|
||||
for &s in succs(b).iter() {
|
||||
preds[s].push(b);
|
||||
}
|
||||
}
|
||||
@@ -795,6 +782,8 @@ impl Module {
|
||||
w.enter(FUNCTION_BLOCK, 4);
|
||||
w.record(1, &[layout.len() as u64]);
|
||||
let mut inst_id = base;
|
||||
// The operands of variable-length records, reused across them.
|
||||
let mut r: Vec<u64> = Vec::new();
|
||||
for b in layout.iter().map(|&b| &self.blocks[b]) {
|
||||
for &i in b.phis.iter().filter(|&&i| live[i as usize]).chain(&b.body) {
|
||||
let inst = &self.insts[i as usize];
|
||||
@@ -810,13 +799,15 @@ impl Module {
|
||||
Op::Select(c, t, f) => w.record(29, &[rel(*t), rel(*f), rel(*c)]),
|
||||
Op::Extract(a, idx) => w.record(26, &[rel(*a), u64::from(*idx)]),
|
||||
Op::Call(f, args) => {
|
||||
let mut r = vec![0, 1 << 15, u64::from(self.funcs[*f as usize].ty.0)];
|
||||
r.clear();
|
||||
r.extend([0, 1 << 15, u64::from(self.funcs[*f as usize].ty.0)]);
|
||||
r.push(rel(V::Func(*f)));
|
||||
r.extend(args.iter().map(|&a| rel(a)));
|
||||
w.record(34, &r);
|
||||
}
|
||||
Op::Phi(incoming) => {
|
||||
let mut r = vec![u64::from(inst.ty.0)];
|
||||
r.clear();
|
||||
r.push(u64::from(inst.ty.0));
|
||||
for &(v, b) in incoming {
|
||||
r.push(signed(i64::from(inst_id) - i64::from(abs(v))));
|
||||
r.push(from(b));
|
||||
@@ -824,7 +815,8 @@ impl Module {
|
||||
w.record(16, &r);
|
||||
}
|
||||
Op::Gep(ty, p, idx) => {
|
||||
let mut r = vec![0, u64::from(ty.0), rel(*p)];
|
||||
r.clear();
|
||||
r.extend([0, u64::from(ty.0), rel(*p)]);
|
||||
r.extend(idx.iter().map(|&i| rel(i)));
|
||||
w.record(43, &r);
|
||||
}
|
||||
@@ -868,6 +860,49 @@ const VALUE_SYMTAB_BLOCK: u64 = 14;
|
||||
const METADATA_BLOCK: u64 = 15;
|
||||
const TYPE_BLOCK: u64 = 17;
|
||||
|
||||
/// Calls `f` with every value `op` reads, in operand order.
|
||||
fn for_each_operand(op: &Op, mut f: impl FnMut(V)) {
|
||||
match op {
|
||||
Op::Bin(_, a, b) | Op::Cmp(_, a, b) | Op::Store(a, b) | Op::AtomicRmw(_, a, b) => {
|
||||
f(*a);
|
||||
f(*b);
|
||||
}
|
||||
Op::Cast(_, v) | Op::Extract(v, _) | Op::Load(v) => f(*v),
|
||||
Op::Select(a, b, c) | Op::CmpXchg(a, b, c) => {
|
||||
f(*a);
|
||||
f(*b);
|
||||
f(*c);
|
||||
}
|
||||
Op::Call(_, args) => args.iter().copied().for_each(f),
|
||||
Op::Phi(inc) => inc.iter().for_each(|x| f(x.0)),
|
||||
Op::Gep(_, p, idx) => {
|
||||
f(*p);
|
||||
idx.iter().copied().for_each(f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A block's successors (at most two), without allocating.
|
||||
struct Succs {
|
||||
to: [usize; 2],
|
||||
len: usize,
|
||||
}
|
||||
|
||||
impl std::ops::Deref for Succs {
|
||||
type Target = [usize];
|
||||
fn deref(&self) -> &[usize] {
|
||||
&self.to[..self.len]
|
||||
}
|
||||
}
|
||||
|
||||
fn successors(term: Option<Term>) -> Succs {
|
||||
match term {
|
||||
Some(Term::Br(t)) => Succs { to: [t, 0], len: 1 },
|
||||
Some(Term::CondBr(_, t, f)) => Succs { to: [t, f], len: 2 },
|
||||
_ => Succs { to: [0, 0], len: 0 },
|
||||
}
|
||||
}
|
||||
|
||||
fn chars(s: &str) -> Vec<u64> {
|
||||
s.bytes().map(u64::from).collect()
|
||||
}
|
||||
@@ -983,4 +1018,177 @@ mod tests {
|
||||
assert_eq!(signed(-3), 7);
|
||||
assert_eq!(signed(2), 4);
|
||||
}
|
||||
|
||||
/// A structured shader's shape: `stages` if/else diamonds in a row, each
|
||||
/// merge with one phi per register (as the lowering makes them), then a
|
||||
/// loop, and a tail that reads only the first `read` registers.
|
||||
fn synthetic(regs: usize, stages: usize, read: usize) -> Module {
|
||||
let mut m = Module::default();
|
||||
let i32t = m.int(32);
|
||||
let void = m.void();
|
||||
let fty = m.func_ty(i32t, vec![i32t, i32t]);
|
||||
let f = m.function("dx.op.synthetic", fty, Some(Attr::ReadNone), false);
|
||||
let main_ty = m.func_ty(void, vec![]);
|
||||
m.function("main", main_ty, None, true);
|
||||
let mut b = m.new_block();
|
||||
let mut cur: Vec<V> = (0..regs).map(|r| m.i32(r as u32)).collect();
|
||||
for s in 0..stages {
|
||||
let c = m.cmp(b, pred::EQ, cur[0], cur[s % regs]);
|
||||
let (t, e, j) = (m.new_block(), m.new_block(), m.new_block());
|
||||
m.terminate(b, Term::CondBr(c, t, e));
|
||||
let x = m.call(t, f, vec![cur[s % regs], cur[(s + 1) % regs]]);
|
||||
m.terminate(t, Term::Br(j));
|
||||
m.terminate(e, Term::Br(j));
|
||||
cur = (0..regs)
|
||||
.map(|r| {
|
||||
let p = m.phi(j, i32t);
|
||||
let tv = if r == s % regs { x } else { cur[r] };
|
||||
m.set_phi(p, vec![(tv, t), (cur[r], e)]);
|
||||
p
|
||||
})
|
||||
.collect();
|
||||
b = j;
|
||||
}
|
||||
// A loop: header phis fed back from an empty latch (which is folded).
|
||||
let (head, body, latch, exit) =
|
||||
(m.new_block(), m.new_block(), m.new_block(), m.new_block());
|
||||
m.terminate(b, Term::Br(head));
|
||||
let phis: Vec<V> = (0..regs).map(|_| m.phi(head, i32t)).collect();
|
||||
let one = m.i32(1);
|
||||
let stepped: Vec<V> = phis
|
||||
.iter()
|
||||
.map(|&p| m.bin(body, Bin::Add, p, one))
|
||||
.collect();
|
||||
let c = m.cmp(body, pred::IULT, stepped[0], cur[0]);
|
||||
m.terminate(head, Term::Br(body));
|
||||
m.terminate(body, Term::CondBr(c, latch, exit));
|
||||
m.terminate(latch, Term::Br(head));
|
||||
for r in 0..regs {
|
||||
m.set_phi(phis[r], vec![(cur[r], b), (stepped[r], latch)]);
|
||||
}
|
||||
let mut acc = stepped[0];
|
||||
for &v in &stepped[1..read.max(1)] {
|
||||
acc = m.bin(exit, Bin::Add, acc, v);
|
||||
}
|
||||
let _ = m.call(exit, f, vec![acc, cur[0]]);
|
||||
m.terminate(exit, Term::Ret);
|
||||
m
|
||||
}
|
||||
|
||||
/// FNV-1a, to pin a written module's exact bytes.
|
||||
fn fnv(bytes: &[u8]) -> u64 {
|
||||
bytes.iter().fold(0xcbf2_9ce4_8422_2325, |h, &b| {
|
||||
(h ^ u64::from(b)).wrapping_mul(0x0100_0000_01b3)
|
||||
})
|
||||
}
|
||||
|
||||
/// Liveness as `write` computed it before the worklist: whole passes over
|
||||
/// the instructions until one changes nothing.
|
||||
fn live_by_passes(m: &Module) -> Vec<bool> {
|
||||
let mut live = vec![false; m.insts.len()];
|
||||
for b in &m.blocks {
|
||||
for &i in &b.body {
|
||||
live[i as usize] = true;
|
||||
}
|
||||
if let Some(Term::CondBr(V::Inst(c), ..)) = b.term {
|
||||
live[c as usize] = true;
|
||||
}
|
||||
}
|
||||
let mut changed = true;
|
||||
while changed {
|
||||
changed = false;
|
||||
for (i, inst) in m.insts.iter().enumerate() {
|
||||
if live[i] {
|
||||
for_each_operand(&inst.op, |v| {
|
||||
if let V::Inst(j) = v
|
||||
&& !live[j as usize]
|
||||
{
|
||||
live[j as usize] = true;
|
||||
changed = true;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
live
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn worklist_liveness_matches_passes() {
|
||||
for (regs, stages, read) in [(1, 0, 1), (1, 1, 1), (4, 3, 1), (8, 17, 3), (16, 40, 16)] {
|
||||
let m = synthetic(regs, stages, read);
|
||||
let live = m.live();
|
||||
assert_eq!(live, live_by_passes(&m), "{regs} regs x {stages} stages");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unread_phis_are_dropped() {
|
||||
let mut m = Module::default();
|
||||
let i32t = m.int(32);
|
||||
let (entry, join) = (m.new_block(), m.new_block());
|
||||
let (a, b) = (m.i32(1), m.i32(2));
|
||||
m.terminate(entry, Term::Br(join));
|
||||
let read = m.phi(join, i32t);
|
||||
let unread = m.phi(join, i32t);
|
||||
// A cycle of phis that only feed each other stays dead.
|
||||
let cyc_a = m.phi(join, i32t);
|
||||
let cyc_b = m.phi(join, i32t);
|
||||
m.set_phi(read, vec![(a, entry)]);
|
||||
m.set_phi(unread, vec![(b, entry)]);
|
||||
m.set_phi(cyc_a, vec![(cyc_b, entry)]);
|
||||
m.set_phi(cyc_b, vec![(cyc_a, entry)]);
|
||||
let sum = m.bin(join, Bin::Add, read, b);
|
||||
m.terminate(join, Term::Ret);
|
||||
let ids = |v: V| match v {
|
||||
V::Inst(i) => i as usize,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
let live = m.live();
|
||||
assert!(live[ids(read)] && live[ids(sum)]);
|
||||
assert!(!live[ids(unread)] && !live[ids(cyc_a)] && !live[ids(cyc_b)]);
|
||||
assert_eq!(live, live_by_passes(&m));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn successors_of_each_terminator() {
|
||||
assert!(successors(None).is_empty());
|
||||
assert!(successors(Some(Term::Ret)).is_empty());
|
||||
assert_eq!(&*successors(Some(Term::Br(7))), &[7]);
|
||||
assert_eq!(&*successors(Some(Term::CondBr(V::Const(0), 3, 5))), &[3, 5]);
|
||||
}
|
||||
|
||||
/// The written bytes are pinned to what the writer produced before the
|
||||
/// worklist and the reused record buffers.
|
||||
#[test]
|
||||
fn written_bytes_unchanged() {
|
||||
for ((regs, stages, read), len, hash) in [
|
||||
((64, 64, 64), 38_916, 0x9586_730c_f953_4a45),
|
||||
((128, 256, 32), 295_952, 0x89e5_de2d_64f0_8785),
|
||||
] {
|
||||
let bytes = synthetic(regs, stages, read).write();
|
||||
assert_eq!((bytes.len(), fnv(&bytes)), (len, hash), "{regs}x{stages}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "benchmark: cargo test --release -p victory-shader bitcode -- --ignored --nocapture"]
|
||||
fn bench_write() {
|
||||
for (regs, stages, read) in [(64, 64, 64), (128, 256, 32), (256, 512, 256)] {
|
||||
let m = synthetic(regs, stages, read);
|
||||
let bytes = m.write();
|
||||
let runs = 5;
|
||||
let t = std::time::Instant::now();
|
||||
for _ in 0..runs {
|
||||
std::hint::black_box(m.write());
|
||||
}
|
||||
let per = t.elapsed() / runs;
|
||||
println!(
|
||||
"write {regs} regs x {stages} stages ({} insts): {per:?}, {} bytes, fnv {:#018x}",
|
||||
m.insts.len(),
|
||||
bytes.len(),
|
||||
fnv(&bytes)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -107,33 +107,46 @@ fn buffer_format(format: u8) -> Option<(&'static [u32], Numeric)> {
|
||||
const NORM: &[Numeric] = &[Unorm, Snorm, Uscaled, Sscaled, Uint, Sint];
|
||||
const ALL: &[Numeric] = &[Unorm, Snorm, Uscaled, Sscaled, Uint, Sint, Float];
|
||||
const WIDE: &[Numeric] = &[Uint, Sint, Float];
|
||||
// `None`: a family not decoded (packed floats, 10_10_10_2).
|
||||
const FAMILIES: [(Option<&[u32]>, &[Numeric]); 14] = [
|
||||
(Some(&[8]), NORM),
|
||||
(Some(&[16]), ALL),
|
||||
(Some(&[8, 8]), NORM),
|
||||
(Some(&[32]), WIDE),
|
||||
(Some(&[16, 16]), ALL),
|
||||
(None, ALL), // 10_11_11
|
||||
(None, ALL), // 11_11_10
|
||||
(None, NORM), // 10_10_10_2
|
||||
(Some(&[10, 10, 10, 2]), NORM), // 2_10_10_10
|
||||
(Some(&[8, 8, 8, 8]), NORM),
|
||||
(Some(&[32, 32]), WIDE),
|
||||
(Some(&[16, 16, 16, 16]), ALL),
|
||||
(Some(&[32, 32, 32]), WIDE),
|
||||
(Some(&[32, 32, 32, 32]), WIDE),
|
||||
// Numbers and order from LLVM's `UfmtGFX10` (`SIDefines.h`); the GFX10
|
||||
// buffer formats have no 5_9_9_9. Channel order from Mesa
|
||||
// (`ac_formats.c`): R11G11B10_FLOAT is 10_11_11, and 10_10_10_2 has
|
||||
// its 2-bit channel in the lowest bits.
|
||||
const FAMILIES: [(&[u32], &[Numeric]); 14] = [
|
||||
(&[8], NORM),
|
||||
(&[16], ALL),
|
||||
(&[8, 8], NORM),
|
||||
(&[32], WIDE),
|
||||
(&[16, 16], ALL),
|
||||
(&[11, 11, 10], ALL), // 10_11_11
|
||||
(&[10, 11, 11], ALL), // 11_11_10
|
||||
(&[2, 10, 10, 10], NORM), // 10_10_10_2
|
||||
(&[10, 10, 10, 2], NORM), // 2_10_10_10
|
||||
(&[8, 8, 8, 8], NORM),
|
||||
(&[32, 32], WIDE),
|
||||
(&[16, 16, 16, 16], ALL),
|
||||
(&[32, 32, 32], WIDE),
|
||||
(&[32, 32, 32, 32], WIDE),
|
||||
];
|
||||
let mut n = usize::from(format.checked_sub(1)?);
|
||||
for (bits, numerics) in FAMILIES {
|
||||
if n < numerics.len() {
|
||||
return Some((bits?, numerics[n]));
|
||||
return Some((bits, numerics[n]));
|
||||
}
|
||||
n -= numerics.len();
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// How far a float channel of `width` bits (16, or the unsigned 11- and
|
||||
/// 10-bit floats of the packed formats) shifts up to be a half:
|
||||
/// the small floats are a half's exponent (5 bits, biased 15) and the top
|
||||
/// of its mantissa, without the sign, so `legacyF16ToF32` widens them
|
||||
/// exactly, denormals, infinities and NaNs included.
|
||||
fn half_shift(width: u32) -> u32 {
|
||||
debug_assert!(matches!(width, 10 | 11 | 16), "{width}-bit float");
|
||||
if width == 16 { 0 } else { 15 - width }
|
||||
}
|
||||
|
||||
struct Lower<'a> {
|
||||
s: Shader,
|
||||
x: &'a Parts<'a>,
|
||||
@@ -2314,6 +2327,29 @@ impl<'a> Lower<'a> {
|
||||
_ => Ok(l.s.m.i32(0)),
|
||||
}
|
||||
};
|
||||
if let Some(op) = name.strip_prefix("s_bitcmp") {
|
||||
// SCC is whether bit src1 (mod 32, or mod 64 for the 64-bit form)
|
||||
// of src0 is clear (bitcmp0) or set (bitcmp1).
|
||||
let (bit, k31) = (src32(self, 1)?, self.s.m.i32(31));
|
||||
let idx = self.s.m.bin(c, Bin::And, bit, k31);
|
||||
let word = if op.ends_with("_b64") {
|
||||
let (a, b) = (half(self, 0, 0)?, half(self, 0, 1)?);
|
||||
let k32 = self.s.m.i32(32);
|
||||
let upper = self.s.m.bin(c, Bin::And, bit, k32);
|
||||
let z = self.s.m.i32(0);
|
||||
let upper = self.s.m.cmp(c, pred::NE, upper, z);
|
||||
self.s.m.select(c, upper, b, a)
|
||||
} else {
|
||||
src32(self, 0)?
|
||||
};
|
||||
let shifted = self.s.m.bin(c, Bin::LShr, word, idx);
|
||||
let one = self.s.m.i32(1);
|
||||
let tested = self.s.m.bin(c, Bin::And, shifted, one);
|
||||
let want = self.s.m.i32(u32::from(op.starts_with('1')));
|
||||
let r = self.s.m.cmp(c, pred::EQ, tested, want);
|
||||
self.set(scc, Val::Bit(r));
|
||||
return Ok(());
|
||||
}
|
||||
if let Some(op) = name.strip_prefix("s_cmp_").filter(|o| o.ends_with("_u64")) {
|
||||
// eq and lg only: both dwords.
|
||||
let mut r = None;
|
||||
@@ -3051,11 +3087,10 @@ impl<'a> Lower<'a> {
|
||||
/// typed loads do (channels past the format's read 0, 0, 0, 1).
|
||||
// shortcut: the descriptor's channel swizzle is taken as the identity
|
||||
// (it is for raw views and the descriptors code builds), elements of 4
|
||||
// bytes or more as dword-aligned, and the packed float and 10_10_10_2
|
||||
// layouts are refused; decode them when a program needs them. Norm
|
||||
// formats divide, which the AMD driver does 1 ULP off the hardware's
|
||||
// correctly rounded conversion for some values; correct it with an
|
||||
// integer check if a title compares converted values exactly.
|
||||
// bytes or more as dword-aligned. Norm formats divide, which the AMD
|
||||
// driver does 1 ULP off the hardware's correctly rounded conversion for
|
||||
// some values; correct it with an integer check if a title compares
|
||||
// converted values exactly.
|
||||
fn format_load(&mut self, h: V, off: V, format: u8, channels: u32) -> Result<Vec<V>, Why> {
|
||||
let (bits, numeric) = buffer_format(format).ok_or("a buffer format")?;
|
||||
let c = self.cur;
|
||||
@@ -3142,9 +3177,16 @@ impl<'a> Lower<'a> {
|
||||
Numeric::Sint => signed(self),
|
||||
Numeric::Float if w == 32 => field,
|
||||
Numeric::Float => {
|
||||
let h = match half_shift(w) {
|
||||
0 => field,
|
||||
k => {
|
||||
let sh = self.s.m.i32(k);
|
||||
self.s.m.bin(c, Bin::Shl, field, sh)
|
||||
}
|
||||
};
|
||||
let f = self
|
||||
.s
|
||||
.op(c, "legacyF16ToF32", Attr::ReadNone, f32t, 131, &[field]);
|
||||
.op(c, "legacyF16ToF32", Attr::ReadNone, f32t, 131, &[h]);
|
||||
float(self, f)
|
||||
}
|
||||
};
|
||||
@@ -5098,28 +5140,50 @@ impl<'a> Lower<'a> {
|
||||
let (op, ty) = rest.rsplit_once('_').ok_or("a compare")?;
|
||||
let c = self.cur;
|
||||
let r = match ty {
|
||||
"f32" if op == "class" => {
|
||||
"f32" | "f16" if op == "class" => {
|
||||
// Bit k of src1 tests class k: signalling NaN, quiet NaN,
|
||||
// -inf, -normal, -denormal, -0, +0, +denormal, +normal, +inf.
|
||||
let x = self.fsrc(inst, 0)?;
|
||||
let x = self.as_int(x);
|
||||
let f16 = ty == "f16";
|
||||
let x = if f16 {
|
||||
let Fields::Valu(v) = &inst.fields else {
|
||||
unreachable!()
|
||||
};
|
||||
let mut h = self.half_src(inst, 0)?;
|
||||
if v.abs & 1 != 0 {
|
||||
let m = self.s.m.i32(0x7FFF);
|
||||
h = self.s.m.bin(c, Bin::And, h, m);
|
||||
}
|
||||
if v.neg & 1 != 0 {
|
||||
let s = self.s.m.i32(0x8000);
|
||||
h = self.s.m.bin(c, Bin::Xor, h, s);
|
||||
}
|
||||
h
|
||||
} else {
|
||||
let x = self.fsrc(inst, 0)?;
|
||||
self.as_int(x)
|
||||
};
|
||||
let mask = self.src(inst, 1)?;
|
||||
let k = |l: &mut Self, v: u32| l.s.m.i32(v);
|
||||
let (k23, k31, kff, mant, quiet) = (
|
||||
k(self, 23),
|
||||
k(self, 31),
|
||||
k(self, 0xFF),
|
||||
k(self, 0x7F_FFFF),
|
||||
k(self, 0x40_0000),
|
||||
let (exp_sh, sign_sh, exp_mask, mant_mask, quiet_bit) = if f16 {
|
||||
(10, 15, 0x1F, 0x03FF, 0x0200)
|
||||
} else {
|
||||
(23, 31, 0xFF, 0x7F_FFFF, 0x40_0000)
|
||||
};
|
||||
let (k_exp, k_sign, k_emax, mant, quiet) = (
|
||||
k(self, exp_sh),
|
||||
k(self, sign_sh),
|
||||
k(self, exp_mask),
|
||||
k(self, mant_mask),
|
||||
k(self, quiet_bit),
|
||||
);
|
||||
let e = self.s.m.bin(c, Bin::LShr, x, k23);
|
||||
let e = self.s.m.bin(c, Bin::And, e, kff);
|
||||
let e = self.s.m.bin(c, Bin::LShr, x, k_exp);
|
||||
let e = self.s.m.bin(c, Bin::And, e, k_emax);
|
||||
let m = self.s.m.bin(c, Bin::And, x, mant);
|
||||
let neg = self.s.m.bin(c, Bin::LShr, x, k31);
|
||||
let neg = self.s.m.bin(c, Bin::LShr, x, k_sign);
|
||||
let zero = k(self, 0);
|
||||
let (e0, e255) = (
|
||||
let (e0, emax) = (
|
||||
self.s.m.cmp(c, pred::EQ, e, zero),
|
||||
self.s.m.cmp(c, pred::EQ, e, kff),
|
||||
self.s.m.cmp(c, pred::EQ, e, k_emax),
|
||||
);
|
||||
let m0 = self.s.m.cmp(c, pred::EQ, m, zero);
|
||||
let q = self.s.m.bin(c, Bin::And, x, quiet);
|
||||
@@ -5129,7 +5193,7 @@ impl<'a> Lower<'a> {
|
||||
(k(self, 8), k(self, 9), k(self, 6), k(self, 7));
|
||||
let t = self.s.m.select(c, m0, zero_c, denorm);
|
||||
let t = self.s.m.select(c, e0, t, normal);
|
||||
let t = self.s.m.select(c, e255, inf, t);
|
||||
let t = self.s.m.select(c, emax, inf, t);
|
||||
let eleven = k(self, 11);
|
||||
let mirrored = self.s.m.bin(c, Bin::Sub, eleven, t);
|
||||
let is_neg = self.s.m.cmp(c, pred::NE, neg, zero);
|
||||
@@ -5138,7 +5202,7 @@ impl<'a> Lower<'a> {
|
||||
let nan_class = self.s.m.select(c, q, one, snan);
|
||||
let yes = self.s.m.bool(true);
|
||||
let nan = self.s.m.bin(c, Bin::Xor, m0, yes);
|
||||
let nan = self.s.m.bin(c, Bin::And, nan, e255);
|
||||
let nan = self.s.m.bin(c, Bin::And, nan, emax);
|
||||
let t = self.s.m.select(c, nan, nan_class, t);
|
||||
let bit = self.s.m.bin(c, Bin::LShr, mask, t);
|
||||
let bit = self.s.m.bin(c, Bin::And, bit, one);
|
||||
@@ -6720,7 +6784,7 @@ impl<'a> Lower<'a> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{Lowered, Numeric, Parts, buffer_format, lower};
|
||||
use super::{Lowered, Numeric, Parts, buffer_format, half_shift, lower};
|
||||
use crate::native::{
|
||||
ComputeIo, HwStage, NativeProgram, ResourceType, RootParam, RootType, StageIo, UsedResource,
|
||||
};
|
||||
@@ -6960,6 +7024,35 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scalar_bit_tests_lower() {
|
||||
// Each tests one bit of a constant into SCC and branches on it (only
|
||||
// code with an effect needs its operands).
|
||||
let tail = [
|
||||
0xBF85_0001, // s_cbranch_scc1 -> s_endpgm
|
||||
0xBE84_0380, // s_mov_b32 s4, 0
|
||||
S_ENDPGM,
|
||||
];
|
||||
let bit_tests: [&[u32]; 4] = [
|
||||
&[0xBE81_0387, 0xBF0C_8301], // s_mov_b32 s1, 7; s_bitcmp0_b32 s1, 3
|
||||
&[0xBE81_0387, 0xBF0D_8301], // s_mov_b32 s1, 7; s_bitcmp1_b32 s1, 3
|
||||
&[
|
||||
0xBE80_0387, // s_mov_b32 s0, 7
|
||||
0xBE81_0380, // s_mov_b32 s1, 0
|
||||
0xBF0E_8300, // s_bitcmp0_b64 s[0:1], 3
|
||||
],
|
||||
&[
|
||||
0xBE80_0387, // s_mov_b32 s0, 7
|
||||
0xBE81_0380, // s_mov_b32 s1, 0
|
||||
0xBF0F_8300, // s_bitcmp1_b64 s[0:1], 3
|
||||
],
|
||||
];
|
||||
for head in bit_tests {
|
||||
let words: Vec<u32> = head.iter().chain(&tail).copied().collect();
|
||||
lowers(&words).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn buffer_formats_by_number() {
|
||||
// The numbers the disassembler names 8_8_8_8_UNORM, 16_16_SNORM,
|
||||
@@ -6975,9 +7068,81 @@ mod tests {
|
||||
buffer_format(77),
|
||||
Some((&[32, 32, 32, 32][..], Numeric::Float))
|
||||
);
|
||||
// Invalid, a packed float, past the table.
|
||||
// Invalid, past the table.
|
||||
assert_eq!(buffer_format(0), None);
|
||||
assert_eq!(buffer_format(36), None);
|
||||
assert_eq!(buffer_format(78), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn packed_buffer_formats_by_number() {
|
||||
// LLVM's `UfmtGFX10`: 10_11_11 is 30-36, 11_11_10 37-43,
|
||||
// 10_10_10_2 44-49. Channels from the lowest bits: R11G11B10_FLOAT
|
||||
// is 10_11_11 (Mesa), 10_10_10_2 has the 2-bit channel first.
|
||||
assert_eq!(buffer_format(30), Some((&[11, 11, 10][..], Numeric::Unorm)));
|
||||
assert_eq!(buffer_format(36), Some((&[11, 11, 10][..], Numeric::Float)));
|
||||
assert_eq!(buffer_format(37), Some((&[10, 11, 11][..], Numeric::Unorm)));
|
||||
assert_eq!(buffer_format(43), Some((&[10, 11, 11][..], Numeric::Float)));
|
||||
assert_eq!(
|
||||
buffer_format(44),
|
||||
Some((&[2, 10, 10, 10][..], Numeric::Unorm))
|
||||
);
|
||||
assert_eq!(
|
||||
buffer_format(45),
|
||||
Some((&[2, 10, 10, 10][..], Numeric::Snorm))
|
||||
);
|
||||
assert_eq!(
|
||||
buffer_format(46),
|
||||
Some((&[2, 10, 10, 10][..], Numeric::Uscaled))
|
||||
);
|
||||
assert_eq!(
|
||||
buffer_format(47),
|
||||
Some((&[2, 10, 10, 10][..], Numeric::Sscaled))
|
||||
);
|
||||
assert_eq!(
|
||||
buffer_format(48),
|
||||
Some((&[2, 10, 10, 10][..], Numeric::Uint))
|
||||
);
|
||||
assert_eq!(
|
||||
buffer_format(49),
|
||||
Some((&[2, 10, 10, 10][..], Numeric::Sint))
|
||||
);
|
||||
}
|
||||
|
||||
/// A half's value (sign, 5-bit exponent biased 15, 10-bit mantissa), as
|
||||
/// `legacyF16ToF32` widens it.
|
||||
fn half(h: u32) -> f32 {
|
||||
let (s, e, m) = ((h >> 15) & 1, (h >> 10) & 31, h & 1023);
|
||||
let v = match e {
|
||||
0 => m as f32 * 2f32.powi(-24),
|
||||
31 if m == 0 => f32::INFINITY,
|
||||
31 => f32::NAN,
|
||||
_ => (1024 + m) as f32 * 2f32.powi(e as i32 - 25),
|
||||
};
|
||||
if s == 1 { -v } else { v }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn small_floats_widen_as_halves() {
|
||||
// Unsigned 11- and 10-bit floats: a 5-bit exponent biased 15 over a
|
||||
// 6- or 5-bit mantissa, no sign; values from the D3D/GL spec.
|
||||
let f11 = |x: u32| half(x << half_shift(11));
|
||||
let f10 = |x: u32| half(x << half_shift(10));
|
||||
assert_eq!(f11(0), 0.0);
|
||||
assert_eq!(f11(0x001), 2f32.powi(-20)); // smallest denormal
|
||||
assert_eq!(f11(0x03F), 63.0 * 2f32.powi(-20)); // largest denormal
|
||||
assert_eq!(f11(0x040), 2f32.powi(-14)); // smallest normal
|
||||
assert_eq!(f11(0x3C0), 1.0);
|
||||
assert_eq!(f11(0x3E0), 1.5);
|
||||
assert_eq!(f11(0x7BF), 65024.0); // largest
|
||||
assert_eq!(f11(0x7C0), f32::INFINITY);
|
||||
assert!(f11(0x7C1).is_nan());
|
||||
assert_eq!(f10(0x001), 2f32.powi(-19));
|
||||
assert_eq!(f10(0x020), 2f32.powi(-14));
|
||||
assert_eq!(f10(0x1E0), 1.0);
|
||||
assert_eq!(f10(0x3DF), 64512.0);
|
||||
assert_eq!(f10(0x3E0), f32::INFINITY);
|
||||
assert!(f10(0x3FF).is_nan());
|
||||
// A 16-bit float is a half already, sign included.
|
||||
assert_eq!(half(0xBC00 << half_shift(16)), -1.0);
|
||||
}
|
||||
}
|
||||
@@ -674,3 +674,73 @@ fn bfe_signed_and_unsigned_match_rdna2() {
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn v_cmp_class_f16_matches_rdna2() {
|
||||
// v_cmp_class_f16_e64 s6, v1, v2 (VOP3 opcode 0x08f); v_cndmask_b32_e64 v0, 0, 1, s6
|
||||
let cmp_lo = build_valu_cs(&[
|
||||
vop3(0x08f, 6, vgpr(1), vgpr(2), 0)[0],
|
||||
vop3(0x08f, 6, vgpr(1), vgpr(2), 0)[1],
|
||||
vop3(0x101, 0, 0x80, 0x81, sgpr(6))[0],
|
||||
vop3(0x101, 0, 0x80, 0x81, sgpr(6))[1],
|
||||
]);
|
||||
// Same with op_sel:[1,0] (high half of v1) + neg on src0 (bit 29 of w1).
|
||||
let [mut w0, mut w1] = vop3(0x08f, 6, vgpr(1), vgpr(2), 0);
|
||||
w0 |= 1 << 11; // opsel[0] = 1 (high half)
|
||||
w1 |= 1 << 29; // neg[0] = 1
|
||||
let cmp_hi_neg = build_valu_cs(&[
|
||||
w0,
|
||||
w1,
|
||||
vop3(0x101, 0, 0x80, 0x81, sgpr(6))[0],
|
||||
vop3(0x101, 0, 0x80, 0x81, sgpr(6))[1],
|
||||
]);
|
||||
|
||||
// All 10 IEEE-754 binary16 classes: bit k corresponds to class k.
|
||||
let classes: &[(u32, u32)] = &[
|
||||
(0x7C01, 0), // sNaN (+sNaN, quiet bit 0x0200 clear)
|
||||
(0xFDFF, 0), // -sNaN (sign ignored for NaNs)
|
||||
(0x7E00, 1), // +qNaN
|
||||
(0xFE01, 1), // -qNaN
|
||||
(0xFC00, 2), // -inf
|
||||
(0xBC00, 3), // -normal (-1.0)
|
||||
(0x8001, 4), // -denormal
|
||||
(0x8000, 5), // -0.0
|
||||
(0x0000, 6), // +0.0
|
||||
(0x0001, 7), // +denormal
|
||||
(0x3C00, 8), // +normal (+1.0)
|
||||
(0x7C00, 9), // +inf
|
||||
];
|
||||
let mut cases = Vec::new();
|
||||
for &(bits, cls) in classes {
|
||||
// Exact class bit -> true; complement of class bit across all 10 bits -> false.
|
||||
cases.push(([bits, 1u32 << cls, 0, 0], 1u32));
|
||||
cases.push(([bits, 0x3FF & !(1u32 << cls), 0, 0], 0u32));
|
||||
}
|
||||
let inputs: Vec<[u32; 4]> = cases.iter().map(|c| c.0).collect();
|
||||
run_cases(&cmp_lo, &inputs, |backend, i, inp, got| {
|
||||
let want = cases[i].1;
|
||||
assert_eq!(
|
||||
got, want,
|
||||
"{backend}: v_cmp_class_f16({:#06x}, {:#05x}) = {got}, want {want}",
|
||||
inp[0], inp[1],
|
||||
);
|
||||
});
|
||||
|
||||
// High half with neg: high half 0x3C00 (+1.0) negated -> -normal (class 3);
|
||||
// high half 0x7C01 (+sNaN) negated -> still sNaN (class 0, not quieted).
|
||||
let hi_cases: &[([u32; 4], u32)] = &[
|
||||
([0x3C00_0000, 1 << 3, 0, 0], 1),
|
||||
([0x3C00_0000, 1 << 8, 0, 0], 0),
|
||||
([0x7C01_FFFF, 1 << 0, 0, 0], 1),
|
||||
([0x7C01_FFFF, 1 << 1, 0, 0], 0),
|
||||
];
|
||||
let hi_in: Vec<[u32; 4]> = hi_cases.iter().map(|c| c.0).collect();
|
||||
run_cases(&cmp_hi_neg, &hi_in, |backend, i, inp, got| {
|
||||
let want = hi_cases[i].1;
|
||||
assert_eq!(
|
||||
got, want,
|
||||
"{backend}: v_cmp_class_f16(-hi({:#010x}), {:#05x}) = {got}, want {want}",
|
||||
inp[0], inp[1],
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -236,7 +236,7 @@ pub(super) const SOPP: &[(u16, &str)] = &[
|
||||
(0x023, "s_waitcnt_depctr"),
|
||||
(0x024, "s_round_mode"),
|
||||
(0x025, "s_denorm_mode"),
|
||||
(0x026, "s_ttracedata_imm"),
|
||||
(0x028, "s_ttracedata_imm"),
|
||||
];
|
||||
|
||||
pub(super) const SMEM: &[(u16, &str)] = &[
|
||||
@@ -1313,3 +1313,21 @@ pub(super) const SCRATCH: &[(u16, &str)] = &[
|
||||
(0x024, "scratch_load_short_d16"),
|
||||
(0x025, "scratch_load_short_d16_hi"),
|
||||
];
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
fn bytes(words: &[u32]) -> Vec<u8> {
|
||||
words.iter().flat_map(|w| w.to_le_bytes()).collect()
|
||||
}
|
||||
|
||||
/// `s_ttracedata_imm 0x1234` as LLVM's GFX10 disassembler tests have it
|
||||
/// (`gfx10_sopp.txt`: `0x34,0x12,0xa8,0xbf`), SOPP opcode 0x28 as in
|
||||
/// AMD's RDNA2 ISA. A thread-trace marker, which translation drops.
|
||||
#[test]
|
||||
fn s_ttracedata_imm_is_sopp_0x28() {
|
||||
let code = bytes(&[0xBFA8_1234, 0xBF81_0000]);
|
||||
let decoded = super::super::decode(&code).unwrap();
|
||||
assert_eq!(decoded[0].name(), Some("s_ttracedata_imm"));
|
||||
assert!(crate::ir::build(&code, false, 1).is_ok());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user