Skip to content

Chunk large in-place RHS functions in build_function_wrapper to keep first-call compile near linear - #5308

Merged
AayushSabharwal merged 22 commits into
SciML:masterfrom
xtalax-droid:forge/rhs-chunking
Nov 4, 2026
Merged

AayushSabharwal merged 22 commits into
SciML:masterfrom
xtalax-droid:forge/rhs-chunking

Conversation

@xtalax-droid

@xtalax-droid xtalax-droid commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Compiling one monolithic generated function is superlinear in its size. For scalarized PDE discretizations (MethodOfLines weno_burgers1d_large, about 59 IR nodes per grid point) the first RHS call on master takes 3/10/28/79 s at n=250/500/1000/2000, mostly in eval/Meta.lower of the huge expression.

build_function_wrapper now splits a large in-place function whose output is a Vector{SymbolicT} into slices of the output, using the number of reachable IR nodes (length(ir)) as the size measure. Each slice is generated by a recursive call of the same wrapper (with outputidxs set to the slice) as an independent anonymous function over its own sub-IR, and the in-place function becomes a sequence of immediate calls of those helpers followed by out. There is no Expr-level analysis of the generated body, and nothing is passed between helpers. This replaces the earlier eval_or_rgf rewrite (it deleted function_chunking.jl, CompilerOptions.chunk_size and the soundness grammar); the diff against master is now about +245/-4.

Where it is on. BuildFunctionWrapperOptions(; chunk_size = 0) is off by default (-1 automatic: over 20000 IR nodes, helpers of about 500 nodes; a positive value splits any function over that many nodes into helpers of about that size). Only generate_rhs passes -1, because the generated in-place RHS is called as f(du, u, p, t) with du distinct from u and p by the SciML interface convention. Observed functions, Jacobians and any other build_function caller are unchanged. A chunked in-place function requires that out does not alias any input (helpers read inputs after earlier helpers wrote out); this is stated in the chunk_size docstring. Aliased calls through the default (unchunked) path give the same results as before; there is a test for that.

Declines. Chunking is skipped for a non-Vector{SymbolicT} output, Tuple output, DDE delays, create_bindings, compress_args, extra_assignments, a preface, non-identity wrap_code, explicit outputidxs, mkarray, and iip_config other than (true, true). In automatic mode it also declines when the slices' sub-IRs together exceed 1.25x the full IR, since helpers recompute shared work (weno: about 1.11; brusselator2d and grayscott2d: 1.5 and above, so those stay unchunked).

RuntimeGeneratedFunctions compatibility. RuntimeGeneratedFunctions 0.5.12 and 0.5.13, both inside the declared [compat], cannot compile functions that contain local function definitions on Julia 1.12 (syntax: malformed expression). A one-off cached probe disables chunking when the loaded version cannot compile the emitted shape, so those users get no chunking instead of an error. [compat] is unchanged.

Evidence (MethodOfLines corpus, fresh process each, on a shared box so compare within rows; master is fe189a7ec7). First RHS call in seconds, warm RHS in microseconds in brackets:

system master this PR
weno n=250 3.15 (2.3) 3.04 (2.3)
weno n=500 10.54 (4.7) 4.04 (4.3)
weno n=1000 28.35 (12.0) 7.87 (9.3)
weno n=2000 79.04 (41.2) 14.87 (36.7)
brusselator N=32 7.1 (3.8) 5.98 (4.1)
brusselator N=64 63.81 (34.1) 59.01 (32.6)
grayscott 12.62 (5.7) 11.14 (5.6)

prob.f output is bitwise identical to master on random u0 for every row. Peak RSS at weno n=2000 drops from 3.57 to 3.0 GB.

Tests. New lib/ModelingToolkitBase/test/function_chunking.jl (registered in InterfaceII): explicit chunk_size gives bitwise-equal results for both variants, small functions are left alone, chunk_size = 0 and the default never split, each decline case, the duplication guard, aliased calls through the unchunked paths, generate_rhs and ODEProblem(...).f on a 3000-equation ring matching the unchunked result bitwise, and probe-aware expectations. The file passes (7 testsets) and basic_transformations.jl passes. The InterfaceI/InterfaceII/Extended group runs were done on an earlier commit of this branch (before the alias handling was removed and the default moved to generate_rhs); InterfaceI had the one known failure components.jl:446, which also fails on master. CI on this head is the check for the groups.

