Make canonicalization patterns report failure when they change nothing - #323
Merged
Merged
Conversation
MLIR's greedy rewrite driver treats every successful pattern as an IR change
and re-scans the region, up to 10 times. NormalizeCast, RemoveUnused and
RemoveUnusedAccessor returned success even when they left the IR untouched
(a cast they do not rewrite, an op that still has uses), so canonicalization
never converged: on a small test every canonicalizer run hit the iteration
limit ("did not converge after scanning 10 times", 9 times), with 714 false
successes from NormalizeCast and 470 from RemoveUnused<LoadOp> alone.
They now return failure when nothing was rewritten or erased. On that test the
driver processes 699 operations instead of 4523, with no convergence warnings.
The emitted LLVM IR is unchanged.
Co-Authored-By: Claude Opus 5 <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.
Problem
MLIR's greedy rewrite driver treats every successful pattern as an IR change: it sets
changed = true(GreedyPatternRewriteDriver.cpp:626) and scans the region again, up to 10 times. Three of our canonicalization patterns returnedsuccess()even when they left the IR untouched:NormalizeCast: for casts it does not rewrite (toany, union casts that need no tag, everything else)RemoveUnused<T>(SymbolRef,Load,Undef,Null, ...): for ops that still have usesRemoveUnusedAccessor<T>: for setters and used gettersSo canonicalization never converged. On a ~30-line test,
-debug-only=greedy-rewritershows 714 false successes fromNormalizeCastand 470 fromRemoveUnused<LoadOp>, and every canonicalizer run ends withThe pattern rewrite did not converge after scanning 10 times(9 times).Fix
Each pattern now returns
failure()unless it actually rewrote or erased the op.Results
00any.ts--emit=mlir-llvm, debug build, best of 300generator.ts, sameThe emitted LLVM IR is identical for
00class,00union_type,00any,00tuple,00interfaceand00generator. The only differing line is the embedded source path.Tests
Full Windows debug suite: 2704/2721. The 17 failures are the same pre-existing set as on
main(00funcs_generic_with_typeof,01symbol,00owned_array_opsunder rc,gc-shared-auto,gc-defaultlib-collector); no new failures.🤖 Generated with Claude Code