Shaders: decode GFX10 packed inline constants by operand type

This commit is contained in:
AlanQiang0418 committed 2026-11-24 01:01:55 -05:00
1 parent 35e8407c9c
commit 580e6891c0
2 files changed
+45 -21

No files matched your search

+24 -21
View File
@@ -1210,6 +1210,19 @@ impl<'a> Lower<'a> {
}
}
/// GFX10 puts an inline f16 constant in the low half of the operand.
fn f16_operand(&mut self, inst: &Inst, op: &Operand) -> Result<V, Why> {
match *op {
Operand::Const(c @ 240..=248) => {
let h = [
0x3800, 0xB800, 0x3C00, 0xBC00, 0x4000, 0xC000, 0x4400, 0xC400, 0x3118,
][usize::from(c - 240)];
Ok(self.s.m.i32(h))
}
_ => self.operand(inst, op),
}
}
fn as_float(&mut self, x: V) -> V {
self.s.m.cast(self.cur, Cast::Bitcast, x, self.s.t.f32)
}
@@ -4078,15 +4091,9 @@ impl<'a> Lower<'a> {
_ => return Err("a byte select on a 16-bit operation"),
}
}
let x = match (op, self.src0) {
(_, Some(x)) if k == 0 => x,
(Operand::Const(c @ 240..=248), _) => {
let h = [
0x3800, 0xB800, 0x3C00, 0xBC00, 0x4000, 0xC000, 0x4400, 0xC400, 0x3118,
][usize::from(c - 240)];
self.s.m.i32(h)
}
_ => self.operand(inst, &op)?,
let x = match self.src0 {
Some(x) if k == 0 => x,
_ => self.f16_operand(inst, &op)?,
};
let c = self.cur;
let x = if high {
@@ -4417,22 +4424,18 @@ impl<'a> Lower<'a> {
/// Half `high` of source `k` of a packed (VOP3P) operation, in the low
/// 16 bits: op_sel picks the half the low result reads, op_sel_hi the
/// half the high result reads. Inline float constants are f16 values.
fn packed_src(&mut self, inst: &Inst, k: usize, high: bool) -> Result<V, Why> {
/// half the high result reads. Packed integer instructions see the f32
/// bits of an inline float constant.
fn packed_src(&mut self, inst: &Inst, k: usize, high: bool, float: bool) -> Result<V, Why> {
let Fields::Valu(v) = &inst.fields else {
unreachable!()
};
let op = *v.src.get(k).ok_or("a missing operand")?;
let sel = if high { v.opsel_hi } else { v.opsel } >> k & 1 != 0;
let x = match op {
Operand::Const(c @ 240..=248) => {
let h = [
0x3800, 0xB800, 0x3C00, 0xBC00, 0x4000, 0xC000, 0x4400, 0xC400, 0x3118,
][usize::from(c - 240)];
// shortcut: taken to fill both halves.
self.s.m.i32(h | h << 16)
}
_ => self.operand(inst, &op)?,
let x = if float {
self.f16_operand(inst, &op)?
} else {
self.operand(inst, &op)?
};
let c = self.cur;
let x = if sel {
@@ -4468,7 +4471,7 @@ impl<'a> Lower<'a> {
for high in [false, true] {
let mut xs = Vec::new();
for k in 0..n {
let h = self.packed_src(inst, k, high)?;
let h = self.packed_src(inst, k, high, float)?;
if float {
let mut f = self
.s
@@ -231,6 +231,27 @@ fn f16_to_f32(bits: u16) -> f32 {
f32::from_bits(f32_bits)
}
#[test]
fn packed_add_selects_halves_of_inline_constants() {
// Add the inline constant 1/(2*pi) to v1 (zero) to read back its two halves.
// The same GFX10 source field (248) has a different bit pattern for a
// packed floating-point and a packed integer instruction.
let cases = [
// v_pk_add_f16 v0, 0x3118, v1
([0xcc0f4000, 0x1a0202f8], 0x0000_3118),
// v_pk_add_f16 v0, 0x3118, v1 op_sel_hi:[0,1] (use low twice)
([0xcc0f4000, 0x120202f8], 0x3118_3118),
// v_pk_add_u16 v0, 0x3e22f983, v1
([0xcc0a4000, 0x1a0202f8], 0x3e22_f983),
];
for (words, expected) in cases {
let shader = build_valu_cs(&words);
run_cases(&shader, &[[0; 4]], |backend, _, _, got| {
assert_eq!(got, expected, "{backend}: {words:08x?}");
});
}
}
#[test]
fn v_med3_f32_matches_rdna2_with_nan() {
// v_med3_f32 v0, v1, v2, v3 (VOP3 opcode 0x157)