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

Add hasbox to check for Core.Box #29

Merged
merged 2 commits into from
Feb 2, 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
24 changes: 24 additions & 0 deletions src/MethodAnalysis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ using Base.Meta: isexpr

export visit, call_type, methodinstance, methodinstances, worlds # findcallers is exported from its own file
export visit_backedges, all_backedges, with_all_backedges, terminal_backedges, direct_backedges
export hasbox

include("visit.jl")
include("backedges.jl")
Expand Down Expand Up @@ -189,6 +190,29 @@ function methodinstances(@nospecialize(types::Type))
return methodinstances(f, types)
end

if isdefined(Base, :code_typed_by_type)
function hasbox(mi::MethodInstance)
try
srcs = Base.code_typed_by_type(mi.specTypes)
for (ci, rt) in srcs
(any(==(Core.Box), ci.slottypes) || any(==(Core.Box), ci.ssavaluetypes)) && return true
end
return false
catch
return false
end
end
else
hasbox(mi::MethodInstance) = error("hasbox requires at least Julia 1.6")
end

"""
hasbox(mi::MethodInstance)
Return `true` if the code for `mi` has a `Core.Box`. This often arises from a limitation in Julia's type-inference,
see https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-captured.
"""
hasbox

if isdefined(Core, :MethodMatch)
include("findcallers.jl")
Expand Down
24 changes: 24 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,30 @@ end
end
end

@testset "hasbox" begin
function abmult(r::Int)
if r < 0
r = -r
end
f = x -> x * r
return f
end
function abmult2(r::Int)
f = x -> x * abs(r)
return f
end
abmult(1)
mi = methodinstance(abmult, (Int,))
abmult2(1)
mi2 = methodinstance(abmult2, (Int,))
if isdefined(Base, :code_typed_by_type)
@test hasbox(mi)
@test !hasbox(mi2)
else
@test_throws ErrorException("hasbox requires at least Julia 1.6") hasbox(mi)
end
end

module Callers

f(x) = rand()
Expand Down