Skip to content

Commit 7fa26f0

Browse files
authored
Move Compiler <-> OpaqueClosure interface code to Compiler (JuliaLang#56576)
After the excision, it is no longer permissable for Base to have `Compiler` data structures in arguments of methods it defines. To comply with this restriction, move the functions for creating OpaqueClosures from IRCode to `Compiler`.
1 parent caa2f7d commit 7fa26f0

File tree

3 files changed

+57
-57
lines changed

3 files changed

+57
-57
lines changed

Compiler/src/Compiler.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ include("optimize.jl")
179179

180180
include("bootstrap.jl")
181181
include("reflection_interface.jl")
182+
include("opaque_closure.jl")
182183

183184
module IRShow end
184185
if !isdefined(Base, :end_base_include)

Compiler/src/opaque_closure.jl

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# This file is a part of Julia. License is MIT: https://julialang.org/license
2+
3+
function compute_ir_rettype(ir::IRCode)
4+
rt = Union{}
5+
for i = 1:length(ir.stmts)
6+
stmt = ir[SSAValue(i)][:stmt]
7+
if isa(stmt, Core.ReturnNode) && isdefined(stmt, :val)
8+
rt = Compiler.tmerge(Compiler.argextype(stmt.val, ir), rt)
9+
end
10+
end
11+
return Compiler.widenconst(rt)
12+
end
13+
14+
function compute_oc_signature(ir::IRCode, nargs::Int, isva::Bool)
15+
argtypes = Vector{Any}(undef, nargs)
16+
for i = 1:nargs
17+
argtypes[i] = Compiler.widenconst(ir.argtypes[i+1])
18+
end
19+
if isva
20+
lastarg = pop!(argtypes)
21+
if lastarg <: Tuple
22+
append!(argtypes, lastarg.parameters)
23+
else
24+
push!(argtypes, Vararg{Any})
25+
end
26+
end
27+
return Tuple{argtypes...}
28+
end
29+
30+
function Core.OpaqueClosure(ir::IRCode, @nospecialize env...;
31+
isva::Bool = false,
32+
slotnames::Union{Nothing,Vector{Symbol}}=nothing,
33+
kwargs...)
34+
# NOTE: we need ir.argtypes[1] == typeof(env)
35+
ir = Core.Compiler.copy(ir)
36+
# if the user didn't specify a definition MethodInstance or filename Symbol to use for the debuginfo, set a filename now
37+
ir.debuginfo.def === nothing && (ir.debuginfo.def = :var"generated IR for OpaqueClosure")
38+
nargtypes = length(ir.argtypes)
39+
nargs = nargtypes-1
40+
sig = compute_oc_signature(ir, nargs, isva)
41+
rt = compute_ir_rettype(ir)
42+
src = ccall(:jl_new_code_info_uninit, Ref{CodeInfo}, ())
43+
if slotnames === nothing
44+
src.slotnames = fill(:none, nargtypes)
45+
else
46+
length(slotnames) == nargtypes || error("mismatched `argtypes` and `slotnames`")
47+
src.slotnames = slotnames
48+
end
49+
src.slotflags = fill(zero(UInt8), nargtypes)
50+
src.slottypes = copy(ir.argtypes)
51+
src.isva = isva
52+
src.nargs = UInt(nargtypes)
53+
src = ir_to_codeinf!(src, ir)
54+
src.rettype = rt
55+
return Base.Experimental.generate_opaque_closure(sig, Union{}, rt, src, nargs, isva, env...; kwargs...)
56+
end

base/opaque_closure.jl

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -39,63 +39,6 @@ end
3939

4040
# OpaqueClosure construction from pre-inferred CodeInfo/IRCode
4141
using Core: CodeInfo, SSAValue
42-
using Base: Compiler
43-
using .Compiler: IRCode
44-
45-
function compute_ir_rettype(ir::IRCode)
46-
rt = Union{}
47-
for i = 1:length(ir.stmts)
48-
stmt = ir[SSAValue(i)][:stmt]
49-
if isa(stmt, Core.ReturnNode) && isdefined(stmt, :val)
50-
rt = Compiler.tmerge(Compiler.argextype(stmt.val, ir), rt)
51-
end
52-
end
53-
return Compiler.widenconst(rt)
54-
end
55-
56-
function compute_oc_signature(ir::IRCode, nargs::Int, isva::Bool)
57-
argtypes = Vector{Any}(undef, nargs)
58-
for i = 1:nargs
59-
argtypes[i] = Compiler.widenconst(ir.argtypes[i+1])
60-
end
61-
if isva
62-
lastarg = pop!(argtypes)
63-
if lastarg <: Tuple
64-
append!(argtypes, lastarg.parameters)
65-
else
66-
push!(argtypes, Vararg{Any})
67-
end
68-
end
69-
return Tuple{argtypes...}
70-
end
71-
72-
function Core.OpaqueClosure(ir::IRCode, @nospecialize env...;
73-
isva::Bool = false,
74-
slotnames::Union{Nothing,Vector{Symbol}}=nothing,
75-
kwargs...)
76-
# NOTE: we need ir.argtypes[1] == typeof(env)
77-
ir = Core.Compiler.copy(ir)
78-
# if the user didn't specify a definition MethodInstance or filename Symbol to use for the debuginfo, set a filename now
79-
ir.debuginfo.def === nothing && (ir.debuginfo.def = :var"generated IR for OpaqueClosure")
80-
nargtypes = length(ir.argtypes)
81-
nargs = nargtypes-1
82-
sig = compute_oc_signature(ir, nargs, isva)
83-
rt = compute_ir_rettype(ir)
84-
src = ccall(:jl_new_code_info_uninit, Ref{CodeInfo}, ())
85-
if slotnames === nothing
86-
src.slotnames = fill(:none, nargtypes)
87-
else
88-
length(slotnames) == nargtypes || error("mismatched `argtypes` and `slotnames`")
89-
src.slotnames = slotnames
90-
end
91-
src.slotflags = fill(zero(UInt8), nargtypes)
92-
src.slottypes = copy(ir.argtypes)
93-
src.isva = isva
94-
src.nargs = nargtypes
95-
src = Core.Compiler.ir_to_codeinf!(src, ir)
96-
src.rettype = rt
97-
return generate_opaque_closure(sig, Union{}, rt, src, nargs, isva, env...; kwargs...)
98-
end
9942

10043
function Core.OpaqueClosure(src::CodeInfo, @nospecialize env...; rettype, sig, nargs, isva=false, kwargs...)
10144
return generate_opaque_closure(sig, Union{}, rettype, src, nargs, isva, env...; kwargs...)

0 commit comments

Comments
 (0)