Skip to content

Commit bea863e

Browse files
authored
Merge pull request #31338 from JuliaLang/jn/opt-faster
optimize (a little bit) faster
2 parents 6ba6a83 + ffa2924 commit bea863e

File tree

8 files changed

+248
-41
lines changed

8 files changed

+248
-41
lines changed

base/compiler/optimize.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ intrinsic_effect_free_if_nothrow(f) = f === Intrinsics.pointerref || is_pure_int
272272
plus_saturate(x::Int, y::Int) = max(x, y, x+y)
273273

274274
# known return type
275-
isknowntype(@nospecialize T) = (T == Union{}) || isconcretetype(T)
275+
isknowntype(@nospecialize T) = (T === Union{}) || isconcretetype(T)
276276

277277
function statement_cost(ex::Expr, line::Int, src::CodeInfo, sptypes::Vector{Any}, slottypes::Vector{Any}, params::Params)
278278
head = ex.head

base/compiler/ssair/inlining.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -923,12 +923,13 @@ end
923923
# functions.
924924
function process_simple!(ir::IRCode, idx::Int, params::Params)
925925
stmt = ir.stmts[idx]
926-
if isexpr(stmt, :splatnew)
926+
stmt isa Expr || return nothing
927+
if stmt.head === :splatnew
927928
inline_splatnew!(ir, idx)
928929
return nothing
929930
end
930931

931-
isexpr(stmt, :call) || return nothing
932+
stmt.head === :call || return nothing
932933

933934
sig = call_sig(ir, stmt)
934935
sig === nothing && return nothing

