Skip to content

Share common subexpressions in the generated source kernels - #65

Merged
xtalax merged 6 commits into
masterfrom
perf/source-codegen-cse
Nov 6, 2026
Merged

xtalax merged 6 commits into
masterfrom
perf/source-codegen-cse

Conversation

@xtalax-droid

Copy link
Copy Markdown
Collaborator

A long manufactured source term (hundreds of additive sin, cos, exp and power products) is recompiled per sum term, so evaluating it dominates the right-hand side. build now takes a cse keyword (default false), and only the :source kernels and coefficient functions pass cse = true.

Plain cse = true also hoists numeric literals into temporaries, so u^2 becomes a libm NaNMath.pow(u, 2) call instead of literal_pow. That is 2x slower on a polynomial source and 25 to 45% slower on the nonlinear residual and Jacobian (benchmark case (iii)), which is why CSE is limited to sources and why a first attempt at CSE on every kernel was not opened as a PR (reproducer: build_function(2*a*g1*v1 + 2*a*g2*v2 + a^2*v1 + a^2*v2, a, g1, g2, v1, v2; expression = Val(true), cse = true) emits ##cse#1 = 2 and NaNMath.pow(a, ##cse#1); reported upstream to SymbolicUtils). build therefore substitutes the numeric-literal temporaries back and restores x^n (which lowers to literal_pow) before compiling (inline_literals). Sources containing ifelse compile without CSE, because CSE hoists both branches in front of the conditional and a guarded registered function or sqrt could throw where the plain code never reaches it (a Codex finding, with a regression test). Mass, bilinear, nonlinear, tangent, strong and Dirichlet kernels are unchanged. Helpers are # comments, not docstrings.

Measured, ns per call of the source function (Julia 1.12, Symbolics 7.48.0): polynomial squares K = 8: 51.3 plain, 106.8 raw CSE, 42.5 here; K = 2: 8.6, 30.4, 10.3; trig with squares K = 8: 1097, 245, 178; long without squares: 2486, 425, 411; ordinary source: 22.3, 10.2, 10.1; the (1+u^2)-style nonlinear tangent (not changed in this PR): 1.6 plain, 11.7 raw CSE, 1.6 with inlining. End to end, n = 16, raw source A (master versus this PR, identical max error): polynomial squares K = 8 rhs per call 1.0 ms to 0.2 ms, solve 0.51 s to 0.30 s; trig with squares K = 8 rhs 15.3 ms to 1.3 ms, solve 2.12 s to 0.30 s; the long source (K = 8) rhs per call 10.2 ms to 1.9 ms at n = 8 and 40.5 ms to 7.4 ms at n = 16, solve 1.95 s to 0.37 s and 6.0 s to 1.14 s. benchmarks/assembly_cost.jl and construction.jl are unchanged within noise (case (iii) ratio 1.27 and 1.29 against master's 1.33), coefficient and f kernel calls allocate 0 B, cold first discretize is unchanged (33.1 s long source and 30.0 s ordinary against 33.5 s and 29.9 s), time to first assembly with the precompile workload is 0.59 and 0.62 s against 0.58 to 0.60 s on master.

Tests (test/Kernels/kernels.jl): "no kernel turns a literal power into a pow call on a hoisted exponent" walks the Expr of every kernel's f, tangent and coeffs for case (iii) and the two-field case and fails if a ^ or pow has a symbol exponent assigned from a number (it fails on the full-kernel CSE commit with NaNMath.pow(a, var"##cse#1")); "common-subexpression elimination of a source agrees with the plain code generator" compares values at 1e-12 on random inputs over an ordinary, a long, a parameter-and-time and a squares source, checks the CSE'd long source evaluates fewer sin calls, and checks an emitted source against the symbolic integrand; plus a guarded-source test (branches stay lazy, a DomainError without the guard) and one that a user function named pow keeps its meaning. QA's explicit-imports check rejected a first version that used non-public Base.literal_pow/NaNMath.pow; the final code emits x^n and recognises pow by name and parent module.

Gates: Julia 1.12 uninstrumented: Kernels, Assembly, Integration, Perf (all four gates), WeakForm, Solution, Space, DAE, QA, Convergence, NavierStokes, Frontend (23 files) all pass; Julia 1.10 with current versions: Kernels, Assembly, DAE; Julia 1.10 downgrade-style (Symbolics 7.39.2, SymbolicUtils 4.53.0, other deps at their floors): Kernels, Assembly, DAE, Integration, NavierStokes pass. The docs build exits 0 with no missing docs, Runic 1.13 is clean and Codex approved (three findings fixed: ifelse laziness, the pow match by name, a first-pass issue). Relation to #64 (the exact substitute fallback that lets long raw sources reach the coefficient path): independent, and src/kernels.jl merges cleanly with it; both append tests at the same place in test/Kernels/kernels.jl, so whichever merges second needs a trivial textual resolution there. If an upstream SymbolicUtils fix stops CSE from hoisting numeric literals, inline_literals can be dropped.


🤖 Posted by an AI agent · harness: Claude Code 2.1.318 · model: claude-sonnet-5-5
Review: unreviewed
Session: 43e03fde-3384-4386-bae9-03e81f19ba7c

t and others added 5 commits November 5, 2026 08:44
build takes a cse keyword (default false) and only the :source kernels and coefficient functions pass cse = true. Common-subexpression elimination also hoists numeric literals, which turns a literal power u^2 into a libm NaNMath.pow call: 25 to 45 percent slower nonlinear residual and Jacobian assembly in benchmark case (iii). Tests check that the nonlinear and coefficient kernels keep literal_pow and that the CSE'd sources agree with the plain code generator.

Agent-Harness: Claude Code 2.1.318
Agent-Model: claude-sonnet-5-5
Agent-Session: 43e03fde-3384-4386-bae9-03e81f19ba7c
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
Common-subexpression elimination hoists literals into temporaries, which turns u^2 into a NaNMath.pow call (2x slower on a polynomial source). build substitutes the literal temporaries back and restores literal_pow before compiling. Tests check that no kernel, source or not, has a pow call on a hoisted exponent.

Agent-Harness: Claude Code 2.1.318
Agent-Model: claude-sonnet-5-5
Agent-Session: 43e03fde-3384-4386-bae9-03e81f19ba7c
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
cse hoists the calls in both branches of an ifelse in front of the conditional, so a guarded registered function or sqrt can throw where the plain code never reaches it. Sources containing ifelse compile without cse.

Agent-Harness: Claude Code 2.1.318
Agent-Model: claude-sonnet-5-5
Agent-Session: 43e03fde-3384-4386-bae9-03e81f19ba7c
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
The rewrite matched any function named pow; it now matches the NaNMath.pow function itself, so a registered user function that is called pow keeps its meaning.

Agent-Harness: Claude Code 2.1.318
Agent-Model: claude-sonnet-5-5
Agent-Session: 43e03fde-3384-4386-bae9-03e81f19ba7c
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
Base.literal_pow and NaNMath.pow are not public, which the QA group's explicit-imports check rejects. A call x^2 on a literal integer lowers to literal_pow by itself, and NaNMath.pow is recognised by its name and parent module.

Agent-Harness: Claude Code 2.1.318
Agent-Model: claude-sonnet-5-5
Agent-Session: 43e03fde-3384-4386-bae9-03e81f19ba7c
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark Results (Julia v1)

Time benchmarks
master 272ddfc... master / 272ddfc...
construction/heat P1 10x10 7.74 ± 0.43 ms 7.92 ± 0.62 ms 0.977 ± 0.093
construction/heat P1 20x20 10.3 ± 0.44 ms 10.6 ± 0.66 ms 0.977 ± 0.074
time_to_load 5.47 ± 0.033 s 5.47 ± 0.022 s 0.999 ± 0.0072
Memory benchmarks
master 272ddfc... master / 272ddfc...
construction/heat P1 10x10 0.0393 M allocs: 2.12 MB 0.0397 M allocs: 2.13 MB 0.994
construction/heat P1 20x20 0.0702 M allocs: 4.73 MB 0.0706 M allocs: 4.75 MB 0.997
time_to_load 0.2 k allocs: 11.8 kB 0.2 k allocs: 11.8 kB 1

Resolve the test/Kernels/kernels.jl import conflict by keeping both sides: build from this branch and AssemblyContext, assemble_linear and the expanded Ferrite and LinearAlgebra imports from master (#64).

Agent-Harness: Claude Code 2.1.318
Agent-Model: claude-haiku-5-5
Agent-Session: 43e03fde-3384-4386-bae9-03e81f19ba7c
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
@xtalax
xtalax merged commit f5bf2b0 into master Nov 6, 2026
51 checks passed
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.

2 participants