@@ -111,6 +111,31 @@ const FMA_RULES = RewriteRule[
111111
112112fma_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
122147and subprogram compilation.
123148"""
124149function 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