-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
post-opt: use augmented post-domtree for
visit_conditional_successors
This commit fixes the first problem that was found while digging into #53613. It turns out that the post-domtree constructed from regular `IRCode` doesn't work for visiting conditional successors for post-opt analysis in cases like: ```julia julia> let code = Any[ # block 1 GotoIfNot(Argument(2), 3), # block 2 ReturnNode(Argument(3)), # block 3 (we should visit this block) Expr(:call, throw, "potential throw"), ReturnNode(), # unreachable ] ir = make_ircode(code; slottypes=Any[Any,Bool,Bool]) visited = BitSet() @test !Core.Compiler.visit_conditional_successors(CC.LazyPostDomtree(ir), ir, #=bb=#1) do succ::Int push!(visited, succ) return false end @test 2 β visited @test 3 β visited end Test Failed at REPL[14]:16 Expression: 2 β visited Evaluated: 2 β BitSet([2]) ``` This might mean that we need to fix on the `postdominates` end, but for now, this commit tries to get around it by using the augmented post domtree in `visit_conditional_successors`, while also enforcing the augmented control flow graph (`construct_augmented_cfg`) to have a single exit node really. Since the augmented post domtree is now enforced to have a single return, we can keep using the current `postdominates` to fix the issue. However, this commit isn't enough to fix the NeuralNetworkReachability segfault as reported in #53613, and we need to tackle the second issue reported there too (#53613 (comment)).
- Loading branch information
Showing
4 changed files
with
316 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
include("test/compiler/newinterp.jl") | ||
|
||
@newinterp FiniteIterateInterpreter | ||
|
||
using Core.Compiler: | ||
AbstractLattice, BaseInferenceLattice, IPOResultLattice, InferenceLattice, | ||
ConditionalsLattice, InterConditionalsLattice, PartialsLattice, ConstsLattice, | ||
SimpleInferenceLattice, | ||
widenlattice, is_valid_lattice_norec, typeinf_lattice, ipo_lattice, optimizer_lattice, | ||
widenconst, tmeet, tmerge, β, β, abstract_eval_special_value, widenreturn | ||
|
||
const CC = Core.Compiler | ||
|
||
struct FiniteIterateLattice{L<:AbstractLattice} <: AbstractLattice | ||
parent::L | ||
end | ||
CC.widenlattice(π::FiniteIterateLattice) = π.parent | ||
CC.is_valid_lattice_norec(::FiniteIterateLattice, @nospecialize(elm)) = _is_finite_lattice(elm) | ||
_is_finite_lattice(@nospecialize t) = ( | ||
isa(t, FiniteIterate) || isa(t, FiniteState) || isa(t, TerminatingCondition)) | ||
|
||
CC.typeinf_lattice(::FiniteIterateInterpreter) = | ||
InferenceLattice(ConditionalsLattice(PartialsLattice(FiniteIterateLattice(ConstsLattice())))) | ||
CC.ipo_lattice(::FiniteIterateInterpreter) = | ||
InferenceLattice(InterConditionalsLattice(PartialsLattice(ConstsLattice()))) | ||
CC.optimizer_lattice(::FiniteIterateInterpreter) = | ||
FiniteIterateLattice(SimpleInferenceLattice.instance) | ||
|
||
struct FiniteIterate | ||
typ | ||
itr | ||
function FiniteIterate(@nospecialize(typ), @nospecialize(itr)) | ||
@assert !_is_finite_lattice(typ) "nested FiniteLattice" | ||
return new(typ, itr) | ||
end | ||
end | ||
struct FiniteState | ||
typ | ||
itr | ||
function FiniteState(@nospecialize(typ), @nospecialize(itr)) | ||
@assert !_is_finite_lattice(typ) "nested FiniteLattice" | ||
return new(typ, itr) | ||
end | ||
end | ||
struct TerminatingCondition end | ||
function CC.tmeet(π::FiniteIterateLattice, @nospecialize(v), @nospecialize(t::Type)) | ||
if isa(v, FiniteIterate) | ||
error("tmeet FiniteIterate") | ||
v = v.typ | ||
elseif isa(v, FiniteState) | ||
error("tmeet FiniteState") | ||
v = v.typ | ||
elseif isa(v, TerminatingCondition) | ||
error("tmeet TerminatingCondition") | ||
if t === Bool | ||
return TerminatingCondition() | ||
end | ||
return Bool | ||
end | ||
return tmeet(widenlattice(π), v, t) | ||
end | ||
function CC.tmerge(π::FiniteIterateLattice, @nospecialize(x), @nospecialize(y)) | ||
if isa(x, FiniteIterate) | ||
if isa(y, FiniteIterate) && x.itr === y.itr | ||
return FiniteIterate(tmerge(widenlattice(π), x.typ, y.typ), x.itr) | ||
end | ||
x = x.typ | ||
elseif isa(y, FiniteIterate) | ||
y = y.typ | ||
end | ||
if isa(x, FiniteState) | ||
if isa(y, FiniteState) && x.itr === y.itr | ||
return FiniteState(tmerge(widenlattice(π), x.typ, y.typ), x.itr) | ||
end | ||
x = x.typ | ||
elseif isa(y, FiniteState) | ||
y = y.typ | ||
end | ||
if isa(x, TerminatingCondition) | ||
if isa(y, TerminatingCondition) | ||
return TerminatingCondition() | ||
end | ||
x = Bool | ||
elseif isa(y, TerminatingCondition) | ||
y = Bool | ||
end | ||
return tmerge(widenlattice(π), x, y) | ||
end | ||
function CC.:β(π::FiniteIterateLattice, @nospecialize(x), @nospecialize(y)) | ||
if isa(x, FiniteIterate) | ||
if isa(y, FiniteIterate) | ||
if x.itr === y.itr | ||
return β(widenlattice(π), x.typ, y.typ) | ||
end | ||
return false | ||
elseif isa(y, FiniteState) | ||
return false | ||
elseif isa(y, TerminatingCondition) | ||
return false | ||
end | ||
x = x.typ | ||
elseif isa(y, FiniteIterate) | ||
return x === Union{} | ||
end | ||
if isa(x, FiniteState) | ||
if isa(y, FiniteState) | ||
if x.itr === y.itr | ||
return β(widenlattice(π), x.typ, y.typ) | ||
end | ||
return false | ||
elseif isa(y, TerminatingCondition) | ||
return false | ||
end | ||
x = x.typ | ||
elseif isa(y, FiniteState) | ||
return x === Union{} | ||
end | ||
if isa(x, TerminatingCondition) | ||
return x !== Union{} | ||
elseif isa(y, TerminatingCondition) | ||
return x === Union{} | ||
end | ||
return β(widenlattice(π), x, y) | ||
end | ||
CC.widenconst(fi::FiniteIterate) = widenconst(fi.typ) | ||
CC.widenconst(fs::FiniteState) = widenconst(fs.typ) | ||
CC.widenconst(::TerminatingCondition) = Bool | ||
function widenfiniteiterate(@nospecialize x) | ||
if isa(x, FiniteIterate) | ||
return x.typ | ||
elseif isa(x, FiniteState) | ||
return x.typ | ||
elseif isa(x, TerminatingCondition) | ||
return Bool | ||
end | ||
return x | ||
end | ||
CC.widenreturn(π::FiniteIterateLattice, @nospecialize(rt), info::CC.BestguessInfo) = | ||
CC.widenreturn(widenlattice(π), widenfiniteiterate(rt), info) | ||
|
||
function CC.abstract_call_known(interp::FiniteIterateInterpreter, @nospecialize(f), | ||
arginfo::CC.ArgInfo, si::CC.StmtInfo, sv::CC.AbsIntState, | ||
max_methods::Int) | ||
res = @invoke CC.abstract_call_known(interp::CC.AbstractInterpreter, f::Any, | ||
arginfo::CC.ArgInfo, si::CC.StmtInfo, sv::CC.AbsIntState, | ||
max_methods::Int) | ||
(; fargs, argtypes) = arginfo | ||
la = length(argtypes) | ||
if la β₯ 2 && fargs !== nothing && CC.istopfunction(f, :iterate) | ||
if res.rt !== Union{} | ||
if argtypes[2] β Tuple | ||
if la == 2 | ||
res = CC.CallMeta(FiniteIterate(res.rt, fargs[2]), res.exct, res.effects, res.info) | ||
elseif la == 3 | ||
a3 = argtypes[3] | ||
if a3 isa FiniteState && a3.itr === fargs[2] | ||
res = CC.CallMeta(FiniteIterate(res.rt, a3.itr), res.exct, res.effects, res.info) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
if la == 3 && CC.istopfunction(f, :(===)) | ||
if CC.widenconditional(res.rt) === Bool && argtypes[2] isa FiniteIterate | ||
return CC.CallMeta(TerminatingCondition(), res.exct, res.effects, res.info) | ||
end | ||
end | ||
return res | ||
end | ||
CC.@nospecs function CC._getfield_tfunc(π::FiniteIterateLattice, s00, name, setfield::Bool) | ||
if isa(s00, FiniteIterate) | ||
if name isa Core.Const && name.val === 2 | ||
rt = CC._getfield_tfunc(widenlattice(π), s00.typ, name, setfield) | ||
if rt !== Union{} | ||
return FiniteState(rt, s00.itr) | ||
end | ||
end | ||
s00 = s00.typ | ||
elseif isa(s00, FiniteState) | ||
s00 = s00.typ | ||
elseif isa(s00, TerminatingCondition) | ||
return Union{} | ||
end | ||
name = widenfiniteiterate(name) | ||
return CC._getfield_tfunc(widenlattice(π), s00, name, setfield) | ||
end | ||
CC.@nospecs function CC.not_int_tfunc(π::FiniteIterateLattice, x) | ||
if isa(x, TerminatingCondition) | ||
return TerminatingCondition() | ||
end | ||
return CC.not_int_tfunc(widenlattice(π), x) | ||
end | ||
function CC.handle_control_backedge!(interp::FiniteIterateInterpreter, frame::CC.InferenceState, | ||
from::Int, to::Int, @nospecialize(condt)) | ||
if condt === TerminatingCondition() | ||
return nothing | ||
end | ||
@invoke CC.handle_control_backedge!(interp::CC.AbstractInterpreter, frame::CC.InferenceState, | ||
from::Int, to::Int, condt::Any) | ||
end | ||
|
||
using Test | ||
@test Base.infer_effects(; interp=FiniteIterateInterpreter()) do | ||
for i = (1,2,3) | ||
end | ||
end |> CC.is_terminates |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.