Skip to content

Commit

Permalink
optimizer: fix #42258, make sure to set ssaflags correctly
Browse files Browse the repository at this point in the history
Essentially, this PR adds missing `ssaflags` deletion of `type_annotate!`.
Built on top of #42260.
  • Loading branch information
aviatesk committed Sep 15, 2021
1 parent 86af793 commit 1b80caf
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion base/compiler/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ mutable struct OptimizationState
if nssavalues isa Int
src.ssavaluetypes = Any[ Any for i = 1:nssavalues ]
else
nssavalues = length(src.ssavaluetypes)
nssavalues = length(src.ssavaluetypes::Vector{Any})
end
nslots = length(src.slotflags)
slottypes = src.slottypes
Expand Down
6 changes: 4 additions & 2 deletions base/compiler/ssair/legacy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ function inflate_ir(ci::CodeInfo, sptypes::Vector{Any}, argtypes::Vector{Any})
code[i] = stmt
end
end
ssavaluetypes = ci.ssavaluetypes
nstmts = length(code)
ssavaluetypes = ci.ssavaluetypes isa Vector{Any} ? copy(ci.ssavaluetypes) : Any[ Any for i = 1:(ci.ssavaluetypes::Int) ]
ssavaluetypes = let
ssavaluetypes = ci.ssavaluetypes
ssavaluetypes isa Vector{Any} ? copy(ssavaluetypes) : Any[ Any for i = 1:(ssavaluetypes::Int) ]
end
stmts = InstructionStream(code, ssavaluetypes, Any[nothing for i = 1:nstmts], copy(ci.codelocs), copy(ci.ssaflags))
ir = IRCode(stmts, cfg, collect(LineInfoNode, ci.linetable), argtypes, Any[], sptypes)
return ir
Expand Down
9 changes: 5 additions & 4 deletions base/compiler/ssair/slot2ssa.jl
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,14 @@ function strip_trailing_junk!(ci::CodeInfo, code::Vector{Any}, info::Vector{Any}
# Remove `nothing`s at the end, we don't handle them well
# (we expect the last instruction to be a terminator)
ssavaluetypes = ci.ssavaluetypes::Vector{Any}
(; codelocs, ssaflags) = ci
for i = length(code):-1:1
if code[i] !== nothing
resize!(code, i)
resize!(ssavaluetypes, i)
resize!(ci.codelocs, i)
resize!(codelocs, i)
resize!(info, i)
resize!(ci.ssaflags, i)
resize!(ssaflags, i)
break
end
end
Expand All @@ -193,9 +194,9 @@ function strip_trailing_junk!(ci::CodeInfo, code::Vector{Any}, info::Vector{Any}
if !isa(term, GotoIfNot) && !isa(term, GotoNode) && !isa(term, ReturnNode)
push!(code, ReturnNode())
push!(ssavaluetypes, Union{})
push!(ci.codelocs, 0)
push!(codelocs, 0)
push!(info, nothing)
push!(ci.ssaflags, IR_FLAG_NULL)
push!(ssaflags, IR_FLAG_NULL)
end
nothing
end
Expand Down
1 change: 1 addition & 0 deletions base/compiler/typeinfer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,7 @@ function type_annotate!(sv::InferenceState, run_optimizer::Bool)
deleteat!(ssavaluetypes, i)
deleteat!(src.codelocs, i)
deleteat!(sv.stmt_info, i)
deleteat!(src.ssaflags, i)
nexpr -= 1
changemap[oldidx] = -1
continue
Expand Down
17 changes: 17 additions & 0 deletions test/compiler/ssair.jl
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,20 @@ end
# Issue #41975 - SSA conversion drops type check
f_if_typecheck() = (if nothing; end; unsafe_load(Ptr{Int}(0)))
@test_throws TypeError f_if_typecheck()

let # https://github.com/JuliaLang/julia/issues/42258
# XXX this test case is not so robust against the future changes within `Core.Compiler`
ci, = only(code_typed(Core.Compiler.setindex!, (Core.Compiler.UseRef,Core.Compiler.NewSSAValue); optimize=true))
idx = filter(1:length(ci.code)) do i
stmt = ci.code[i]
if Meta.isexpr(stmt, :call)
t = Core.Compiler.argextype(stmt.args[1], ci, Any[])
ft = Core.Compiler.widenconst(t)
return ft === typeof(BoundsError) || ft === typeof(throw)
end
return false
end
@test all(ci.ssaflags[idx]) do flag
flag & Core.Compiler.IR_FLAG_THROW_BLOCK 0
end
end

0 comments on commit 1b80caf

Please sign in to comment.