From a0a68a54d6fca33c5717a431882f99ccae3041ef Mon Sep 17 00:00:00 2001 From: Yichao Yu Date: Mon, 14 Sep 2020 16:06:09 -0400 Subject: [PATCH] Tighter array eltype for `renumber_ssa` (#37499) Unlike `renumber_ssa2` in `compiler/ir.jl` which actually takes an array with elements of many different types, all callers of `renumber_ssa` (and `renumber_ssa!`) only ever assign `SSAValue` to the array. Also no one ever checks `isassigned` on this array so a `Vector{SSAValue}` should work just fine here. Try to maintain a similar level of error checking by replacing `#undef` with `SSAValue(-1)` (already used by `construct_ssa!`) and adding an assertion in `renumber_ssa` to check for cases that throws `UndefVarError` previously. Also removed an unused parameter. --- base/compiler/ssair/slot2ssa.jl | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/base/compiler/ssair/slot2ssa.jl b/base/compiler/ssair/slot2ssa.jl index 5c2b6106770b3..057bb72ff1152 100644 --- a/base/compiler/ssair/slot2ssa.jl +++ b/base/compiler/ssair/slot2ssa.jl @@ -57,21 +57,19 @@ function scan_slot_def_use(nargs::Int, ci::CodeInfo, code::Vector{Any}) result end -function renumber_ssa(stmt::SSAValue, ssanums::Vector{Any}, new_ssa::Bool=false, used_ssa::Union{Nothing, Vector{Int}}=nothing) +function renumber_ssa(stmt::SSAValue, ssanums::Vector{SSAValue}, new_ssa::Bool=false) id = stmt.id if id > length(ssanums) return stmt end val = ssanums[id] - if isa(val, SSAValue) && used_ssa !== nothing - used_ssa[val.id] += 1 - end + @assert val.id > 0 return val end -function renumber_ssa!(@nospecialize(stmt), ssanums::Vector{Any}, new_ssa::Bool=false, used_ssa::Union{Nothing, Vector{Int}}=nothing) - isa(stmt, SSAValue) && return renumber_ssa(stmt, ssanums, new_ssa, used_ssa) - return ssamap(val->renumber_ssa(val, ssanums, new_ssa, used_ssa), stmt) +function renumber_ssa!(@nospecialize(stmt), ssanums::Vector{SSAValue}, new_ssa::Bool=false) + isa(stmt, SSAValue) && return renumber_ssa(stmt, ssanums, new_ssa) + return ssamap(val->renumber_ssa(val, ssanums, new_ssa), stmt) end function make_ssa!(ci::CodeInfo, code::Vector{Any}, idx, slot, @nospecialize(typ)) @@ -428,8 +426,11 @@ function domsort_ssa!(ir::IRCode, domtree::DomTree) end end result = InstructionStream(nstmts + ncritbreaks + nnewfallthroughs) - inst_rename = Vector{Any}(undef, length(ir.stmts) + length(ir.new_nodes)) - for i = 1:length(ir.new_nodes) + inst_rename = Vector{SSAValue}(undef, length(ir.stmts) + length(ir.new_nodes)) + @inbounds for i = 1:length(ir.stmts) + inst_rename[i] = SSAValue(-1) + end + @inbounds for i = 1:length(ir.new_nodes) inst_rename[i + length(ir.stmts)] = SSAValue(i + length(result)) end bb_start_off = 0 @@ -802,7 +803,7 @@ function construct_ssa!(ci::CodeInfo, ir::IRCode, domtree::DomTree, defuse, narg # Convert into IRCode form nstmts = length(ir.stmts) new_code = Vector{Any}(undef, nstmts) - ssavalmap = Any[SSAValue(-1) for _ in 0:length(ci.ssavaluetypes)] + ssavalmap = fill(SSAValue(-1), length(ci.ssavaluetypes) + 1) result_types = Any[Any for _ in 1:nstmts] # Detect statement positions for assignments and construct array for (bb, idx) in bbidxiter(ir)