← Back to Pull requests

aotcompile: Release the Julia heap before compiling (let's use all the threads) #63531

Open
xal-0 opened this pull request with 220 lines diff across 14 files
View on GitHub
xal-0 xal-0 commented on Oct 02, 2026 04:53 UTC
Author

One big problem with our current AOT compilation strategy is the existence of a bottleneck with very high peak memory use. In particular, after running the precompilation workload (accumulating heap objects) and emitting LLVM IR, we serialize the heap image. In the worst case, we'll then hold on to the heap, the IR, and the serialized heap all the way until we're done compiling the LLVM IR, a process which requires even more memory. We also consume more memory when using more threads during multithreaded compilation, forcing us to use a single thread on 32-bit platforms.

We've already moved away from holding on to the heap image after creating it with #61649. Native images with embedded heaps continue to hold on to the heap image through the compilation process, but we can at least write the .ji and delete it when compiling a split image. We should move towards using these in more situations.

If we're careful, we can avoid running any Julia code after emitting LLVM IR and serializing the heap image, provided we run the post output hooks and clean up. This PR does that and then tears down the heap so we can use more memory for multithreaded LLVM compilation.

Combined with #63363, we might even consider re-enabling multithreaded compilation on 32-bit platforms. The total amount of resident memory growth after serialization was reduced by that change, and with this change, we free up so much space that the heap serialization step will almost always be the point of peak memory use when using <16 threads.

<img width="2000" height="972" alt="download" src="https://github.com/user-attachments/assets/24987575-5a78-45c6-b602-86be0c05dfa5" />
<img width="2000" height="924" alt="download-1" src="https://github.com/user-attachments/assets/16042984-421e-4fb8-a736-8706d6c19b1e" />

As an experiment, I enabled multithreaded image compilation on 32-bit Windows (our favourite platform). It seems like we could get away with it (though I will enable it in a separate PR):

<img width="2000" height="923" alt="download-2" src="https://github.com/user-attachments/assets/92023a2c-74e0-4803-9647-8df94fc953a4" />

There's some tricky stuff in here, since I wanted to avoid breaking our ctrl+c backtraces after we delete the heap (jl_write_native_output_at_exit), and a few places have been modified to check the new jl_heap_released variable and do something reasonable.

Assisted-by: Claude:claude-opus-5.5
Assisted-by: Claude:claude-fable-5.1

Diff: 220 lines (+194 / -26) across 14 files Reviewer: xal-0 Review duration: 40483m
All Vouch review confidence checks passed
Residual risk 0.15 = Change Risk (0.68) × (1 − Review Confidence (0.79)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.68 · 220 lines diff across 14 files
0.68
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.87 · 5 substantive comment(s)
0.87
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
78%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.323
Self Review self_review
0.140
Real PR Conversation Timeline
6 items
xal-0 xal-0 commented on code on Oct 02, 2026 05:09 UTC
Code Review
src/jlapi.c:1
@@ -1299,6 +1299,8 @@ static void jl_resolve_sysimg_location(JL_IMAGE_SEARCH rel, const char* julia_bi
         jl_options.outputji = absrealpath(jl_options.outputji, 0);
     if (jl_options.outputbc)
         jl_options.outputbc = absrealpath(jl_options.outputbc, 0);
+    if (jl_options.outputunoptbc)
+        jl_options.outputunoptbc = absrealpath(jl_options.outputunoptbc, 0);

Note for reviewers: this change is actually necessary since it also strdups the path, which comes from argv and is owned by libuv since uv_setup_args. We trash it after calling uv_library_shutdown.

KristofferC KristofferC commented on code on Oct 02, 2026 07:48 UTC
Code Review
src/jlapi.c:1
@@ -1299,6 +1299,8 @@ static void jl_resolve_sysimg_location(JL_IMAGE_SEARCH rel, const char* julia_bi
         jl_options.outputji = absrealpath(jl_options.outputji, 0);
     if (jl_options.outputbc)
         jl_options.outputbc = absrealpath(jl_options.outputbc, 0);
+    if (jl_options.outputunoptbc)
+        jl_options.outputunoptbc = absrealpath(jl_options.outputunoptbc, 0);

If it has to be a github comment for reviewers it might make sense for an inline code comment as well?

xal-0 xal-0 commented on code on Oct 02, 2026 16:05 UTC
Code Review
src/jlapi.c:1
@@ -1299,6 +1299,8 @@ static void jl_resolve_sysimg_location(JL_IMAGE_SEARCH rel, const char* julia_bi
         jl_options.outputji = absrealpath(jl_options.outputji, 0);
     if (jl_options.outputbc)
         jl_options.outputbc = absrealpath(jl_options.outputbc, 0);
+    if (jl_options.outputunoptbc)
+        jl_options.outputunoptbc = absrealpath(jl_options.outputunoptbc, 0);

Sure.

KristofferC KristofferC commented on Oct 11, 2026 12:12 UTC
Contributor

Could we report serialization and compilation separately in the sysimage output timing? AFAIU, it will now only report the the serialization part.

xal-0 xal-0 commented on Oct 23, 2026 00:19 UTC
Author

Unfortunately there is no nice way of timing just the LLVM compilation from Julia. The most recent commit has Claude's suggestion, which I don't like, but it's nicer than adding code to the runtime for this purpose. If I can get this merged while removing the print, or renaming it to just "Serialization", I'd rather that. Are we gathering this info from the logs to detect regressions anywhere?

Reviewers
Assignees
xal-0 xal-0
Labels
risk: low confidence: 78.6% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 68.3% (HIGH) · Higher = larger blast radius
68.3%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 78.6% · Lower = rushed or shallow review
78.6%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 14.6% · Safe to merge
14.6%