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

In IR2OC constructor, widenconst OC argtypes #46108

Merged
merged 2 commits into from
Jul 23, 2022
Merged
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
2 changes: 1 addition & 1 deletion base/compiler/ssair/legacy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Mainly used for testing or interactive use.
"""
inflate_ir(ci::CodeInfo, linfo::MethodInstance) = inflate_ir!(copy(ci), linfo)
inflate_ir(ci::CodeInfo, sptypes::Vector{Any}, argtypes::Vector{Any}) = inflate_ir!(copy(ci), sptypes, argtypes)
inflate_ir(ci::CodeInfo) = inflate_ir(ci, Any[], Any[ Any for i = 1:length(ci.slotflags) ])
inflate_ir(ci::CodeInfo) = inflate_ir(ci, Any[], Any[ ci.slottypes === nothing ? Any : ci.slottypes[i] for i = 1:length(ci.slotflags) ])
Keno marked this conversation as resolved.
Show resolved Hide resolved

function replace_code_newstyle!(ci::CodeInfo, ir::IRCode, nargs::Int)
@assert isempty(ir.new_nodes)
Expand Down
17 changes: 16 additions & 1 deletion base/opaque_closure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ function compute_ir_rettype(ir::IRCode)
return Core.Compiler.widenconst(rt)
end

function compute_oc_argtypes(ir, nargs, isva)
argtypes = ir.argtypes[2:end]
@assert nargs == length(argtypes)
argtypes = Core.Compiler.anymap(Core.Compiler.widenconst, argtypes)
if isva
lastarg = pop!(argtypes)
if lastarg <: Tuple
append!(argtypes, lastarg.parameters)
else
push!(argtypes, Vararg{Any})
end
end
Tuple{argtypes...}
end

function Core.OpaqueClosure(ir::IRCode, env...;
nargs::Int = length(ir.argtypes)-1,
isva::Bool = false,
Expand All @@ -57,7 +72,7 @@ function Core.OpaqueClosure(ir::IRCode, env...;
# NOTE: we need ir.argtypes[1] == typeof(env)

ccall(:jl_new_opaque_closure_from_code_info, Any, (Any, Any, Any, Any, Any, Cint, Any, Cint, Cint, Any),
Tuple{ir.argtypes[2:end]...}, Union{}, rt, @__MODULE__, src, 0, nothing, nargs, isva, env)
compute_oc_argtypes(ir, nargs, isva), Union{}, rt, @__MODULE__, src, 0, nothing, nargs, isva, env)
end

function Core.OpaqueClosure(src::CodeInfo, env...)
Expand Down
20 changes: 13 additions & 7 deletions test/opaque_closure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,18 @@ end

let ci = code_typed((x, y...)->(x, y), (Int, Int))[1][1]
ir = Core.Compiler.inflate_ir(ci)
@test OpaqueClosure(ir; nargs=2, isva=true)(40, 2) === (40, (2,))
@test OpaqueClosure(ci)(40, 2) === (40, (2,))
end
let oc = OpaqueClosure(ir; nargs=2, isva=true)
@test oc(40, 2) === (40, (2,))
@test_throws MethodError oc(1,2,3)
end
let oc = OpaqueClosure(ci)
@test oc(40, 2) === (40, (2,))
@test_throws MethodError oc(1,2,3)
end

let ci = code_typed((x, y...)->(x, y), (Int, Int))[1][1]
ir = Core.Compiler.inflate_ir(ci)
@test_throws MethodError OpaqueClosure(ir; nargs=2, isva=true)(1, 2, 3)
@test_throws MethodError OpaqueClosure(ci)(1, 2, 3)
ir = Core.Compiler.inflate_ir(ci, Any[], Any[Tuple{}, Int, Tuple{Int}])
let oc = OpaqueClosure(ir; nargs=2, isva=true)
@test oc(40, 2) === (40, (2,))
@test_throws MethodError oc(1,2,3)
end
end