Risks. The thresholds come from two real systems; the failure mode is performance only. About 11% of statements are computed twice at chunk boundaries on weno (warm RHS shows no regression). Only the in-place variant is chunked, so a user who calls only the out-of-place function, or uses eval_expression = true on RGF 0.5.12/0.5.13, gets no benefit. This is a contained interim: keeping array equations unscalarized and emitting loops (the #5306 route) would remove the need for it.


🤖 Posted by an AI agent · harness: Claude Code 2.1.309 · model: claude-sonnet-5-5
Review: unreviewed
Session: cb8da724-d117-4db6-8c52-5fb7b3215b80

xtalax and others added 10 commits October 23, 2026 04:40
First-call compile of one monolithic generated function is superlinear (~n^1.6 for scalarized PDE discretizations; a 117-statements-per-point RHS took 77 s at n=2000). eval_or_rgf now outlines the straight-line body of functions with more than 2000 statements into local capture-free helpers of 500 statements, which keeps compile time near linear while producing bit-identical results and no warm-call overhead. Controlled by CompilerOptions(chunk_size): -1 automatic, 0 off, k>0 explicit.

Agent-Harness: Claude Code 2.1.309
Agent-Model: claude-sonnet-5-5
Agent-Session: d4c19451-93a1-485d-8264-e76dcbdd3b1d
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
Address review: helpers keep the original bounds-check context (no @inbounds unless the original body had it); constant-index argument reads are only recomputed when nothing writes the argument; bodies containing closures, inner functions, macros or unknown mutation, and rebinding of arguments or of a value a later helper reads, are left unchunked; the pass never throws (falls back to the unchunked function); restore the 3-argument CompilerOptions constructor.

Agent-Harness: Claude Code 2.1.309
Agent-Model: claude-sonnet-5-5
Agent-Session: d4c19451-93a1-485d-8264-e76dcbdd3b1d
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
Replace the mutation/alias flow analysis with a strict whitelist: arguments (and plain aliases of them) may only be indexed, renamed, compared, or returned; any other bare use, compound assignment, unknown head or non-Base function object leaves the function unchunked. Automatic chunking now needs more than 20000 statements and at most 0.15 returned values per helper statement, so densely coupled bodies (brusselator2d N=32: ~29% traffic, 40% slower warm RHS) are left alone while 1D stencils (~6%) still chunk.

Agent-Harness: Claude Code 2.1.309
Agent-Model: claude-sonnet-5-5
Agent-Session: d4c19451-93a1-485d-8264-e76dcbdd3b1d
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
Reads of arguments are recomputed in helpers only when every statement using them precedes the first array write, so aliasing and views cannot matter. Callees must come from an exact allowlist of pure functions and may not be shadowed by a parameter or local; embedded Fix1/closures and other function objects leave the function unchunked.

Agent-Harness: Claude Code 2.1.309
Agent-Model: claude-sonnet-5-5
Agent-Session: d4c19451-93a1-485d-8264-e76dcbdd3b1d
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
Only literals and plain renamings of function arguments are duplicated into helpers; every read of an argument happens once, at its original statement, and is passed to later helpers as a value. This removes the ordering rule and makes recomputation hazards (alias of a read, impure getindex) impossible by construction. Broadcast calls f.(args) now go through the same callee allowlist; broadcast assignment is not whitelisted.

Agent-Harness: Claude Code 2.1.309
Agent-Model: claude-sonnet-5-5
Agent-Session: d4c19451-93a1-485d-8264-e76dcbdd3b1d
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
…Options

Helper names use a prefix that no symbol in the function starts with. The 3-argument CompilerOptions constructor accepts any argument types and forwards to the converting constructor with the default chunk_size.

Agent-Harness: Claude Code 2.1.309
Agent-Model: claude-sonnet-5-5
Agent-Session: d4c19451-93a1-485d-8264-e76dcbdd3b1d
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
A name assigned inside an if/ternary/&&/call argument is not definitely initialized, so passing it to a helper could read an undefined variable. The whitelist now accepts bindings only in top-level statements (through plain blocks, assignment right-hand sides and @inbounds); anything else leaves the function unchunked.

Agent-Harness: Claude Code 2.1.309
Agent-Model: claude-sonnet-5-5
Agent-Session: d4c19451-93a1-485d-8264-e76dcbdd3b1d
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
A bare 'local a' (or 'local a, b') declares without initializing, so passing the name to a helper could read an undefined variable. Such functions are now left unchunked.

Agent-Harness: Claude Code 2.1.309
Agent-Model: claude-sonnet-5-5
Agent-Session: d4c19451-93a1-485d-8264-e76dcbdd3b1d
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
… property test

Only the expression heads that occur in generated RHS code are accepted (=, local, block, ref, call, broadcast call, @inbounds); conditionals, tuples, type assertions and the like leave the function unchunked. Every local that is read must be bound by an earlier statement. A seeded random-function property test compares chunked and unchunked results, including thrown errors.

Agent-Harness: Claude Code 2.1.309
Agent-Model: claude-sonnet-5-5
Agent-Session: d4c19451-93a1-485d-8264-e76dcbdd3b1d
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
…lpers

RuntimeGeneratedFunctions 0.5.12 and 0.5.13 (inside the declared compat) throw 'syntax: malformed expression' for functions containing a local function definition on Julia 1.12. eval_or_rgf probes once, cached, whether the loaded package handles such a function and leaves the function unchunked if not. The eval_expression path is unaffected.

Agent-Harness: Claude Code 2.1.309
Agent-Model: claude-sonnet-5-5
Agent-Session: d4c19451-93a1-485d-8264-e76dcbdd3b1d
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>

@AayushSabharwal AayushSabharwal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't been through the entire thing in detail, but this looks like it's rebuilding at the Expr level a lot of information that already exists in IRStructure. Could it be better if build_function_wrapper recognized the size of the function (using the length of the reachable IR statements) and did the chunking there?

Comment on lines +403 to +406
# Cached result of probing whether the loaded RuntimeGeneratedFunctions can compile a function
# containing a local function definition (as chunked helpers are). RuntimeGeneratedFunctions
# 0.5.12 and 0.5.13 fail on Julia 1.12 with "syntax: malformed expression".
const _RGF_LOCAL_FUNCTIONS = Ref{Union{Nothing, Bool}}(nothing)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be better to use a compat bound instead?


_is_inbounds_call(ex) = ex isa Expr && ex.head === :macrocall && ex.args[1] === Symbol("@inbounds")

function _is_binding_free(ex)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function _is_binding_free(ex)
function _is_binding_free(@nospecialize(ex))

end

# Every symbol mentioned in `ex`, including ones that are only names of globals or functions.
function _collect_symbols!(out::Vector{Symbol}, ex)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function _collect_symbols!(out::Vector{Symbol}, ex)
function _collect_symbols!(out::Vector{Symbol}, @nospecialize(ex))

Avoids the dynamic dispatch

@xtalax-droid

Copy link
Copy Markdown
Contributor Author

Re the review on this PR: noted. Chunking inside build_function_wrapper, driven by the reachable IR statement count, is being evaluated as a replacement for the Expr-level pass; results (or a technical question) will follow here once there is a pushed state.


🤖 Posted by an AI agent · harness: Claude Code 2.1.309 · model: claude-sonnet-5-5
Review: unreviewed
Session: cb8da724-d117-4db6-8c52-5fb7b3215b80

xtalax and others added 6 commits October 23, 2026 17:29
Agent-Harness: Claude Code 2.1.309
Agent-Model: claude-sonnet-5-5
Agent-Session: cb8da724-d117-4db6-8c52-5fb7b3215b80
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
Replace the Expr-level chunking pass with a decision in build_function_wrapper driven by the
reachable IR node count: a large Vector{SymbolicT} output is split into slices, each generated
as an independent anonymous function by a recursive wrapper call. Drops function_chunking.jl
and CompilerOptions.chunk_size.

Agent-Harness: Claude Code 2.1.309
Agent-Model: claude-sonnet-5-5
Agent-Session: cb8da724-d117-4db6-8c52-5fb7b3215b80
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
…ests follow the RGF probe

Helpers run in sequence, so with out === u a later helper read values an earlier one had overwritten (unchunked code reads all inputs first). The chunked function now writes to a scratch similar(out) only when out might alias an array input, then copyto!s it back. The tests accept the intentional no-chunking fallback when the RGF probe fails and always check equality.

Agent-Harness: Claude Code 2.1.309
Agent-Model: claude-sonnet-5-5
Agent-Session: cb8da724-d117-4db6-8c52-5fb7b3215b80
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
… the alias path

The alias check now looks inside tuples, arrays of arrays and MTKParameters, and the alias path starts from a copy of out so entries the helpers skip (skipzeros) keep their values.

Agent-Harness: Claude Code 2.1.309
Agent-Model: claude-sonnet-5-5
Agent-Session: cb8da724-d117-4db6-8c52-5fb7b3215b80
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
…s inputs

Runtime alias detection over arbitrary inputs cannot be complete. Solvers never pass an output that aliases an input, so the chunked function writes straight into out and the chunk_size docstring states the contract.

Agent-Harness: Claude Code 2.1.309
Agent-Model: claude-sonnet-5-5
Agent-Session: cb8da724-d117-4db6-8c52-5fb7b3215b80
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
chunk_size now defaults to 0. generate_rhs passes automatic chunking because in-place SciML functions never alias du with u or p; other callers must opt in, which the docstring says requires a non-aliasing out.

Agent-Harness: Claude Code 2.1.309
Agent-Model: claude-sonnet-5-5
Agent-Session: cb8da724-d117-4db6-8c52-5fb7b3215b80
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
@xtalax-droid xtalax-droid changed the title Chunk very large generated functions into local helpers to keep first-call compile near linear Chunk large in-place RHS functions in build_function_wrapper to keep first-call compile near linear Oct 23, 2026
@xtalax-droid

Copy link
Copy Markdown
Contributor Author

Pushed 1bf7de6: the chunking now lives in build_function_wrapper and is driven by length(ir) (reachable IR nodes). Each slice of a large Vector{SymbolicT} output is generated by a recursive wrapper call as an independent function over its own sub-IR, so the Expr-level pass in eval_or_rgf, function_chunking.jl and CompilerOptions.chunk_size are gone (diff vs master about +245/-4, was +946). It is off by default in BuildFunctionWrapperOptions; only generate_rhs requests automatic chunking. On the MOL corpus the weno first RHS call at n=2000 goes from 79 s on master to 14.9 s, with prob.f bitwise identical to master on every system measured; brusselator2d and grayscott2d are declined by the duplication guard and unchanged. The PR title and description are updated with the details, including the non-aliasing requirement for enabled chunked functions and the RGF 0.5.12/0.5.13 probe. The InterfaceI/II/Extended group runs predate the last two commits, so CI on this head is the check for the groups.


🤖 Posted by an AI agent · harness: Claude Code 2.1.309 · model: claude-sonnet-5-5
Review: unreviewed
Session: cb8da724-d117-4db6-8c52-5fb7b3215b80

Comment on lines +810 to +813
# Whether the loaded RuntimeGeneratedFunctions can compile a function containing a local
# function definition, as chunked helpers are. Versions 0.5.12 and 0.5.13 fail on Julia 1.12
# with "syntax: malformed expression". Cached after the first probe.
const _RGF_LOCAL_FUNCTIONS = Ref{Union{Nothing, Bool}}(nothing)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should just put a compat bound on RGF or check pkgversion

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced the runtime probe with a compat bound. RuntimeGeneratedFunctions 0.5.13 fails to compile a function containing a local function definition on Julia 1.12 (syntax: malformed expression) and 0.5.14 compiles it, so lib/ModelingToolkitBase/Project.toml now requires 0.5.14. The probe and its cache are gone.


🤖 Posted by an AI agent · harness: Claude Code 2.1.312 · model: claude-sonnet-5-5
Review: unreviewed
Session: de59b94f-0227-49ea-adb5-b158639dad0b

function _chunk_ranges(ir::IRStructure{VartypeT}, expr, chunk_size::Int)
chunk_size == 0 && return nothing
expr isa AbstractVector{SymbolicT} || return nothing
nstmts = length(ir)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a correct estimate. MTK reuses the same ir in a lot of places, so it contains a lot more statements than just the ones required for generating any given function. This should use SU.get_reachability to get the reachable indices.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. The size is now the number of statements reachable from the output elements (SU.get_reachability! over the dependency graph), so unrelated work in a shared ir is not counted. The test "chunk sizes are the statements reachable from each slice" checks that the full system IR and its subset give identical ranges.


🤖 Posted by an AI agent · harness: Claude Code 2.1.312 · model: claude-sonnet-5-5
Review: unreviewed
Session: de59b94f-0227-49ea-adb5-b158639dad0b

nstmts = length(ir)
target = chunk_size < 0 ? DEFAULT_CHUNK_SIZE : chunk_size
nstmts > (chunk_size < 0 ? DEFAULT_CHUNK_THRESHOLD : target) || return nothing
per_chunk = max(1, fld(target * length(expr), nstmts))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This heuristic doesn't make sense to me. What does (chunk size) * (# array elements) / (# ir statements) measure?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That expression assumed a uniform per-element cost, which does not hold, so it is removed. Each slice now grows until its own reachable statement count reaches the target (500 in automatic mode), so the size is measured directly. The only remaining ratio is the automatic-mode guard: the sum of the slices' reachable counts must be at most 1.25 times the whole function's count.


🤖 Posted by an AI agent · harness: Claude Code 2.1.312 · model: claude-sonnet-5-5
Review: unreviewed
Session: de59b94f-0227-49ea-adb5-b158639dad0b

ranges = [i:min(i + per_chunk - 1, length(expr)) for i in 1:per_chunk:length(expr)]
length(ranges) > 1 || return nothing
if chunk_size < 0
chunked = sum(r -> length(SU.subset_ir(ir, view(expr, r))), ranges)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use get_reachability to measure the reachable statements from each subset. subset_ir does a lot of extra work, all this needs is a graph traversal.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. The duplication guard sums the per-slice reachable counts from the same get_reachability! traversals that choose the slice boundaries. subset_ir is no longer called.


🤖 Posted by an AI agent · harness: Claude Code 2.1.312 · model: claude-sonnet-5-5
Review: unreviewed
Session: de59b94f-0227-49ea-adb5-b158639dad0b

),
)
)
push!(helpers, build_function_wrapper(sys, expr[range], orig_args, helper_opts)[2])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do this chunking earlier somehow? Otherwise we do all the build_function_wrapper work on the big expression, only to discard it and repeat the work on individual chunks.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done for the in-place function. The chunk plan is made directly after the expression is inserted into the IR, and each slice goes through the wrapper on its own, with no whole-function subset_ir, optimisation or codegen for the in-place variant. When the container type is not given explicitly, the out-of-place function is the ordinary whole-function codegen, because its container is decided by the runtime arguments. Its Expr is still generated but is only compiled when called, which the default ODEProblem path with iip = true never does. Measured on weno, first RHS call: n = 1000 takes 7.9 s here against 45.1 s on master, and n = 2000 takes 15.5 s against 107 s; generating the extra out-of-place Expr was within run-to-run noise of the previous commit.


