[MFMA] Add 16x16x16 bf16/f16 support with fly-fix-bitcast-width pass - #1012
Draft
RichardChamberlain1 wants to merge 1 commit into
Draft
[MFMA] Add 16x16x16 bf16/f16 support with fly-fix-bitcast-width pass#1012RichardChamberlain1 wants to merge 1 commit into
RichardChamberlain1 wants to merge 1 commit into
Conversation
Fix MFMA 16x16x16 bf16/f16 lowering crash when BufferCopy128b loads 128-bit values that feed 64-bit MFMA operands. The MLIR canonicalizer folds extract_strided_slice + bitcast chains back to the wider source, producing invalid width-changing bitcasts (e.g. i128 → vector<4xi16>) that LLVM rejects. Three layers of defense: 1. emitAtomCallSSA matchWidth (CDNA3/CDNA4 MmaAtom.cpp): handles the SSA path with vector extract_strided_slice when source is wider than the MFMA operand. 2. ConvertAtomCallToSSAForm narrowToMmaWidth: narrows register values to match the MMA atom's expected operand width at pass 06. 3. fly-fix-bitcast-width pass (new): runs before the canonicalizer, inserts llvm.freeze on narrowing extract_strided_slice results whose source traces back to a wider integer type, blocking the canonicalizer from folding the chain into an invalid bitcast. Verified correct for mma_k=32 block_n=32 (no regression), mma_k=16 block_n=32, and mma_k=16 block_n=64. Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
BufferCopy128bloads 128-bit values that feed 64-bit MFMA operandsfly-fix-bitcast-widthcompiler pass to prevent MLIR canonicalizer from folding extract chains into width-mismatched bitcastsmatchWidthhelper in CDNA3/CDNA4emitAtomCallSSAfor SSA-path width narrowingnarrowToMmaWidthinConvertAtomCallToSSAFormfor early operand narrowingBug
When
BufferCopy128bloadsi128and the 16x16x16 MFMA expectsvector<4xi16>(64 bits), the MLIR canonicalizer folds:into:
which LLVM rejects with
"Invalid cast!"(128 != 64 bits).Fix
Three layers of defense:
emitAtomCallSSAmatchWidth (CDNA3/MmaAtom.cpp,CDNA4/MmaAtom.cpp): When the incoming SSA value is a wider vector than the MFMA operand, bitcast to the target element type at full width thenvector.extract_strided_sliceto narrow. Handles the SSA-only path (FileCheck tests).ConvertAtomCallToSSAFormnarrowToMmaWidth (ConvertAtomCallToSSAForm.cpp): After loading the register value viaPtrLoadOp, compares the loaded type's width against the MMA atom's expected operand width (fromgetThrValLayoutA/B()cosize). InsertsExtractStridedSliceOpif wider.fly-fix-bitcast-widthpass (FixBitcastWidth.cpp): Runs before the canonicalizer in the ROCm pipeline. Walks allvector.extract_strided_sliceops whose source traces back through bitcasts to a wider integer type (e.g.i128fromrocdl.raw.ptr.buffer.load). Insertsllvm.freezeon the extract result, which blocks the canonicalizer from folding the extract chain back to the wider source.freezeis an identity on well-defined values so correctness is preserved.Files changed
lib/Dialect/FlyROCDL/CDNA3/MmaAtom.cppmatchWidthlambda replaces direct bitcastlib/Dialect/FlyROCDL/CDNA4/MmaAtom.cppmatchWidthfixlib/Dialect/Fly/Transforms/FixBitcastWidth.cppfly-fix-bitcast-widthpasslib/Dialect/Fly/Transforms/ConvertAtomCallToSSAForm.cppnarrowToMmaWidthhelperlib/Dialect/Fly/CMakeLists.txtinclude/flydsl/Dialect/Fly/Transforms/Passes.tdpython/flydsl/compiler/backends/rocm.pytests/mlir/Conversion/mma_atom.mlirtests/mlir/Conversion/mma_atom_16x16x16.mlirVerification
All three configurations produce correct results with matching LLVM/FlyDSL build:
mma_k=32, block_n=32mma_k=16, block_n=32mma_k=16, block_n=64Test plan
bash scripts/build.sh -j64compiles cleanlyfly-opt tests/mlir/Conversion/mma_atom.mlirandmma_atom_16x16x16.mlirpassbash scripts/run_tests.sh— no regressionsmma_k=32 block_n=32produces correct attention outputmma_k=16 block_n=32produces correct attention outputmma_k=16 block_n=64produces correct attention output