Skip to content

Commit f20ae00

Browse files
mlechuc42f
andcommitted
Update CodeInfo struct and handling
Set has_image_globalref in linearization Co-authored-by: Claire Foster <[email protected]>
1 parent 5416caa commit f20ae00

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

src/eval.jl

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,29 @@ if VERSION < _CodeInfo_need_ver
2020
else
2121
# debuginfo changed completely as of https://github.com/JuliaLang/julia/pull/52415
2222
# nargs / isva was added as of https://github.com/JuliaLang/julia/pull/54341
23+
# field rettype added in https://github.com/JuliaLang/julia/pull/54655
24+
# field has_image_globalref added in https://github.com/JuliaLang/julia/pull/57433
2325
# CodeInfo constructor. TODO: Should be in Core
2426
let
2527
fns = fieldnames(Core.CodeInfo)
2628
fts = fieldtypes(Core.CodeInfo)
2729
conversions = [:(convert($t, $n)) for (t,n) in zip(fts, fns)]
2830

29-
expected_fns = (:code, :debuginfo, :ssavaluetypes, :ssaflags, :slotnames, :slotflags, :slottypes, :parent, :method_for_inference_limit_heuristics, :edges, :min_world, :max_world, :nargs, :propagate_inbounds, :has_fcall, :nospecializeinfer, :isva, :inlining, :constprop, :purity, :inlining_cost)
30-
expected_fts = (Vector{Any}, Core.DebugInfo, Any, Vector{UInt32}, Vector{Symbol}, Vector{UInt8}, Any, Any, Any, Any, UInt64, UInt64, UInt64, Bool, Bool, Bool, Bool, UInt8, UInt8, UInt16, UInt16)
31+
expected_fns = (:code, :debuginfo, :ssavaluetypes, :ssaflags, :slotnames, :slotflags, :slottypes, :rettype, :parent, :edges, :min_world, :max_world, :method_for_inference_limit_heuristics, :nargs, :propagate_inbounds, :has_fcall, :has_image_globalref, :nospecializeinfer, :isva, :inlining, :constprop, :purity, :inlining_cost)
32+
expected_fts = (Vector{Any}, Core.DebugInfo, Any, Vector{UInt32}, Vector{Symbol}, Vector{UInt8}, Any, Any, Any, Any, UInt64, UInt64, Any, UInt64, Bool, Bool, Bool, Bool, Bool, UInt8, UInt8, UInt16, UInt16)
3133