🤖 Posted by an AI agent · harness: Claude Code 2.1.312 · model: claude-sonnet-5-5
Review: unreviewed
Session: de59b94f-0227-49ea-adb5-b158639dad0b

# single concrete `CodegenFunctionOptions` type and is compiled once.
codegen_options = setproperties(opts.codegen, (; wrap_code, similarto, optimize))
if chunk_ranges !== nothing
helpers = Any[]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
helpers = Any[]
helpers = Expr[]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied, the helper vectors are Vector{Expr}.


🤖 Posted by an AI agent · harness: Claude Code 2.1.312 · model: claude-sonnet-5-5
Review: unreviewed
Session: de59b94f-0227-49ea-adb5-b158639dad0b


# Combine the in-place helper functions `helpers` (generated `:function` expressions with the
# same signature) into one function with that signature which calls each of them in turn.
function _call_chunks(helpers::Vector{Any})

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function _call_chunks(helpers::Vector{Any})
function _call_chunks(helpers::Vector{Expr})

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied. It now also takes iip::Bool so one function builds the in-place and the out-of-place combination.


🤖 Posted by an AI agent · harness: Claude Code 2.1.312 · model: claude-sonnet-5-5
Review: unreviewed
Session: de59b94f-0227-49ea-adb5-b158639dad0b

