Skip to content

[stinkytofu] Use typed enums for DPP and MFMA modifier fields#7258

Open
darrenhsieh-amd wants to merge 2 commits intodevelopfrom
users/darrenh/st_modifiers_enum_types
Open

[stinkytofu] Use typed enums for DPP and MFMA modifier fields#7258
darrenhsieh-amd wants to merge 2 commits intodevelopfrom
users/darrenh/st_modifiers_enum_types

Conversation

@darrenhsieh-amd
Copy link
Copy Markdown
Contributor

@darrenhsieh-amd darrenhsieh-amd commented May 10, 2026

Motivation

The DPP and MFMA modifier fields were raw ints and free-form strings — hardware semantics buried in field names, re-parsed at every use. Move DPP and MFMA modifier fields onto typed enums for compile-time safety and direct hardware mapping. The new enums also cover the full hardware encoding space (all 9-bit dpp_ctrl shapes, the full F8F6F4 matrix-format family), where the pre-refactor int/string fields only expressed the partial set earlier code happened to need.

Technical Details

Commit 1 — DPP:

  • Introduce DppCtrl enum covering the full 9-bit hardware dpp_ctrl field with three encoding shapes:
    • singleton (e.g. ROW_MIRROR = 0x140)
    • parameterized base + amount (e.g. row_shl:N = ROW_SHL0 + N)
    • bit-packed quad_perm (4×2-bit lanes)
  • Add *_FIRST / *_LAST sentinels per parameterized range (ROW_SHL_FIRST/LAST, ROW_SHARE_FIRST/LAST, ROW_XMASK_FIRST/LAST) for compile-time validity bounds.
  • Add dppCtrlToAsmStr / parseDppCtrlFromAsm for round-trip asm <-> enum conversion.
  • Add small builders: dppRowShl, dppRowShr, dppRowRor, dppQuadPerm, dppRowShare, dppRowXmask.
  • Replace row_shr / row_bcast int fields in DPPModifiers with the enum.

Commit 2 — MFMA:

  • Split the previous MFMA modifier into:
    • MFMAModifiers — slim, just negBits (MFMANegBits sub-struct with per-source neg_lo / neg_hi) plus reuseA / reuseB.
    • MatrixFmtModifiers — separate TypedModifier so cost-override .def patterns can match it independently. Fields: fmtA, fmtB, scaleFmtA, scaleFmtB + isMXMFMA() / empty() helpers.
  • New enums: MatrixFmt (FP8/BF8/FP6/BF6/FP4) and MatrixScaleFmt (E8/E5M3/E4M3) with matrixFmtToStr / parseMatrixFmt and matrixScaleFmtToStr / parseMatrixScaleFmt helpers.
  • Converter (ToStinkyTofuUtils.cpp): refactored extractNegModifiers, extractMatrixFormats, and handleMFMAModifiers / handleMXMFMAModifiers to attach the new typed modifiers.

Test Plan

st unit tests(including filecheckes) all passed.
codegen tests all passed.

Submission Checklist

Copy link
Copy Markdown
Contributor

@KKyang KKyang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice cleanup — typed enums are a clear improvement over raw ints/strings. A few things:

  1. Stale README: tools/stinkytofu-opt/README.md line 314 still shows the old mod.mfma serialization format (inputPermute, scaleStr, negStr, neg_lo, neg_hi). Should be updated to match the new format.

  2. Unused parameter in handleMXMFMAModifiers: After the refactor, mxmfmaInst is never read — the function only parses from instString. If string parsing is the intended approach, the rocisa::MXMFMAInstruction* parameter should be removed from the signature (and callers updated). If the structured fields from rocisa are more reliable, consider using them instead of string extraction.

  3. No range check in DPP builder helpers (nit): e.g. dppRowShr(16) silently produces 0x120 which is ROW_ROR0 — a valid but wrong DppCtrl. A debug assert on n >= 1 && n <= 15 would be cheap insurance.

…re encoding

DPPModifiers previously carried only 3 ints (row_shr, row_bcast,
bound_ctrl) — too narrow for the actual dpp_ctrl encoding and dropped
quad_perm, row_shl, row_share, row_xmask, DPP8, etc. Replace with a
DppCtrl enum and full field set matching the hardware, plus deserialize
support that was previously missing.
… types

MFMAModifiers previously carried matrix format and neg-bit data as raw
strings (inputPermute, negStr, scaleStr) plus redundant rocisa-pipeline
ints (isMXMFMA, mxInstType, mxScaleAType, mxScaleBType). Replace with
typed MatrixFmt/MatrixScaleFmt enums and an MFMANegBits sub-struct, and
move matrix data/scale formats to a separate MatrixFmtModifiers so the
cost-override table can match on it as a first-class Modifier type.
@darrenhsieh-amd darrenhsieh-amd force-pushed the users/darrenh/st_modifiers_enum_types branch from 8562152 to 681d260 Compare May 11, 2026 08:55
@darrenhsieh-amd
Copy link
Copy Markdown
Contributor Author

Thanks for the feedback.
All done. As for the item2 I took first option. Since rocisa's tostring() has emit-decision rules. That might need rocisa side to provide some accessors like getMatrixFmtEmit(). (We can have that in separate PR if we decide to support it.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants