Skip to content

Commit 9932de3

Browse files
mlechuc42f
andcommitted
Update CodeInfo struct and handling
Co-authored-by: Claire Foster <[email protected]>
1 parent 5416caa commit 9932de3

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

src/eval.jl

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ function macroexpand(mod::Module, ex)
1212
ex1
1313
end
1414

15+
function codeinfo_has_image_globalref(@nospecialize(e))
16+
if e isa GlobalRef
17+
return 0x00 !== @ccall jl_object_in_image(e.mod::Any)::UInt8
18+
elseif e isa Core.CodeInfo
19+
return any(codeinfo_has_image_globalref, e.code)
20+
elseif e isa Expr
21+
return any(codeinfo_has_image_globalref, e.args)
22+
else
23+
return false
24+
end
25+
end
26+
1527
_CodeInfo_need_ver = v"1.12.0-DEV.512"
1628
if VERSION < _CodeInfo_need_ver
1729
function _CodeInfo(args...)
@@ -20,19 +32,29 @@ if VERSION < _CodeInfo_need_ver
2032
else
2133
# debuginfo changed completely as of https://github.com/JuliaLang/julia/pull/52415
2234
# nargs / isva was added as of https://github.com/JuliaLang/julia/pull/54341
35+
# field rettype added in https://github.com/JuliaLang/julia/pull/54655
36+
# field has_image_globalref added in https://github.com/JuliaLang/julia/pull/57433
2337
# CodeInfo constructor. TODO: Should be in Core
2438
let
2539
fns = fieldnames(Core.CodeInfo)
2640
fts = fieldtypes(Core.CodeInfo)
2741
conversions = [:(convert($t, $n)) for (t,n) in zip(fts, fns)]
2842

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)
43+
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)
44+
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)
3145

32-
code = if fns != expected_fns || fts != expected_fts
46+
code = if fns != expected_fns
47+
unexpected_fns = collect(setdiff(Set(fns), Set(expected_fns)))
48+
missing_fns = collect(setdiff(Set(expected_fns), Set(fns)))
3349
:(function _CodeInfo(args...)
34-
error("Unrecognized CodeInfo layout: Maybe version $VERSION is to new for this version of JuliaLowering?")
35-
end)
50+
error("Unrecognized CodeInfo fields: Maybe version $VERSION is too new for this version of JuliaLowering?"
51+
* isempty(unexpected_fns) ? "" : "\nUnexpected fields found: $($unexpected_fns)"
52+
* isempty(missing_fns) ? "" : "\nMissing fields: $($missing_fns)")
53+
end)
54+
elseif fts != expected_fts
55+
:(function _CodeInfo(args...)
56+
error("Unrecognized CodeInfo field types: Maybe version $VERSION is too new for this version of JuliaLowering?")
57+
end)
3658
else
3759
:(function _CodeInfo($(fns...))
3860
$(Expr(:new, :(Core.CodeInfo), conversions...))
@@ -142,6 +164,8 @@ function to_code_info(ex, mod, funcname, slots)
142164

143165
debuginfo = finish_ir_debug_info!(current_codelocs_stack)
144166

167+
has_image_globalref = any(codeinfo_has_image_globalref, stmts)
168+
145169
# TODO: Set ssaflags based on call site annotations:
146170
# - @inbounds annotations
147171
# - call site @inline / @noinline
@@ -172,6 +196,7 @@ function to_code_info(ex, mod, funcname, slots)
172196
max_world = typemax(Csize_t)
173197
isva = false
174198
inlining_cost = 0xffff
199+
rettype = Any
175200

176201
_CodeInfo(
177202
stmts,
@@ -181,14 +206,16 @@ function to_code_info(ex, mod, funcname, slots)
181206
slotnames,
182207
slotflags,
183208
slottypes,
209+
rettype,
184210
parent,
185-
method_for_inference_limit_heuristics,
186211
edges,
187212
min_world,
188213
max_world,
214+
method_for_inference_limit_heuristics,
189215
nargs,
190216
propagate_inbounds,
191217
has_fcall,
218+
has_image_globalref,
192219
nospecializeinfer,
193220
isva,
194221
inlining,

src/linear_ir.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ 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

test/misc.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,12 @@ 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+
elower(s) = JuliaLowering.to_lowered_expr(
51+
test_mod, JuliaLowering.lower(
52+
test_mod, parsestmt(JuliaLowering.SyntaxTree, s)))
53+
@test elower("function foo end").args[1].has_image_globalref === false
54+
@test elower("Base.push!([], 1)").args[1].has_image_globalref === true
55+
end
56+
4957
end

0 commit comments

Comments
 (0)