Skip to content

Commit 2e0abf9

Browse files
authored
Merge pull request #156 from JuliaGPU/tb/algebraic_simplfications
Add algebraic simplifications to get rid of 1-based index IR bloat
2 parents edfceaa + c11c65d commit 2e0abf9

File tree

2 files changed

+278
-124
lines changed

2 files changed

+278
-124
lines changed

src/compiler/codegen/passes/pipeline.jl

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,31 @@ const FMA_RULES = RewriteRule[
111111

112112
fma_fusion_pass!(sci::StructuredIRCode) = rewrite_patterns!(sci, FMA_RULES)
113113

114+
#=============================================================================
115+
Algebraic Simplification (rewrite)
116+
=============================================================================#
117+
118+
# Cancel inverse addi/subi pairs: x+c-c → x, x-c+c → x.
119+
# Repeated ~c binds enforce that both operands are the same value.
120+
121+
const ALGEBRA_RULES = RewriteRule[
122+
@rewrite Intrinsics.subi(Intrinsics.addi(~x, ~c), ~c) => ~x
123+
@rewrite Intrinsics.addi(Intrinsics.subi(~x, ~c), ~c) => ~x
124+
]
125+
126+
algebra_pass!(sci::StructuredIRCode) = rewrite_patterns!(sci, ALGEBRA_RULES)
127+
128+
#=============================================================================
129+
Combined Rule Set
130+
=============================================================================#
131+
132+
const ALL_REWRITE_RULES = RewriteRule[
133+
NORMALIZE_RULES...,
134+
ALGEBRA_RULES...,
135+
SVE_RULES...,
136+
FMA_RULES...,
137+
]
138+
114139
#=============================================================================
115140
Pass Pipeline
116141
=============================================================================#
@@ -122,10 +147,8 @@ Run the full pass pipeline on a StructuredIRCode. Called for both kernel
122147
and subprogram compilation.
123148
"""
124149
function run_passes!(sci::StructuredIRCode)
125-
# Rewrite passes (order matters: normalize before optimize, SVE before FMA)
126-
normalize_pass!(sci)
127-
scalar_view_elim_pass!(sci)
128-
fma_fusion_pass!(sci)
150+
# All rewrite rules in one fixpoint pass
151+
rewrite_patterns!(sci, ALL_REWRITE_RULES)
129152

130153
# Memory ordering
131154
alias_result = alias_analysis_pass!(sci)

0 commit comments

Comments
 (0)