Files
betterxen/crates/isa/generate_rdna2.py
T
elforeign 425327a0f2 shader: add s_add_i32 and s_sub_i32 with signed-overflow SCC
#185 left both forms unsupported because the pinned XML says only
"carry-out" for them. The AMD RDNA 2 ISA reference guide (GPUOpen,
section 12.1 SOP2 Instructions, p. 101-102, and section 5.3 Scalar
Condition Code, p. 33) defines SCC for both as signed overflow:
add overflows when the operands share a sign that the result lacks;
subtract when the operands' signs differ and the result's sign differs
from S0's. The guide also confirms the unsigned rules used in #185.

Adds both forms to the generated opcodes, the decoder, the reference
(exact signed result must fit in i32), the independent interpreter (the
guide's sign-bit rule over its ripple adder) and the translator, plus
400 boundary programs, eight hand-worked overflow and no-overflow cases
(-1 + 1 and 0 - 1 carry or borrow but do not overflow) and two signed
overflows in the scalar ALU fixture. Inverting the overflow rule in any
one implementation fails three tests. s_min_i32 and the other SOP2
forms stay unsupported.

Refs #185

Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com>
2026-11-24 01:48:52 -06:00

129 lines
4.7 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_CMP_EQ_U32", "ENC_SOPC"),
("S_ADD_U32", "ENC_SOP2"),
("S_SUB_U32", "ENC_SOP2"),
("S_ADD_I32", "ENC_SOP2"),
("S_SUB_I32", "ENC_SOP2"),
("S_ADDC_U32", "ENC_SOP2"),
("S_SUBB_U32", "ENC_SOP2"),
("S_AND_B32", "ENC_SOP2"),
("S_OR_B32", "ENC_SOP2"),
("S_XOR_B32", "ENC_SOP2"),
("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"),
("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])