mirror of
https://github.com/MacosPorters/betterxen.git
synced 2026-11-27 04:35:27 +00:00
Adds a compare operation with a declared relation and the sopc_scalar
operand rule, so all twelve s_cmp_*_{i32,u32} forms are registry data.
The decoder dispatches them through the generated OP table and the
translator resolves SCC with ScalarCompareOp::test; the hand-written
s_cmp_eq_u32 path and its opcode constant are gone. A new LLVM-assembled
fixture records every compare's SCC, and the separately written oracle
from task/215-stage-d-oracle supplies the reference side.
Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com>
131 lines
4.8 KiB
Python
131 lines
4.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Generate first-word opcode matches from the pinned, public AMD RDNA 2 XML."""
|
|
|
|
import hashlib
|
|
import sys
|
|
from pathlib import Path
|
|
from xml.etree import ElementTree
|
|
|
|
|
|
SOURCE_SHA256 = "d671ecbc36543674ab59e9b2feffd56718fd2137e7ad913c314f2e2508b9f4f7"
|
|
VARIANTS = (
|
|
("BUFFER_LOAD_DWORD", "ENC_MUBUF"),
|
|
("BUFFER_STORE_DWORD", "ENC_MUBUF"),
|
|
("S_WAITCNT", "ENC_SOPP"),
|
|
("S_WAITCNT_VSCNT", "ENC_SOPK"),
|
|
("V_MOV_B32", "ENC_VOP1"),
|
|
("V_ADD_NC_U32", "ENC_VOP3"),
|
|
("S_ENDPGM", "ENC_SOPP"),
|
|
("S_MOV_B32", "ENC_SOP1"),
|
|
("S_CBRANCH_SCC0", "ENC_SOPP"),
|
|
("S_CBRANCH_EXECZ", "ENC_SOPP"),
|
|
("S_CBRANCH_VCCZ", "ENC_SOPP"),
|
|
("S_BRANCH", "ENC_SOPP"),
|
|
("V_LSHLREV_B32", "ENC_VOP2"),
|
|
("V_LSHRREV_B32", "ENC_VOP2"),
|
|
("V_ASHRREV_I32", "ENC_VOP2"),
|
|
("V_AND_B32", "ENC_VOP2"),
|
|
("V_OR_B32", "ENC_VOP2"),
|
|
("V_XOR_B32", "ENC_VOP2"),
|
|
("V_ADD_F32", "ENC_VOP2"),
|
|
("V_MUL_F32", "ENC_VOP2"),
|
|
("V_CMP_LT_I32", "ENC_VOPC"),
|
|
("V_CMP_EQ_I32", "ENC_VOPC"),
|
|
("V_CMP_LE_I32", "ENC_VOPC"),
|
|
("V_CMP_GT_I32", "ENC_VOPC"),
|
|
("V_CMP_NE_I32", "ENC_VOPC"),
|
|
("V_CMP_GE_I32", "ENC_VOPC"),
|
|
("V_CMP_LT_U32", "ENC_VOPC"),
|
|
("V_CMP_EQ_U32", "ENC_VOPC"),
|
|
("V_CMP_LE_U32", "ENC_VOPC"),
|
|
("V_CMP_GT_U32", "ENC_VOPC"),
|
|
("V_CMP_NE_U32", "ENC_VOPC"),
|
|
("V_CMP_GE_U32", "ENC_VOPC"),
|
|
("EXP", "ENC_EXP"),
|
|
("V_INTERP_P1_F32", "ENC_VINTRP"),
|
|
("V_INTERP_P2_F32", "ENC_VINTRP"),
|
|
("IMAGE_SAMPLE_LZ", "ENC_MIMG"),
|
|
)
|
|
|
|
|
|
def read_field(encoding, name):
|
|
fields = encoding.findall("MicrocodeFormat/BitMap/Field")
|
|
field = next(field for field in fields if field.findtext("FieldName") == name)
|
|
ranges = field.findall("BitLayout/Range")
|
|
return [
|
|
(int(item.findtext("BitOffset")), int(item.findtext("BitCount")))
|
|
for item in sorted(ranges, key=lambda x: int(x.attrib.get("Order", 0)))
|
|
]
|
|
|
|
|
|
def bits_to_word(value, ranges):
|
|
word = 0
|
|
mask = 0
|
|
for offset, size in ranges:
|
|
low_mask = (1 << size) - 1
|
|
word |= (value & low_mask) << offset
|
|
mask |= low_mask << offset
|
|
value >>= size
|
|
if value or mask >= (1 << 32):
|
|
raise ValueError("encoding field does not fit in the first word")
|
|
return mask, word
|
|
|
|
|
|
def main(xml_path):
|
|
data = Path(xml_path).read_bytes()
|
|
if hashlib.sha256(data).hexdigest() != SOURCE_SHA256:
|
|
raise ValueError("RDNA 2 XML does not match the approved input SHA-256")
|
|
|
|
root = ElementTree.fromstring(data)
|
|
isa = root.find("ISA")
|
|
encodings = {entry.findtext("EncodingName"): entry for entry in isa.findall("Encodings/Encoding")}
|
|
instructions = {
|
|
entry.findtext("InstructionName"): entry for entry in isa.findall("Instructions/Instruction")
|
|
}
|
|
print("// Generated by crates/isa/generate_rdna2.py. See docs/research/rdna2.md.")
|
|
print("// Source: AMD RDNA 2 XML, 2026-02-20, schema 1.1.1.")
|
|
print("// Copyright (c) 2026 Advanced Micro Devices, Inc., or its affiliates.")
|
|
print("// SPDX-License-Identifier: MIT")
|
|
print()
|
|
print("use super::Opcode;")
|
|
print()
|
|
for instruction_name, encoding_name in VARIANTS:
|
|
encoding = encodings[encoding_name]
|
|
variant = next(
|
|
variant
|
|
for variant in instructions[instruction_name].findall("InstructionEncodings/InstructionEncoding")
|
|
if variant.findtext("EncodingName") == encoding_name
|
|
and variant.findtext("EncodingCondition") == "default"
|
|
)
|
|
opcode = int(variant.findtext("Opcode"))
|
|
identifiers = [
|
|
int(ident.text, 2) for ident in encoding.findall("EncodingIdentifiers/EncodingIdentifier")
|
|
]
|
|
prefix_mask, _ = bits_to_word(0, read_field(encoding, "ENCODING"))
|
|
prefixes = {identifier & prefix_mask for identifier in identifiers}
|
|
if len(prefixes) != 1:
|
|
raise ValueError(f"{encoding_name} has an ambiguous encoding prefix")
|
|
# EXP identifies the export instruction through the encoding prefix alone.
|
|
if encoding_name == "ENC_EXP" and opcode == 0:
|
|
opcode_mask, opcode_bits = 0, 0
|
|
else:
|
|
opcode_mask, opcode_bits = bits_to_word(opcode, read_field(encoding, "OP"))
|
|
mask = prefix_mask | opcode_mask
|
|
bits = prefixes.pop() | opcode_bits
|
|
if bits not in {identifier & mask for identifier in identifiers}:
|
|
raise ValueError(f"{instruction_name} is absent from {encoding_name} identifiers")
|
|
size = int(encoding.findtext("BitCount")) // 8
|
|
print(
|
|
f"pub const {instruction_name}: Opcode = Opcode {{\n"
|
|
f" mask: 0x{mask:08x},\n"
|
|
f" bits: 0x{bits:08x},\n"
|
|
f" bytes: {size},\n"
|
|
"};"
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) != 2:
|
|
raise SystemExit("usage: generate_rdna2.py amdgpu_isa_rdna2.xml")
|
|
main(sys.argv[1])
|