Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle sparams before grouping varargs #56

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/trace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,7 @@ end
rewrite_special_cases(st) = st

function get_static_params(t::Tracer, v_fargs::VecOrTuple)
fvals = [v isa V ? t.tape[v].val : v for v in v_fargs]
fn, vals... = fvals
mi = Base.method_instances(fn, map(Core.Typeof, vals), Base.get_world_counter())[1]
mi = Base.method_instances(code_signature(t.tape.c, v_fargs)..., Base.get_world_counter())[1]
mi_dict = Dict(zip(sparam_names(mi), mi.sparam_vals))
return mi.sparam_vals, mi_dict
end
Expand Down Expand Up @@ -574,10 +572,10 @@ function trace!(t::Tracer, v_fargs)
# note: we need to extract IR before vararg grouping, which may change
# v_fargs, thus invalidating method search
ir = getcode(code_signature(t.tape.c, v_fargs)...)
sparams, sparams_dict = get_static_params(t, v_fargs)
v_fargs = group_varargs!(t, v_fargs)
frame = Frame(t.tape, ir, v_fargs...)
push!(t.stack, frame)
sparams, sparams_dict = get_static_params(t, v_fargs)
bi = 1
prev_bi = 0
cf = nothing
Expand Down
8 changes: 8 additions & 0 deletions test/test_trace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,9 @@ multiarg_fn(x) = only(x)
multiarg_fn(x, y) = only(x) + only(y)
multiarg_fn(x, y, z) = only(x) + only(y) + only(z)

function vararg_fn_sparam(x, xs::Vararg{T,N}) where {T,N}
T(N)
end

@testset "trace: varargs, splatting" begin

Expand Down Expand Up @@ -307,6 +310,11 @@ multiarg_fn(x, y, z) = only(x) + only(y) + only(z)
_, tape = trace(vararg_wrapper, (1, 2, 3))
@test play!(tape, vararg_wrapper, (4, 5, 6)) == vararg_wrapper((4, 5, 6))

res, tape = trace(vararg_fn_sparam, 1, 1.0, 1.0)
@test res === 2.0
@test play!(tape, vararg_fn_sparam, 1, 1.0, 1.0) == vararg_fn_sparam(1, 1.0, 1.0)
@test compile(tape)(vararg_fn_sparam, 1, 1.0, 1.0) == vararg_fn_sparam(1, 1.0, 1.0)

# tuple/vararg unpacking
f = t -> multiarg_fn(t...)
_, tape = trace(f, (1, 2))
Expand Down
Loading