rocm/rocm-libraries/add-logical-ir-instruction
Claude Code and agent skill by rocm/rocm-libraries
- community
Checking that the SKILL.md file is still in the repository.
Guide for adding a new instruction to the stinkytofu logical IR path, covering the IR definition, arch mapping, the rocisa->logical bridge, and testing.
Install
npx vskill@latest install rocm/rocm-libraries/add-logical-ir-instructionSKILL.md
The same SKILL.md was published earlier as rocm/rocm-libraries/add-logical-ir-instruction.
Adding a New Instruction to the Logical IR Path
This guide explains how to fully integrate a new instruction into the stinkytofu logical IR path. Use this when:
- A new ISA introduces a hardware instruction that needs logical IR representation
- An existing rocisa instruction has no matching logical opcode yet, so the rocisa->logical bridge throws
no logical opcode for rocisa::XXX
Architecture Overview
KernelWriter builds rocisa instructions (imported through the tensilelite/ir.py facade). toStinkyTofuModule — compiled into _rocisa — walks that rocisa tree and converts each instruction into a LogicalInstruction in C++ (the rocisa->logical bridge), then the logical module is lowered to asm IR. There is no Python adaptor: the former rocisa_stinkytofu_adaptor shim and the ROCISA_BACKEND env switch were removed.
Direct logical build (future):tensilelite/ir.pyis the import facade through whichKernelWriter will eventually build logical instructions directly (via the Step 2 Pythonbinding) instead of rocisa. Until that migration flips for an instruction, the rocisa->logicalbridge (Step 4) is the live path.
Files at a Glance
| Step | File | Action | |
|---|---|---|---|
| 1 | stinkytofu/src/ir/logical/LogicalInstructionDefs.inc | Add IRInstDef entry | |
| 2 | (build-time auto-generated, no manual edit) | Verify build succeeds | |
| 3a | stinkytofu/hardware/src/gfx/common/Gfx125xRocisaMaps.hpp | Add logical-to-mnemonic mapping | |
| 3b | stinkytofu/hardware/src/gfx/Gfx1250/Gfx1250Instructions.def | Add .logical reverse mapping | |
| 3b' | stinkytofu/hardware/src/gfx/Gfx1250v0/Gfx1250v0Instructions.def | Same as 3b (if this arch variant also supports it) | |
| 4 | stinkytofu/src/conversion/rocisa/ToStinkyTofuUtils.cpp | Bridge rocisa->logical: usually automatic by name; add a logicalOpcodeFor case / operand handling only if needed | |
| 5 | stinkytofu/src/transforms/logical/ToStinkyAsmPass.cpp | (Optional) Special lowering logic | |
| 6 | stinkytofu/tests/unit/logical/LogicalToAsmMultiArchTest.cpp | Add test case |
Step 1: Define the Logical IR Instruction
File: stinkytofu/src/ir/logical/LogicalInstructionDefs.inc
Add a new IRInstDef entry. The nine fields in order are:
Example: Ternary vector ALU instruction
Example: DS load instruction
Common Flags Reference
| Flag | Meaning | |
|---|---|---|
IF_VALU | Vector ALU | |
IF_SALU | Scalar ALU | |
IF_Commutative | Operands are commutative | |
IF_DSRead | DS (LDS) read | |
IF_DSWrite | DS (LDS) write | |
IF_BufferRead | Buffer (MUBUF) read | |
IF_BufferWrite | Buffer (MUBUF) write | |
IF_FlatRead | Flat read | |
IF_FlatWrite | Flat write | |
IF_GlobalRead | Global read | |
IF_GlobalWrite | Global write | |
IF_Branch | Branch instruction |
Important: numSrcs Must Be Correct
numSrcs determines how many source operand parameters the tablegen-generated factory function accepts, and how many sources the rocisa->logical bridge copies. If set incorrectly, operands are dropped or the factory is called with the wrong number of arguments and fails at runtime.
Step 2: Verify Auto-Generated Artifacts (No Manual Edit)
When building stinkytofu, tools/tablegen/GenLogicalIR.cpp reads LogicalInstructionDefs.inc and auto-generates three files:
- `LogicalOpcodes_generated.inc` — enum values (e.g.
VAdd3U32 = 10) - `LogicalInstructions_generated.hpp` — C++ factory functions
- `PythonBindings_generated.inc` — nanobind Python bindings (used by the direct-build path and tests)
The factory function signature is derived automatically from the .inc fields:
numSrcs-> number of source parametershasDest-> whether a dest parameter existssupportsDPP-> whether anstd::optional<DPPModifiers> dppparameter is includedsupportsSDWA-> whether anstd::optional<SDWAModifiers> sdwaparameter is included
For example, VAdd3U32 (numSrcs=3, hasDest=true, DPP=true, SDWA=true) generates:
The corresponding Python binding:
Verification: After build completes, confirm the three_generatedfiles exist underbuild/with no tablegen errors.
Step 3: Architecture Mapping
You need to tell stinkytofu which hardware mnemonic this logical opcode maps to on a specific architecture. This requires two places:
3a. Rocisa Mapping Table
File: stinkytofu/hardware/src/gfx/common/Gfx125xRocisaMaps.hpp
Add a {logical_name, hw_mnemonic} pair in the map literal:
This table is used by ToStinkyAsmPass to look up the hardware mnemonic from the logical name.
3b. Hardware Instruction Definition
File: stinkytofu/hardware/src/gfx/Gfx1250/Gfx1250Instructions.def (and Gfx1250v0/Gfx1250v0Instructions.def for variants)
Add a .logical reverse mapping in the appropriate DEF_BATCH block:
.def files group instructions by encoding format using DEF_BATCH macros. Each instruction entry has three parts:
- InstName — C++ hardware instruction ID (convention:
<LogicalName>Inst) - mnemonic — assembly mnemonic (
"v_add3_u32") - .logical — reverse pointer to the logical opcode name
At build time, GenLogicalToAsmMapping.cpp reads all .def files to generate LogicalToAsmMappings_generated.inc.
Step 4: Bridge rocisa → Logical IR (C++)
File: stinkytofu/src/conversion/rocisa/ToStinkyTofuUtils.cpp
KernelWriter builds rocisa instructions; toStinkyTofuModule walks that tree and converts each rocisa instruction into a LogicalInstruction via createLogicalFromRocisa. This bridge is entirely in C++ — there is no Python adaptor anymore.
For most instructions, nothing to do. logicalOpcodeFor() falls through to logical::parseOpcode(className), which matches the rocisa class name to the logical opcode of the same name. So if your rocisa class is VAdd3U32 and you defined logical opcode VAdd3U32 in Step 1, it bridges automatically.
Add a special case in logicalOpcodeFor() only when:
- Name differs between rocisa class and logical opcode (e.g. rocisa
SSchedulingFence-> logicalSchedulingFence), or - Family dispatch by C++ type (e.g.
MFMAInstruction/SMFMAInstruction/MXMFMAInstruction, or theSWaitCntfamily).
Operands and modifiers are copied automatically by fillLogicalOperands() (dst / srcs) and applyLogicalPayload() (VOP3P / SDWA / DPP / True16 / DS / FLAT / MUBUF / SMEM / memtoken, dispatched by rocisa type). Add a branch there only if the instruction carries an unusual modifier not already handled by its category.
Step 5: Special Lowering (Optional)
File: stinkytofu/src/transforms/logical/ToStinkyAsmPass.cpp
Most instruction lowering (logical -> asm IR) follows the generic path: look up the arch mapping for the mnemonic, copy operands and modifiers. However, the following cases require special handling:
When Special Handling Is Needed
- Instruction splitting — e.g.
VCmpXon architectures that don't supportv_cmpxmust be split intov_cmp+s_mov_exec - Implicit operands — e.g. automatically inserting VCC carry operands
- Special metadata — e.g. MFMA's
MFMAData(type, m, n, k, etc.) needs to be converted to asm IR modifiers - Conditional mnemonic selection — e.g. choosing different hw instructions based on data type
When Special Handling Is NOT Needed
Most ALU, compare, and branch instructions only need to exist in the arch mapping table. The generic path in ToStinkyAsmPass handles them automatically.
Step 6: Testing
File: stinkytofu/tests/unit/logical/LogicalToAsmMultiArchTest.cpp
6a. Add a Factory Switch Case
In the createInstruction function, add a case:
The number of arguments = hasDest(1) + numSrcs. Choose vgpr/sgpr based on instruction category.
6b. Add to SKIP_LOWERING (if Step 3 was skipped)
If the instruction has no arch mapping (Step 3 was skipped — no entry in Gfx125xRocisaMaps.hpp or Gfx1250Instructions.def), you must add its opcode to the SKIP_LOWERING set inside AllInstructionsAllArchitectures:
Without this, the comprehensive test attempts to lower every opcode on every architecture. An opcode with no arch mapping will hit an UNREACHABLE in ToStinkyAsmPass and abort the entire test binary (not just fail one case).
6c. (Optional) Add Exact Mnemonic Verification
Add an entry in EXPECTED_LOWERING_GFX1250 (or the corresponding architecture map):
Instructions not listed in this table are still tested (lowering success counts as a pass), but the generated mnemonic is not verified. Adding entries here is recommended for important instructions.
Full Checklist
When adding instruction XXX, verify each item:
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingshared/stinkytofu/.cursor/skills/add-logical-ir-instruction/SKILL.md
Popularity
- Repository stars
- 441
- Repository forks
- 420
- 7-day trend score
- 30
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/rocm/rocm-libraries/add-logical-ir-instruction)