File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -131,3 +131,29 @@ def constant_fold_binary_op_extended(
131131 return left * right
132132
133133 return None
134+
135+
136+ def try_constant_fold (builder : IRBuilder , expr : Expression ) -> Value | None :
137+ """Return the constant value of an expression if possible.
138+
139+ Return None otherwise.
140+ """
141+ value = constant_fold_expr (builder , expr )
142+ if value is not None :
143+ return builder .load_literal_value (value )
144+ return None
145+
146+
147+ def folding_candidate (
148+ transform : Callable [[IRBuilder , Expression ], Value | None ],
149+ ) -> Callable [[IRBuilder , Expression ], Value | None ]:
150+ """Mark a transform function as a candidate for constant folding.
151+
152+ Candidate functions will attempt to short-circuit the transformation
153+ by constant folding the expression and will only proceed to transform
154+ the expression if folding is not possible.
155+ """
156+ def constant_fold_wrap (builder : IRBuilder , expr : Expression ) -> Value | None :
157+ folded = try_constant_fold (builder , expr )
158+ return folded if folded is not None else transform (builder , expr )
159+ return constant_fold_wrap
You can’t perform that action at this time.
0 commit comments