Skip to content

Commit

Permalink
optimize more tuple splatting independent of length when possible (#3…
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson authored May 19, 2020
1 parent cebd4fa commit 9d70d45
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
26 changes: 20 additions & 6 deletions base/compiler/ssair/inlining.jl
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,26 @@ function inline_apply!(ir::IRCode, idx::Int, sig::Signature, params::Optimizatio
if arg_start > length(atypes)
return nothing
end
ft = atypes[arg_start]
if ft isa Const && ft.val === Core.tuple
# if one argument is a tuple already, and the rest are empty, we can just return it
# e.g. rewrite `((t::Tuple)...,)` to `t`
nonempty_idx = 0
for i = (arg_start + 1):length(atypes)
ti = atypes[i]
ti Tuple{} && continue
if ti Tuple && nonempty_idx == 0
nonempty_idx = i
continue
end
nonempty_idx = 0
break
end
if nonempty_idx != 0
ir.stmts[idx] = stmt.args[nonempty_idx]
return nothing
end
end
# Try to figure out the signature of the function being called
# and if rewrite_apply_exprargs can deal with this form
for i = (arg_start + 1):length(atypes)
Expand All @@ -905,12 +925,6 @@ function inline_apply!(ir::IRCode, idx::Int, sig::Signature, params::Optimizatio
end
# Independent of whether we can inline, the above analysis allows us to rewrite
# this apply call to a regular call
ft = atypes[arg_start]
if length(atypes) == arg_start+1 && ft isa Const && ft.val === Core.tuple && atypes[arg_start+1] Tuple
# rewrite `((t::Tuple)...,)` to `t`
ir.stmts[idx] = stmt.args[arg_start+1]
return nothing
end
stmt.args, atypes = rewrite_apply_exprargs!(ir, idx, stmt.args, atypes, arg_start)
has_free_typevars(ft) && return nothing
f = singleton_type(ft)
Expand Down
5 changes: 5 additions & 0 deletions test/compiler/inline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ end
f_identity_splat(t) = (t...,)
@test length(code_typed(f_identity_splat, (Tuple{Int,Int},))[1][1].code) == 1

# splatting one tuple into (,) plus zero or more empties should reduce
# this pattern appears for example in `fill_to_length`
f_splat_with_empties(t) = (()..., t..., ()..., ()...)
@test length(code_typed(f_splat_with_empties, (NTuple{200,UInt8},))[1][1].code) == 1

# check that <: can be fully eliminated
struct SomeArbitraryStruct; end
function f_subtype()
Expand Down

0 comments on commit 9d70d45

Please sign in to comment.