Skip to content
Merged
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
36 changes: 36 additions & 0 deletions crates/xtp-shader/src/emit/valu_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -998,3 +998,39 @@ fn v_cmp_class_f16_matches_rdna2() {
);
});
}

#[test]
fn legacy_mad_mac_round_product_and_preserve_zero_rule() {
let a = 1.0 + 1.0 / 4096.0;
let c = -(1.0 + 1.0 / 2048.0);
let cases = [
// The rounded product cancels c. A fused operation gives 2^-24.
([a, a, c, 0.0], 0.0),
([0.0, f32::INFINITY, 1.0, 0.0], 1.0),
([f32::NAN, 0.0, 1.0, 0.0], 1.0),
// The legacy product is +0, so adding -0 must still give +0.
([0.0, -5.0, -0.0, 0.0], 0.0),
];
let inputs = cases.map(|(input, _)| input.map(f32::to_bits));

let mad = vop3(0x140, 0, vgpr(1), vgpr(2), vgpr(3));
let mac_e32 = 0x006 << 25 | 2 << 9 | u32::from(vgpr(1)); // v0, v1, v2
let mac_e64 = vop3(0x106, 0, vgpr(1), vgpr(2), 0);
let acc = vop1(0x001, 0, vgpr(3)); // v_mov_b32 v0, v3
let programs: [(&str, &[u32]); 3] = [
("v_mad_legacy_f32", &mad),
("v_mac_legacy_f32_e32", &[acc, mac_e32]),
("v_mac_legacy_f32_e64", &[acc, mac_e64[0], mac_e64[1]]),
];
let shaders = programs.map(|(name, body)| (name, build_valu_cs(body)));
for (name, shader) in shaders {
run_cases(&shader, &inputs, |backend, i, _, got| {
assert_eq!(
got,
f32::to_bits(cases[i].1),
"{backend}: {name} with {:?}, got {got:#010x}",
&cases[i].0[..3]
);
});
}
}
2 changes: 2 additions & 0 deletions crates/xtp-shader/src/gfx10/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ pub(super) const VOP2: &[(u16, &str)] = &[
(0x003, "v_add_f32"),
(0x004, "v_sub_f32"),
(0x005, "v_subrev_f32"),
// The console combines GFX10.1 MAD/MAC with BVH instructions, like gfx1013.
(0x006, "v_mac_legacy_f32"),
(0x007, "v_mul_legacy_f32"),
(0x008, "v_mul_f32"),
Expand Down Expand Up @@ -661,6 +662,7 @@ pub(super) const VOPC: &[(u16, &str)] = &[
];

pub(super) const VOP3: &[(u16, &str)] = &[
// The console combines GFX10.1 MAD/MAC with BVH instructions, like gfx1013.
(0x140, "v_mad_legacy_f32"),
(0x141, "v_mad_f32"),
(0x142, "v_mad_i32_i24"),
Expand Down
42 changes: 35 additions & 7 deletions crates/xtp-shader/tests/gfx10_conformance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@
//! 2. Decode to the exact same dword length as LLVM MC (100% length agreement).
//! 3. Match LLVM MC's mnemonic (after accounting for encoding-form suffixes
//! such as `_e32`, `_e64`, `_dpp`, `_sdwa`), or match a pinned decoder gap
//! or unresolved legacy opcode name.
//! or intentional GFX10.1 legacy opcode name.

use std::collections::{BTreeMap, BTreeSet};
use std::path::Path;

use xtp_shader::gfx10::{self, Encoding};

/// XTP follows Mesa 22.3.0's GFX10 (gfx1010) column in `aco_opcodes.py`.
/// LLVM's GFX10.3 `gfx1030_new.s` calls VOP3 0x140 `v_fma_legacy_f32`
/// and VOP2 0x006 `v_fmac_legacy_f32`. Which forms the console decodes
/// remains open. These pairs pin the current names for the two opcodes.
const UNRESOLVED_LEGACY_NAMES: &[(&str, &str)] = &[
/// LLVM 19.1.7 gates MAD/MAC on `HasMadMacF32Insts` and legacy FMA/FMAC on
/// `HasFmaLegacy32` (GFX10.3, in the `GFX10_B` decoder namespace).
/// `gfx1013` combines the GFX10.1 MAD/MAC feature with `GFX10_AEncoding` for BVH.
/// The title's programs use `v_mad_f32` and `v_mac_f32` alongside BVH instructions,
/// so XTP keeps the GFX10.1 legacy names at VOP3 0x140 and VOP2 0x006.
const GFX10_1_LEGACY_NAMES: &[(&str, &str)] = &[
("v_fma_legacy_f32", "v_mad_legacy_f32"),
("v_fmac_legacy_f32", "v_mac_legacy_f32"),
];
Expand Down Expand Up @@ -102,10 +103,37 @@ fn strip_llvm_suffix(m: &str) -> &str {
m
}

#[test]
fn decodes_gfx10_1_legacy_mad_mac() {
// GFX10.1 encodings from the checked-in LLVM MC corpus.
let cases: &[(&[u32], Encoding, u16, &str)] = &[
(
&[0xd540_0005, 0x000e_0501],
Encoding::Vop3,
0x140,
"v_mad_legacy_f32",
),
(&[0x0c0a_0401], Encoding::Vop2, 0x006, "v_mac_legacy_f32"),
(
&[0xd506_0005, 0x0000_0501],
Encoding::Vop3,
0x106,
"v_mac_legacy_f32",
),
];
for &(words, encoding, opcode, name) in cases {
let bytes: Vec<_> = words.iter().flat_map(|w| w.to_le_bytes()).collect();
let inst = gfx10::decode_one(&bytes).unwrap();
assert_eq!(inst.words, words);
assert_eq!((inst.encoding, inst.opcode), (encoding, opcode));
assert_eq!(inst.name(), Some(name));
}
}

#[test]
fn gfx10_decoder_matches_llvm_mc_corpus() {
let corpus = load_corpus();
let known: BTreeMap<&str, Option<&str>> = UNRESOLVED_LEGACY_NAMES
let known: BTreeMap<&str, Option<&str>> = GFX10_1_LEGACY_NAMES
.iter()
.map(|&(llvm, xtp)| (llvm, Some(xtp)))
.chain(KNOWN_DECODER_GAPS.iter().map(|&llvm| (llvm, None)))
Expand Down
5 changes: 3 additions & 2 deletions docs/analysis/shader.md
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,9 @@ change, then those that do, then geometry, then 32-lane GPUs.
families against the generated LLVM MC GFX10/GFX10.3 corpus. The checked-in
data keeps one representative per mnemonic, length and decoder selector
combination. Regenerate it with `cargo xtask gfx10-corpus`.
- The test pins nine decoder gaps and two unresolved legacy MAD/MAC versus
FMA/FMAC names. It flags pins when the corresponding difference goes away.
- PR #68 filled the decoder gaps, and the test keeps the legacy MAD/MAC
names because the console combines GFX10.1 MAD/MAC with BVH instructions,
like `gfx1013`.

**G1 — ES + GS pairing and GS stage data** (M, deps: S0)
- Goal: `native.rs` returns ES and GS of one pipeline as a pair with the GS
Expand Down