Skip to content

Commit f2f18e9

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

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

src/eval.jl

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ 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+
else
21+
return false
22+
end
23+
end
24+
1525
_CodeInfo_need_ver = v"1.12.0-DEV.512"
1626
if VERSION < _CodeInfo_need_ver
1727
function _CodeInfo(args...)
@@ -20,19 +30,29 @@ if VERSION < _CodeInfo_need_ver
2030
else
2131
# debuginfo changed completely as of https://github.com/JuliaLang/julia/pull/52415
2232
# nargs / isva was added as of https://github.com/JuliaLang/julia/pull/54341
33+
# field rettype added in https://github.com/JuliaLang/julia/pull/54655
34+
# field has_image_globalref added in https://github.com/JuliaLang/julia/pull/57433
2335
# CodeInfo constructor. TODO: Should be in Core
2436
let
2537
fns = fieldnames(Core.CodeInfo)
2638
fts = fieldtypes(Core.CodeInfo)
2739
conversions = [:(convert($t, $n)) for (t,n) in zip(fts, fns)]
2840

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)
41+
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)
42+
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)
3143

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

143163
debuginfo = finish_ir_debug_info!(current_codelocs_stack)
144164

165+
has_image_globalref = any(codeinfo_has_image_globalref, stmts)
166+
145167
# TODO: Set ssaflags based on call site annotations:
146168
# - @inbounds annotations
147169
# - call site @inline / @noinline
@@ -172,6 +194,7 @@ function to_code_info(ex, mod, funcname, slots)
172194
max_world = typemax(Csize_t)
173195
isva = false
174196
inlining_cost = 0xffff
197+
rettype = Any
175198

176199
_CodeInfo(
177200
stmts,
@@ -181,14 +204,16 @@ function to_code_info(ex, mod, funcname, slots)
181204
slotnames,
182205
slotflags,
183206
slottypes,
207+
rettype,
184208
parent,
185-
method_for_inference_limit_heuristics,
186209
edges,
187210
min_world,
188211
max_world,
212+
method_for_inference_limit_heuristics,
189213
nargs,
190214
propagate_inbounds,
191215
has_fcall,
216+
has_image_globalref,
192217
nospecializeinfer,
193218
isva,
194219
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(mod, s) = JuliaLowering.to_lowered_expr(
51+
mod, JuliaLowering.lower(
52+
mod, parsestmt(JuliaLowering.SyntaxTree, s)))
53+
@test elower(test_mod, "x + y").args[1].has_image_globalref === false
54+
@test elower(Main, "x + y").args[1].has_image_globalref === true
55+
end
56+
4957
end

0 commit comments

Comments
 (0)