isa: check identification against LLVM and add literal-only forms

The extension-word length rules were written by hand because the XML
gives none, so they needed an outside oracle. An LLVM test now compares
names and lengths for one instruction per family and extension word.
It found five instructions the XML lists only under a literal encoding,
which the first table omitted; they are now base forms that always carry
a literal.

Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com>
This commit is contained in:
elforeign committed 2026-11-24 00:20:26 -06:00
1 parent 54506125d4
commit 1493dcf83b
8 files changed
+971 -821

No files matched your search

+2 -2
View File
@@ -11,8 +11,8 @@ remains unsupported.
### Added
- Added complete gfx1030 instruction metadata generated from AMD's public RDNA 2 XML: 1,460 base forms across 21 encoding families with fields, operands, flags and allowed extension words. A committed, normalised snapshot lets `crates/isa/generate_rdna2_meta.py --check` verify the generated table offline; CI runs it.
- Added `xenora_isa::meta::identify`, which finds any gfx1030 instruction's form and exact length, including literal, SDWA, DPP and NSA words, and rejects truncated, unassigned and disallowed-extension encodings. It agrees with every LLVM-assembled fixture. Identification does not make an instruction executable: 25 forms have semantic decoding.
- Added complete gfx1030 instruction metadata generated from AMD's public RDNA 2 XML: 1,465 base forms across 21 encoding families with fields, operands, flags and allowed extension words. A committed, normalised snapshot lets `crates/isa/generate_rdna2_meta.py --check` verify the generated table offline; CI runs it.
- Added `xenora_isa::meta::identify`, which finds any gfx1030 instruction's form and exact length, including literal, SDWA, DPP and NSA words, and rejects truncated, unassigned and disallowed-extension encodings. It agrees with every LLVM-assembled fixture and with LLVM 23.1.6 on a probe set covering each family and extension word. Identification does not make an instruction executable: 25 forms have semantic decoding.
- Added `xenora shaders coverage [--json]` and `xenora shaders inspect <file>` for per-family coverage and stream listings that separate decoding failures from unsupported semantics.
- Added `dbt::tests::sib_store_executes_through_the_native_dbt`, an independent DBT execution
test that runs the SIB store form `mov [rsp + 0x20], rbx` through the native ARM64 DBT and
+5 -5
View File
@@ -2243,7 +2243,7 @@
{"condition":"has_lit_0","encoding":"SOPC_INST_LITERAL","instruction":"S_CMP_LT_U32","kind":"variant","opcode":10}
{"condition":"has_lit_0_has_lit_1","encoding":"SOPC_INST_LITERAL","instruction":"S_CMP_LT_U32","kind":"variant","opcode":10}
{"condition":"has_lit_1","encoding":"SOPC_INST_LITERAL","instruction":"S_CMP_LT_U32","kind":"variant","opcode":10}
{"condition":"default","encoding":"SOPK_INST_LITERAL","instruction":"S_SETREG_IMM32_B32","kind":"variant","opcode":21}
{"condition":"default","encoding":"SOPK_INST_LITERAL","flags":[],"group":"SALU","instruction":"S_SETREG_IMM32_B32","kind":"variant","opcode":21,"operands":[["SIMM16","FMT_NUM_B16","OPR_HWREG",16,false,true,false,true],["LITERAL","FMT_NUM_B32","OPR_SIMM32",32,true,false,false,true]],"subgroups":["NOT_ASSIGNED"]}
{"condition":"has_lit","encoding":"VOP1_INST_LITERAL","instruction":"V_BFREV_B32","kind":"variant","opcode":56}
{"condition":"has_lit","encoding":"VOP1_INST_LITERAL","instruction":"V_CEIL_F16","kind":"variant","opcode":92}
{"condition":"has_lit","encoding":"VOP1_INST_LITERAL","instruction":"V_CEIL_F32","kind":"variant","opcode":34}
@@ -2503,16 +2503,16 @@
{"condition":"has_lit","encoding":"VOP2_INST_LITERAL","instruction":"V_CVT_PKRTZ_F16_F32","kind":"variant","opcode":47}
{"condition":"has_lit","encoding":"VOP2_INST_LITERAL","instruction":"V_DOT2C_F32_F16","kind":"variant","opcode":2}
{"condition":"has_lit","encoding":"VOP2_INST_LITERAL","instruction":"V_DOT4C_I32_I8","kind":"variant","opcode":13}
{"condition":"default","encoding":"VOP2_INST_LITERAL","instruction":"V_FMAAK_F16","kind":"variant","opcode":56}
{"condition":"default","encoding":"VOP2_INST_LITERAL","flags":[],"group":"VALU","instruction":"V_FMAAK_F16","kind":"variant","opcode":56,"operands":[["VDST","FMT_NUM_F16","OPR_VGPR",16,false,true,false,true],["SRC0","FMT_NUM_F16","OPR_SRC",16,true,false,false,true],["VSRC1","FMT_NUM_F16","OPR_VGPR",16,true,false,false,true],["LITERAL","FMT_NUM_F16","OPR_SIMM16",16,true,false,false,true]],"subgroups":["NOT_ASSIGNED"]}
{"condition":"has_lit","encoding":"VOP2_INST_LITERAL","instruction":"V_FMAAK_F16","kind":"variant","opcode":56}
{"condition":"default","encoding":"VOP2_INST_LITERAL","instruction":"V_FMAAK_F32","kind":"variant","opcode":45}
{"condition":"default","encoding":"VOP2_INST_LITERAL","flags":[],"group":"VALU","instruction":"V_FMAAK_F32","kind":"variant","opcode":45,"operands":[["VDST","FMT_NUM_F32","OPR_VGPR",32,false,true,false,true],["SRC0","FMT_NUM_F32","OPR_SRC",32,true,false,false,true],["VSRC1","FMT_NUM_F32","OPR_VGPR",32,true,false,false,true],["LITERAL","FMT_NUM_F32","OPR_SIMM32",32,true,false,false,true]],"subgroups":["NOT_ASSIGNED"]}
{"condition":"has_lit","encoding":"VOP2_INST_LITERAL","instruction":"V_FMAAK_F32","kind":"variant","opcode":45}
{"condition":"has_lit","encoding":"VOP2_INST_LITERAL","instruction":"V_FMAC_F16","kind":"variant","opcode":54}
{"condition":"has_lit","encoding":"VOP2_INST_LITERAL","instruction":"V_FMAC_F32","kind":"variant","opcode":43}
{"condition":"has_lit","encoding":"VOP2_INST_LITERAL","instruction":"V_FMAC_LEGACY_F32","kind":"variant","opcode":6}
{"condition":"default","encoding":"VOP2_INST_LITERAL","instruction":"V_FMAMK_F16","kind":"variant","opcode":55}
{"condition":"default","encoding":"VOP2_INST_LITERAL","flags":[],"group":"VALU","instruction":"V_FMAMK_F16","kind":"variant","opcode":55,"operands":[["VDST","FMT_NUM_F16","OPR_VGPR",16,false,true,false,true],["SRC0","FMT_NUM_F16","OPR_SRC",16,true,false,false,true],["LITERAL","FMT_NUM_F16","OPR_SIMM16",16,true,false,false,true],["VSRC1","FMT_NUM_F16","OPR_VGPR",16,true,false,false,true]],"subgroups":["NOT_ASSIGNED"]}
{"condition":"has_lit","encoding":"VOP2_INST_LITERAL","instruction":"V_FMAMK_F16","kind":"variant","opcode":55}
{"condition":"default","encoding":"VOP2_INST_LITERAL","instruction":"V_FMAMK_F32","kind":"variant","opcode":44}
{"condition":"default","encoding":"VOP2_INST_LITERAL","flags":[],"group":"VALU","instruction":"V_FMAMK_F32","kind":"variant","opcode":44,"operands":[["VDST","FMT_NUM_F32","OPR_VGPR",32,false,true,false,true],["SRC0","FMT_NUM_F32","OPR_SRC",32,true,false,false,true],["LITERAL","FMT_NUM_F32","OPR_SIMM32",32,true,false,false,true],["VSRC1","FMT_NUM_F32","OPR_VGPR",32,true,false,false,true]],"subgroups":["NOT_ASSIGNED"]}
{"condition":"has_lit","encoding":"VOP2_INST_LITERAL","instruction":"V_FMAMK_F32","kind":"variant","opcode":44}
{"condition":"has_lit","encoding":"VOP2_INST_LITERAL","instruction":"V_LDEXP_F16","kind":"variant","opcode":59}
{"condition":"has_lit","encoding":"VOP2_INST_LITERAL","instruction":"V_LSHLREV_B32","kind":"variant","opcode":26}
+16 -5
View File
@@ -204,7 +204,7 @@ def snapshot_from_xml(data):
}
# Extension variants keep only what classifies them; their
# operands repeat the base form with a literal, SDWA, DPP or NSA word.
if encoding_name in BASE_ENCODINGS:
if encoding_name in BASE_ENCODINGS or condition == "default":
record.update({
"flags": sorted(flag for flag, value in flags.items() if value),
"group": text(group, "Name"),
@@ -256,22 +256,33 @@ def forms_from_snapshot(snapshot):
entry.add("always_literal")
entry.add(kind)
based = {(variant["instruction"], variant["encoding"]) for variant in variants if variant["encoding"] in BASE_ENCODINGS}
forms = []
for variant in variants:
encoding_name = variant["encoding"]
if encoding_name not in BASE_ENCODINGS:
identity = encoding_name
if encoding_name in EXTENSIONS:
# A few instructions exist only with a literal (S_SETREG_IMM32_B32,
# V_FMAMK_F32, ...): the XML lists them under the literal encoding's
# default condition and gives them no base variant.
base, kind = EXTENSIONS[encoding_name]
if kind != "literal" or variant["condition"] != "default" or (variant["instruction"], base) in based:
continue
encoding_name = base
elif encoding_name not in BASE_ENCODINGS:
continue
# Three VOP1/VOP3 relative moves name the excluded extensions instead of "default".
if variant["condition"] != "default" and not variant["condition"].startswith("Nothas_"):
fail(f"{variant['instruction']} has non-default base condition {variant['condition']}")
encoding = encodings[encoding_name]
id_encoding = encodings[identity]
fields = dict((name, ranges) for name, ranges in encoding["fields"])
prefix_mask, _ = place(0, fields["ENCODING"])
prefixes = {identifier & prefix_mask for identifier in encoding["identifiers"]}
prefixes = {identifier & prefix_mask for identifier in id_encoding["identifiers"]}
if len(prefixes) != 1:
fail(f"{encoding_name} has an ambiguous encoding prefix")
prefix = prefixes.pop()
id_mask = encoding["identifier_mask"]
id_mask = id_encoding["identifier_mask"] & 0xFFFFFFFF
opcode = variant["opcode"]
if "OP" in fields:
op_mask, op_bits = place(opcode, fields["OP"])
@@ -285,7 +296,7 @@ def forms_from_snapshot(snapshot):
# first-dword identifiers cannot express; carry it separately.
low = 0xFFFFFFFF
candidates = sorted({
identifier for identifier in encoding["identifiers"]
identifier & 0xFFFFFFFF for identifier in id_encoding["identifiers"]
if identifier & (prefix_mask | op_mask) & low == (prefix | op_bits) & low
})
if len(candidates) != 1:
File diff suppressed because it is too large. Load diff
+41
View File
@@ -0,0 +1,41 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// One instruction per line for each family and extension word; checked against llvm-mc.
s_mov_b32 s0, 0x12345678
s_add_u32 s0, s1, 0x1234
s_cmp_eq_u32 s0, 0xdeadbeef
s_movk_i32 s0, 0x1234
s_setreg_imm32_b32 hwreg(HW_REG_MODE), 0x0
s_load_dwordx4 s[0:3], s[4:5], 0x10
s_nop 0
s_endpgm
v_mov_b32 v0, 0x12345678
v_mov_b32_sdwa v0, v1 dst_sel:BYTE_0 dst_unused:UNUSED_PAD src0_sel:DWORD
v_mov_b32_dpp v0, v1 quad_perm:[1,0,3,2] row_mask:0xf bank_mask:0xf
v_mov_b32_dpp v0, v1 dpp8:[1,0,3,2,5,4,7,6]
v_mov_b32_dpp v0, v1 dpp8:[1,0,3,2,5,4,7,6] fi:1
v_add_f32_sdwa v0, v1, v2 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_1 src1_sel:DWORD
v_add_f32_dpp v0, v1, v2 row_shl:1 row_mask:0xf bank_mask:0xf
v_cmp_eq_u32_e32 vcc_lo, 0x1234, v1
v_cmp_eq_u32_sdwa s0, v1, v2 src0_sel:DWORD src1_sel:DWORD
v_fmamk_f32 v0, v1, 0x40000000, v2
v_fmaak_f32 v0, v1, v2, 0x40000000
v_fma_f32 v0, v1, v2, 0x3f800000
v_add_co_u32 v0, s0, v1, v2
v_readlane_b32 s0, v1, 0
v_pk_add_f16 v0, v1, v2
v_pk_fma_f16 v0, v1, v2, 0x3c00
v_interp_p1_f32 v0, v1, attr0.x
ds_write_b32 v0, v1
ds_read_b32 v0, v1 offset:16
buffer_load_dword v0, off, s[0:3], 0
tbuffer_load_format_x v0, off, s[0:3], 0 format:[BUF_FMT_32_FLOAT]
tbuffer_load_format_d16_x v0, off, s[0:3], 0 format:[BUF_FMT_32_FLOAT]
tbuffer_store_format_d16_xyzw v[0:1], off, s[0:3], 0 format:[BUF_FMT_32_FLOAT]
image_sample v[0:3], v[0:1], s[0:7], s[8:11] dmask:0xf dim:SQ_RSRC_IMG_2D
image_sample v[0:3], [v0, v2], s[0:7], s[8:11] dmask:0xf dim:SQ_RSRC_IMG_2D
image_sample_c v[0:3], [v0, v2, v4, v6], s[0:7], s[8:11] dmask:0xf dim:SQ_RSRC_IMG_2D_ARRAY
image_msaa_load v[0:3], v[0:2], s[0:7] dmask:0xf dim:SQ_RSRC_IMG_2D_MSAA unorm
flat_load_dword v0, v[0:1]
global_load_dword v0, v[0:1], off
scratch_load_dword v0, v1, off
exp mrt0 v0, v0, v0, v0 done
+2 -2
View File
@@ -70,8 +70,8 @@ fn hand_assembled_words_identify() {
assert_eq!(name_of(&[0x7e00_0301]), ("V_MOV_B32", 4));
// VOP2 v_add_f32 v0, v1, v2: opcode 3 at bits 30:25, VSRC1 = 2 at bits 16:9.
assert_eq!(name_of(&[0x0600_0501]), ("V_ADD_F32", 4));
// VOP3 v_add_f32_e64 v0, v1, v2: VOP3 opcode 259 = 256 + VOP2 opcode 3.
assert_eq!(name_of(&[0xd503_0000, 0x0002_0501]), ("V_ADD_F32", 8));
// VOP3 v_add_f32_e64 v0, v1, v2 (LLVM 23 encoding): opcode 259 = 256 + VOP2 opcode 3.
assert_eq!(name_of(&[0xd503_0000, 0x0202_0501]), ("V_ADD_F32", 8));
}
#[test]
+77
View File
@@ -0,0 +1,77 @@
//! LLVM oracle for instruction identification: names and lengths from llvm-mc encodings.
use std::process::Command;
use xenora_isa::meta::identify;
const SOURCE: &str = include_str!("llvm/identify.s");
fn encodings() -> Vec<(String, Vec<u8>)> {
let executable = std::env::var_os("LLVM_BIN").map_or_else(
|| "llvm-mc".into(),
|directory| std::path::PathBuf::from(directory).join("llvm-mc"),
);
let path = std::env::temp_dir().join(format!("xenora-identify-{}.s", std::process::id()));
std::fs::write(&path, SOURCE).unwrap();
let output = Command::new(executable)
.args([
"-triple=amdgcn-amd-amdhsa",
"-mcpu=gfx1030",
"-mattr=+wavefrontsize32",
"-show-encoding",
])
.arg(&path)
.output()
.expect("launch llvm-mc (provide its directory in LLVM_BIN or put it on PATH)");
std::fs::remove_file(&path).ok();
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
String::from_utf8(output.stdout)
.unwrap()
.lines()
.filter_map(|line| {
let (text, encoding) = line.split_once("; encoding: [")?;
let mnemonic = text.split_whitespace().next()?.to_ascii_uppercase();
let bytes = encoding
.trim_end_matches(']')
.split(',')
.map(|byte| u8::from_str_radix(byte.trim().trim_start_matches("0x"), 16).unwrap())
.collect();
Some((mnemonic, bytes))
})
.collect()
}
fn base_name(mnemonic: &str) -> &str {
["_E32", "_E64", "_SDWA", "_DPP"]
.iter()
.fold(mnemonic, |name, suffix| name.trim_end_matches(suffix))
}
#[test]
#[ignore = "requires LLVM with the gfx1030 AMDGPU target"]
fn identification_matches_llvm_names_and_lengths() {
let encodings = encodings();
let expected_lines = SOURCE
.lines()
.filter(|line| !line.is_empty() && !line.starts_with("//"))
.count();
assert_eq!(encodings.len(), expected_lines);
let mut stream = Vec::new();
for (mnemonic, bytes) in &encodings {
let found = identify(bytes).unwrap_or_else(|error| panic!("{mnemonic}: {error}"));
assert_eq!(found.form.name, base_name(mnemonic), "{mnemonic}");
assert_eq!(found.len, bytes.len(), "{mnemonic}");
stream.extend_from_slice(bytes);
}
let mut offset = 0;
for (mnemonic, bytes) in &encodings {
let found = identify(&stream[offset..]).unwrap();
assert_eq!(found.len, bytes.len(), "{mnemonic} in stream");
offset += found.len;
}
assert_eq!(offset, stream.len());
}
+14 -3
View File
@@ -42,7 +42,7 @@ python3 crates/isa/generate_rdna2.py path/to/amdgpu_isa_rdna2.xml | diff -u crat
## Complete instruction metadata
`crates/isa/generate_rdna2_meta.py` reduces the same approved XML to every
gfx1030 base instruction form: 1,460 forms in 21 encoding families, with
gfx1030 base instruction form: 1,465 forms in 21 encoding families, with
identifying bits, field layouts, operands (field, data format, operand type,
width, direction), control-flow flags and allowed extension words. It runs in
two stages so regeneration and CI checks need no network and no XML:
@@ -66,8 +66,19 @@ VOP1, VOP2 and VOPC `SRC0` values 0xF9, 0xFA, 0xE9 and 0xEA add an SDWA, DPP16
or DPP8 control dword (from the RDNA 2 ISA guide's SDWA and DPP sections, not
the XML); MIMG `NSA` adds that many address dwords. An extension the form does
not list is rejected. Every LLVM-assembled fixture identifies with exact
boundaries and matching mnemonics. A broader LLVM comparison of the extension
rules is still open because it needs the LLVM oracle.
boundaries and matching mnemonics. `crates/isa/tests/llvm/identify.s` holds one
instruction per family and extension word (literal, SDWA, DPP16, DPP8 with and
without fetch-inactive, VOP3 and VOP3P literals, NSA images, MTBUF opcodes
above 7, MSAA loads); its LLVM oracle compares every name and length:
```sh
LLVM_BIN=path/to/llvm/bin cargo test --locked -p xenora-isa --test meta_llvm -- --include-ignored
```
The oracle found five instructions that the XML lists only under a literal
encoding (`S_SETREG_IMM32_B32`, `V_FMAMK_F32`, `V_FMAAK_F32`, `V_FMAMK_F16`,
`V_FMAAK_F16`); the generator now treats them as base forms that always carry a
literal.
Identification is not execution. `xenora shaders coverage` reports forms
discovered in metadata against forms the semantic decoder accepts