Merge pull request #637 from AlanQiang0418/codex/gfx10-packed-inline-constants
Shaders: decode GFX10 packed inline constants by operand type
This commit is contained in:
2 files changed
+45
-21
No files matched your search
@@ -1231,6 +1231,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 {
|
fn as_float(&mut self, x: V) -> V {
|
||||||
self.s.m.cast(self.cur, Cast::Bitcast, x, self.s.t.f32)
|
self.s.m.cast(self.cur, Cast::Bitcast, x, self.s.t.f32)
|
||||||
}
|
}
|
||||||
@@ -4147,15 +4160,9 @@ impl<'a> Lower<'a> {
|
|||||||
_ => return Err("a byte select on a 16-bit operation"),
|
_ => return Err("a byte select on a 16-bit operation"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let x = match (op, self.src0) {
|
let x = match self.src0 {
|
||||||
(_, Some(x)) if k == 0 => x,
|
Some(x) if k == 0 => x,
|
||||||
(Operand::Const(c @ 240..=248), _) => {
|
_ => self.f16_operand(inst, &op)?,
|
||||||
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 c = self.cur;
|
let c = self.cur;
|
||||||
let x = if high {
|
let x = if high {
|
||||||
@@ -4574,22 +4581,18 @@ impl<'a> Lower<'a> {
|
|||||||
|
|
||||||
/// Half `high` of source `k` of a packed (VOP3P) operation, in the low
|
/// 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
|
/// 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.
|
/// half the high result reads. Packed integer instructions see the f32
|
||||||
fn packed_src(&mut self, inst: &Inst, k: usize, high: bool) -> Result<V, Why> {
|
/// 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 {
|
let Fields::Valu(v) = &inst.fields else {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
};
|
};
|
||||||
let op = *v.src.get(k).ok_or("a missing operand")?;
|
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 sel = if high { v.opsel_hi } else { v.opsel } >> k & 1 != 0;
|
||||||
let x = match op {
|
let x = if float {
|
||||||
Operand::Const(c @ 240..=248) => {
|
self.f16_operand(inst, &op)?
|
||||||
let h = [
|
} else {
|
||||||
0x3800, 0xB800, 0x3C00, 0xBC00, 0x4000, 0xC000, 0x4400, 0xC400, 0x3118,
|
self.operand(inst, &op)?
|
||||||
][usize::from(c - 240)];
|
|
||||||
// shortcut: taken to fill both halves.
|
|
||||||
self.s.m.i32(h | h << 16)
|
|
||||||
}
|
|
||||||
_ => self.operand(inst, &op)?,
|
|
||||||
};
|
};
|
||||||
let c = self.cur;
|
let c = self.cur;
|
||||||
let x = if sel {
|
let x = if sel {
|
||||||
@@ -4625,7 +4628,7 @@ impl<'a> Lower<'a> {
|
|||||||
for high in [false, true] {
|
for high in [false, true] {
|
||||||
let mut xs = Vec::new();
|
let mut xs = Vec::new();
|
||||||
for k in 0..n {
|
for k in 0..n {
|
||||||
let h = self.packed_src(inst, k, high)?;
|
let h = self.packed_src(inst, k, high, float)?;
|
||||||
if float {
|
if float {
|
||||||
let mut f = self
|
let mut f = self
|
||||||
.s
|
.s
|
||||||
|
|||||||
@@ -236,6 +236,27 @@ fn f16_to_f32(bits: u16) -> f32 {
|
|||||||
f32::from_bits(f32_bits)
|
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]
|
#[test]
|
||||||
fn v_med3_f32_matches_rdna2_with_nan() {
|
fn v_med3_f32_matches_rdna2_with_nan() {
|
||||||
// v_med3_f32 v0, v1, v2, v3 (VOP3 opcode 0x157)
|
// v_med3_f32 v0, v1, v2, v3 (VOP3 opcode 0x157)
|
||||||
|
|||||||
Reference in new issue
Block a user