Repository navigation
Fix type instability with many array references on Julia 1.11+ - #573
Merged
Merged
Conversation
`determine_eltype` emitted a single `promote_type` call with one argument per array reference. On Julia >= 1.11, `promote_type` with 36 or more arguments is not inferred (`Base.afoldl` has `max_args = 34`), so loops with many array references became type-unstable: the element type and vector width were `Any`, and every SIMD operation used dynamic dispatch. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MEJYUviAUshj5fZt8LTe8A
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #573 +/- ##
==========================================
+ Coverage 83.81% 86.92% +3.11%
==========================================
Files 40 40
Lines 9574 9684 +110
==========================================
+ Hits 8024 8418 +394
+ Misses 1550 1266 -284 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
determine_eltypeemits a singlepromote_typecall with oneeltype(array)argument per entry ofls.includedactualarrays, and that has one entry per array reference, not per array. On Julia ≥ 1.11,promote_typewith 36 or more arguments is no longer inferred (Base.afoldlhasmax_args = 34), so the inferred element type isAny. That makespick_vector_width,MM,__vload,vadd_nswetc. dynamic, so every SIMD operation in the loop goes through runtime dispatch and allocates.This PR builds a chain of two-argument
promote_typecalls instead, which is always inferred. The result is the same, sincepromote_typefolds pairwise anyway.Impact
We hit this in Trixi.jl: a hand-written
@turboflux-differencing kernel for MHD (flux_differencing_kernel_turbo!with nonconservative terms insrc/solvers/dgsem_structured/dg_3d_turbo.jl) has about 40 array references per loop. On Julia 1.12 (Ryzen 7 7840U):The results agree with the non-
@turboreference implementation to round-off. The existingsum_way_too_unrolledtest intest/manyarrayrefs.jl(272 references) is affected too, but it only checks the result.Tests
I added a type-stability check to
test/manyarrayrefs.jl:sum_way_too_unrolledwith 6×7 = 42 references must not allocate. On Julia 1.12.6 it fails onmaster(68560 bytes allocated) and passes with this change.Pkg.test()on Julia 1.12.6 has one failure,test/dot.jl:12(choose_ordergives unroll 4 instead of the expected 8 for 32 registers). It is pre-existing: the released v0.12.174 gives the same result on this machine (AVX-512, Zen 4), and the code changed here doesn't affectchoose_order. All other test groups pass.A complementary change would be to deduplicate
ls.includedactualarraysper array (e.g.addsetv!instead ofpush!inreconstruct_loopset.jl, assplit_loops.jlalready does). That would shorten the generated expression, but on its own it would not fix loops that reference 36+ distinct arrays, so I kept this PR minimal.This PR was prepared with substantial assistance from Claude Code (Anthropic's AI coding assistant). I reviewed the changes.
🤖 Generated with Claude Code
https://claude.ai/code/session_01MEJYUviAUshj5fZt8LTe8A