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

optimizer: support callsite annotations of @inline and @noinline #40754

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
support nested callsite annotations
  • Loading branch information
aviatesk committed Jun 19, 2021
commit f03b07f26f84168938ab1a6536c9e11dc3d2d321
31 changes: 19 additions & 12 deletions base/compiler/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ end
is_stmt_inline(stmt_flag::UInt8) = stmt_flag & IR_FLAG_INLINE != 0
is_stmt_inline(::Nothing) = false
is_stmt_noinline(stmt_flag::UInt8) = stmt_flag & IR_FLAG_NOINLINE != 0
is_stmt_noinline(::Nothing) = false # not used right fow
is_stmt_noinline(::Nothing) = false # not used for now

# These affect control flow within the function (so may not be removed
# if there is no usage within the function), but don't affect the purity
Expand Down Expand Up @@ -375,8 +375,7 @@ function convert_to_ircode(ci::CodeInfo, code::Vector{Any}, coverage::Bool, narg
renumber_ir_elements!(code, changemap, labelmap)

inbounds_depth = 0 # Number of stacked inbounds
inline = false # whether or not in explicit inline scope
noinline = false # whether or not in explicit noinline scope
inline_flags = BitVector()
meta = Any[]
flags = fill(0x00, length(code))
for i = 1:length(code)
Expand All @@ -392,12 +391,18 @@ function convert_to_ircode(ci::CodeInfo, code::Vector{Any}, coverage::Bool, narg
end
stmt = nothing
elseif isexpr(stmt, :inline)
arg1 = stmt.args[1]::Bool
inline = arg1
if stmt.args[1]::Bool
push!(inline_flags, true)
else
pop!(inline_flags)
end
stmt = nothing
elseif isexpr(stmt, :noinline)
arg1 = stmt.args[1]::Bool
noinline = arg1
if stmt.args[1]::Bool
push!(inline_flags, false)
else
pop!(inline_flags)
end
stmt = nothing
else
stmt = normalize(stmt, meta)
Expand All @@ -407,14 +412,16 @@ function convert_to_ircode(ci::CodeInfo, code::Vector{Any}, coverage::Bool, narg
if inbounds_depth > 0
flags[i] |= IR_FLAG_INBOUNDS
end
if inline
flags[i] |= IR_FLAG_INLINE
end
if noinline
flags[i] |= IR_FLAG_NOINLINE
if !isempty(inline_flags)
if last(inline_flags)
flags[i] |= IR_FLAG_INLINE
else
flags[i] |= IR_FLAG_NOINLINE
end
end
end
end
@assert isempty(inline_flags) "malformed meta flags"
strip_trailing_junk!(ci, code, stmtinfo, flags)
cfg = compute_basic_blocks(code)
types = Any[]
Expand Down
13 changes: 13 additions & 0 deletions test/compiler/inline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,15 @@ end
force_noinline_constprop_explicit() = @noinline inlined_constprop_explicit(0)
@inline Base.@aggressive_constprop inlined_constprop_implicit(a) = a+g
force_noinline_constprop_implicit() = @noinline inlined_constprop_implicit(0)

@noinline notinlined(a) = a
function nested(a0, b0)
@noinline begin
a = @inline notinlined(a0) # this call should be inlined
b = notinlined(b0) # this call should NOT be inlined
return a, b
end
end
end

let ci = code_typed1(m.force_inline_explicit, (Int,))
Expand Down Expand Up @@ -511,6 +520,10 @@ end
let ci = code_typed1(m.force_noinline_constprop_implicit)
@test any(x->isinvoke(x, :inlined_constprop_implicit), ci.code)
end

let ci = code_typed1(m.nested, (Int,Int))
@test count(x->isinvoke(x, :notinlined), ci.code) == 1
end
end

# Test that we can inline small constants even if they are not isbits
Expand Down