Skip to content

Commit ecc8167

Browse files
Copilotyebaigithub-actions[bot]CompatHelper Juliawilltebbutt
authored
Add Julia v1.12 compatibility (#714)
* Initial plan * Add Julia v1.12 compatibility for BBCode and IR infrastructure Co-authored-by: yebai <[email protected]> * Add 'pre' version to CI workflow Signed-off-by: Hong Ge <[email protected]> * Fix inlining_policy function name for Julia v1.12+ compatibility and format code Co-authored-by: yebai <[email protected]> * CompatHelper: bump compat for JET in [weakdeps] to 0.10, (keep existing compat) (#691) * CompatHelper: bump compat for JET in [weakdeps] to 0.10, (keep existing compat) * Set tangent_type for AbstractLogger to NoTangent (#710) * Initial plan * Set tangent_type for Logging.ConsoleLogger to NoTangent Co-authored-by: yebai <[email protected]> * Apply suggestions from code review Signed-off-by: Hong Ge <[email protected]> * Add ConsoleLogger test to verify AbstractLogger tangent type coverage Co-authored-by: yebai <[email protected]> * Add Base.CoreLogging.SimpleLogger to type union Signed-off-by: Hong Ge <[email protected]> * Update test/tangents.jl Signed-off-by: Hong Ge <[email protected]> --------- Signed-off-by: Hong Ge <[email protected]> Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: yebai <[email protected]> * Add better error messages for tangent accessors when types are passed (#707) * Initial plan * Add better error messages for tangent accessors when types are passed Co-authored-by: yebai <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: Hong Ge <[email protected]> Co-authored-by: yebai <[email protected]> * Tidy up (#715) * Tidy up error-generating methods * Remove redundant tests * Remove more redundant test cases * Bump patch version * add rule for complex logdet (#664) * working complex logdet * bump version * proper getrf! tests * improve tests * move tests * generalize tests * formatting and version --------- Signed-off-by: Hong Ge <[email protected]> Signed-off-by: Mateus Araújo <[email protected]> Co-authored-by: Hong Ge <[email protected]> * Delete .github/copilot-instructions.md (#722) Signed-off-by: Hong Ge <[email protected]> * Uncomment b_binv_test_case for PlanarLayer (#727) Signed-off-by: Hong Ge <[email protected]> * Add DiffEq integration test for Mooncake (#726) * Initial plan * Add DiffEq integration test with original example from issue Co-authored-by: yebai <[email protected]> * Finalize DiffEq integration test with comprehensive structure Co-authored-by: yebai <[email protected]> * Remove @info and @test_broken statements to reduce clutter Co-authored-by: yebai <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: yebai <[email protected]> * fix formatting (#728) * Fix DiffEq integration test by restricting to reverse mode only (#731) * Initial plan * Fix DiffEq integration test by testing reverse mode only Co-authored-by: yebai <[email protected]> * formatting --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: yebai <[email protected]> Co-authored-by: Hong Ge <[email protected]> * Fix IR docs duplication and improve documentation structure (#718) * Initial plan * Update IR docs title and remove duplication, add comparison section Co-authored-by: yebai <[email protected]> * Address PR feedback: update title and simplify Optics reference Co-authored-by: yebai <[email protected]> * Restore IRCode field details that were incorrectly removed Co-authored-by: yebai <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: yebai <[email protected]> * Add robust Julia version-dependent JET version handling to CI workflow (#733) * Fix changelog version (#693) * remove `const M` (#696) * remove const M * fix formatting * Update Project.toml Signed-off-by: Hong Ge <[email protected]> --------- Signed-off-by: Hong Ge <[email protected]> * Update README.md (#697) Signed-off-by: Hong Ge <[email protected]> * minimal print io output (#699) * minimal print * vers bump * remove prev tests * include show in derived * Update algorithmic_differentiation.md (#703) I noticed a typo. I likely won't look at this again for a week or so, so please feel free to merge for me. Signed-off-by: Will Tebbutt <[email protected]> * Fix 660 (#705) * Fix + tests * Bump patch version * Use _stable_typeof directly * Comment on the use of _stable_typeof * Extend arrayify for complex numbers (#706) * Fix + tests * Bump patch version * Use _stable_typeof directly * Add arrayify tests * Widen permitted types * Bump patch version again * Formatting * Uncomment blas tests * Create contribution guidelines in copilot-instructions.md (#713) Added contribution guidelines for the automatic differentiation package. Signed-off-by: Hong Ge <[email protected]> * Set tangent_type for AbstractLogger to NoTangent (#710) * Initial plan * Set tangent_type for Logging.ConsoleLogger to NoTangent Co-authored-by: yebai <[email protected]> * Apply suggestions from code review Signed-off-by: Hong Ge <[email protected]> * Add ConsoleLogger test to verify AbstractLogger tangent type coverage Co-authored-by: yebai <[email protected]> * Add Base.CoreLogging.SimpleLogger to type union Signed-off-by: Hong Ge <[email protected]> * Update test/tangents.jl Signed-off-by: Hong Ge <[email protected]> --------- Signed-off-by: Hong Ge <[email protected]> Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: yebai <[email protected]> * Add better error messages for tangent accessors when types are passed (#707) * Initial plan * Add better error messages for tangent accessors when types are passed Co-authored-by: yebai <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: Hong Ge <[email protected]> Co-authored-by: yebai <[email protected]> * Tidy up (#715) * Tidy up error-generating methods * Remove redundant tests * Remove more redundant test cases * Bump patch version * add rule for complex logdet (#664) * working complex logdet * bump version * proper getrf! tests * improve tests * move tests * generalize tests * formatting and version --------- Signed-off-by: Hong Ge <[email protected]> Signed-off-by: Mateus Araújo <[email protected]> Co-authored-by: Hong Ge <[email protected]> * Delete .github/copilot-instructions.md (#722) Signed-off-by: Hong Ge <[email protected]> * Uncomment b_binv_test_case for PlanarLayer (#727) Signed-off-by: Hong Ge <[email protected]> * Add DiffEq integration test for Mooncake (#726) * Initial plan * Add DiffEq integration test with original example from issue Co-authored-by: yebai <[email protected]> * Finalize DiffEq integration test with comprehensive structure Co-authored-by: yebai <[email protected]> * Remove @info and @test_broken statements to reduce clutter Co-authored-by: yebai <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: yebai <[email protected]> * fix formatting (#728) * Fix DiffEq integration test by restricting to reverse mode only (#731) * Initial plan * Fix DiffEq integration test by testing reverse mode only Co-authored-by: yebai <[email protected]> * formatting --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: yebai <[email protected]> Co-authored-by: Hong Ge <[email protected]> * Fix IR docs duplication and improve documentation structure (#718) * Initial plan * Update IR docs title and remove duplication, add comparison section Co-authored-by: yebai <[email protected]> * Address PR feedback: update title and simplify Optics reference Co-authored-by: yebai <[email protected]> * Restore IRCode field details that were incorrectly removed Co-authored-by: yebai <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: yebai <[email protected]> * Initial plan * Add Julia version-dependent JET version selection to CI workflow Co-authored-by: yebai <[email protected]> * Improve JET version handling to support complex constraints and remove need for explicit version on Julia 1.12+ Co-authored-by: yebai <[email protected]> * Apply suggestions from code review Signed-off-by: Hong Ge <[email protected]> * Update CI.yml Signed-off-by: Hong Ge <[email protected]> --------- Signed-off-by: Hong Ge <[email protected]> Signed-off-by: Will Tebbutt <[email protected]> Signed-off-by: Mateus Araújo <[email protected]> Co-authored-by: Penelope Yong <[email protected]> Co-authored-by: Hong Ge <[email protected]> Co-authored-by: Astitva Aggarwal <[email protected]> Co-authored-by: Will Tebbutt <[email protected]> Co-authored-by: Copilot <[email protected]> Co-authored-by: Hong Ge <[email protected]> Co-authored-by: Mateus Araújo <[email protected]> --------- Signed-off-by: Hong Ge <[email protected]> Signed-off-by: Mateus Araújo <[email protected]> Signed-off-by: Will Tebbutt <[email protected]> Co-authored-by: CompatHelper Julia <[email protected]> Co-authored-by: Copilot <[email protected]> Co-authored-by: yebai <[email protected]> Co-authored-by: Hong Ge <[email protected]> Co-authored-by: Will Tebbutt <[email protected]> Co-authored-by: Mateus Araújo <[email protected]> Co-authored-by: Penelope Yong <[email protected]> Co-authored-by: Astitva Aggarwal <[email protected]> * Prevent exporting names already declared as public This is disallowed by the language and results in an error. * Extend required interface CC.add_edges_impl! * Adapt to stackless inference change (using CC.Future etc) * Always return a boolean from `src_inlining_policy` * Fix relocation of a few Experimental functions to CC * Fix `_ir_abstract_constant_propagation` rename * Adjust to `ir_abstract_constant_propagation` changes * Support `invoke` CodeInstance arguments * Adjust to opaque macro behavior change * Define better slotnames for debugging * Fix opaque closure construction * Adjust to 1.12 opaque closure creation change * More fixes * Fix IRCode construction bug * Adjust to binding changes for primal type extraction * Rules for new intrinsics * Set appropriate world bounds on the `CodeInfo` * Optimize opaque closures * Avoid failures on <1.12 * Don't attempt to set bounds if no inferred code is available * More <1.12 fixes * Don't optimize if IR interp can't be performed * More fixes/tests * Revert use of `Base.allocations` It is not defined on <1.12, and the macro form seems fine. * Fix bad line info information * Make line info logic more robust, fix a few more things * [WIP] add rules for `svec` and `_svec_len` * Don't add _svec_len rule for < 1.12 * Try to avoid error in IRInterp * Allow type unstability for `compute_oc_signature` * Attempt to fix allocations * Still use the same Tuple type for < 1.12 * Unconditionally set ir.argtypes[1] to an accurate Tuple * Fix `primal_type` bug * Fix construction error for IRCode * Fix more issues * Discard unwanted change * Make version check static * Add some @zero_derivatives for logging (#815) * Mark Base.fixup_stdlib_path as having @zero_derivative This comes up because in Julia 1.12 logmsg_code gets the folder of Core.Compiler if needed to resolve paths, which results in a ccall with no known rule. See JuliaLang/julia#57274. * Make Base.CoreLogging.handle_message_nothrow as having @zero_derivative This comes up in logmsg_code, because in Julia v1.12 handle_message_nothrow got a try-catch structure, which creates an UpsilonNode that Mooncake can't handle. * Update avoiding_non_differentiable_code.jl Signed-off-by: Hong Ge <[email protected]> * fix format * Fix handle_message_nothrow @zero_derivative rule * Improve a comment * Fix duplicate svec rule after merge * Remove JET version adjustment from CI workflow Removed JET version adjustment step based on Julia version from CI workflow. Signed-off-by: Hong Ge <[email protected]> * Fix typo in 1.12- * Also test buildkit and ext on 1.12? * Use an eval to avoid duplicating BBCode * Remove useless line (thanks Copilot) * Update get_primal_type for GlobalRef * Update isva comment * Require Julia 1.12.1 or newer * Fix stdin reference not working in 1.11 * Update patch_for_319 following Julia 1.12 changes * Binding partitions are lazily populated! * Set the IR's valid worlds to only the interpreter world (#832) * Try to set the IR valid worlds to only the interpreter world * Add explanation to set_valid_worlds! * Tweak comment * Update dispatch_doctor.jl Signed-off-by: Hong Ge <[email protected]> * Import _Utils from DispatchDoctor in tests Signed-off-by: Hong Ge <[email protected]> * reformat * Use a single world for inference * Disable JET tests for Julia 1.12 temporarily. Julia 1.12 has a few Base functions that are not type-stable, causing false-positive JET test failures in Mooncake. Signed-off-by: Hong Ge <[email protected]> * re-enable JET test for Julia 1.12 and format. * Mark compute_oc_signature as unstable Signed-off-by: Hong Ge <[email protected]> * Add documentation for resolve_unbound_globalrefs function Co-authored-by: yebai <[email protected]> * Add unstable block for version-specific functions Signed-off-by: Hong Ge <[email protected]> * Update allocation tests for Julia version compatibility Signed-off-by: Hong Ge <[email protected]> * Update frule allocation tests for Julia version compatibility * refactor count_allocs * Update JET version to include 0.11 (#841) Signed-off-by: Hong Ge <[email protected]> * fix allocation counts for primals (plus `_new_` frule) (#843) * Fix allocation counters for `sum` and `rand` primals This fixes: - `sum(abs2, randn(11))` on 1.12 - `rand(rng, Float64)` on 1.10 and 1.11 * Use `eval` instead * Fix for `_new_` (I think) * Just get rid of the varargs in count_allocs * Add a warning * Fix all the horrible edge cases * Final fixes (please don't let me down) * Format * Skip certain stability and performance tests for 1.12 (#844) * Refactor stability rules for Float types in Julia Updated stability and allocation rules for Float types based on Julia version. Signed-off-by: Hong Ge <[email protected]> * Remove Float16 cases from test cases Signed-off-by: Hong Ge <[email protected]> * Update avoiding_non_differentiable_code.jl Signed-off-by: Hong Ge <[email protected]> * reformat --------- Signed-off-by: Hong Ge <[email protected]> * Clarify GlobalRef resolution docs Signed-off-by: Hong Ge <[email protected]> * Fix for TwicePrecision allocs (#845) * Add rules for LAPACK.lacpy! and BLAS.trsv! (#839) * Add rules for LAPACK.lacpy! * LAPACK.lacpy! is exclusive to Julia 1.11+ * Add rule for BLAS.trsv! * Isolate sensitive trsm! rule --------- Co-authored-by: Hong Ge <[email protected]> * re-enable F16, only F32 and F64 are type unstable on 1.12 Signed-off-by: Hong Ge <[email protected]> * Update Project.toml Co-authored-by: Markus Hauru <[email protected]> Signed-off-by: Bruno Ploumhans <[email protected]> --------- Signed-off-by: Hong Ge <[email protected]> Signed-off-by: Mateus Araújo <[email protected]> Signed-off-by: Will Tebbutt <[email protected]> Signed-off-by: Bruno Ploumhans <[email protected]> Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: yebai <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: CompatHelper Julia <[email protected]> Co-authored-by: Hong Ge <[email protected]> Co-authored-by: Will Tebbutt <[email protected]> Co-authored-by: Mateus Araújo <[email protected]> Co-authored-by: Penelope Yong <[email protected]> Co-authored-by: Astitva Aggarwal <[email protected]> Co-authored-by: serenity4 <[email protected]> Co-authored-by: Markus Hauru <[email protected]> Co-authored-by: Bruno Ploumhans <[email protected]>
1 parent fa23279 commit ecc8167

23 files changed

+985
-167
lines changed

.buildkite/pipeline.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ steps:
2222
matrix:
2323
setup:
2424
version:
25-
- "1.11"
2625
- "1.10"
26+
- "1.11"
27+
- "1.12"
2728
label:
2829
- "cuda"
2930
- "nnlib"

.github/workflows/CI.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ jobs:
4949
version:
5050
- 'lts'
5151
- '1.11'
52+
- '1.12'
5253
arch:
5354
- x64
5455
include:
@@ -108,8 +109,9 @@ jobs:
108109
{test_type: 'integration_testing', label: 'flux'},
109110
]
110111
version:
111-
- '1.11'
112112
- 'lts'
113+
- '1.11'
114+
- '1.12'
113115
arch:
114116
- x64
115117
steps:

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Flux = "0.16.3"
5858
FunctionWrappers = "1.1.3"
5959
GPUArraysCore = "0.1, 0.2"
6060
Graphs = "1"
61-
JET = "0.9, 0.10"
61+
JET = "0.9, 0.10, 0.11"
6262
LinearAlgebra = "1"
6363
Logging = "1"
6464
LuxLib = "1.11"
@@ -72,7 +72,7 @@ SpecialFunctions = "2"
7272
StableRNGs = "1"
7373
Static = "1.1.1"
7474
Test = "1"
75-
julia = "~1.10.8, 1.11.6"
75+
julia = "~1.10.8, 1.11.6, 1.12.1"
7676

7777
[extras]
7878
AllocCheck = "9b6a8646-10ed-4001-bbdc-1d2f46dfbb1a"

ext/MooncakeFunctionWrappersExt.jl

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ end
8080
function _function_wrapper_tangent(R, obj::Tobj, A, obj_tangent) where {Tobj}
8181

8282
# Analyse types.
83-
_, _, fwd_sig, rvs_sig = _construct_types(R, A)
83+
fwd_oc_type, rvs_oc_type, _, _ = _construct_types(R, A)
84+
(fwd_sig, fwd_ret) = fwd_oc_type.parameters
85+
(rvs_sig, rvs_ret) = rvs_oc_type.parameters
8486

8587
# Construct reference to obj_tangent that we can read / write-to.
8688
obj_tangent_ref = Ref{tangent_type(Tobj)}(obj_tangent)
@@ -93,17 +95,33 @@ function _function_wrapper_tangent(R, obj::Tobj, A, obj_tangent) where {Tobj}
9395
pb_stack = Stack{pullback_type(typeof(rule), (Tobj, A.parameters...))}()
9496

9597
# Construct reverse-pass. Note: this closes over `pb_stack`.
96-
run_rvs_pass = Base.Experimental.@opaque rvs_sig dy -> begin
97-
obj_rdata, dx... = pop!(pb_stack)(dy)
98-
obj_tangent_ref[] = increment_rdata!!(obj_tangent_ref[], obj_rdata)
99-
return NoRData(), dx...
98+
@static if VERSION v"1.12-"
99+
run_rvs_pass = Base.Experimental.@opaque rvs_sig -> rvs_ret dy -> begin
100+
obj_rdata, dx... = pop!(pb_stack)(dy)
101+
obj_tangent_ref[] = increment_rdata!!(obj_tangent_ref[], obj_rdata)
102+
return NoRData(), dx...
103+
end
104+
else
105+
run_rvs_pass = Base.Experimental.@opaque rvs_sig dy -> begin
106+
obj_rdata, dx... = pop!(pb_stack)(dy)
107+
obj_tangent_ref[] = increment_rdata!!(obj_tangent_ref[], obj_rdata)
108+
return NoRData(), dx...
109+
end
100110
end
101111

102112
# Construct fowards-pass. Note: this closes over the reverse-pass and `pb_stack`.
103-
run_fwds_pass = Base.Experimental.@opaque fwd_sig (x...) -> begin
104-
y, pb = rule(CoDual(obj, fdata(obj_tangent_ref[])), x...)
105-
push!(pb_stack, pb)
106-
return y, run_rvs_pass
113+
@static if VERSION v"1.12-"
114+
run_fwds_pass = Base.Experimental.@opaque fwd_sig -> fwd_ret (x...) -> begin
115+
y, pb = rule(CoDual(obj, fdata(obj_tangent_ref[])), x...)
116+
push!(pb_stack, pb)
117+
return y, run_rvs_pass
118+
end
119+
else
120+
run_fwds_pass = Base.Experimental.@opaque fwd_sig (x...) -> begin
121+
y, pb = rule(CoDual(obj, fdata(obj_tangent_ref[])), x...)
122+
push!(pb_stack, pb)
123+
return y, run_rvs_pass
124+
end
107125
end
108126

109127
t = FunctionWrapperTangent(run_fwds_pass, obj_tangent_ref)

src/Mooncake.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ include("public.jl")
161161
end
162162
#! format: on
163163

164-
@public Config, value_and_pullback!!, prepare_pullback_cache, value_and_derivative!!
165-
@public prepare_derivative_cache, Dual
164+
@public Config, value_and_pullback!!, prepare_pullback_cache
165+
@public Dual
166166

167167
# Public, exported
168168
export value_and_gradient!!, prepare_gradient_cache, value_and_derivative!!

src/interpreter/abstract_interpretation.jl

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# in conjunction with the contexts above to decide what should be inlined and what should
33
# not be inlined. Similar strategies are employed by Enzyme and Diffractor.
44

5-
# The most important bit of this code is `inlining_policy` -- the rest is copy + pasted
5+
# The most important bit of this code is `inlining_policy` (renamed to `src_inlining_policy` in Julia v1.12+) -- the rest is copy + pasted
66
# boiler plate, largely taken from https://github.com/JuliaLang/julia/blob/2fe4190b3d26b4eee52b2b1b1054ddd6e38a941e/test/compiler/newinterp.jl#L11
77
#
88
# Credit: much of the code in here is copied over from the main Julia repo, and from
@@ -121,6 +121,11 @@ end
121121
CC.nsplit_impl(info::NoInlineCallInfo) = CC.nsplit(info.info)
122122
CC.getsplit_impl(info::NoInlineCallInfo, idx::Int) = CC.getsplit(info.info, idx)
123123
CC.getresult_impl(info::NoInlineCallInfo, idx::Int) = CC.getresult(info.info, idx)
124+
@static if VERSION > v"1.12-"
125+
CC.add_edges_impl(edges::Vector{Any}, info::NoInlineCallInfo) = CC.add_edges!(
126+
edges, info.info
127+
)
128+
end
124129

125130
function Core.Compiler.abstract_call_gf_by_type(
126131
interp::MooncakeInterpreter{C,M},
@@ -133,7 +138,7 @@ function Core.Compiler.abstract_call_gf_by_type(
133138
) where {C,M}
134139

135140
# invoke the default abstract call to get the default CC.CallMeta.
136-
cm = @invoke CC.abstract_call_gf_by_type(
141+
ret = @invoke CC.abstract_call_gf_by_type(
137142
interp::CC.AbstractInterpreter,
138143
f::Any,
139144
arginfo::CC.ArgInfo,
@@ -142,16 +147,25 @@ function Core.Compiler.abstract_call_gf_by_type(
142147
sv::CC.AbsIntState,
143148
max_methods::Int,
144149
)
150+
is_primitive(C, M, atype) || return ret
151+
# Insert a `NoInlineCallInfo` to prevent any potential inlining.
152+
@static if VERSION < v"1.12-"
153+
call = ret::CC.CallMeta
154+
info = NoInlineCallInfo(call.info, atype)
155+
return rewrap_callmeta(call, info)
156+
else
157+
return CC.Future{CC.CallMeta}(ret::CC.Future, interp, sv) do call, interp, sv
158+
info = NoInlineCallInfo(call.info, atype)
159+
return rewrap_callmeta(call, info)
160+
end
161+
end
162+
end
145163

146-
# Check to see whether the call in question is a Mooncake primitive. If it is, set its
147-
# call info such that in the `CC.inlining_policy` it is not inlined away.
148-
callinfo = is_primitive(C, M, atype) ? NoInlineCallInfo(cm.info, atype) : cm.info
149-
150-
# Construct a CallMeta correctly depending on the version of Julia.
164+
function rewrap_callmeta(call::CC.CallMeta, info::CC.CallInfo)
151165
@static if VERSION v"1.11-"
152-
return CC.CallMeta(cm.rt, cm.exct, cm.effects, callinfo)
166+
return CC.CallMeta(call.rt, call.exct, call.effects, info)
153167
else
154-
return CC.CallMeta(cm.rt, cm.effects, callinfo)
168+
return CC.CallMeta(call.rt, call.effects, info)
155169
end
156170
end
157171

@@ -179,7 +193,7 @@ end
179193
)
180194
end
181195

182-
else # 1.11 and up.
196+
elseif VERSION < v"1.12-" # 1.11
183197
function CC.inlining_policy(
184198
interp::MooncakeInterpreter,
185199
@nospecialize(src),
@@ -194,6 +208,22 @@ else # 1.11 and up.
194208
interp::CC.AbstractInterpreter, src::Any, info::CC.CallInfo, stmt_flag::UInt32
195209
)
196210
end
211+
212+
else # 1.12 and up.
213+
function CC.src_inlining_policy(
214+
interp::MooncakeInterpreter,
215+
@nospecialize(src),
216+
@nospecialize(info::CC.CallInfo),
217+
stmt_flag::UInt32,
218+
)
219+
# Do not inline away primitives.
220+
info isa NoInlineCallInfo && return false
221+
222+
# If not a primitive, AD doesn't care about it. Use the usual inlining strategy.
223+
return @invoke CC.src_inlining_policy(
224+
interp::CC.AbstractInterpreter, src::Any, info::CC.CallInfo, stmt_flag::UInt32
225+
)
226+
end
197227
end
198228

199229
"""

src/interpreter/bbcode.jl

Lines changed: 109 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -272,45 +272,57 @@ These should be assumed to be ordered.
272272
"""
273273
collect_stmts(bb::BBlock)::Vector{IDInstPair} = collect(zip(bb.inst_ids, bb.insts))
274274

275-
"""
276-
BBCode(
275+
@eval begin
276+
"""
277+
BBCode(
278+
blocks::Vector{BBlock}
279+
argtypes::Vector{Any}
280+
sptypes::Vector{CC.VarState}
281+
linetable::Vector{Core.LineInfoNode} (v1.11 and lower)
282+
debuginfo::CC.DebugInfoStream (v1.12+)
283+
meta::Vector{Expr}
284+
valid_worlds::CC.WorldRange (v1.12+)
285+
)
286+
287+
A `BBCode` is a data structure which is similar to `IRCode`, but adds additional structure.
288+
289+
In particular, a `BBCode` comprises a sequence of basic blocks (`BBlock`s), each of which
290+
comprise a sequence of statements. Moreover, each `BBlock` has its own unique `ID`, as does
291+
each statment.
292+
293+
The consequence of this is that new basic blocks can be inserted into a `BBCode`. This is
294+
distinct from `IRCode`, in which to create a new basic block, one must insert additional
295+
statments which you know will create a new basic block -- this is generally quite an
296+
unreliable process, while inserting a new `BBlock` into `BBCode` is entirely predictable.
297+
Furthermore, inserting a new `BBlock` does not change the `ID` associated to the other
298+
blocks, meaning that you can safely assume that references from existing basic block
299+
terminators / phi nodes to other blocks will not be modified by inserting a new basic block.
300+
301+
Additionally, since each statment in each basic block has its own unique `ID`, new
302+
statments can be inserted without changing references between other blocks. `IRCode` also
303+
has some support for this via its `new_nodes` field, but eventually all statements will be
304+
renamed upon `compact!`ing the `IRCode`, meaning that the name of any given statement will
305+
eventually change.
306+
307+
Finally, note that the basic blocks in a `BBCode` support the custom `Switch` statement.
308+
This statement is not valid in `IRCode`, and is therefore lowered into a collection of
309+
`GotoIfNot`s and `GotoNode`s when a `BBCode` is converted back into an `IRCode`.
310+
"""
311+
struct BBCode
277312
blocks::Vector{BBlock}
278313
argtypes::Vector{Any}
279314
sptypes::Vector{CC.VarState}
280-
linetable::Vector{Core.LineInfoNode}
315+
$(
316+
if VERSION > v"1.12-"
317+
:(debuginfo::CC.DebugInfoStream)
318+
else
319+
:(linetable::Vector{Core.LineInfoNode})
320+
end
321+
)
281322
meta::Vector{Expr}
282-
)
283-
284-
A `BBCode` is a data structure which is similar to `IRCode`, but adds additional structure.
285-
286-
In particular, a `BBCode` comprises a sequence of basic blocks (`BBlock`s), each of which
287-
comprise a sequence of statements. Moreover, each `BBlock` has its own unique `ID`, as does
288-
each statment.
289-
290-
The consequence of this is that new basic blocks can be inserted into a `BBCode`. This is
291-
distinct from `IRCode`, in which to create a new basic block, one must insert additional
292-
statments which you know will create a new basic block -- this is generally quite an
293-
unreliable process, while inserting a new `BBlock` into `BBCode` is entirely predictable.
294-
Furthermore, inserting a new `BBlock` does not change the `ID` associated to the other
295-
blocks, meaning that you can safely assume that references from existing basic block
296-
terminators / phi nodes to other blocks will not be modified by inserting a new basic block.
297-
298-
Additionally, since each statment in each basic block has its own unique `ID`, new
299-
statments can be inserted without changing references between other blocks. `IRCode` also
300-
has some support for this via its `new_nodes` field, but eventually all statements will be
301-
renamed upon `compact!`ing the `IRCode`, meaning that the name of any given statement will
302-
eventually change.
303-
304-
Finally, note that the basic blocks in a `BBCode` support the custom `Switch` statement.
305-
This statement is not valid in `IRCode`, and is therefore lowered into a collection of
306-
`GotoIfNot`s and `GotoNode`s when a `BBCode` is converted back into an `IRCode`.
307-
"""
308-
struct BBCode
309-
blocks::Vector{BBlock}
310-
argtypes::Vector{Any}
311-
sptypes::Vector{CC.VarState}
312-
linetable::Vector{Core.LineInfoNode}
313-
meta::Vector{Expr}
323+
$(VERSION > v"1.12-" ? :(valid_worlds::CC.WorldRange) : nothing)
324+
end
325+
export BBCode
314326
end
315327

316328
"""
@@ -319,14 +331,27 @@ end
319331
Make a new `BBCode` whose `blocks` is given by `new_blocks`, and fresh copies are made of
320332
all other fields from `ir`.
321333
"""
322-
function BBCode(ir::Union{IRCode,BBCode}, new_blocks::Vector{BBlock})
323-
return BBCode(
324-
new_blocks,
325-
CC.copy(ir.argtypes),
326-
CC.copy(ir.sptypes),
327-
CC.copy(ir.linetable),
328-
CC.copy(ir.meta),
329-
)
334+
@static if VERSION > v"1.12-"
335+
function BBCode(ir::Union{IRCode,BBCode}, new_blocks::Vector{BBlock})
336+
return BBCode(
337+
new_blocks,
338+
CC.copy(ir.argtypes),
339+
CC.copy(ir.sptypes),
340+
CC.copy(ir.debuginfo),
341+
CC.copy(ir.meta),
342+
ir.valid_worlds,
343+
)
344+
end
345+
else
346+
function BBCode(ir::Union{IRCode,BBCode}, new_blocks::Vector{BBlock})
347+
return BBCode(
348+
new_blocks,
349+
CC.copy(ir.argtypes),
350+
CC.copy(ir.sptypes),
351+
CC.copy(ir.linetable),
352+
CC.copy(ir.meta),
353+
)
354+
end
330355
end
331356

332357
# Makes use of the above outer constructor for `BBCode`.
@@ -640,20 +665,47 @@ function CC.IRCode(bb_code::BBCode)
640665
insts = _ids_to_line_numbers(bb_code)
641666
cfg = control_flow_graph(bb_code)
642667
insts = _lines_to_blocks(insts, cfg)
643-
return IRCode(
644-
CC.InstructionStream(
645-
map(x -> x.stmt, insts),
646-
map(x -> x.type, insts),
647-
map(x -> x.info, insts),
648-
map(x -> x.line, insts),
649-
map(x -> x.flag, insts),
650-
),
651-
cfg,
652-
CC.copy(bb_code.linetable),
653-
CC.copy(bb_code.argtypes),
654-
CC.copy(bb_code.meta),
655-
CC.copy(bb_code.sptypes),
656-
)
668+
@static if VERSION > v"1.12-"
669+
lines = CC.copy(bb_code.debuginfo.codelocs)
670+
n = length(insts)
671+
if length(lines) > 3n
672+
resize!(lines, 3n)
673+
elseif length(lines) < 3n
674+
for _ in (length(lines) + 1):3n
675+
push!(lines, 0)
676+
end
677+
end
678+
return IRCode(
679+
CC.InstructionStream(
680+
Any[x.stmt for x in insts],
681+
Any[x.type for x in insts],
682+
CC.CallInfo[x.info for x in insts],
683+
lines,
684+
UInt32[x.flag for x in insts],
685+
),
686+
cfg,
687+
CC.copy(bb_code.debuginfo),
688+
CC.copy(bb_code.argtypes),
689+
CC.copy(bb_code.meta),
690+
CC.copy(bb_code.sptypes),
691+
bb_code.valid_worlds,
692+
)
693+
else
694+
return IRCode(
695+
CC.InstructionStream(
696+
Any[x.stmt for x in insts],
697+
Any[x.type for x in insts],
698+
CC.CallInfo[x.info for x in insts],
699+
Int32[x.line for x in insts],
700+
UInt32[x.flag for x in insts],
701+
),
702+
cfg,
703+
CC.copy(bb_code.linetable),
704+
CC.copy(bb_code.argtypes),
705+
CC.copy(bb_code.meta),
706+
CC.copy(bb_code.sptypes),
707+
)
708+
end
657709
end
658710

659711
"""

0 commit comments

Comments
 (0)