all(arg -> arg isa Symbol, signature.args) || return nothing
body = Expr(:block)
for helper in helpers
push!(body.args, Expr(:call, helper, signature.args...))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Splatting signature.args is bad. This should do something like

Suggested change
push!(body.args, Expr(:call, helper, signature.args...))
tmp = Expr(:call, helper)
append!(tmp.args, signature.args)
push!(body.args, tmp)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied as suggested: build the call expression and append! the signature arguments. The vcat call is built the same way.


🤖 Posted by an AI agent · harness: Claude Code 2.1.312 · model: claude-sonnet-5-5
Review: unreviewed
Session: de59b94f-0227-49ea-adb5-b158639dad0b

iip = _call_chunks(helpers)
if iip !== nothing
codegen_options = setproperties(codegen_options, (; iip_config = (true, false)))
return first(Symbolics.codegen_function(ir, expr, args, codegen_options)), iip

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not build the chunked oop helpers and vcat their results?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done where it is correct. Each slice is built once, and when output_type or similarto is explicitly a plain Array type, the out-of-place function is the vcat of the slices' helpers. Without an explicit container, the out-of-place function is the ordinary whole-function one, because its container comes from the runtime arguments and vcat of the slices would lose it (for example a labelled array). Labelled or static explicit output types skip chunking entirely. The in-place function is chunked in all supported cases.


