From b7d0debc959eb031594146b9e70c8209a5bffdb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ralph=20K=C3=BCpper?= Date: Wed, 17 Jun 2026 22:04:32 +0200 Subject: [PATCH] perf(codegen): fold nan-box bitcast round-trips at emit time (#5334 lever C) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A nan-boxed JS value is an `f64`, so any bit-level work unboxes it (`bitcast double -> i64`) and any value production re-boxes it (`bitcast i64 -> double`). Untyped lowering routinely boxes a value and then immediately unboxes it again (or vice versa) across adjacent ops — e.g. a raw-i64 helper result boxed to the canonical JS double and then re-unboxed for the next bit operation. Each such pair is identity: `bitcast(bitcast(x)) == x`. `clang -O1+` folds these via instcombine, but oversized modules are forced to `-O0` (see #4880, the >threshold IR path), where instcombine never runs — so the round-trips survive into the object file and bloat the IR clang must chew through. Fold them at emit time: the block builder records each bitcast result's pre-bitcast source of the opposite type (scoped to the block, so the source provably dominates), and the inverse bitcast returns that source directly instead of emitting a second cast. Only the redundant re-cast is elided; the first cast stays (its result may still be live). Safe and perf-neutral by construction — it emits strictly fewer instructions and never changes value semantics (verified: dynamic untyped test produces byte-identical program output; folded IR drops the redundant casts; full perry-codegen suite green; new block unit tests cover the fold and the no-fold cases). Second step of the IR-efficiency roadmap (#5334, Tier 2 lever C). --- crates/perry-codegen/src/block.rs | 68 +++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/crates/perry-codegen/src/block.rs b/crates/perry-codegen/src/block.rs index bb86a82efba..7f026859ecb 100644 --- a/crates/perry-codegen/src/block.rs +++ b/crates/perry-codegen/src/block.rs @@ -10,6 +10,7 @@ //! control-flow merges (if/else value context, short-circuit logical ops). use std::cell::Cell; +use std::collections::HashMap; use std::rc::Rc; use crate::codegen::FpContractMode; @@ -83,6 +84,19 @@ pub struct LlBlock { terminated: bool, counter: Rc, fp_flags: FpFlags, + /// Provenance of nan-box bitcast results emitted in THIS block, for + /// round-trip folding (#5334 lever C). A nan-boxed JS value is an `f64`, + /// so any bit-level work unboxes (`bitcast double -> i64`) and any value + /// production re-boxes (`bitcast i64 -> double`). When a value is boxed + /// then immediately unboxed again (or vice versa) the pair is identity: + /// `bitcast(bitcast(x)) == x`. `clang -O1+` folds these via instcombine, + /// but the oversized-module path forces `-O0` (see #4880), which does not + /// — so we fold at emit time. Maps a bitcast RESULT reg to its + /// pre-bitcast source of the OPPOSITE type, scoped to this block so the + /// source is guaranteed to dominate (SSA, emitted earlier in the same + /// block). + i64_from_double: HashMap, + double_from_i64: HashMap, } impl LlBlock { @@ -101,6 +115,8 @@ impl LlBlock { terminated: false, counter, fp_flags, + i64_from_double: HashMap::new(), + double_from_i64: HashMap::new(), } } @@ -410,14 +426,31 @@ impl LlBlock { // -------- Conversions / bitcasts -------- pub fn bitcast_i64_to_double(&mut self, val: &str) -> String { + // #5334 lever C: fold `bitcast i64 (bitcast double %x to i64) to double` + // back to %x — the round-trip is identity. The source dominates (same + // block, emitted earlier), so reusing it is always valid SSA. + if let Some(src) = self.i64_from_double.get(val) { + return src.clone(); + } let r = self.reg(); self.emit(format!("{} = bitcast i64 {} to double", r, val)); + if !self.terminated { + self.double_from_i64.insert(r.clone(), val.to_string()); + } r } pub fn bitcast_double_to_i64(&mut self, val: &str) -> String { + // #5334 lever C: fold `bitcast double (bitcast i64 %x to double) to i64` + // back to %x — the round-trip is identity. + if let Some(src) = self.double_from_i64.get(val) { + return src.clone(); + } let r = self.reg(); self.emit(format!("{} = bitcast double {} to i64", r, val)); + if !self.terminated { + self.i64_from_double.insert(r.clone(), val.to_string()); + } r } @@ -819,6 +852,41 @@ mod tests { ) } + #[test] + fn nanbox_bitcast_roundtrip_folds_to_source() { + // #5334 lever C: i64 -> double -> i64 collapses to the original i64, + // and the reverse collapses to the original double. Only the first + // bitcast of each pair is emitted. + let mut b = fresh(); + let dbl = b.bitcast_i64_to_double("%arg"); // %r1 = bitcast i64 %arg to double + let back = b.bitcast_double_to_i64(&dbl); // folds -> %arg (no new instr) + assert_eq!(back, "%arg"); + + let unboxed = b.bitcast_double_to_i64("%v"); // %r2 = bitcast double %v to i64 + let reboxed = b.bitcast_i64_to_double(&unboxed); // folds -> %v + assert_eq!(reboxed, "%v"); + + assert_eq!( + b.to_ir(), + "entry.0:\n %r1 = bitcast i64 %arg to double\n %r2 = bitcast double %v to i64" + ); + } + + #[test] + fn nanbox_bitcast_non_roundtrip_still_emits() { + // A lone unbox with no inverse is untouched, and a fresh unbox of a + // different value is not mistakenly folded. + let mut b = fresh(); + let a = b.bitcast_double_to_i64("%a"); // %r1 + let c = b.bitcast_double_to_i64("%c"); // %r2 (different source, no fold) + assert_eq!(a, "%r1"); + assert_eq!(c, "%r2"); + assert_eq!( + b.to_ir(), + "entry.0:\n %r1 = bitcast double %a to i64\n %r2 = bitcast double %c to i64" + ); + } + #[test] fn fadd_emits_expected_ir_default() { // Default mode: no fast-math FMF flags emitted, bit-exact with