aotcompile: Release the Julia heap before compiling (let's use all the threads) #63531
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.
If it has to be a github comment for reviewers it might make sense for an inline code comment as well?
Sure.
Could we report serialization and compilation separately in the sysimage output timing? AFAIU, it will now only report the the serialization part.
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?
xal-0
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
.jiand 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 newjl_heap_releasedvariable and do something reasonable.Assisted-by: Claude:claude-opus-5.5
Assisted-by: Claude:claude-fable-5.1