|
| 1 | +//! VCR-RA const-CSE (#242) — CI-gated reduction + frozen-safety oracle. |
| 2 | +//! |
| 3 | +//! The optimized (non-`--relocatable`) ARM path re-materializes a constant at |
| 4 | +//! every use (`i32.const N` → a fresh `movw`/`movt` each time). gale measured |
| 5 | +//! this on silicon: flat_flight spends 61% of its const materializations on |
| 6 | +//! values already held in a register. `SYNTH_CONST_CSE=1` enables a |
| 7 | +//! pressure-neutral const cache (`optimizer_bridge.rs`) that aliases a repeated |
| 8 | +//! const to the register already holding it, emitting nothing. |
| 9 | +//! |
| 10 | +//! This is byte-CHANGING codegen, so the flag ships DEFAULT-OFF. Two claims are |
| 11 | +//! locked here as executable CI gates; semantic equivalence under flag-ON is the |
| 12 | +//! separate `const_cse_differential.py` unicorn-vs-wasmtime oracle: |
| 13 | +//! |
| 14 | +//! 1. FROZEN-SAFE (OFF ≡ pre-change baseline). With the flag OFF the optimized |
| 15 | +//! path emits a SPECIFIC, pinned `.text` — the golden below was captured |
| 16 | +//! against the pre-const-CSE tree (verified equal by a `git stash` compare: |
| 17 | +//! post-change-OFF hash == pre-change hash, both `8c3dfcbb…`). The frozen |
| 18 | +//! differential fixtures (control_step / flight_algo) compile `--relocatable` |
| 19 | +//! → they exercise the DIRECT path and never touch this code, so this golden |
| 20 | +//! is the ONLY gate pinning optimized-path-OFF bytes. A golden, not a |
| 21 | +//! compile-twice determinism check: determinism alone would not catch the |
| 22 | +//! flag-off path drifting away from the byte-identical baseline. |
| 23 | +//! |
| 24 | +//! 2. REAL REDUCTION ON HEADROOM. On `large3` — a >16-bit const reused 3× with |
| 25 | +//! ample free registers — the flag-ON `.text` is STRICTLY SMALLER (the two |
| 26 | +//! redundant `movw`+`movt` pairs collapse to register aliases). If a future |
| 27 | +//! change makes CSE inert on headroom, this fails. |
| 28 | +//! |
| 29 | +//! WHAT THIS DOES NOT CLAIM — the named prerequisites for a default-ON flip |
| 30 | +//! (a separate, silicon-gated release, NOT this PR): |
| 31 | +//! - reg_effect DEF-COMPLETENESS. The cache's "never stale-wrong" property |
| 32 | +//! rests on `liveness::reg_effect` reporting EVERY GP-register a non-const op |
| 33 | +//! writes (so the reconciliation clears a clobbered alias). That is a broader |
| 34 | +//! property than the #513 reg_effect↔rewrite_op *consistency* oracle, which |
| 35 | +//! only pins that the two AGREE — they could agree and both under-report. The |
| 36 | +//! flip must be gated on op-coverage of reg_effect, not on #513. |
| 37 | +//! - ALIAS-EVICTION. Aliasing `dest` to an existing register makes two live |
| 38 | +//! vregs share one physical register, breaking the spill model's vreg↔reg |
| 39 | +//! bijection. If the OLDER alias is chosen as a spill victim while the |
| 40 | +//! younger keeps the alias, the freed register is reused under the younger → |
| 41 | +//! stale read. Not reachable in today's fixtures (the IR optimizer dedups |
| 42 | +//! two consecutive identical consts before they reach this pass), but the |
| 43 | +//! flip must either prove unreachability or make the spill path alias-aware. |
| 44 | +
|
| 45 | +use std::collections::HashMap; |
| 46 | +use std::path::Path; |
| 47 | +use std::process::Command; |
| 48 | + |
| 49 | +use object::read::elf::ElfFile32; |
| 50 | +use object::{Object, ObjectSection}; |
| 51 | + |
| 52 | +/// Golden FNV-1a-64 of the flag-OFF optimized-path `.text` for `const_cse.wat`. |
| 53 | +/// Captured against the pre-const-CSE tree (stash-compare verified). Re-bless |
| 54 | +/// ONLY when an intentional optimized-path lowering change is made — a surprise |
| 55 | +/// failure here means the supposedly-frozen flag-off path drifted. |
| 56 | +const GOLDEN_OFF_TEXT_FNV1A: u64 = 0xa68a_a2da_e5af_e4a7; |
| 57 | +const GOLDEN_OFF_TEXT_LEN: usize = 576; |
| 58 | + |
| 59 | +fn synth() -> &'static str { |
| 60 | + env!("CARGO_BIN_EXE_synth") |
| 61 | +} |
| 62 | + |
| 63 | +fn fixture() -> std::path::PathBuf { |
| 64 | + Path::new(env!("CARGO_MANIFEST_DIR")) |
| 65 | + .join("../..") |
| 66 | + .join("scripts/repro/const_cse.wat") |
| 67 | +} |
| 68 | + |
| 69 | +/// Compile the const-CSE fixture via the optimized path. `cse` toggles |
| 70 | +/// `SYNTH_CONST_CSE`; returns the raw ELF bytes. |
| 71 | +fn compile(out: &str, cse: bool) -> Vec<u8> { |
| 72 | + let mut cmd = Command::new(synth()); |
| 73 | + if cse { |
| 74 | + cmd.env("SYNTH_CONST_CSE", "1"); |
| 75 | + } |
| 76 | + let status = cmd |
| 77 | + .args([ |
| 78 | + "compile", |
| 79 | + fixture().to_str().unwrap(), |
| 80 | + "-o", |
| 81 | + out, |
| 82 | + "-b", |
| 83 | + "arm", |
| 84 | + "--target", |
| 85 | + "cortex-m4", |
| 86 | + "--all-exports", |
| 87 | + ]) |
| 88 | + .status() |
| 89 | + .expect("run synth compile"); |
| 90 | + assert!(status.success(), "synth compile failed (cse={cse})"); |
| 91 | + std::fs::read(out).expect("read ELF") |
| 92 | +} |
| 93 | + |
| 94 | +/// Map every named section to its bytes. |
| 95 | +fn sections(elf: &[u8]) -> HashMap<String, Vec<u8>> { |
| 96 | + let obj = ElfFile32::<object::Endianness>::parse(elf).expect("parse ELF"); |
| 97 | + let mut out = HashMap::new(); |
| 98 | + for sec in obj.sections() { |
| 99 | + if let Ok(name) = sec.name() |
| 100 | + && !name.is_empty() |
| 101 | + { |
| 102 | + out.insert(name.to_string(), sec.data().unwrap_or(&[]).to_vec()); |
| 103 | + } |
| 104 | + } |
| 105 | + out |
| 106 | +} |
| 107 | + |
| 108 | +/// `.text` bytes of one named function, by reading its symbol size. |
| 109 | +fn func_text_len(elf: &[u8], name: &str) -> usize { |
| 110 | + use object::ObjectSymbol; |
| 111 | + let obj = ElfFile32::<object::Endianness>::parse(elf).expect("parse ELF"); |
| 112 | + for sym in obj.symbols() { |
| 113 | + if sym.name() == Ok(name) { |
| 114 | + return sym.size() as usize; |
| 115 | + } |
| 116 | + } |
| 117 | + panic!("symbol {name} not found"); |
| 118 | +} |
| 119 | + |
| 120 | +fn fnv1a64(bytes: &[u8]) -> u64 { |
| 121 | + let mut h: u64 = 0xcbf2_9ce4_8422_2325; |
| 122 | + for &b in bytes { |
| 123 | + h ^= b as u64; |
| 124 | + h = h.wrapping_mul(0x0000_0100_0000_01b3); |
| 125 | + } |
| 126 | + h |
| 127 | +} |
| 128 | + |
| 129 | +/// CLAIM 1 — flag OFF emits the pinned, pre-change-identical `.text`. |
| 130 | +#[test] |
| 131 | +fn const_cse_off_matches_frozen_baseline_242() { |
| 132 | + let off = compile("/tmp/const_cse_off.elf", false); |
| 133 | + let text = sections(&off).remove(".text").expect(".text present"); |
| 134 | + assert_eq!( |
| 135 | + text.len(), |
| 136 | + GOLDEN_OFF_TEXT_LEN, |
| 137 | + "flag-off .text length drifted from the frozen baseline" |
| 138 | + ); |
| 139 | + assert_eq!( |
| 140 | + fnv1a64(&text), |
| 141 | + GOLDEN_OFF_TEXT_FNV1A, |
| 142 | + "flag-off optimized-path .text drifted from the pre-const-CSE baseline \ |
| 143 | + — the default-off path is supposed to be byte-identical; re-bless the \ |
| 144 | + golden ONLY if this was an intentional optimized-path lowering change" |
| 145 | + ); |
| 146 | +} |
| 147 | + |
| 148 | +/// CLAIM 2 — flag ON strictly shrinks `large3` (a >16-bit const reused 3× with |
| 149 | +/// register headroom): the redundant movw+movt pairs become register aliases. |
| 150 | +#[test] |
| 151 | +fn const_cse_on_shrinks_headroom_function_242() { |
| 152 | + let off = compile("/tmp/const_cse_red_off.elf", false); |
| 153 | + let on = compile("/tmp/const_cse_red_on.elf", true); |
| 154 | + |
| 155 | + let off_len = func_text_len(&off, "large3"); |
| 156 | + let on_len = func_text_len(&on, "large3"); |
| 157 | + |
| 158 | + assert!( |
| 159 | + on_len < off_len, |
| 160 | + "const-CSE must shrink large3 on headroom: off={off_len}B on={on_len}B" |
| 161 | + ); |
| 162 | + |
| 163 | + // Each eliminated `i32.const 100000` removes a movw(4)+movt(4) = 8 bytes; |
| 164 | + // two of the three are redundant, so expect ~16 bytes saved. |
| 165 | + assert!( |
| 166 | + off_len - on_len >= 8, |
| 167 | + "expected ≥8B saved (≥1 movw+movt pair), got {}B", |
| 168 | + off_len - on_len |
| 169 | + ); |
| 170 | +} |
0 commit comments