base/compiler/ssair/ir.jl

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -780,8 +780,10 @@ function renumber_ssa2(val::SSAValue, ssanums::Vector{Any}, used_ssa::Vector{Int
780780
if do_rename_ssa
781781
val = ssanums[id]
782782
end
783-
if isa(val, SSAValue) && used_ssa !== nothing
784-
used_ssa[val.id] += 1
783+
if isa(val, SSAValue)
784+
if used_ssa !== nothing
785+
used_ssa[val.id] += 1
786+
end
785787
end
786788
return val
787789
end
@@ -904,7 +906,7 @@ function process_node!(compact::IncrementalCompact, result::Vector{Any},
904906
elseif isa(stmt, Expr)
905907
stmt = renumber_ssa2!(stmt, ssa_rename, used_ssas, late_fixup, result_idx, do_rename_ssa)::Expr
906908
if compact.allow_cfg_transforms && isexpr(stmt, :enter)
907-
stmt.args[1] = compact.bb_rename[stmt.args[1]]
909+
stmt.args[1] = compact.bb_rename[stmt.args[1]::Int]
908910
end
909911
result[result_idx] = stmt
910912
result_idx += 1
@@ -913,15 +915,18 @@ function process_node!(compact::IncrementalCompact, result::Vector{Any},
913915
# type equality. We may want to consider using == in either a separate pass or if
914916
# performance turns out ok
915917
stmt = renumber_ssa2!(stmt, ssa_rename, used_ssas, late_fixup, result_idx, do_rename_ssa)::PiNode
916-
if !isa(stmt.val, AnySSAValue) && !isa(stmt.val, GlobalRef)
917-
valtyp = isa(stmt.val, QuoteNode) ? typeof(stmt.val.value) : typeof(stmt.val)
918+
pi_val = stmt.val
919+
if isa(pi_val, SSAValue)
920+
if stmt.typ === compact.result_types[pi_val.id]
921+
ssa_rename[idx] = pi_val
922+
return result_idx
923+
end
924+
elseif !isa(pi_val, AnySSAValue) && !isa(pi_val, GlobalRef)
925+
valtyp = isa(pi_val, QuoteNode) ? typeof(pi_val.value) : typeof(pi_val)
918926
if valtyp === stmt.typ
919-
ssa_rename[idx] = stmt.val
927+
ssa_rename[idx] = pi_val
920928
return result_idx
921929
end
922-
elseif isa(stmt.val, SSAValue) && stmt.typ === compact.result_types[stmt.val.id]
923-
ssa_rename[idx] = stmt.val
924-
return result_idx
925930
end
926931
result[result_idx] = stmt
927932
result_idx += 1

base/compiler/ssair/legacy.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function inflate_ir(ci::CodeInfo, linfo::MethodInstance)
1111
end
1212

1313
function inflate_ir(ci::CodeInfo, sptypes::Vector{Any}, argtypes::Vector{Any})
14-
code = copy_exprargs(ci.code)
14+
code = copy_exprargs(ci.code) # TODO: this is a huge hot-spot
1515
for i = 1:length(code)
1616
if isa(code[i], Expr)
1717
code[i] = normalize_expr(code[i])

base/compiler/ssair/passes.jl

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -191,16 +191,16 @@ function walk_to_defs(compact::IncrementalCompact, @nospecialize(defssa), @nospe
191191
def = compact[defssa]
192192
if isa(def, PhiNode)
193193
push!(visited_phinodes, defssa)
194-
possible_predecessors = let def=def, typeconstraint=typeconstraint
195-
collect(Iterators.filter(1:length(def.edges)) do n
196-
isassigned(def.values, n) || return false
197-
val = def.values[n]
198-
if is_old(compact, defssa) && isa(val, SSAValue)
199-
val = OldSSAValue(val.id)
200-
end
201-
edge_typ = widenconst(compact_exprtype(compact, val))
202-
return typeintersect(edge_typ, typeconstraint) !== Union{}
203-
end)
194+
possible_predecessors = Int[]
195+
for n in 1:length(def.edges)
196+
isassigned(def.values, n) || continue
197+
val = def.values[n]
198+
if is_old(compact, defssa) && isa(val, SSAValue)
199+
val = OldSSAValue(val.id)
200+
end
201+
edge_typ = widenconst(compact_exprtype(compact, val))
202+
typeintersect(edge_typ, typeconstraint) === Union{} && continue
203+
push!(possible_predecessors, n)
204204
end
205205
for n in possible_predecessors
206206
pred = def.edges[n]
@@ -829,15 +829,16 @@ function adce_erase!(phi_uses, extra_worklist, compact, idx)
829829
end
830830
end
831831

832-
function count_uses(stmt, uses)
832+
function count_uses(@nospecialize(stmt), uses::Vector{Int})
833833
for ur in userefs(stmt)
834-
if isa(ur[], SSAValue)
835-
uses[ur[].id] += 1
834+
use = ur[]
835+
if isa(use, SSAValue)
836+
uses[use.id] += 1
836837
end
837838
end
838839
end
839840

840-
function mark_phi_cycles(compact, safe_phis, phi)
841+
function mark_phi_cycles(compact::IncrementalCompact, safe_phis::BitSet, phi::Int)
841842
worklist = Int[]
842843
push!(worklist, phi)
843844
while !isempty(worklist)
@@ -864,7 +865,7 @@ function adce_pass!(ir::IRCode)
864865
end
865866
non_dce_finish!(compact)
866867
for phi in all_phis
867-
count_uses(compact.result[phi], phi_uses)
868+
count_uses(compact.result[phi]::PhiNode, phi_uses)
868869
end
869870
# Perform simple DCE for unused values
870871
extra_worklist = Int[]
@@ -880,7 +881,7 @@ function adce_pass!(ir::IRCode)
880881
changed = true
881882
while changed
882883
changed = false
883-
safe_phis = IdSet{Int}()
884+
safe_phis = BitSet()
884885
for phi in all_phis
885886
# Save any phi cycles that have non-phi uses
886887
if compact.used_ssas[phi] - phi_uses[phi] != 0
@@ -898,7 +899,7 @@ function adce_pass!(ir::IRCode)
898899
end
899900
end
900901
end
901-
complete(compact)
902+
return complete(compact)
902903
end
903904

904905
function type_lift_pass!(ir::IRCode)

base/compiler/ssair/queries.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
Determine whether a statement is side-effect-free, i.e. may be removed if it has no uses.
55
"""
66
function stmt_effect_free(@nospecialize(stmt), @nospecialize(rt), src, sptypes::Vector{Any})
7-
isa(stmt, Union{PiNode, PhiNode}) && return true
8-
isa(stmt, Union{ReturnNode, GotoNode, GotoIfNot}) && return false
9-
isa(stmt, GlobalRef) && return isdefined(stmt.mod, stmt.name)
7+
isa(stmt, PiNode) && return true
8+
isa(stmt, PhiNode) && return true
9+
isa(stmt, ReturnNode) && return false
10+
isa(stmt, GotoNode) && return false
11+
isa(stmt, GotoIfNot) && return false
1012
isa(stmt, Slot) && return false # Slots shouldn't occur in the IR at this point, but let's be defensive here
13+
isa(stmt, GlobalRef) && return isdefined(stmt.mod, stmt.name)
1114
if isa(stmt, Expr)
1215
e = stmt::Expr
1316
head = e.head

base/compiler/tfuncs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ add_tfunc(pointerref, 3, 3,
410410
add_tfunc(pointerset, 4, 4, (@nospecialize(a), @nospecialize(v), @nospecialize(i), @nospecialize(align)) -> a, 5)
411411

412412
# more accurate typeof_tfunc for vararg tuples abstract only in length
413-
function typeof_concrete_vararg(@nospecialize(t))
413+
function typeof_concrete_vararg(t::DataType)
414414
np = length(t.parameters)
415415
for i = 1:np
416416
p = t.parameters[i]

0 commit comments

Comments
 (0)