Skip to content

Commit

Permalink
inlining: minor optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
aviatesk committed Dec 19, 2021
1 parent d959b43 commit f8316f3
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions base/compiler/ssair/inlining.jl
Original file line number Diff line number Diff line change
Expand Up @@ -334,27 +334,24 @@ function ir_inline_item!(compact::IncrementalCompact, idx::Int, argexprs::Vector
end
nargs_def = def.nargs::Int32
isva = nargs_def > 0 && def.isva
sig = def.sig
if isva
vararg = mk_tuplecall!(compact, argexprs[nargs_def:end], topline)
argexprs = Any[argexprs[1:(nargs_def - 1)]..., vararg]
argexprs = fix_va_argexprs!(compact, argexprs, nargs_def, topline)
end
if def.is_for_opaque_closure
# Replace the first argument by a load of the capture environment
argexprs[1] = insert_node_here!(compact,
NewInstruction(Expr(:call, GlobalRef(Core, :getfield), argexprs[1], QuoteNode(:captures)),
spec.ir.argtypes[1], topline))
end
flag = compact.result[idx][:flag]
boundscheck_idx = boundscheck
if boundscheck_idx === :default || boundscheck_idx === :propagate
if (flag & IR_FLAG_INBOUNDS) != 0
boundscheck_idx = :off
if boundscheck === :default || boundscheck === :propagate
if (compact.result[idx][:flag] & IR_FLAG_INBOUNDS) != 0
boundscheck = :off
end
end
# If the iterator already moved on to the next basic block,
# temporarily re-open in again.
local return_value
sig = def.sig
# Special case inlining that maintains the current basic block if there's only one BB in the target
if spec.linear_inline_eligible
#compact[idx] = nothing
Expand All @@ -364,7 +361,7 @@ function ir_inline_item!(compact::IncrementalCompact, idx::Int, argexprs::Vector
# face of rename_arguments! mutating in place - should figure out
# something better eventually.
inline_compact[idx′] = nothing
stmt′ = ssa_substitute!(idx′, stmt′, argexprs, sig, sparam_vals, linetable_offset, boundscheck_idx, compact)
stmt′ = ssa_substitute!(idx′, stmt′, argexprs, sig, sparam_vals, linetable_offset, boundscheck, compact)
if isa(stmt′, ReturnNode)
val = stmt′.val
isa(val, SSAValue) && (compact.used_ssas[val.id] += 1)
Expand All @@ -390,7 +387,7 @@ function ir_inline_item!(compact::IncrementalCompact, idx::Int, argexprs::Vector
inline_compact = IncrementalCompact(compact, spec.ir, compact.result_idx)
for ((_, idx′), stmt′) in inline_compact
inline_compact[idx′] = nothing
stmt′ = ssa_substitute!(idx′, stmt′, argexprs, sig, sparam_vals, linetable_offset, boundscheck_idx, compact)
stmt′ = ssa_substitute!(idx′, stmt′, argexprs, sig, sparam_vals, linetable_offset, boundscheck, compact)
if isa(stmt′, ReturnNode)
if isdefined(stmt′, :val)
val = stmt′.val
Expand Down Expand Up @@ -441,6 +438,24 @@ function ir_inline_item!(compact::IncrementalCompact, idx::Int, argexprs::Vector
return_value
end

function fix_va_argexprs!(compact::IncrementalCompact,
argexprs::Vector{Any}, nargs_def::Int32, line_idx::Int32)
newargexprs = Any[]
for i in 1:(nargs_def-1)
push!(newargexprs, argexprs[i])
end
tuple_call = Expr(:call, TOP_TUPLE)
tuple_typs = Any[]
for i in nargs_def:length(argexprs)
arg = argexprs[i]
push!(tuple_call.args, arg)
push!(tuple_typs, argextype(arg, compact))
end
tuple_typ = tuple_tfunc(tuple_typs)
push!(newargexprs, insert_node_here!(compact, NewInstruction(tuple_call, tuple_typ, line_idx)))
return newargexprs
end

const FATAL_TYPE_BOUND_ERROR = ErrorException("fatal error in type inference (type bound)")

function ir_inline_unionsplit!(compact::IncrementalCompact, idx::Int,
Expand Down Expand Up @@ -482,11 +497,12 @@ function ir_inline_unionsplit!(compact::IncrementalCompact, idx::Int,
if !isa(case, ConstantCase)
argexprs′ = copy(argexprs)
for i = 1:length(mparams)
argex = argexprs[i]
(isa(argex, SSAValue) || isa(argex, Argument)) || continue
a, m = aparams[i], mparams[i]
(isa(argexprs[i], SSAValue) || isa(argexprs[i], Argument)) || continue
if !(a <: m)
argexprs′[i] = insert_node_here!(compact,
NewInstruction(PiNode(argexprs′[i], m), m, line))
NewInstruction(PiNode(argex, m), m, line))
end
end
end
Expand Down Expand Up @@ -1336,12 +1352,6 @@ function assemble_inline_todo!(ir::IRCode, state::InliningState)
todo
end

function mk_tuplecall!(compact::IncrementalCompact, args::Vector{Any}, line_idx::Int32)
e = Expr(:call, TOP_TUPLE, args...)
etyp = tuple_tfunc(Any[argextype(args[i], compact) for i in 1:length(args)])
return insert_node_here!(compact, NewInstruction(e, etyp, line_idx))
end

function linear_inline_eligible(ir::IRCode)
length(ir.cfg.blocks) == 1 || return false
terminator = ir[SSAValue(last(ir.cfg.blocks[1].stmts))]
Expand Down

0 comments on commit f8316f3

Please sign in to comment.