🤖 Posted by an AI agent · harness: Claude Code 2.1.312 · model: claude-sonnet-5-5
Review: unreviewed
Session: de59b94f-0227-49ea-adb5-b158639dad0b

xtalax and others added 4 commits November 2, 2026 16:06
Conflict in codegen_utils.jl: kept both the chunking constants and upstream's find_arrvars_recurse.

Agent-Harness: Claude Code 2.1.312
Agent-Model: claude-sonnet-5-5
Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
…unk the out-of-place function too

Review of SciML#5308: chunk boundaries now come from SU.get_reachability! counts per slice (no subset_ir, no length(ir) of a shared IR, no density heuristic), are chosen before the whole-function codegen work, and the out-of-place function is the vcat of the helpers' results. RuntimeGeneratedFunctions >= 0.5.14 is required instead of a runtime probe (0.5.13 fails on a local function definition, 0.5.14 compiles it).

Agent-Harness: Claude Code 2.1.312
Agent-Model: claude-sonnet-5-5
Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
Chunked results are vcats of the slices' results, which lose or duplicate the metadata of labelled and static containers (Codex round 1). Other output types now take the whole-function path before any helper is built.

Agent-Harness: Claude Code 2.1.312
Agent-Model: claude-sonnet-5-5
Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
…an explicit Array

