From e42a7f9f7ad5458262a66832a2e0f2444c8b6e75 Mon Sep 17 00:00:00 2001 From: Rob Vermaas Date: Thu, 30 Jul 2026 13:07:33 +0200 Subject: [PATCH] Restore AllocOptPass in the -O1 scalar optimizer pipeline MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Backport the `else if (O.getSpeedupLevel() >= 1)` branch of buildScalarOptimizerPipeline that upstream added in JuliaLang/julia#52850 ("Add passes to -O1 pipeline to reduce allocations in reinterpret"). That branch was carried to release-1.10 (#57731) and release-1.11 (#57732) on 2025-03-11, but never landed on release-1.12 — it is present on master, release-1.13 and v1.11.9, and absent on release-1.12, backports-release-1.12 and v1.12.6. We could not find a revert or a rationale for the omission. Without AllocOptPass at -O1, a `Ref` holding an immutable value which never escapes and is used only via `pointer_from_objref` is heap-allocated: julia 1.10.11 -O1 @allocated = 0 julia 1.11.9 -O1 @allocated = 0 julia 1.12.6 -O1 @allocated = 32 <-- regression julia 1.12.6 -O2 @allocated = 0 raicode runs its test suite at -O1 and asserts that several hot paths (TypeNormalization's byte-string hashing and fast_reinterpret, among others) allocate nothing, so -O1 no longer gives a usable "this code does not allocate" signal. Production builds run at -O2/-O3 and are unaffected. The added pass list is byte-identical to v1.11.9's. All six passes are already used elsewhere in this file and their headers are already included, so this adds no new dependencies. --- src/pipeline.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/pipeline.cpp b/src/pipeline.cpp index 39f896ba656d2..eb7fa684c5c74 100644 --- a/src/pipeline.cpp +++ b/src/pipeline.cpp @@ -465,6 +465,19 @@ static void buildScalarOptimizerPipeline(FunctionPassManager &FPM, PassBuilder * FPM.addPass(IRCEPass()); FPM.addPass(InstCombinePass()); FPM.addPass(JumpThreadingPass()); + } else if (O.getSpeedupLevel() >= 1) { + // RAI: backport of the -O1 branch added upstream by JuliaLang/julia#52850, + // which was carried to release-1.10 (#57731) and release-1.11 (#57732) but + // never to release-1.12. Without AllocOptPass here, a non-escaping Ref that + // is only used via pointer_from_objref is heap-allocated at -O1, while it is + // elided on 1.10/1.11 and at -O2+ on every version. Our test suite runs at + // -O1 and asserts such code is allocation-free. + JULIA_PASS(FPM.addPass(AllocOptPass())); + FPM.addPass(SROAPass(SROAOptions::ModifyCFG)); + FPM.addPass(MemCpyOptPass()); + FPM.addPass(SCCPPass()); + FPM.addPass(InstCombinePass()); + FPM.addPass(ADCEPass()); } if (O.getSpeedupLevel() >= 3) { FPM.addPass(GVNPass());