Skip to content

Commit

Permalink
fix partially_inline! with isdefined (JuliaLang#40628)
Browse files Browse the repository at this point in the history
  • Loading branch information
simeonschaub authored and johanmon committed Jul 5, 2021
1 parent a051bdb commit 9d05232
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
29 changes: 28 additions & 1 deletion base/meta.jl
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,10 @@ function _partially_inline!(@nospecialize(x), slot_replacements::Vector{Any},
if isa(x, Expr)
head = x.head
if head === :static_parameter
return QuoteNode(static_param_values[x.args[1]])
if isassigned(static_param_values, x.args[1])
return QuoteNode(static_param_values[x.args[1]])
end
return x
elseif head === :cfunction
@assert !isa(type_signature, UnionAll) || !isempty(spvals)
if !isa(x.args[2], QuoteNode) # very common no-op
Expand Down Expand Up @@ -413,6 +416,30 @@ function _partially_inline!(@nospecialize(x), slot_replacements::Vector{Any},
x.args[2] += statement_offset
elseif head === :enter
x.args[1] += statement_offset
elseif head === :isdefined
arg = x.args[1]
# inlining a QuoteNode or literal into `Expr(:isdefined, x)` is invalid, replace with true
if isa(arg, Core.SlotNumber)
id = arg.id
if 1 <= id <= length(slot_replacements)
replacement = slot_replacements[id]
if isa(replacement, Union{Core.SlotNumber, GlobalRef, Symbol})
return Expr(:isdefined, replacement)
else
@assert !isa(replacement, Expr)
return true
end
end
return Expr(:isdefined, Core.SlotNumber(id + slot_offset))
elseif isexpr(arg, :static_parameter)
if isassigned(static_param_values, arg.args[1])
return true
end
return x
else
@assert isa(arg, Union{GlobalRef, Symbol})
return x
end
elseif !is_meta_expr_head(head)
partially_inline!(x.args, slot_replacements, type_signature, static_param_values,
slot_offset, statement_offset, boundscheck)
Expand Down
30 changes: 26 additions & 4 deletions test/meta.jl
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,29 @@ ci = code_lowered(f, Tuple{Int})[1]

g(::Val{x}) where {x} = x ? 1 : 0
ci = code_lowered(g, Tuple{Val{true}})[1]
@test Meta.partially_inline!(ci.code, [], Tuple{typeof(g),Val{true}}, Any[Val{true}], 0, 0, :propagate)[1] ==
Core.GotoIfNot(QuoteNode(Val{true}), 3)
@test Meta.partially_inline!(ci.code, [], Tuple{typeof(g),Val{true}}, Any[Val{true}], 0, 2, :propagate)[1] ==
Core.GotoIfNot(QuoteNode(Val{true}), 5)
@test Meta.partially_inline!(ci.code, [], Tuple{typeof(g),Val{true}}, Any[true], 0, 0, :propagate)[1] ==
Core.GotoIfNot(QuoteNode(true), 3)
@test Meta.partially_inline!(ci.code, [], Tuple{typeof(g),Val{true}}, Any[true], 0, 2, :propagate)[1] ==
Core.GotoIfNot(QuoteNode(true), 5)

@testset "inlining with isdefined" begin
isdefined_slot(x) = @isdefined(x)
ci = code_lowered(isdefined_slot, Tuple{Int})[1]
@test Meta.partially_inline!(copy(ci.code), [], Tuple{typeof(isdefined_slot), Int},
[], 0, 0, :propagate)[1] == Expr(:isdefined, Core.SlotNumber(2))
@test Meta.partially_inline!(copy(ci.code), [isdefined_slot, 1], Tuple{typeof(isdefined_slot), Int},
[], 0, 0, :propagate)[1] == true

isdefined_sparam(::T) where {T} = @isdefined(T)
ci = code_lowered(isdefined_sparam, Tuple{Int})[1]
@test Meta.partially_inline!(copy(ci.code), [], Tuple{typeof(isdefined_sparam), Int},
Any[Int], 0, 0, :propagate)[1] == true
@test Meta.partially_inline!(copy(ci.code), [], Tuple{typeof(isdefined_sparam), Int},
[], 0, 0, :propagate)[1] == Expr(:isdefined, Expr(:static_parameter, 1))

@eval isdefined_globalref(x) = $(Expr(:isdefined, GlobalRef(Base, :foo)))
ci = code_lowered(isdefined_globalref, Tuple{Int})[1]
@test Meta.partially_inline!(copy(ci.code), Any[isdefined_globalref, 1], Tuple{typeof(isdefined_globalref), Int},
[], 0, 0, :propagate)[1] == Expr(:isdefined, GlobalRef(Base, :foo))

end

0 comments on commit 9d05232

Please sign in to comment.