Add algebraic simplifications to get rid of 1-based index IR bloat#156
Merged
Conversation
Adds an algebra_pass! with two rewrite rules that cancel inverse addi/subi pairs (x+c-c → x, x-c+c → x). This eliminates the redundant subi instructions generated by the 1-based to 0-based index conversion in load/store operations (e.g. bid() + One() - One()). Two supporting changes to the rewrite framework: - DefEntry no longer caches a stale operands copy; pattern matching now reads live operands from the instruction via resolve_call, so bindings reflect updates from prior rewrites within the same pass. - RBind consumed tracking only marks the root instruction, leaving shared intermediates matchable by subsequent rules (e.g. a single addi used by multiple subi sites). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Replace the single-pass linear scan driver with a LIFO worklist that processes until fixpoint, inspired by MLIR's GreedyPatternRewriteDriver. Key changes: - No stale MatchContext: DefEntry reads live operands from the IR via resolve_call. Use counts computed on-demand via uses() — always fresh. - Worklist with notifications: when a rewrite fires, affected instructions (users, operand-producers) are re-added to the worklist, enabling cascading rewrites across rule sets. - Unified rule set: all rewrite rules (normalize, algebra, SVE, FMA) run in a single fixpoint invocation instead of separate passes. - Trivial dead-op elimination on worklist pop: keeps use counts accurate for one_use patterns (e.g. FMA fusion after SVE removes transparent op chains). Full DCE still runs after for complex dead code. - Safe intermediate deletion: substitution rewrites only delete matched intermediates that have no remaining uses, fixing a bug where transparent-op tracing could add multi-use intermediates to matched_ssas. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Replace the manual defs-scanning workaround in _add_users_to_worklist! with the new users(block, val) API from IRStructurizer. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The Worklist, DefEntry, defs dict, matched_ssas, and all notification/apply functions now use SSAValue instead of Int, eliminating the same kind of type confusion between IR references and literal integers that was fixed in IRStructurizer's normalize_key. Co-Authored-By: Claude Opus 4.6 (1M context) <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.
This PR reworks the rewriter so that we can implement algebraic simplifcations, with as goal to eliminate the IR bloat from 1-based indices (
addiandsubieverywhere). As an example, let's look atvaddfrom the README:On #155, made it so that the
subihappens later, and once per index, reducing the number of redundant operations:However, the
addi/subipairs essentially remained, but essentially were just less numerous. Instead, this PR reworks the IR rewriter to support algebraic simplifications that allows us to eliminate theaddi/subipairs entirely, resulting in much cleaner IR: