Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move typeassert effect-free modeling to the proper place #43830

Merged
merged 1 commit into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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