Skip to content

Commit

Permalink
Move typeassert effect-free modeling to the proper place (JuliaLang#4…
Browse files Browse the repository at this point in the history
…3830)

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).
  • Loading branch information
Keno authored and N5N3 committed Jan 24, 2022
1 parent 2d50bfa commit 0993c30
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
21 changes: 10 additions & 11 deletions base/compiler/ssair/inlining.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,9 @@ end
function check_effect_free!(ir::IRCode, idx::Int, @nospecialize(stmt), @nospecialize(rt))
if stmt_effect_free(stmt, rt, ir)
ir.stmts[idx][:flag] |= IR_FLAG_EFFECT_FREE
return true
end
return false
end

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

check_effect_free!(ir, idx, stmt, rt)
if check_effect_free!(ir, idx, stmt, rt)
if sig.f === typeassert || sig.ft typeof(typeassert)
# typeassert is a no-op if effect free
ir.stmts[idx][:inst] = stmt.args[2]
return nothing
end
end

if sig.f !== Core.invoke && is_builtin(sig)
# No inlining for builtins (other invoke/apply)
# No inlining for builtins (other invoke/apply/typeassert)
return nothing
end

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

if params.inlining
if isa(type, Const) # || isconstType(type)
Expand Down
8 changes: 8 additions & 0 deletions base/compiler/tfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1687,6 +1687,14 @@ function _builtin_nothrow(@nospecialize(f), argtypes::Array{Any,1}, @nospecializ
elseif f === Core.ifelse
length(argtypes) == 3 || return false
return argtypes[1] Bool
elseif f === typeassert
length(argtypes) == 2 || return false
a3 = argtypes[2]
if (isType(a3) && !has_free_typevars(a3) && argtypes[1] a3.parameters[1]) ||
(isa(a3, Const) && isa(a3.val, Type) && argtypes[1] a3.val)
return true
end
return false
end
return false
end
Expand Down

0 comments on commit 0993c30

Please sign in to comment.