Skip to content

Commit

Permalink
effects: make :terminates_globally functional on recursive functions
Browse files Browse the repository at this point in the history
As with loop, it's hard to prove termination of recursive call automatically.
But with this commit we can manually specify termination at least.
```julia
Base.@assume_effects :terminates_globally function recur_termination1(x)
    x == 1 && return 1
    1 < x < 20 || throw("bad")
    return x * recur_termination1(x-1)
end
@test fully_eliminated() do
    recur_termination1(12)
end

Base.@assume_effects :terminates_globally function recur_termination2(x)
    x == 1 && return 1
    1 < x < 20 || throw("bad")
    return _recur_termination2(x)
end
_recur_termination2(x) = x * recur_termination2(x-1)
@test fully_eliminated() do
    recur_termination2(12)
end
```
  • Loading branch information
aviatesk committed Feb 16, 2022
1 parent 3e5cd3c commit 2d8290e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
10 changes: 7 additions & 3 deletions base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -596,9 +596,13 @@ function abstract_call_method(interp::AbstractInterpreter, method::Method, @nosp
end
if edgecycle
# Some sort of recursion was detected. Even if we did not limit types,
# we cannot guarantee that the call will terminate.
edge_effects = tristate_merge(edge_effects,
Effects(EFFECTS_TOTAL, terminates=TRISTATE_UNKNOWN))
# we cannot guarantee that the call will terminate, unless specified explicitly
if decode_effects_override(method.purity).terminates_globally
terminates_override = ALWAYS_TRUE
else
terminates_override = TRISTATE_UNKNOWN
end
edge_effects = Effects(edge_effects, terminates = terminates_override)
end
return MethodCallResult(rt, edgecycle, edgelimited, edge, edge_effects)
end
Expand Down
18 changes: 18 additions & 0 deletions test/compiler/inline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1069,3 +1069,21 @@ end
@test fully_eliminated() do
issue41694(2)
end

Base.@assume_effects :terminates_globally function recur_termination1(x)
x == 1 && return 1
1 < x < 20 || throw("bad")
return x * recur_termination1(x-1)
end
@test fully_eliminated() do
recur_termination1(12)
end
Base.@assume_effects :terminates_globally function recur_termination2(x)
x == 1 && return 1
1 < x < 20 || throw("bad")
return _recur_termination2(x)
end
_recur_termination2(x) = x * recur_termination2(x-1)
@test fully_eliminated() do
recur_termination2(12)
end

0 comments on commit 2d8290e

Please sign in to comment.