Skip to content

Commit dd395f3

Browse files
KenoLilithHafner
authored andcommitted
Move typeassert effect-free modeling to the proper place (JuliaLang#43830)
This was open-coded inside inlining, but the effect-free modeling code should be with the other builtins (in preparation of calling it from more places).
1 parent 191107a commit dd395f3

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

base/compiler/ssair/inlining.jl

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,9 @@ end
10531053
function check_effect_free!(ir::IRCode, idx::Int, @nospecialize(stmt), @nospecialize(rt))
10541054
if stmt_effect_free(stmt, rt, ir)
10551055
ir.stmts[idx][:flag] |= IR_FLAG_EFFECT_FREE
1056+
return true
10561057
end
1058+
return false
10571059
end
10581060

10591061
# Handles all analysis and inlining of intrinsics and builtins. In particular,
@@ -1107,10 +1109,16 @@ function process_simple!(ir::IRCode, idx::Int, state::InliningState, todo::Vecto
11071109
return nothing
11081110
end
11091111

1110-
check_effect_free!(ir, idx, stmt, rt)
1112+
if check_effect_free!(ir, idx, stmt, rt)
1113+
if sig.f === typeassert || sig.ft typeof(typeassert)
1114+
# typeassert is a no-op if effect free
1115+
ir.stmts[idx][:inst] = stmt.args[2]
1116+
return nothing
1117+
end
1118+
end
11111119

11121120
if sig.f !== Core.invoke && is_builtin(sig)
1113-
# No inlining for builtins (other invoke/apply)
1121+
# No inlining for builtins (other invoke/apply/typeassert)
11141122
return nothing
11151123
end
11161124

@@ -1379,15 +1387,6 @@ function early_inline_special_case(
13791387
ir::IRCode, stmt::Expr, @nospecialize(type), sig::Signature,
13801388
params::OptimizationParams)
13811389
(; f, ft, argtypes) = sig
1382-
if (f === typeassert || ft typeof(typeassert)) && length(argtypes) == 3
1383-
# typeassert(x::S, T) => x, when S<:T
1384-
a3 = argtypes[3]
1385-
if (isType(a3) && !has_free_typevars(a3) && argtypes[2] a3.parameters[1]) ||
1386-
(isa(a3, Const) && isa(a3.val, Type) && argtypes[2] a3.val)
1387-
val = stmt.args[2]
1388-
return SomeCase(val === nothing ? QuoteNode(val) : val)
1389-
end
1390-
end
13911390

13921391
if params.inlining
13931392
if isa(type, Const) # || isconstType(type)

base/compiler/tfuncs.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,6 +1687,14 @@ function _builtin_nothrow(@nospecialize(f), argtypes::Array{Any,1}, @nospecializ
16871687
elseif f === Core.ifelse
16881688
length(argtypes) == 3 || return false
16891689
return argtypes[1] Bool
1690+
elseif f === typeassert
1691+
length(argtypes) == 2 || return false
1692+
a3 = argtypes[2]
1693+
if (isType(a3) && !has_free_typevars(a3) && argtypes[1] a3.parameters[1]) ||
1694+
(isa(a3, Const) && isa(a3.val, Type) && argtypes[1] a3.val)
1695+
return true
1696+
end
1697+
return false
16901698
end
16911699
return false
16921700
end

0 commit comments

Comments
 (0)