ml_switcheroo.compiler.frontends.sass

SASS Frontend (Parser & Lifter).

Handles the parsing of NVIDIA SASS assembly text into an Abstract Syntax Tree (AST) and the lifting of that AST into the high-level 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 SASS operation line.

Label

Represents a jump target label.

Memory

Represents a memory address operand.

Operand

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

Predicate

Represents a predicate register (e.g., @P0, !P1).

Register

Represents a general-purpose register (e.g., R0, RZ).

SassNode

Abstract base class for all SASS AST nodes.

SassParser

Recursive descent parser for NVIDIA SASS.

SassLexer

Regex-based Lexer for NVIDIA SASS assembly.

Token

Represents a lexical unit.

TokenType

Enumeration of valid SASS token types.

SassLifter

Reconstructs a LogicalGraph from a sequence of SASS AST nodes.

SassAnalyzer

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

Package Contents

class ml_switcheroo.compiler.frontends.sass.Comment[source]

Bases: SassNode

Represents a line comment.

Format: // {text}

text

The comment content.

text: str
__str__() str[source]

Returns the valid SASS string representation of the node.

class ml_switcheroo.compiler.frontends.sass.Directive[source]

Bases: SassNode

Represents an assembler directive.

Format: .{name} {params}

name

The directive name (e.g., “headerflags”).

Type:

str

params

List of string parameters.

Type:

List[str]

name: str
params: List[str] = []
__str__() str[source]

Returns the valid SASS string representation of the node.

class ml_switcheroo.compiler.frontends.sass.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., “0x1”).

Type:

bool

value: int | float
is_hex: bool = False
__str__() str[source]

Returns the valid SASS string representation of the node.

class ml_switcheroo.compiler.frontends.sass.Instruction[source]

Bases: SassNode

Represents a single SASS operation line.

Format: @{predicate} {opcode} {operands};

opcode

The instruction mnemonic (e.g., “FADD”, “MOV”).

Type:

str

operands

List of operand nodes.

Type:

List[Operand]

predicate

Optional predicate guard executed before instruction.

Type:

Optional[Predicate]

opcode: str
operands: List[Operand] = []
predicate: Predicate | None = None
__str__() str[source]

Returns the valid SASS string representation of the node.

class ml_switcheroo.compiler.frontends.sass.Label[source]

Bases: SassNode

Represents a jump target label.

Format: {name}:

name

The label identifier.

Type:

str

name: str
__str__() str[source]

Returns the valid SASS string representation of the node.

class ml_switcheroo.compiler.frontends.sass.Memory[source]

Bases: Operand

Represents a memory address operand.

Supports Constant Bank access (e.g., c[0x0][0x4]) and Global/Local addressing (e.g., [R1], [R1 + 0x4]).

base

The base register (e.g., “R1”) or constant bank string (e.g., “c[0x0]”).

Type:

Union[str, Register]

offset

Optional byte offset to add to the base.

Type:

Optional[int]

base: str | Register
offset: int | None = None
__str__() str[source]

Returns the valid SASS string representation of the node.

class ml_switcheroo.compiler.frontends.sass.Operand[source]

Bases: SassNode

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

class ml_switcheroo.compiler.frontends.sass.Predicate[source]

Bases: Operand

Represents a predicate register (e.g., @P0, !P1).

Used both as instruction guards (preceding the opcode) and as operands in logical instructions (ISETP).

name

The predicate identifier (e.g., “P0”, “PT”).

Type:

str

negated

If True, indicates logical NOT (e.g., “!P0”).

Type:

bool

name: str
negated: bool = False
__str__() str[source]

Returns the valid SASS string representation of the node.

class ml_switcheroo.compiler.frontends.sass.Register[source]

Bases: Operand

Represents a general-purpose register (e.g., R0, RZ).

name

The register identifier (e.g., “R0”, “RZ”).

Type:

str

negated

If True, prepends a negation sign (e.g., “-R0”).

absolute

If True, wraps in absolute value pipes (e.g., “|R0|”).

name: str
negated: bool = False
absolute: bool = False
__str__() str[source]

Returns the valid SASS string representation of the node.

class ml_switcheroo.compiler.frontends.sass.SassNode[source]

Bases: abc.ABC

Abstract base class for all SASS AST nodes.

abstractmethod __str__() str[source]

Returns the valid SASS string representation of the node.

class ml_switcheroo.compiler.frontends.sass.SassParser(code: str)[source]

Recursive descent parser for NVIDIA SASS.

lexer
tokens
pos = 0
parse() List[ml_switcheroo.compiler.frontends.sass.nodes.SassNode][source]

Parses the entire code block.

Returns:

A list of AST nodes.

Return type:

List[SassNode]

class ml_switcheroo.compiler.frontends.sass.SassLexer[source]

Regex-based Lexer for NVIDIA SASS assembly.

PATTERNS: List[Tuple[TokenType, str]]
regex_pairs
tokenize(text: str) Generator[Token, None, None][source]

Tokenizes the input string.

Parameters:

text (str) – Raw SASS source code.

Yields:

Token – Token objects.

Raises:

ValueError – If an unrecognized character sequence is encountered.

class ml_switcheroo.compiler.frontends.sass.Token[source]

Represents a lexical unit.

kind

The type of token.

Type:

TokenType

value

The raw string content.

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.sass.TokenType(*args, **kwds)[source]

Bases: enum.Enum

Enumeration of valid SASS token types.

LABEL_DEF
DIRECTIVE
COMMENT
SEMICOLON
COMMA
PREDICATE
REGISTER
MEMORY
IMMEDIATE
IDENTIFIER
class ml_switcheroo.compiler.frontends.sass.SassLifter[source]

Reconstructs a LogicalGraph from a sequence of SASS AST nodes.

lift(nodes: List[ml_switcheroo.compiler.frontends.sass.nodes.SassNode]) ml_switcheroo.compiler.ir.LogicalGraph[source]

Parses a list of SASS nodes to build a LogicalGraph.

Captures instructions within BEGIN/END blocks to feed into the Analyzer. Captures orphan instructions into individual functional nodes (1:1 mapping).

class ml_switcheroo.compiler.frontends.sass.SassAnalyzer[source]

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

static analyze_block(kind: str, instructions: List[ml_switcheroo.compiler.frontends.sass.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., {“kernel_size”: 3}).

Return type:

Dict[str, Any]