32-
code = if fns != expected_fns || fts != expected_fts
34+
code = if fns != expected_fns
35+
unexpected_fns = collect(setdiff(Set(fns), Set(expected_fns)))
36+
missing_fns = collect(setdiff(Set(expected_fns), Set(fns)))
3337
:(function _CodeInfo(args...)
34-
error("Unrecognized CodeInfo layout: Maybe version $VERSION is to new for this version of JuliaLowering?")
35-
end)
38+
error("Unrecognized CodeInfo fields: Maybe version $VERSION is too new for this version of JuliaLowering?"
39+
* isempty(unexpected_fns) ? "" : "\nUnexpected fields found: $($unexpected_fns)"
40+
* isempty(missing_fns) ? "" : "\nMissing fields: $($missing_fns)")
41+
end)
42+
elseif fts != expected_fts
43+
:(function _CodeInfo(args...)
44+
error("Unrecognized CodeInfo field types: Maybe version $VERSION is too new for this version of JuliaLowering?")
45+
end)
3646
else
3747
:(function _CodeInfo($(fns...))
3848
$(Expr(:new, :(Core.CodeInfo), conversions...))
@@ -142,6 +152,8 @@ function to_code_info(ex, mod, funcname, slots)
142152

143153
debuginfo = finish_ir_debug_info!(current_codelocs_stack)
144154

155+
has_image_globalref = false
156+
145157
# TODO: Set ssaflags based on call site annotations:
146158
# - @inbounds annotations
147159
# - call site @inline / @noinline
@@ -172,6 +184,7 @@ function to_code_info(ex, mod, funcname, slots)
172184
max_world = typemax(Csize_t)
173185
isva = false
174186
inlining_cost = 0xffff
187+
rettype = Any
175188

176189
_CodeInfo(
177190
stmts,
@@ -181,14 +194,16 @@ function to_code_info(ex, mod, funcname, slots)
181194
slotnames,
182195
slotflags,
183196
slottypes,
197+
rettype,
184198
parent,
185-
method_for_inference_limit_heuristics,
186199
edges,
187200
min_world,
188201
max_world,
202+
method_for_inference_limit_heuristics,
189203
nargs,
190204
propagate_inbounds,
191205
has_fcall,
206+
has_image_globalref,
192207
nospecializeinfer,
193208
isva,
194209
inlining,

src/linear_ir.jl

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,15 @@ end
6969
Context for creating linear IR.
7070
7171
One of these is created per lambda expression to flatten the body down to
72-
a sequence of statements (linear IR).
72+
a sequence of statements (linear IR), which eventually becomes one CodeInfo.
7373
"""
7474
struct LinearIRContext{GraphType} <: AbstractLoweringContext
7575
graph::GraphType
7676
code::SyntaxList{GraphType, Vector{NodeId}}
7777
bindings::Bindings
7878
next_label_id::Ref{Int}
7979
is_toplevel_thunk::Bool
80+
has_image_globalref::Ref{Bool}
8081
lambda_bindings::LambdaBindings
8182
return_type::Union{Nothing, SyntaxTree{GraphType}}
8283
break_targets::Dict{String, JumpTarget{GraphType}}
@@ -92,8 +93,9 @@ function LinearIRContext(ctx, is_toplevel_thunk, lambda_bindings, return_type)
9293
graph = syntax_graph(ctx)
9394
rett = isnothing(return_type) ? nothing : reparent(graph, return_type)
9495
GraphType = typeof(graph)
96+
has_image_globalref = Ref(false)
9597
LinearIRContext(graph, SyntaxList(ctx), ctx.bindings, Ref(0),
96-
is_toplevel_thunk, lambda_bindings, rett,
98+
is_toplevel_thunk, has_image_globalref, lambda_bindings, rett,
9799
Dict{String,JumpTarget{GraphType}}(), SyntaxList(ctx), SyntaxList(ctx),
98100
Vector{FinallyHandler{GraphType}}(), Dict{String,JumpTarget{GraphType}}(),
99101
Vector{JumpOrigin{GraphType}}(), ctx.mod)
@@ -973,9 +975,16 @@ function _renumber(ctx, ssa_rewrites, slot_rewrites, label_table, ex)
973975
if binfo.kind !== :global
974976
throw(LoweringError(ex, "Found unexpected binding of kind $(binfo.kind)"))
975977
end
978+
if !ctx.has_image_globalref[]
979+
has_ref = 0x00 !== @ccall jl_object_in_image(binfo.mod::Any)::UInt8
980+
ctx.has_image_globalref[] = has_ref
981+
end
976982
makeleaf(ctx, ex, K"globalref", binfo.name, mod=binfo.mod)
977983
end
978984
end
985+
elseif k == K"top" # reference to Base
986+
ctx.has_image_globalref[] = true
987+
ex
979988
elseif k == K"meta"
980989
# Somewhat-hack for Expr(:meta, :generated, gen) which has
981990
# weird top-level semantics for `gen`, but we still need to translate
@@ -1094,7 +1103,8 @@ function compile_lambda(outer_ctx, ex)
10941103
# @info "" @ast ctx ex [K"block" ctx.code...]
10951104
code = renumber_body(ctx, ctx.code, slot_rewrites)
10961105
@ast ctx ex [K"code_info"(is_toplevel_thunk=ex.is_toplevel_thunk,
1097-
slots=slots)
1106+
slots=slots,
1107+
has_image_globalref=ctx.has_image_globalref[])
10981108
[K"block"(ex[3])
10991109
code...
11001110
]
@@ -1112,13 +1122,14 @@ loops, etc) to gotos and exception handling to enter/leave. We also convert
11121122
function linearize_ir(ctx, ex)
11131123
graph = ensure_attributes(ctx.graph,
11141124
slots=Vector{Slot},
1125+
has_image_globalref=Bool,
11151126
mod=Module,
11161127
id=Int)
11171128
# TODO: Cleanup needed - `_ctx` is just a dummy context here. But currently
11181129
# required to call reparent() ...
11191130
GraphType = typeof(graph)
11201131
_ctx = LinearIRContext(graph, SyntaxList(graph), ctx.bindings,
1121-
Ref(0), false, LambdaBindings(), nothing,
1132+
Ref(0), false, Ref(false), LambdaBindings(), nothing,
11221133
Dict{String,JumpTarget{typeof(graph)}}(),
11231134
SyntaxList(graph), SyntaxList(graph),
11241135
Vector{FinallyHandler{GraphType}}(),

test/misc.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,14 @@ cf_float = JuliaLowering.include_string(test_mod, """
4646
""")
4747
@test @ccall($cf_float(2::Float64, 3::Float64)::Float64) == 32.0
4848

49+
@testset "CodeInfo: has_image_globalref" begin
50+
@test JuliaLowering.lower(test_mod, parsestmt(JuliaLowering.SyntaxTree, """
51+
function foo end
52+
""")).has_image_globalref === false
53+
54+
@test JuliaLowering.lower(test_mod, parsestmt(JuliaLowering.SyntaxTree, """
55+
Base.push!([], 1)
56+
""")).has_image_globalref === true
57+
end
58+
4959
end

0 commit comments

Comments
 (0)