mirror of
https://github.com/MacosPorters/betterxen.git
synced 2026-11-27 00:10:43 +00:00
#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>
418 lines
15 KiB
Rust
418 lines
15 KiB
Rust
//! Typed operands and instruction forms for the bounded gfx1030 compute and graphics subsets.
|
|
|
|
use std::fmt;
|
|
|
|
pub mod meta;
|
|
mod rdna2;
|
|
#[rustfmt::skip]
|
|
mod rdna2_meta;
|
|
pub use rdna2::{
|
|
BUFFER_LOAD_DWORD, BUFFER_STORE_DWORD, EXP, IMAGE_SAMPLE_LZ, S_ADD_I32, S_ADD_U32, S_ADDC_U32,
|
|
S_AND_B32, S_BRANCH, S_CBRANCH_EXECZ, S_CBRANCH_SCC0, S_CBRANCH_VCCZ, S_CMP_EQ_U32, S_ENDPGM,
|
|
S_MOV_B32, S_OR_B32, S_SUB_I32, S_SUB_U32, S_SUBB_U32, S_WAITCNT, S_WAITCNT_VSCNT, S_XOR_B32,
|
|
V_ADD_F32, V_ADD_NC_U32, V_AND_B32, V_ASHRREV_I32, V_INTERP_P1_F32, V_INTERP_P2_F32,
|
|
V_LSHLREV_B32, V_LSHRREV_B32, V_MOV_B32, V_MUL_F32, V_OR_B32, V_XOR_B32,
|
|
};
|
|
|
|
/// Selects the instruction encoding independently of the container and execution ABI.
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
pub enum Architecture {
|
|
/// LLVM's gfx1030 target for AMD RDNA 2.
|
|
Gfx1030,
|
|
}
|
|
|
|
/// A generated first-word opcode pattern and its base length, before literal extensions.
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
pub struct Opcode {
|
|
mask: u32,
|
|
bits: u32,
|
|
bytes: u8,
|
|
}
|
|
|
|
impl Opcode {
|
|
/// Checks the format and opcode bits. The decoder must still validate operands.
|
|
pub fn matches(self, word: u32) -> bool {
|
|
word & self.mask == self.bits
|
|
}
|
|
|
|
/// Returns the base instruction size in bytes.
|
|
pub fn byte_len(self) -> u8 {
|
|
self.bytes
|
|
}
|
|
}
|
|
|
|
/// A 32-bit source for an unmodified vector move.
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
pub enum VectorSource {
|
|
/// VGPR index.
|
|
Register(u8),
|
|
/// Normal SGPR index (s0-s105), read as a wave-uniform value.
|
|
Scalar(u8),
|
|
/// Inline integer or literal word, represented by its bits.
|
|
Immediate(u32),
|
|
}
|
|
|
|
impl fmt::Display for VectorSource {
|
|
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
match self {
|
|
Self::Register(register) => write!(formatter, "v{register}"),
|
|
Self::Scalar(register) => write!(formatter, "s{register}"),
|
|
Self::Immediate(value) => write!(formatter, "0x{value:x}"),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A scalar 32-bit source.
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
pub enum ScalarSource {
|
|
/// A normal SGPR index.
|
|
Register(u8),
|
|
/// Inline integer or literal word, represented by its bits.
|
|
Immediate(u32),
|
|
}
|
|
|
|
impl fmt::Display for ScalarSource {
|
|
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
match *self {
|
|
Self::Register(register) => write!(formatter, "s{register}"),
|
|
Self::Immediate(value) => match value as i32 {
|
|
integer @ -16..=64 => write!(formatter, "{integer}"),
|
|
_ => write!(formatter, "0x{value:x}"),
|
|
},
|
|
}
|
|
}
|
|
}
|
|
|
|
/// SOP2 32-bit integer operations in the supported scalar subset.
|
|
///
|
|
/// SCC behaviour follows the pinned AMD RDNA 2 XML descriptions: additions
|
|
/// store the carry-out, subtractions the borrow, ADDC and SUBB also consume
|
|
/// SCC as carry-in or borrow-in, signed forms store signed overflow (RDNA 2 ISA
|
|
/// guide 12.1), and bitwise operations set SCC iff the result is nonzero. See
|
|
/// docs/shaders/scalar-alu.md for the sources.
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
pub enum ScalarAluOp {
|
|
/// `s_add_u32`: D = S0 + S1, SCC = carry-out.
|
|
AddU32,
|
|
/// `s_sub_u32`: D = S0 - S1, SCC = borrow (S1 > S0).
|
|
SubU32,
|
|
/// `s_add_i32`: D = S0 + S1, SCC = signed overflow.
|
|
AddI32,
|
|
/// `s_sub_i32`: D = S0 - S1, SCC = signed overflow.
|
|
SubI32,
|
|
/// `s_addc_u32`: D = S0 + S1 + SCC, SCC = carry-out.
|
|
AddcU32,
|
|
/// `s_subb_u32`: D = S0 - S1 - SCC, SCC = borrow.
|
|
SubbU32,
|
|
/// `s_and_b32`: D = S0 & S1, SCC = D != 0.
|
|
AndB32,
|
|
/// `s_or_b32`: D = S0 | S1, SCC = D != 0.
|
|
OrB32,
|
|
/// `s_xor_b32`: D = S0 ^ S1, SCC = D != 0.
|
|
XorB32,
|
|
}
|
|
|
|
impl ScalarAluOp {
|
|
/// Lower-case mnemonic.
|
|
pub fn mnemonic(self) -> &'static str {
|
|
match self {
|
|
Self::AddU32 => "s_add_u32",
|
|
Self::SubU32 => "s_sub_u32",
|
|
Self::AddI32 => "s_add_i32",
|
|
Self::SubI32 => "s_sub_i32",
|
|
Self::AddcU32 => "s_addc_u32",
|
|
Self::SubbU32 => "s_subb_u32",
|
|
Self::AndB32 => "s_and_b32",
|
|
Self::OrB32 => "s_or_b32",
|
|
Self::XorB32 => "s_xor_b32",
|
|
}
|
|
}
|
|
|
|
/// The 32-bit result and the new SCC, with `carry_in` the SCC read by
|
|
/// ADDC and SUBB (ignored by the other operations).
|
|
pub fn evaluate(self, src0: u32, src1: u32, carry_in: bool) -> (u32, bool) {
|
|
let carry = u32::from(carry_in && self.reads_scc());
|
|
match self {
|
|
Self::AddU32 | Self::AddcU32 => {
|
|
let (partial, first) = src0.overflowing_add(src1);
|
|
let (value, second) = partial.overflowing_add(carry);
|
|
(value, first || second)
|
|
}
|
|
Self::SubU32 | Self::SubbU32 => {
|
|
let (partial, first) = src0.overflowing_sub(src1);
|
|
let (value, second) = partial.overflowing_sub(carry);
|
|
(value, first || second)
|
|
}
|
|
Self::AddI32 => {
|
|
let (value, overflow) = (src0 as i32).overflowing_add(src1 as i32);
|
|
(value as u32, overflow)
|
|
}
|
|
Self::SubI32 => {
|
|
let (value, overflow) = (src0 as i32).overflowing_sub(src1 as i32);
|
|
(value as u32, overflow)
|
|
}
|
|
Self::AndB32 => (src0 & src1, src0 & src1 != 0),
|
|
Self::OrB32 => (src0 | src1, src0 | src1 != 0),
|
|
Self::XorB32 => (src0 ^ src1, src0 ^ src1 != 0),
|
|
}
|
|
}
|
|
|
|
/// Whether the operation reads SCC as a carry-in or borrow-in.
|
|
pub fn reads_scc(self) -> bool {
|
|
matches!(self, Self::AddcU32 | Self::SubbU32)
|
|
}
|
|
}
|
|
|
|
/// Output targeted by a supported uncompressed four-component export.
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
pub enum ExportTarget {
|
|
/// Vertex clip position.
|
|
Position0,
|
|
/// Vertex parameter slot zero.
|
|
Parameter0,
|
|
/// Pixel color attachment zero.
|
|
RenderTarget0,
|
|
}
|
|
|
|
/// Phase of a pixel parameter interpolation.
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
pub enum InterpolationPhase {
|
|
/// Initialize the destination with the base and I-coordinate terms.
|
|
First,
|
|
/// Add the J-coordinate term to the destination.
|
|
Second,
|
|
}
|
|
|
|
/// Instruction forms accepted by the gfx1030 decoder.
|
|
///
|
|
/// Vector operations use VGPR indices and scalar operations use SGPR indices.
|
|
/// The buffer `resource` is the first SGPR of its four-word descriptor.
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
#[allow(missing_docs)]
|
|
pub enum Instruction {
|
|
/// Indexed 32-bit buffer read into a VGPR.
|
|
BufferLoadDword { data: u8, index: u8, resource: u8 },
|
|
/// Indexed 32-bit buffer write from a VGPR.
|
|
BufferStoreDword { data: u8, index: u8, resource: u8 },
|
|
/// `s_waitcnt vmcnt(0)`.
|
|
WaitLoad,
|
|
/// `s_waitcnt_vscnt null, 0`.
|
|
WaitStore,
|
|
/// VOP1 unmodified 32-bit vector move.
|
|
MoveB32 { dst: u8, src: VectorSource },
|
|
/// VOP3 unsigned 32-bit addition with no carry or input/output modifiers.
|
|
AddNoCarryU32 { dst: u8, src0: u8, src1: u8 },
|
|
/// SOP1 32-bit move to a normal SGPR or an EXEC/VCC half.
|
|
ScalarMoveB32 { dst: u8, src: ScalarSource },
|
|
/// SOP2 32-bit integer operation writing a normal SGPR (s0-s105) and SCC.
|
|
ScalarAlu {
|
|
op: ScalarAluOp,
|
|
dst: u8,
|
|
src0: ScalarSource,
|
|
src1: ScalarSource,
|
|
},
|
|
/// SOPC 32-bit equality comparison writing SCC.
|
|
ScalarCompareEqU32 {
|
|
src0: ScalarSource,
|
|
src1: ScalarSource,
|
|
},
|
|
/// SOPP jump to a stream byte offset when SCC is clear.
|
|
BranchSccZero { target: usize },
|
|
/// SOPP jump when the Wave32 execution mask is empty.
|
|
BranchExecZero { target: usize },
|
|
/// SOPP jump when the Wave32 vector condition mask is empty.
|
|
BranchVccZero { target: usize },
|
|
/// SOPP unconditional jump to a stream byte offset.
|
|
Branch { target: usize },
|
|
/// VOP2 logical left shift with the count in src0.
|
|
VectorLshlrevB32 { dst: u8, src0: u8, src1: u8 },
|
|
/// VOP2 logical right shift with the count in src0.
|
|
VectorLshrrevB32 { dst: u8, src0: u8, src1: u8 },
|
|
/// VOP2 arithmetic right shift with the count in src0.
|
|
VectorAshrrevI32 { dst: u8, src0: u8, src1: u8 },
|
|
/// VOP2 bitwise AND.
|
|
VectorAndB32 { dst: u8, src0: u8, src1: u8 },
|
|
/// VOP2 bitwise OR.
|
|
VectorOrB32 { dst: u8, src0: u8, src1: u8 },
|
|
/// VOP2 bitwise XOR.
|
|
VectorXorB32 { dst: u8, src0: u8, src1: u8 },
|
|
/// VOP2 single-precision addition. Execution requires an explicit FP mode.
|
|
VectorAddF32 { dst: u8, src0: u8, src1: u8 },
|
|
/// VOP2 single-precision multiplication. Execution requires an explicit FP mode.
|
|
VectorMulF32 { dst: u8, src0: u8, src1: u8 },
|
|
/// Full, uncompressed four-component graphics export.
|
|
Export {
|
|
target: ExportTarget,
|
|
sources: [u8; 4],
|
|
},
|
|
/// Parameter interpolation with a source barycentric coordinate and an attribute component.
|
|
Interpolate {
|
|
phase: InterpolationPhase,
|
|
dst: u8,
|
|
coordinate: u8,
|
|
attribute: u8,
|
|
component: u8,
|
|
},
|
|
/// MIMG image_sample_lz 2D RGBA8 texture sample with level of detail zero.
|
|
ImageSampleLz {
|
|
/// Four contiguous destination VGPRs [dst..dst+3] receiving RGBA.
|
|
dst: u8,
|
|
/// Two contiguous coordinate VGPRs [coord..coord+1] containing (u, v).
|
|
coord: u8,
|
|
/// First SGPR of the eight-word resource descriptor s[resource..resource+7].
|
|
resource: u8,
|
|
/// First SGPR of the four-word sampler descriptor s[sampler..sampler+3].
|
|
sampler: u8,
|
|
},
|
|
/// Terminates the wave and waits for outstanding memory operations.
|
|
EndProgram,
|
|
}
|
|
|
|
/// Formats an SGPR destination with LLVM's names for the EXEC and VCC halves.
|
|
struct ScalarDestination(u8);
|
|
|
|
impl fmt::Display for ScalarDestination {
|
|
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
match self.0 {
|
|
106 => formatter.write_str("vcc_lo"),
|
|
107 => formatter.write_str("vcc_hi"),
|
|
126 => formatter.write_str("exec_lo"),
|
|
127 => formatter.write_str("exec_hi"),
|
|
register => write!(formatter, "s{register}"),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Instruction {
|
|
/// Returns the mnemonic for the exact encoding family represented.
|
|
pub fn mnemonic(self) -> &'static str {
|
|
match self {
|
|
Self::BufferLoadDword { .. } => "buffer_load_dword",
|
|
Self::BufferStoreDword { .. } => "buffer_store_dword",
|
|
Self::WaitLoad => "s_waitcnt",
|
|
Self::WaitStore => "s_waitcnt_vscnt",
|
|
Self::MoveB32 { .. } => "v_mov_b32_e32",
|
|
Self::AddNoCarryU32 { .. } => "v_add_nc_u32_e64",
|
|
Self::ScalarMoveB32 { .. } => "s_mov_b32",
|
|
Self::ScalarAlu { op, .. } => op.mnemonic(),
|
|
Self::ScalarCompareEqU32 { .. } => "s_cmp_eq_u32",
|
|
Self::BranchSccZero { .. } => "s_cbranch_scc0",
|
|
Self::BranchExecZero { .. } => "s_cbranch_execz",
|
|
Self::BranchVccZero { .. } => "s_cbranch_vccz",
|
|
Self::Branch { .. } => "s_branch",
|
|
Self::VectorLshlrevB32 { .. } => "v_lshlrev_b32_e32",
|
|
Self::VectorLshrrevB32 { .. } => "v_lshrrev_b32_e32",
|
|
Self::VectorAshrrevI32 { .. } => "v_ashrrev_i32_e32",
|
|
Self::VectorAndB32 { .. } => "v_and_b32_e32",
|
|
Self::VectorOrB32 { .. } => "v_or_b32_e32",
|
|
Self::VectorXorB32 { .. } => "v_xor_b32_e32",
|
|
Self::VectorAddF32 { .. } => "v_add_f32_e32",
|
|
Self::VectorMulF32 { .. } => "v_mul_f32_e32",
|
|
Self::Export { .. } => "exp",
|
|
Self::Interpolate {
|
|
phase: InterpolationPhase::First,
|
|
..
|
|
} => "v_interp_p1_f32_e32",
|
|
Self::Interpolate {
|
|
phase: InterpolationPhase::Second,
|
|
..
|
|
} => "v_interp_p2_f32_e32",
|
|
Self::ImageSampleLz { .. } => "image_sample_lz",
|
|
Self::EndProgram => "s_endpgm",
|
|
}
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for Instruction {
|
|
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
formatter.write_str(self.mnemonic())?;
|
|
match *self {
|
|
Self::BufferLoadDword {
|
|
data,
|
|
index,
|
|
resource,
|
|
}
|
|
| Self::BufferStoreDword {
|
|
data,
|
|
index,
|
|
resource,
|
|
} => {
|
|
let last = u16::from(resource) + 3;
|
|
write!(
|
|
formatter,
|
|
" v{data}, v{index}, s[{resource}:{last}], 0 idxen"
|
|
)
|
|
}
|
|
Self::WaitLoad => formatter.write_str(" vmcnt(0)"),
|
|
Self::WaitStore => formatter.write_str(" null, 0"),
|
|
Self::MoveB32 { dst, src } => write!(formatter, " v{dst}, {src}"),
|
|
Self::AddNoCarryU32 { dst, src0, src1 }
|
|
| Self::VectorLshlrevB32 { dst, src0, src1 }
|
|
| Self::VectorLshrrevB32 { dst, src0, src1 }
|
|
| Self::VectorAshrrevI32 { dst, src0, src1 }
|
|
| Self::VectorAndB32 { dst, src0, src1 }
|
|
| Self::VectorOrB32 { dst, src0, src1 }
|
|
| Self::VectorXorB32 { dst, src0, src1 }
|
|
| Self::VectorAddF32 { dst, src0, src1 }
|
|
| Self::VectorMulF32 { dst, src0, src1 } => {
|
|
write!(formatter, " v{dst}, v{src0}, v{src1}")
|
|
}
|
|
Self::ScalarMoveB32 { dst, src } => {
|
|
write!(formatter, " {}, {src}", ScalarDestination(dst))
|
|
}
|
|
Self::ScalarAlu {
|
|
dst, src0, src1, ..
|
|
} => write!(formatter, " {}, {src0}, {src1}", ScalarDestination(dst)),
|
|
Self::ScalarCompareEqU32 { src0, src1 } => write!(formatter, " {src0}, {src1}"),
|
|
Self::Branch { target }
|
|
| Self::BranchSccZero { target }
|
|
| Self::BranchExecZero { target }
|
|
| Self::BranchVccZero { target } => {
|
|
write!(formatter, " -> 0x{target:04x}")
|
|
}
|
|
Self::Export {
|
|
target,
|
|
sources: [a, b, c, d],
|
|
} => {
|
|
let (name, flags) = match target {
|
|
ExportTarget::Position0 => ("pos0", " done"),
|
|
ExportTarget::Parameter0 => ("param0", ""),
|
|
ExportTarget::RenderTarget0 => ("mrt0", " done vm"),
|
|
};
|
|
write!(formatter, " {name}, v{a}, v{b}, v{c}, v{d}{flags}")
|
|
}
|
|
Self::Interpolate {
|
|
dst,
|
|
coordinate,
|
|
attribute,
|
|
component,
|
|
..
|
|
} => {
|
|
let channel = ["x", "y", "z", "w"]
|
|
.get(usize::from(component))
|
|
.ok_or(fmt::Error)?;
|
|
write!(
|
|
formatter,
|
|
" v{dst}, v{coordinate}, attr{attribute}.{channel}"
|
|
)
|
|
}
|
|
Self::ImageSampleLz {
|
|
dst,
|
|
coord,
|
|
resource,
|
|
sampler,
|
|
} => {
|
|
let last_dst = dst + 3;
|
|
let last_coord = coord + 1;
|
|
let last_res = resource + 7;
|
|
let last_samp = sampler + 3;
|
|
write!(
|
|
formatter,
|
|
" v[{dst}:{last_dst}], v[{coord}:{last_coord}], s[{resource}:{last_res}], s[{sampler}:{last_samp}] dmask:0xf dim:SQ_RSRC_IMG_2D"
|
|
)
|
|
}
|
|
Self::EndProgram => Ok(()),
|
|
}
|
|
}
|
|
}
|