Stream: helpdesk (published)

Topic: ✔ Avoid specialization in generated function


view this post on Zulip DrChainsaw (Jul 27 2023 at 00:38):

Not sure if this shows what I think it does, but is is possible to only generate the following function for unique values of `N?

let
 cnt= Ref(0)
 @generated function nexprtest(x::Tuple{Vararg{Any, N}}, @nospecialize(m)) where N #@nospecialize does not seem to help here
   xis = ntuple( i -> Symbol(:x_, i), Val(N))
   cnt[] += 1
   quote
    @nexprs $N j->((x_j, m) = (2 * x[j], m+m))
    m, tuple($(xis...))
   end
 end
@show nexprtest((3.0, 4), 4) # Generate the first time for N = 2 :)
@show cnt[]
@show nexprtest((4.0, 3), 2) # Will not generate :)
@show cnt[]
@show nexprtest((3.0, 4), 4.0) # Does generate since last arg has different type :(
@show cnt[]
@show nexprtest((3, 4), 4) # Does generate since first arg has different type :(
@show cnt[]
end

# Output:
nexprtest((3.0, 4), 4) = (16, (6.0, 8))
cnt[] = 1
nexprtest((4.0, 3), 2) = (8, (8.0, 6))
cnt[] = 1
nexprtest((3.0, 4), 4.0) = (16.0, (6.0, 8))
cnt[] = 2
nexprtest((3, 4), 4) = (16, (6, 8))
cnt[] = 3
3

view this post on Zulip Mason Protter (Jul 27 2023 at 02:34):

Yeah, generated functions always generate for the specific args

view this post on Zulip Notification Bot (Jul 27 2023 at 08:48):

DrChainsaw has marked this topic as resolved.


Last updated: Nov 22 2024 at 04:41 UTC