refactor: Introduce GeneratedFunctionOptions for the code-generation entry points - #4768
Merged
Conversation
AayushSabharwal
force-pushed
the
as/generated-fn-opts
branch
from
July 16, 2026 11:15
8de2bdf to
53a2c4d
Compare
AayushSabharwal
marked this pull request as ready for review
July 16, 2026 11:15
Add `GeneratedFunctionOptions{expression, wrap_gfw}` (in ModelingToolkitBase's
codegen_utils.jl): the "output/compile" options layer one level above
`BuildFunctionWrapperOptions`. `expression`/`wrap_gfw` are `Bool` type parameters
(they gate the return type, so generators branch on them statically); the fields
are `eval_expression`, `eval_module`, `compiler_options::CompilerOptions`, and a
nested `codegen::Symbolics.CodegenFunctionOptions` for the code-generation options
threaded down to `build_function_wrapper`.
Convert the code-generation entry points that funnel into `maybe_compile_function`
to take a positional `GeneratedFunctionOptions` plus a strictly-typed set of
function-specific keyword arguments (no `kwargs...` sink on the primaries):
generate_rhs, generate_jacobian, generate_W, generate_tgrad, generate_dae_jacobian,
generate_diffusion_function, generate_cost(_gradient/_hessian), generate_cons,
generate_constraint_jacobian/_hessian, generate_bvp_cost, generate_boundary_conditions,
generate_history, generate_control_jacobian, generate_update_A/_b,
generate_custom_function, and generate_semiquadratic_functions/_jacobian.
Function-specific options (`simplify`, `sparse`, `return_sparsity`, `implicit_dae`,
`scalar`, `cachesyms`, `stiff_*`, ...) become strict typed keyword arguments;
`checkbounds` flows through `opts.codegen`; wrapper-derived codegen options
(`wrap_code`, `similarto`, `iip_config`) are written back with `setproperties`.
The backwards-compatible keyword forms move to new `codegen_compat.jl` files (one
in ModelingToolkitBase, one at the top level for the semiquadratic generators) to
keep `codegen.jl` focused on the primary methods. `generate_custom_function`'s
primary moves from abstractsystem.jl to codegen.jl so its
`opts::GeneratedFunctionOptions` annotation is defined after the struct.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
AayushSabharwal
force-pushed
the
as/generated-fn-opts
branch
from
July 16, 2026 11:55
53a2c4d to
b7e1708
Compare
Thread the `GeneratedFunctionOptions` options struct through the remaining codegen entry points that still splatted `kwargs...` into `Symbolics.CodegenFunctionOptions`, eliminating per-kwarg-combination specialization and the silent-drop kwarg sinks. Each function gains a positional `opts::GeneratedFunctionOptions` primary method; the historical keyword signatures are preserved by backward-compatibility wrappers. - build_explicit_observed_function (codegen.jl): expression/eval_expression/ eval_module/compiler_options + codegen via `opts`. The inert `try_namespaced` and codegen-level `expression` (never fields of CodegenFunctionOptions; the wrapper always emits Exprs) are dropped without behavior change. - compile_condition, compile_explicit_affect (callbacks.jl): the latter's previously-dropped `kwargs...` now flow through `opts.codegen`, with per-call `wrap_code`/`outputidxs`/`iip_config` applied via `setproperties`. - generate_control_function (inputoutput.jl). - SymbolicTstops (problem_utils.jl) and CacheWriter (sccnonlinearproblem.jl): their keyword wrappers live adjacent to the primaries, after the `struct` definitions of the same name, so they register as methods on that binding. Move `codegen_utils.jl` before `callbacks.jl` in the include order so the `opts::GeneratedFunctionOptions` signature annotations resolve at definition time; its only definition-time dependencies are Symbolics symbols and AbstractSystem. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Update the source callers of the code-generation entry points converted in the
previous commits so they pass a positional `GeneratedFunctionOptions` struct
instead of splatting keyword arguments. Each `*Function`/`*Problem` constructor
now builds one options struct and threads it into every generator call
(generate_rhs/tgrad/jacobian/diffusion/history, the cost/constraint/bvp/update/
semiquadratic generators, build_explicit_observed_function, SymbolicTstops,
CacheWriter); per-call structural options (simplify, sparse, implicit_dae,
cachesyms, ...) remain loose keyword arguments.
Behavior-preserving details worth calling out:
- `GeneratedFunctionOptions` defaults `expression = Val{true}`, whereas
`build_explicit_observed_function` and `SymbolicTstops` historically default
to compiled output; callsites that relied on that now pass
`expression = Val{false}` explicitly.
- `cachesyms` reaches `LinearFunction` through `kwargs` in the SCC path and is a
structural (not codegen) kwarg, so it is extracted and forwarded as such
rather than dropped into the codegen options.
- `compiler_options` is pulled out of `kwargs` where the generator consumes it.
- optimal_control_interface's historical `expression_module = eval_module` was
never a recognized keyword (silently dropped, so `eval_module` defaulted); the
migration preserves that and documents it inline.
Callers that merely forward an opaque `kwargs...` sink (compile_condition and
generate_control_function forwarders, the generic observed helper in
problem_utils) are left on their keyword back-compat wrappers.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
AayushSabharwal
force-pushed
the
as/generated-fn-opts
branch
from
July 16, 2026 13:09
b7e1708 to
cf1ad2b
Compare
Contributor
Benchmark Results (Julia vlts)Time benchmarks
Memory benchmarks
|
Contributor
Benchmark Results (Julia v1)Time benchmarks
Memory benchmarks
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up of #4762