ml_switcheroo.compiler.frontends.rdnaΒΆ

RDNA Frontend Package.

Contains the parser and lifter logic for converting AMD RDNA/GCN assembly text into Abstract Syntax Trees (AST) and then into the Logical Graph IR.

SubmodulesΒΆ

ClassesΒΆ

Comment

Represents a line comment.

Directive

Represents an assembler directive.

Immediate

Represents a literal constant value.

Instruction

Represents a single RDNA operation line.

Label

Represents a jump target label.

LabelRef

Represents a reference to a label (e.g. as a jump target) or generic identifier operand.

Memory

Represents a memory address operand, typically used in generic loads/stores.

Modifier

Represents an instruction modifier or attribute.

Operand

Base class for instruction operands (Registers, Immediates, etc.).

RdnaNode

Abstract base class for all RDNA AST nodes.

SGPR

Represents a Scalar General Purpose Register (e.g., s0, s10).

VGPR

Represents a Vector General Purpose Register (e.g., v0, v255).

RdnaParser

Recursive descent parser for AMD RDNA / GCN assembly.

RdnaLexer

Regex-based Lexer for AMD RDNA / GCN assembly.

Token

Represents a lexical unit.

TokenType

Enumeration of valid RDNA token types.

RdnaLifter

Reconstructs a LogicalGraph from a sequence of RDNA AST nodes.

RdnaAnalyzer

Analyzes sequences of RDNA instructions to reverse-engineer high-level parameters.

FunctionsΒΆ

c_SGPR(β†’Β SGPR)

Helper to create a single SGPR.

c_VGPR(β†’Β VGPR)

Helper to create a single VGPR.

Package ContentsΒΆ

class ml_switcheroo.compiler.frontends.rdna.Comment[source]ΒΆ

Bases: RdnaNode

Represents a line comment. RDNA assembly uses ; for comments.

Format: ; {text}

textΒΆ

The comment content.

Type:

str

text: strΒΆ
__str__() β†’ str[source]ΒΆ

Returns the valid RDNA string representation of the node.

class ml_switcheroo.compiler.frontends.rdna.Directive[source]ΒΆ

Bases: RdnaNode

Represents an assembler directive.

Format: .{name} {params}

nameΒΆ

The directive name (e.g., β€œglobal_base”).

Type:

str

paramsΒΆ

List of string parameters.

Type:

List[str]

name: strΒΆ
params: List[str] = []ΒΆ
__str__() β†’ str[source]ΒΆ

Returns the valid RDNA string representation of the node.

class ml_switcheroo.compiler.frontends.rdna.Immediate[source]ΒΆ

Bases: Operand

Represents a literal constant value.

valueΒΆ

The numeric value.

Type:

Union[int, float]

is_hexΒΆ

If True, renders as hex string (e.g., 0xff).

Type:

bool

value: int | floatΒΆ
is_hex: bool = FalseΒΆ
__str__() β†’ str[source]ΒΆ

Returns the valid RDNA string representation of the node.

class ml_switcheroo.compiler.frontends.rdna.Instruction[source]ΒΆ

Bases: RdnaNode

Represents a single RDNA operation line.

Format: {opcode} {operands} Modifiers are treated as operands in the list for flexible placement.

opcodeΒΆ

The instruction mnemonic (e.g., β€œv_add_f32”, β€œs_mov_b32”).

Type:

str

operandsΒΆ

List of operand nodes.

Type:

List[Operand]

opcode: strΒΆ
operands: List[Operand] = []ΒΆ
__str__() β†’ str[source]ΒΆ

Returns the valid RDNA string representation of the node.

class ml_switcheroo.compiler.frontends.rdna.Label[source]ΒΆ

Bases: RdnaNode

Represents a jump target label.

Format: {name}:

nameΒΆ

The label identifier.

Type:

str

name: strΒΆ
__str__() β†’ str[source]ΒΆ

Returns the valid RDNA string representation of the node.

class ml_switcheroo.compiler.frontends.rdna.LabelRef[source]ΒΆ

Bases: Operand

Represents a reference to a label (e.g. as a jump target) or generic identifier operand.

nameΒΆ

The label identifier.

Type:

str

name: strΒΆ
__str__() β†’ str[source]ΒΆ

Returns the valid RDNA string representation of the node.

class ml_switcheroo.compiler.frontends.rdna.Memory[source]ΒΆ

Bases: Operand

Represents a memory address operand, typically used in generic loads/stores. RDNA often passes the address as a register or register pair, but explicit offset syntax exists.

Format: method[base + offset] or simply register references depending on op. This node models the bracketed syntax global_load_dword v0, v[1:2], off. Usually, RDNA just uses registers as operands, but sometimes specific addressing notation is used. Here we treat it as an explicit container if needed, primarily for offsets.

baseΒΆ

The base register.

Type:

Union[SGPR, VGPR]

offsetΒΆ

Immediate byte offset.

Type:

Optional[int]

base: SGPR | VGPRΒΆ
offset: int | None = NoneΒΆ
__str__() β†’ str[source]ΒΆ

Returns the valid RDNA string representation of the node.

class ml_switcheroo.compiler.frontends.rdna.Modifier[source]ΒΆ