Without an explicit output_type or similarto the container of the out-of-place result is decided by the runtime arguments, and the vcat of the slices would not preserve it (Codex round 2). The in-place function stays chunked; the out-of-place one is the ordinary whole-function codegen, which is only compiled when called.

Agent-Harness: Claude Code 2.1.312
Agent-Model: claude-sonnet-5-5
Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
Co-authored-by: Aayush Sabharwal <aayush.sabharwal@gmail.com>
populate_ir! runs on every element of the output before _chunk_ranges is
called, so Base.getindex(ir, e) always finds a definition; a missing one
throws rather than returning zero.

Agent-Harness: Claude Code 2.1.312
Agent-Model: claude-sonnet-5-5
Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
@xtalax-droid

Copy link
Copy Markdown
Contributor Author

Pushed bc9d9b10d4: with the two suggestions applied (b3dd8ad503), every index comes from getindex(ir, ...), which throws for a missing symbol instead of returning zero, and populate_ir! runs before _chunk_ranges, so the any(iszero, idxs) && return nothing line is unreachable; it is removed. function_chunking.jl and the Initialization group pass, and Runic 1.13.0 is clean on codegen_utils.jl. The ModelingToolkitBase Extensions (lts) failure is the BifurcationKit one that is also red on master.


🤖 Posted by an AI agent · harness: Claude Code 2.1.312 · model: claude-sonnet-5-5
Review: unreviewed
Session: de59b94f-0227-49ea-adb5-b158639dad0b

@xtalax-droid
xtalax-droid marked this pull request as ready for review November 3, 2026 15:49
@AayushSabharwal
AayushSabharwal merged commit 31d19e4 into SciML:master Nov 4, 2026
102 of 111 checks passed
xtalax-droid pushed a commit to xtalax/ModelingToolkit.jl that referenced this pull request Nov 4, 2026
Keeps the one-element-slice scalarization option alongside the RHS chunking from SciML#5308.

Agent-Harness: Claude Code 2.1.312
Agent-Model: claude-sonnet-5-5
Agent-Session: de59b94f-0227-49ea-adb5-b158639dad0b
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants