Skip to content

Commit

Permalink
Slightly generalize _compute_sparam elision (#48144)
Browse files Browse the repository at this point in the history
To catch a case that occurs in FuncPipelines.jl and was
causing precision issues in #48066.
  • Loading branch information
Keno authored Jan 6, 2023
1 parent fd41b59 commit 1508425
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
25 changes: 18 additions & 7 deletions base/compiler/ssair/passes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,6 @@ end
end && return nothing

arg = sig.parameters[i]
isa(arg, DataType) || return nothing

rarg = def.args[2 + i]
isa(rarg, SSAValue) || return nothing
Expand All @@ -805,6 +804,10 @@ end
rarg = argdef.args[1]
isa(rarg, SSAValue) || return nothing
argdef = compact[rarg][:inst]
else
isa(arg, DataType) || return nothing
isType(arg) || return nothing
arg = arg.parameters[1]
end

is_known_call(argdef, Core.apply_type, compact) || return nothing
Expand All @@ -815,15 +818,23 @@ end
applyT = applyT.val

isa(applyT, UnionAll) || return nothing
# N.B.: At the moment we only lift the valI == 1 case, so we
# only need to look at the outermost tvar.
applyTvar = applyT.var
applyTbody = applyT.body

isa(applyTbody, DataType) || return nothing
applyTbody.name == arg.name || return nothing
length(applyTbody.parameters) == length(arg.parameters) == 1 || return nothing
applyTbody.parameters[1] === applyTvar || return nothing
arg.parameters[1] === tvar || return nothing
return LiftedValue(argdef.args[3])
arg = unwrap_unionall(arg)
applyTbody = unwrap_unionall(applyTbody)

(isa(arg, DataType) && isa(applyTbody, DataType)) || return nothing
applyTbody.name === arg.name || return nothing
length(applyTbody.parameters) == length(arg.parameters) || return nothing
for i = 1:length(applyTbody.parameters)
if applyTbody.parameters[i] === applyTvar && arg.parameters[i] === tvar
return LiftedValue(argdef.args[3])
end
end
return nothing
end

# NOTE we use `IdSet{Int}` instead of `BitSet` for in these passes since they work on IR after inlining,
Expand Down
8 changes: 8 additions & 0 deletions test/compiler/irpasses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1221,3 +1221,11 @@ function a47180(b; stdout )
c
end
@test isa(a47180(``; stdout), Base.AbstractCmd)

# Test that _compute_sparams can be eliminated for NamedTuple
named_tuple_elim(name::Symbol, result) = NamedTuple{(name,)}(result)
let src = code_typed1(named_tuple_elim, Tuple{Symbol, Tuple})
@test count(iscall((src, Core._compute_sparams)), src.code) == 0 &&
count(iscall((src, Core._svec_ref)), src.code) == 0 &&
count(iscall(x->!isa(argextype(x, src).val, Core.Builtin)), src.code) == 0
end

0 comments on commit 1508425

Please sign in to comment.