@@ -17,6 +17,22 @@ function _extract_rounding_kwargs(ctx::CGCtx, args)
1717 kwargs
1818end
1919
20+ # Constant-fold scalar operations at codegen time.
21+ # Returns a CGVal if all operands are compile-time constants and `op` is
22+ # applicable to them, nothing otherwise.
23+ function try_const_fold (ctx:: CGCtx , op, args)
24+ vals = Any[]
25+ for arg in args
26+ c = get_constant (ctx, arg)
27+ c === nothing && return nothing
28+ val = something (c)
29+ val isa Number || return nothing
30+ push! (vals, val)
31+ end
32+ applicable (op, vals... ) || return nothing
33+ emit_value! (ctx, op (vals... ))
34+ end
35+
2036function emit_binop! (ctx:: CGCtx , args, encoder:: Function ; kwargs... )
2137 cb = ctx. cb
2238 tt = ctx. tt
@@ -103,15 +119,15 @@ end
103119@intrinsic absi (x:: Tile{<:Integer} )
104120tfunc (𝕃, :: typeof (Intrinsics. absi), @nospecialize (x)) = CC. widenconst (x)
105121function emit_intrinsic! (ctx:: CGCtx , :: typeof (Intrinsics. absi), args)
106- emit_unop! (ctx, args, encode_AbsIOp!)
122+ @something try_const_fold (ctx, abs, args) emit_unop! (ctx, args, encode_AbsIOp!)
107123end
108124
109125# cuda_tile.addi
110126@intrinsic addi (x:: T , y:: T ) where {T<: Integer }
111127@intrinsic addi (a:: Tile{T} , b:: Tile{T} ) where {T<: Integer }
112128tfunc (𝕃, :: typeof (Intrinsics. addi), @nospecialize (x), @nospecialize (y)) = CC. widenconst (x)
113129function emit_intrinsic! (ctx:: CGCtx , :: typeof (Intrinsics. addi), args)
114- emit_binop! (ctx, args, encode_AddIOp!)
130+ @something try_const_fold (ctx, + , args) emit_binop! (ctx, args, encode_AddIOp!)
115131end
116132
117133# cuda_tile.cldi (ceiling division, toward positive infinity)
198214@intrinsic muli (a:: Tile{T} , b:: Tile{T} ) where {T<: Integer }
199215tfunc (𝕃, :: typeof (Intrinsics. muli), @nospecialize (x), @nospecialize (y)) = CC. widenconst (x)
200216function emit_intrinsic! (ctx:: CGCtx , :: typeof (Intrinsics. muli), args)
201- emit_binop! (ctx, args, encode_MulIOp!)
217+ @something try_const_fold (ctx, * , args) emit_binop! (ctx, args, encode_MulIOp!)
202218end
203219
204220# cuda_tile.mulhii
214230@intrinsic negi (a:: Tile{<:Integer} )
215231tfunc (𝕃, :: typeof (Intrinsics. negi), @nospecialize (x)) = CC. widenconst (x)
216232function emit_intrinsic! (ctx:: CGCtx , :: typeof (Intrinsics. negi), args)
217- emit_unop! (ctx, args, encode_NegIOp!; overflow= IntegerOverflow. None)
233+ @something try_const_fold (ctx, - , args) emit_unop! (ctx, args, encode_NegIOp!; overflow= IntegerOverflow. None)
218234end
219235
220236# cuda_tile.remi
231247@intrinsic shli (a:: Tile{T} , b:: Tile{T} ) where {T<: Integer }
232248tfunc (𝕃, :: typeof (Intrinsics. shli), @nospecialize (x), @nospecialize (y)) = CC. widenconst (x)
233249function emit_intrinsic! (ctx:: CGCtx , :: typeof (Intrinsics. shli), args)
234- emit_binop! (ctx, args, encode_ShLIOp!)
250+ @something try_const_fold (ctx, << , args) emit_binop! (ctx, args, encode_ShLIOp!)
235251end
236252
237253# cuda_tile.shri
248264@intrinsic subi (a:: Tile{T} , b:: Tile{T} ) where {T<: Integer }
249265tfunc (𝕃, :: typeof (Intrinsics. subi), @nospecialize (x), @nospecialize (y)) = CC. widenconst (x)
250266function emit_intrinsic! (ctx:: CGCtx , :: typeof (Intrinsics. subi), args)
251- emit_binop! (ctx, args, encode_SubIOp!)
267+ @something try_const_fold (ctx, - , args) emit_binop! (ctx, args, encode_SubIOp!)
252268end
253269
254270
@@ -259,15 +275,15 @@ end
259275@intrinsic absf (a:: Tile{<:AbstractFloat} )
260276tfunc (𝕃, :: typeof (Intrinsics. absf), @nospecialize (x)) = CC. widenconst (x)
261277function emit_intrinsic! (ctx:: CGCtx , :: typeof (Intrinsics. absf), args)
262- emit_unop! (ctx, args, encode_AbsFOp!)
278+ @something try_const_fold (ctx, abs, args) emit_unop! (ctx, args, encode_AbsFOp!)
263279end
264280
265281# cuda_tile.addf
266282@intrinsic addf (x:: T , y:: T , rounding_mode= nothing , flush_to_zero= false ) where {T<: AbstractFloat }
267283@intrinsic addf (a:: Tile{T} , b:: Tile{T} , rounding_mode= nothing , flush_to_zero= false ) where {T<: AbstractFloat }
268284tfunc (𝕃, :: typeof (Intrinsics. addf), @nospecialize args... ) = CC. widenconst (args[1 ])
269285function emit_intrinsic! (ctx:: CGCtx , :: typeof (Intrinsics. addf), args)
270- emit_binop! (ctx, args[1 : 2 ], encode_AddFOp!; _extract_rounding_kwargs (ctx, args)... )
286+ @something try_const_fold (ctx, + , args) emit_binop! (ctx, args[1 : 2 ], encode_AddFOp!; _extract_rounding_kwargs (ctx, args)... )
271287end
272288
273289# cuda_tile.cmpf
@@ -308,31 +324,31 @@ end
308324@intrinsic divf (a:: Tile{T} , b:: Tile{T} , rounding_mode= nothing , flush_to_zero= false ) where {T<: AbstractFloat }
309325tfunc (𝕃, :: typeof (Intrinsics. divf), @nospecialize args... ) = CC. widenconst (args[1 ])
310326function emit_intrinsic! (ctx:: CGCtx , :: typeof (Intrinsics. divf), args)
311- emit_binop! (ctx, args[1 : 2 ], encode_DivFOp!; _extract_rounding_kwargs (ctx, args)... )
327+ @something try_const_fold (ctx, / , args) emit_binop! (ctx, args[1 : 2 ], encode_DivFOp!; _extract_rounding_kwargs (ctx, args)... )
312328end
313329
314330# cuda_tile.mulf
315331@intrinsic mulf (x:: T , y:: T , rounding_mode= nothing , flush_to_zero= false ) where {T<: AbstractFloat }
316332@intrinsic mulf (a:: Tile{T} , b:: Tile{T} , rounding_mode= nothing , flush_to_zero= false ) where {T<: AbstractFloat }
317333tfunc (𝕃, :: typeof (Intrinsics. mulf), @nospecialize args... ) = CC. widenconst (args[1 ])
318334function emit_intrinsic! (ctx:: CGCtx , :: typeof (Intrinsics. mulf), args)
319- emit_binop! (ctx, args[1 : 2 ], encode_MulFOp!; _extract_rounding_kwargs (ctx, args)... )
335+ @something try_const_fold (ctx, * , args) emit_binop! (ctx, args[1 : 2 ], encode_MulFOp!; _extract_rounding_kwargs (ctx, args)... )
320336end
321337
322338# cuda_tile.negf
323339@intrinsic negf (x:: T ) where {T<: AbstractFloat }
324340@intrinsic negf (a:: Tile{<:AbstractFloat} )
325341tfunc (𝕃, :: typeof (Intrinsics. negf), @nospecialize (x)) = CC. widenconst (x)
326342function emit_intrinsic! (ctx:: CGCtx , :: typeof (Intrinsics. negf), args)
327- emit_unop! (ctx, args, encode_NegFOp!)
343+ @something try_const_fold (ctx, - , args) emit_unop! (ctx, args, encode_NegFOp!)
328344end
329345
330346# cuda_tile.subf
331347@intrinsic subf (x:: T , y:: T , rounding_mode= nothing , flush_to_zero= false ) where {T<: AbstractFloat }
332348@intrinsic subf (a:: Tile{T} , b:: Tile{T} , rounding_mode= nothing , flush_to_zero= false ) where {T<: AbstractFloat }
333349tfunc (𝕃, :: typeof (Intrinsics. subf), @nospecialize args... ) = CC. widenconst (args[1 ])
334350function emit_intrinsic! (ctx:: CGCtx , :: typeof (Intrinsics. subf), args)
335- emit_binop! (ctx, args[1 : 2 ], encode_SubFOp!; _extract_rounding_kwargs (ctx, args)... )
351+ @something try_const_fold (ctx, - , args) emit_binop! (ctx, args[1 : 2 ], encode_SubFOp!; _extract_rounding_kwargs (ctx, args)... )
336352end
337353
338354
@@ -350,7 +366,7 @@ function tfunc(𝕃, ::typeof(Intrinsics.andi), @nospecialize(x), @nospecialize(
350366 return CC. widenconst (x)
351367end
352368function emit_intrinsic! (ctx:: CGCtx , :: typeof (Intrinsics. andi), args)
353- emit_binop! (ctx, args, encode_AndIOp!)
369+ @something try_const_fold (ctx, & , args) emit_binop! (ctx, args, encode_AndIOp!)
354370end
355371
356372# cuda_tile.ori
@@ -365,13 +381,13 @@ function tfunc(𝕃, ::typeof(Intrinsics.ori), @nospecialize(x), @nospecialize(y
365381 return CC. widenconst (x)
366382end
367383function emit_intrinsic! (ctx:: CGCtx , :: typeof (Intrinsics. ori), args)
368- emit_binop! (ctx, args, encode_OrIOp!)
384+ @something try_const_fold (ctx, | , args) emit_binop! (ctx, args, encode_OrIOp!)
369385end
370386
371387# cuda_tile.xori
372388@intrinsic xori (x:: T , y:: T ) where {T<: Integer }
373389@intrinsic xori (a:: Tile{T} , b:: Tile{T} ) where {T<: Integer }
374390tfunc (𝕃, :: typeof (Intrinsics. xori), @nospecialize (x), @nospecialize (y)) = CC. widenconst (x)
375391function emit_intrinsic! (ctx:: CGCtx , :: typeof (Intrinsics. xori), args)
376- emit_binop! (ctx, args, encode_XOrIOp!)
392+ @something try_const_fold (ctx, xor, args) emit_binop! (ctx, args, encode_XOrIOp!)
377393end
0 commit comments