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 support for OpaqueClosure #68

Merged
merged 1 commit into from
Jan 31, 2024
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
6 changes: 3 additions & 3 deletions src/AllocCheck.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ module AllocCheck

import LLVM, GPUCompiler
using GPUCompiler: JuliaContext, safe_name
using LLVM: BasicBlock, ConstantExpr, ConstantInt, IRBuilder, UndefValue, blocks, br!,
called_operand, dispose, dominates, instructions, metadata, name, opcode,
operands, position!, ret!, successors, switch!, uses, user
using LLVM: BasicBlock, ConstantExpr, ConstantInt, InlineAsm, IRBuilder, UndefValue,
blocks, br!, called_operand, dispose, dominates, instructions, metadata,
name, opcode, operands, position!, ret!, successors, switch!, uses, user

include("static_backtrace.jl")
include("abi_call.jl")
Expand Down
16 changes: 13 additions & 3 deletions src/classify.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function classify_runtime_fn(name::AbstractString; ignore_throw::Bool)
"f__call_in_world", "f__call_in_world_total", "f_intrinsic_call", "f_invoke",
"f_opaque_closure_call", "apply", "apply_generic", "gf_invoke",
"gf_invoke_by_method", "gf_invoke_lookup_worlds", "invoke", "invoke_api",
"jl_call", "jl_call0", "jl_call1", "jl_call2", "jl_call3")
"call", "call0", "call1", "call2", "call3", "unknown_fptr")
return (:dispatch, may_alloc)
else
return (:runtime, may_alloc)
Expand All @@ -37,13 +37,15 @@ end

const generic_method_offsets = Dict{String,Int}(("jl_f__apply_latest" => 2, "ijl_f__apply_latest" => 2,
"jl_f__call_latest" => 2, "ijl_f__call_latest" => 2, "jl_f_invoke" => 2, "jl_invoke" => 1,
"jl_apply_generic" => 1, "ijl_f_invoke" => 2, "ijl_invoke" => 1, "ijl_apply_generic" => 1))
"jl_apply_generic" => 1, "ijl_f_invoke" => 2, "ijl_invoke" => 1, "ijl_apply_generic" => 1,
"jl_unknown_fptr" => 0, "ijl_unknown_fptr" => 0))

function resolve_dispatch_target(inst::LLVM.Instruction)
@assert isa(inst, LLVM.CallInst)
fun = LLVM.called_operand(inst)
if isa(fun, LLVM.Function) && in(LLVM.name(fun), keys(generic_method_offsets))
offset = generic_method_offsets[LLVM.name(fun)]
offset == 0 && return nothing
flib = operands(inst)[offset]
flib = unwrap_ptr_casts(flib)
flib = look_through_loads(flib)
Expand Down Expand Up @@ -241,8 +243,16 @@ function rename_call!(call::LLVM.CallInst, mod::LLVM.Module)
fn, file, line, linfo, fromC, inlined = last(frames)

fname = string(fn)
elseif isa(callee, InlineAsm)
return # these are emitted for GC operations
elseif isa(callee, LLVM.Function)
return # function is already compile-time-known and expanded
else
return
# Call to a runtime-determined function pointer, usually an OpaqueClosure
# or a ccall that we were not able to fully resolve.
#
# We label this as a DynamicDispatch to an unknown function target.
fname = "jl_unknown_fptr"
end

# Re-write function call to use a locally-created version with a nice name
Expand Down
27 changes: 26 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ function run_gc_explicitly()
GC.gc()
end

function call_jl_call(x::Int, y::Int)
return ccall(:jl_call2, Any, (#= Function =# Any, Any, Any), +, x, y)
end

const f = Base.Experimental.@opaque (x::Int, y::Int) -> x + y
function call_opaque_closure(x::Int, y::Int)
return f(x, y)
end

@testset "Number of Allocations" begin
@test length(check_allocs(mod, (Float64,Float64); ignore_throw=false)) == 0
@test length(check_allocs(sin, (Float64,); ignore_throw=false)) > 0
Expand All @@ -58,6 +67,15 @@ end
@test length(check_allocs(run_gc_explicitly, (); ignore_throw = false)) == 0

@test_throws MethodError check_allocs(sin, (String,); ignore_throw=false)

allocs = check_allocs(call_jl_call, (Int, Int); ignore_throw = false)
@test length(allocs) == 2
@test all((alloc isa AllocationSite && allocs[1].type === Int64) ||
(alloc isa DynamicDispatch && allocs[2].fname === nothing)
for alloc in allocs)

allocs = check_allocs(call_opaque_closure, (Int, Int); ignore_throw = false)
@test length(allocs) > 0 && any(alloc isa DynamicDispatch for alloc in allocs)
end

@testset "@check_allocs macro (syntax)" begin
Expand Down Expand Up @@ -183,9 +201,16 @@ end
@test any(x isa AllocationSite && x.type == Memory # uses jl_genericmemory_copy_slice
for x in check_allocs(copy, (Vector{Int},)))

@test all(x isa AllocationSite && x.type == Memory{UInt8} # uses jl_string_to_genericmemory
@test all(x isa DynamicDispatch || (x isa AllocationSite && x.type == Memory{UInt8}) # uses jl_string_to_genericmemory
for x in check_allocs(Base.array_new_memory, (Memory{UInt8}, Int)))

# Marked broken because the `Expr(:foreigncall, QuoteNode(:jl_alloc_string), ...)` should be resolved
# by AllocCheck.jl, but is instead (conservatively) marked as a DynamicDisaptch.
#
# We get thrown off by the `jl_load_and_lookup` machinery here.
@test_broken all(x isa AllocationSite && x.type == Memory{UInt8} # uses jl_string_to_genericmemory
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I relaxed this test and marked the original as broken

We need to expand our resolution here to walk through the PhiNode in the LLVM IR and notice the ccall / jl_load_and_lookup machinery.

for x in check_allocs(Base.array_new_memory, (Memory{UInt8}, Int)))

@test all(x isa AllocationSite && x.type == Memory{Int} # uses jl_alloc_genericmemory
for x in check_allocs(Base.array_new_memory, (Memory{Int}, Int)))

Expand Down
Loading