Skip to content

Commit

Permalink
add new :total_or_throw utility setting for @assume_effects
Browse files Browse the repository at this point in the history
This setting is particularly useful since it allows the compiler to
evaluate a call of the applied method when all the call arguments are
fully known, no matter if the call results in an error or not.
  • Loading branch information
aviatesk committed Mar 28, 2022
1 parent 4751165 commit 192f3bf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
18 changes: 17 additions & 1 deletion base/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,20 @@ and is a convenient shortcut.
distinction that the `:consistent`-cy requirement applies world-age wise rather
than globally as described above. However, in particular, a method annotated
`@Base.pure` is always `:total`.
---
# `:total_or_throw`
This `setting` combines the following other assertions:
- `:consistent`
- `:effect_free`
- `:terminates_globally`
and is a convenient shortcut.
!!! note
This setting is particularly useful since it allows the compiler to evaluate a call of
the applied method when all the call arguments are fully known, no matter if the call
results in an error or not.
"""
macro assume_effects(args...)
(consistent, effect_free, nothrow, terminates_globally, terminates_locally) =
Expand All @@ -537,12 +551,14 @@ macro assume_effects(args...)
terminates_locally = true
elseif setting === :total
consistent = effect_free = nothrow = terminates_globally = true
elseif setting === :total_or_throw
consistent = effect_free = terminates_globally = true
else
throw(ArgumentError("@assume_effects $setting not supported"))
end
end
ex = args[end]
isa(ex, Expr) || throw(ArgumentError("Bad expression `$ex` in @constprop [settings] ex"))
isa(ex, Expr) || throw(ArgumentError("Bad expression `$ex` in `@assume_effects [settings] ex`"))
if ex.head === :macrocall && ex.args[1] == Symbol("@ccall")
ex.args[1] = GlobalRef(Base, Symbol("@ccall_effects"))
insert!(ex.args, 3, Core.Compiler.encode_effects_override(Core.Compiler.EffectsOverride(
Expand Down
2 changes: 1 addition & 1 deletion test/compiler/inline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@ recur_termination22(x) = x * recur_termination21(x-1)
end

const ___CONST_DICT___ = Dict{Any,Any}(:a => 1, :b => 2)
Base.@assume_effects :consistent :effect_free :terminates_globally consteval(
Base.@assume_effects :total_or_throw consteval(
f, args...; kwargs...) = f(args...; kwargs...)
@test fully_eliminated() do
consteval(getindex, ___CONST_DICT___, :a)
Expand Down

0 comments on commit 192f3bf

Please sign in to comment.