Repository navigation
Avoid boxing captured variables - #1068
Merged
Merged
Conversation
Captured locals that are reassigned, or local closures that call themselves, are stored in a `Core.Box`, which is untyped. Hoist the recursive helpers of the IR passes to top-level functions (or use a worklist), return values from blocks instead of assigning outer variables, and use a typed `Ref` where a callback genuinely keeps state.
Uses `Test.detect_closure_boxes`, available from Julia 1.14.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1068 +/- ##
==========================================
+ Coverage 92.42% 92.47% +0.05%
==========================================
Files 31 31
Lines 7323 7309 -14
==========================================
- Hits 6768 6759 -9
+ Misses 555 550 -5 ☔ 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.
Julia 1.14's
Test.detect_closure_boxes(JuliaLang/julia#60478) reports methods whose lowered code puts a captured variable in aCore.Box, which is untyped. It flagged 18 methods here; this fixes all of them and adds a test (guarded on the detector being available, so it currently runs on the nightly CI job).check_user/scan_uses, bothrewrite_uses!,repoint!,walk,normalize_metadata!,remapType,has_load) are now top-level functions taking their state as arguments. The two identical use scanners inAddKernelStateandadd_input_arguments!share one helper;box_used_only_by_instructionsrecurses on itself and returns early; the Metal global-user scan is a worklist loop.doblock (native_codeincompile_method_instance, thecode_nativeresults,ft/fincall!/call_spirv_builtin!) are assigned once, or declaredlocalin the block.explain_error'smiandmcgen'sspirv(passed todump_outputinstead) are computed once, andlower_is_fpclass!caches the sign test in the loop instead of a closure.emittedtable incheck_irand the@device_codefile counter areRefs.Compile times of a few PTX, Metal and SPIR-V kernels are unchanged (within noise, if anything slightly faster). Tested on Julia 1.10, 1.13 and 1.14.