Bases: Operand

Represents an instruction modifier or attribute. e.g., glc, slc, off.

nameΒΆ

The modifier string.

Type:

str

name: strΒΆ
__str__() β†’ str[source]ΒΆ

Returns the valid RDNA string representation of the node.

class ml_switcheroo.compiler.frontends.rdna.Operand[source]ΒΆ

Bases: RdnaNode

Base class for instruction operands (Registers, Immediates, etc.).

__str__() β†’ str[source]ΒΆ

Returns the valid RDNA string representation of the node.

class ml_switcheroo.compiler.frontends.rdna.RdnaNode[source]ΒΆ

Bases: abc.ABC

Abstract base class for all RDNA AST nodes.

abstractmethod __str__() β†’ str[source]ΒΆ

Returns the valid RDNA string representation of the node.

class ml_switcheroo.compiler.frontends.rdna.SGPR[source]ΒΆ

Bases: Operand

Represents a Scalar General Purpose Register (e.g., s0, s10).

indexΒΆ

The register index.

Type:

int

countΒΆ

If > 1, represents a range/multi-register (e.g., s[0:3]).

Type:

int

index: intΒΆ
count: int = 1ΒΆ
__str__() β†’ str[source]ΒΆ

Returns the valid RDNA string representation of the node.

class ml_switcheroo.compiler.frontends.rdna.VGPR[source]ΒΆ

Bases: Operand

Represents a Vector General Purpose Register (e.g., v0, v255).

indexΒΆ

The register index.

Type:

int

countΒΆ

If > 1, represents a range/multi-register (e.g., v[0:3]).

Type:

int

index: intΒΆ
count: int = 1ΒΆ
__str__() β†’ str[source]ΒΆ

Returns the valid RDNA string representation of the node.

ml_switcheroo.compiler.frontends.rdna.c_SGPR(idx: int) β†’ SGPR[source]ΒΆ

Helper to create a single SGPR.

ml_switcheroo.compiler.frontends.rdna.c_VGPR(idx: int) β†’ VGPR[source]ΒΆ

Helper to create a single VGPR.

class ml_switcheroo.compiler.frontends.rdna.RdnaParser(code: str)[source]ΒΆ

Recursive descent parser for AMD RDNA / GCN assembly.

lexerΒΆ
tokensΒΆ
pos = 0ΒΆ
parse() β†’ List[ml_switcheroo.compiler.frontends.rdna.nodes.RdnaNode][source]ΒΆ

Parses the entire code block.

class ml_switcheroo.compiler.frontends.rdna.RdnaLexer[source]ΒΆ

Regex-based Lexer for AMD RDNA / GCN assembly.

PATTERNS: List[Tuple[TokenType, str]]ΒΆ
regex_pairsΒΆ
tokenize(text: str) β†’ Generator[Token, None, None][source]ΒΆ

Tokenizes the input string.

Parameters:

text (str) – Raw RDNA source code.

Yields:

Token – Token objects.

Raises:

ValueError – If an unrecognized character sequence is encountered.

class ml_switcheroo.compiler.frontends.rdna.Token[source]ΒΆ

Represents a lexical unit.

kindΒΆ

The type of token.

Type:

TokenType

valueΒΆ

The raw string value.

Type:

str

lineΒΆ

Line number in source (1-based).

Type:

int

columnΒΆ

Column number in source (1-based).

Type:

int

kind: TokenTypeΒΆ
value: strΒΆ
line: intΒΆ
column: intΒΆ
class ml_switcheroo.compiler.frontends.rdna.TokenType(*args, **kwds)[source]ΒΆ

Bases: enum.Enum

Enumeration of valid RDNA token types.

LABEL_DEFΒΆ
DIRECTIVEΒΆ
COMMENTΒΆ
COMMAΒΆ
RBRACKETΒΆ
LBRACKETΒΆ
COLONΒΆ
SGPRΒΆ
VGPRΒΆ
SPECIAL_REGΒΆ
IMMEDIATEΒΆ
MODIFIERΒΆ
IDENTIFIERΒΆ
class ml_switcheroo.compiler.frontends.rdna.RdnaLifter[source]ΒΆ

Reconstructs a LogicalGraph from a sequence of RDNA AST nodes.

lift(nodes: List[ml_switcheroo.compiler.frontends.rdna.nodes.RdnaNode]) β†’ ml_switcheroo.compiler.ir.LogicalGraph[source]ΒΆ

Parses a list of RDNA nodes to build a LogicalGraph.

class ml_switcheroo.compiler.frontends.rdna.RdnaAnalyzer[source]ΒΆ

Analyzes sequences of RDNA instructions to reverse-engineer high-level parameters.

static analyze_block(kind: str, instructions: List[ml_switcheroo.compiler.frontends.rdna.nodes.Instruction]) β†’ Dict[str, Any][source]ΒΆ

Extracts metadata from a block of instructions based on the operation kind.

Parameters:
  • kind (str) – The operation type (e.g. β€œConv2d”, β€œLinear”).

  • instructions (List[Instruction]) – The assembly lines inside the block.

Returns:

Extracted parameters (e.g., {β€œk”: 3}).

Return type:

Dict[str, Any]