From 6a6f24ecc4dade55b7cbf4fa88361d29e512e904 Mon Sep 17 00:00:00 2001 From: James Braid Date: Sun, 29 Nov 2026 21:43:26 -0700 Subject: [PATCH] jitv2: tell an empty code page by a flag, not by the stub's address In a build with many codegen units and no LTO, a physical code page with no compiled code looks like it has some. The CPU then skips compiling pages it should compile, and publishing treats the page as holding live code. Nine tests fail that way with --features jitv2,rex-jit,gputrace. They include the sync-compile tests in mips_exec_test and exception_mid_run_counts_like_the_interpreter. An empty page points its code pointer (func) at a stub, jit_no_code. has_code() and publish_from() decided "empty" by comparing func with the address of jit_no_code. Rust does not promise one address per function, and a small function like this can be instantiated once per codegen unit. With 256 units and no LTO, one run printed the stored stub at 0x555b2a7497b8 and the constant at the comparison site at 0x555b2a81dbf8. The comparison never matched. Dev and the ship profile (fat LTO, one unit) keep a single copy, so they passed. Add a holds_code flag next to func, written under publish_lock, and have has_code() read it. publish() treats a null func as "publish no code" and stores the stub. func is still never null when empty: a CPU that passed is_runnable can load it just as retire_code swaps the code out, so it must stay callable. Dispatch is unchanged. The two tests that compared func with the stub now call it and check it returns EXEC_FALLBACK. Cost, on the ship profile with --features jitv2: the 6248 indirect calls in the MipsCpu functions are identical before and after, and the inline-compile check on CPU arrival goes from load, lea, cmp, je to movzbl, test, je. perf stat instructions:u for iris-bench run --quick --cpu r10000, six interleaved runs each, is 182.9G mean (sd 5.8G) on base 2adb78f8a and 184.6G (sd 4.4G) here. The difference is inside the run-to-run spread. has_code() is on no per-instruction path. Verified: the nine tests pass in a build with 256 codegen units and no LTO. nextest passes in dev with default features and with jitv2,rex-jit,gputrace, and iris-checks passes after the release-features build. Production lines: +15 -6 Test lines: +9 -3 --- src/cpu/jitv2/comp.rs | 2 +- src/cpu/jitv2/equiv_test.rs | 2 +- src/cpu/jitv2/jitv2.rs | 32 ++++++++++++++++++++++++-------- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/cpu/jitv2/comp.rs b/src/cpu/jitv2/comp.rs index 49114b884..8284ca026 100644 --- a/src/cpu/jitv2/comp.rs +++ b/src/cpu/jitv2/comp.rs @@ -258,7 +258,7 @@ fn prepare_multi_entry_compile( // Publish no code for this generation and deny every offset, so // arrivals interpret the page without asking again until the // release moves the generation (`step_jit`'s denylist gate). - if page.publish(&[0; crate::cpu::jitv2::BITMAP_WORDS], crate::cpu::jitv2::NO_CODE as *const (), gen, 0, 0, req.compiled_for_fr1) { + if page.publish(&[0; crate::cpu::jitv2::BITMAP_WORDS], std::ptr::null(), gen, 0, 0, req.compiled_for_fr1) { page.denylist_all(); } page.mark_prepare_bounced(); diff --git a/src/cpu/jitv2/equiv_test.rs b/src/cpu/jitv2/equiv_test.rs index e338c45e0..d70309b39 100644 --- a/src/cpu/jitv2/equiv_test.rs +++ b/src/cpu/jitv2/equiv_test.rs @@ -4221,7 +4221,7 @@ mod tests { let page = unsafe { &*exec.pcp }; let mut entries = [0u64; crate::cpu::jitv2::BITMAP_WORDS]; entries[0] = 1; - assert!(page.publish(&entries, crate::cpu::jitv2::NO_CODE as *const (), page.current_gen(), 0, 0, false)); + assert!(page.publish(&entries, std::ptr::null(), page.current_gen(), 0, 0, false)); assert!(page.is_runnable(0) && !page.has_code()); exec.core.pc = pc; exec.core.gpr[1] = 0; diff --git a/src/cpu/jitv2/jitv2.rs b/src/cpu/jitv2/jitv2.rs index 98776dc1b..48bd05c21 100644 --- a/src/cpu/jitv2/jitv2.rs +++ b/src/cpu/jitv2/jitv2.rs @@ -1031,6 +1031,9 @@ pub struct PhysicalCodePage { /// published yet. Never null. Validity is owned by `compiled`'s bits together with `entry_gen` /// matching `current_gen()` — see `is_runnable`. func: std::sync::atomic::AtomicPtr<()>, + /// Whether `func` is compiled code. Not read off `func`: a function has no + /// single address across codegen units. Written under `publish_lock`. + holds_code: std::sync::atomic::AtomicBool, /// The FR mode `func` was compiled for. Distinct from the pin (`fr1`), /// which is the mode the *next* compile targets: the two disagree while a /// page used under both modes waits for its recompile. `publish` needs @@ -1573,6 +1576,7 @@ impl PhysicalCodePage { denied: new_bitmap_all_set(), compiled: new_bitmap(), func: std::sync::atomic::AtomicPtr::new(NO_CODE as *mut ()), + holds_code: std::sync::atomic::AtomicBool::new(false), func_fr1: std::sync::atomic::AtomicBool::new(false), tenancy: AtomicU32::new(0), referenced: std::sync::atomic::AtomicBool::new(false), @@ -1626,6 +1630,7 @@ impl PhysicalCodePage { // page permanently denylisted everywhere). for word in self.denied.iter() { word.store(u64::MAX, Ordering::Relaxed); } self.func.store(NO_CODE as *mut (), Ordering::Relaxed); + self.holds_code.store(false, Ordering::Relaxed); self.func_fr1.store(false, Ordering::Relaxed); self.code_epoch.store(0, Ordering::Relaxed); // The snapshot describes the `func` just dropped — see @@ -2009,9 +2014,10 @@ impl PhysicalCodePage { } /// Whether `func` is real code rather than the `jit_no_code` stub. + /// Advisory outside `publish_lock`: a publish can change it right after the read. #[inline] pub fn has_code(&self) -> bool { - self.func.load(Ordering::Relaxed) != NO_CODE as *mut () + self.holds_code.load(Ordering::Relaxed) } /// The page's compiled function, or `jit_no_code` if unpublished. Callers must @@ -2380,8 +2386,9 @@ impl PhysicalCodePage { /// all-allowed (§13.6): a page-gen bump means the bytes are provably /// different from whatever got denied last time. /// - /// For code that lives in no rotating arena (the stub, a test's - /// function); compiled code goes through [`Self::publish_from`]. + /// A null `func` publishes no code: the page keeps `jit_no_code`. For code + /// in no rotating arena (a test's function); compiled code goes through + /// [`Self::publish_from`]. pub fn publish( &self, new_entries: &[u64; BITMAP_WORDS], @@ -2441,10 +2448,12 @@ impl PhysicalCodePage { // because no concurrent reader trusts it without first Acquire- // loading `entry_gen` (via `is_runnable`) and observing it equal // to current_gen(); `publish_lock` excludes any concurrent writer of - // these same three fields. - self.func.store(func as *mut (), Ordering::Release); + // these same four fields. + let real = !func.is_null(); + self.func.store(if real { func as *mut () } else { NO_CODE as *mut () }, Ordering::Release); + self.holds_code.store(real, Ordering::Relaxed); self.func_fr1.store(fr1, Ordering::Relaxed); - self.code_epoch.store(if func == NO_CODE as *const () { 0 } else { arena.epoch() }, Ordering::Relaxed); + self.code_epoch.store(if real { arena.epoch() } else { 0 }, Ordering::Relaxed); self.compiles_since_flush.fetch_add(1, Ordering::Relaxed); self.instr_count.store(instr_count as u32, Ordering::Relaxed); self.code_size.store(code_size, Ordering::Relaxed); @@ -2574,6 +2583,7 @@ impl PhysicalCodePage { } for word in self.compiled.iter() { word.store(0, Ordering::Release); } self.func.store(NO_CODE as *mut (), Ordering::Release); + self.holds_code.store(false, Ordering::Relaxed); self.func_fr1.store(false, Ordering::Relaxed); self.code_epoch.store(0, Ordering::Relaxed); self.instr_count.store(0, Ordering::Relaxed); @@ -4798,7 +4808,7 @@ mod tests { assert!(page.retire_code(a.epoch())); assert!(!page.is_runnable(0) && !page.is_runnable(2) && !page.has_code()); - assert_eq!(page.func(), NO_CODE as *const (), "never null: a CPU may be about to call it"); + assert!(func_is_stub(&page), "never null: a CPU may be about to call it"); assert!(!page.is_requested(7), "entry requests go with the code"); assert!(!page.has_compile_snapshot(), "nothing may re-validate the function that went"); assert!(!page.retire_code(a.epoch()), "nothing left to take"); @@ -4812,6 +4822,12 @@ mod tests { assert_eq!(page.code_epoch(), b.epoch()); } + /// The page's `func` is callable and sends the CPU to the interpreter. + fn func_is_stub(page: &PhysicalCodePage) -> bool { + let stub: JitFn = unsafe { std::mem::transmute(page.func()) }; + unsafe { stub(std::ptr::null_mut()) == crate::cpu::mips_exec::EXEC_FALLBACK } + } + fn claimed_page(fr1: bool) -> Box { let mut page = Box::new(PhysicalCodePage::new(UNCLAIMED_PFN, std::ptr::null())); page.claim(CodeKey::flat(0x1234 << 12), std::ptr::null(), fr1); @@ -6226,7 +6242,7 @@ mod tests { assert_eq!(jit.page_at(at(0, 2), &dev, false), Some(slot)); let page = &jit.pages[slot as usize]; assert!(!page.has_code() && !page.is_runnable(0) && !page.is_requested(5) && !page.is_denylisted(9)); - assert_eq!(page.func(), NO_CODE as *const ()); + assert!(func_is_stub(&page)); } #[test]