Skip to content

Commit

Permalink
effects: fix :nothrow modeling of getglobal (#50765)
Browse files Browse the repository at this point in the history
  • Loading branch information
aviatesk committed Aug 4, 2023
1 parent 21fe723 commit 821f6a5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
21 changes: 10 additions & 11 deletions base/compiler/tfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2347,19 +2347,18 @@ function getfield_effects(𝕃::AbstractLattice, arginfo::ArgInfo, @nospecialize
end

function getglobal_effects(argtypes::Vector{Any}, @nospecialize(rt))
2 length(argtypes) 3 || return EFFECTS_THROWS
consistent = inaccessiblememonly = ALWAYS_FALSE
nothrow = false
if length(argtypes) 2
M, s = argtypes[1], argtypes[2]
if getglobal_nothrow(M, s)
nothrow = true
# typeasserts below are already checked in `getglobal_nothrow`
Mval, sval = (M::Const).val::Module, (s::Const).val::Symbol
if isconst(Mval, sval)
consistent = ALWAYS_TRUE
if is_mutation_free_argtype(rt)
inaccessiblememonly = ALWAYS_TRUE
end
M, s = argtypes[1], argtypes[2]
if (length(argtypes) == 3 ? getglobal_nothrow(M, s, argtypes[3]) : getglobal_nothrow(M, s))
nothrow = true
# typeasserts below are already checked in `getglobal_nothrow`
Mval, sval = (M::Const).val::Module, (s::Const).val::Symbol
if isconst(Mval, sval)
consistent = ALWAYS_TRUE
if is_mutation_free_argtype(rt)
inaccessiblememonly = ALWAYS_TRUE
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions test/compiler/effects.jl
Original file line number Diff line number Diff line change
Expand Up @@ -998,3 +998,15 @@ f50198() = (hf50198(Ref(:x)[]); nothing)
f50311(x, s) = Symbol(s)
g50311(x) = Val{f50311((1.0, x), "foo")}()
@test fully_eliminated(g50311, Tuple{Float64})

# getglobal effects
const my_defined_var = 42
@test Base.infer_effects() do
getglobal(@__MODULE__, :my_defined_var, :monotonic)
end |> Core.Compiler.is_foldable_nothrow
@test Base.infer_effects() do
getglobal(@__MODULE__, :my_defined_var, :foo)
end |> !Core.Compiler.is_nothrow
@test Base.infer_effects() do
getglobal(@__MODULE__, :my_defined_var, :foo, nothing)
end |> !Core.Compiler.is_nothrow

0 comments on commit 821f6a5

Please sign in to comment.