From a3a7832fd79fe2580d73d98f88eb7e1eb4398dc5 Mon Sep 17 00:00:00 2001 From: tignear Date: Tue, 27 Oct 2026 14:20:55 +0900 Subject: [PATCH] perf(settle): bound estimated partitions and preserve sparse updates Keep estimated partition overhead bounded as designs grow, targeting at most 64 coarse partitions with at least 16000 expression nodes per partition. A fraction of dirty flags is not a cost estimate when clean partitions contain most of the work: use full settle only when all partitions are dirty or an untracked raw view requires it. Partitioned settle remains disabled by default for O1/O2. Validation: frontend-core and SLT unit suites, partition APIs across backends, a 3-of-9 update regression retaining the partial path, and raw-pointer full-settle preservation; clippy and formatting. Change-Id: I9e4342b5784cd4503a7987b3251cdbf654e72040 --- .../src/symbolic/assembly.rs | 32 ++++++++- crates/celox-slt/src/scheduler.rs | 19 ++---- crates/celox/tests/settle_partitions.rs | 66 +++++++++++++++++++ 3 files changed, 102 insertions(+), 15 deletions(-) diff --git a/crates/celox-frontend-core/src/symbolic/assembly.rs b/crates/celox-frontend-core/src/symbolic/assembly.rs index 88712b02971..3805e53f6a6 100644 --- a/crates/celox-frontend-core/src/symbolic/assembly.rs +++ b/crates/celox-frontend-core/src/symbolic/assembly.rs @@ -2604,7 +2604,14 @@ pub fn schedule_symbolic_rtl_reporting_loops( /// [`SettlePartitionMode::Estimated`] partitions. const MIN_PARTITIONED_SETTLE_COST: usize = 20_000; /// Expression nodes per partition of an estimated partitioned settle. -const SETTLE_PARTITION_COST: usize = 2_000; +const SETTLE_PARTITION_COST: usize = 16_000; +/// Bound flag tests, publication checks, and their compiler analyses on large +/// designs instead of growing the number of partitions with every source node. +const TARGET_ESTIMATED_SETTLE_PARTITIONS: usize = 64; + +fn estimated_settle_partition_cost(cost: usize) -> usize { + SETTLE_PARTITION_COST.max(cost.div_ceil(TARGET_ESTIMATED_SETTLE_PARTITIONS)) +} /// The cost per partition of a partitioned settle of `comb_blocks`, or /// `None` to keep one pass. @@ -2634,7 +2641,7 @@ fn settle_partitioning( .iter() .map(|path| scheduler::path_cost(arena, path)) .sum(); - (cost >= MIN_PARTITIONED_SETTLE_COST).then_some(SETTLE_PARTITION_COST) + (cost >= MIN_PARTITIONED_SETTLE_COST).then(|| estimated_settle_partition_cost(cost)) } } } @@ -6305,3 +6312,24 @@ mod tests { assert!(copied > 50 * 4 + 50, "{copied} probes"); } } + +#[cfg(test)] +mod estimated_settle_budget_tests { + use super::*; + + #[test] + fn estimated_guard_count_stays_bounded_as_the_program_grows() { + for cost in [ + 20_000, + 100_000, + 1_000_000, + 3_000_000, + 100_000_000, + usize::MAX, + ] { + let target = estimated_settle_partition_cost(cost); + assert!(target >= SETTLE_PARTITION_COST); + assert!(cost.div_ceil(target) <= TARGET_ESTIMATED_SETTLE_PARTITIONS); + } + } +} diff --git a/crates/celox-slt/src/scheduler.rs b/crates/celox-slt/src/scheduler.rs index 12ac16440e6..410c897e497 100644 --- a/crates/celox-slt/src/scheduler.rs +++ b/crates/celox-slt/src/scheduler.rs @@ -3513,12 +3513,6 @@ struct PlannedPartition { pinned: bool, } -/// Run every partition of a settle that starts with at least -/// `1 / FULL_SETTLE_DIVISOR` of its flags set (or the untracked byte), and -/// skip comparing what the partitions write: running the whole settle then -/// costs about a single pass. -const FULL_SETTLE_DIVISOR: usize = 3; - /// A chunk of an object a partition writes, compared before and after the /// partition runs. struct WatchedChunk { @@ -3929,9 +3923,10 @@ impl<'a, Addr: Clone + Eq + Ord + Hash + Debug + Copy + Display> WorkLowering<'a } } - /// Start a settle of `count` partitions: run all of them when the - /// untracked byte or at least `1 / FULL_SETTLE_DIVISOR` of their flags - /// are set, recorded in the full byte. + /// Start a settle of `count` partitions. Skip publication comparisons + /// when every partition will run anyway, or an untracked raw view requires + /// a full settle. A fraction of dirty flags is not a cost estimate: a + /// clean partition can contain most of the program's work. fn begin_settle(&mut self, flags: Addr, count: usize) { let all = self.builder.alloc_bit(count * 8, false); self.builder.emit(SIRInstruction::Load( @@ -3945,10 +3940,8 @@ impl<'a, Addr: Clone + Eq + Ord + Hash + Debug + Copy + Display> WorkLowering<'a self.builder .emit(SIRInstruction::Unary(dirty, UnaryOp::PopCount, all)); let threshold = self.builder.alloc_bit(count_width, false); - self.builder.emit(SIRInstruction::Imm( - threshold, - SIRValue::new(count.div_ceil(FULL_SETTLE_DIVISOR) as u64), - )); + self.builder + .emit(SIRInstruction::Imm(threshold, SIRValue::new(count as u64))); let busy = self.builder.alloc_bit(1, false); self.builder.emit(SIRInstruction::Binary( busy, diff --git a/crates/celox/tests/settle_partitions.rs b/crates/celox/tests/settle_partitions.rs index 4237e5286b3..b9ff2be6e69 100644 --- a/crates/celox/tests/settle_partitions.rs +++ b/crates/celox/tests/settle_partitions.rs @@ -147,6 +147,72 @@ fn partitioned_native_matches_single_pass() { assert_eq!(trace!(partitioned().build_native().unwrap()), reference()); } +/// A count of dirty partitions says nothing about their share of the work: +/// unrelated partitions can contain much wider expressions or larger processes. +/// Updating one input must not promote its three readers to a full settle. +#[test] +fn sparse_partition_updates_do_not_promote_unrelated_work() { + const SOURCE: &str = r#" +module Top ( + a: input logic<8>, b: input logic<8>, c: input logic<8>, + a0: output logic<8>, a1: output logic<8>, a2: output logic<8>, + b0: output logic<8>, b1: output logic<8>, b2: output logic<8>, + c0: output logic<8>, c1: output logic<8>, c2: output logic<8>, +) { + assign a0 = a + 8'd1; assign a1 = a + 8'd2; assign a2 = a + 8'd3; + assign b0 = b + 8'd1; assign b1 = b + 8'd2; assign b2 = b + 8'd3; + assign c0 = c + 8'd1; assign c1 = c + 8'd2; assign c2 = c + 8'd3; +} +"#; + let mut sim = Simulator::builder(SOURCE, "Top") + .settle_partition(SettlePartition::Always) + .build_interpreter() + .unwrap(); + let settle = sim.layout().settle.as_ref().unwrap().clone(); + let flags = || { + let (memory, _) = sim.memory_as_ptr(); + // SAFETY: these offsets describe this simulator's state allocation. + unsafe { + std::slice::from_raw_parts(memory.add(settle.flags_offset), settle.partitions + 2) + .to_vec() + } + }; + // Initial evaluation still runs every partition. + let initial = flags(); + assert!(initial[..settle.partitions].iter().all(|&flag| flag == 1)); + sim.eval_comb().unwrap(); + let a = sim.signal("a"); + sim.modify(|io| io.set(a, 7u8)).unwrap(); + let (memory, _) = sim.memory_as_ptr(); + let pending = + unsafe { std::slice::from_raw_parts(memory.add(settle.flags_offset), settle.partitions) }; + let dirty = pending.iter().filter(|&&flag| flag != 0).count(); + assert_eq!(dirty, 3); + assert_eq!(settle.partitions, 9); + sim.eval_comb().unwrap(); + let (memory, _) = sim.memory_as_ptr(); + assert_eq!( + unsafe { *memory.add(settle.flags_offset + settle.partitions + 1) }, + 0, + "a partial update promoted unrelated partitions to a full settle" + ); + let a2 = sim.signal("a2"); + let b2 = sim.signal("b2"); + assert_eq!(sim.get(a2), BigUint::from(10u8)); + assert_eq!(sim.get(b2), BigUint::from(3u8)); + + // A retained raw view still requires full evaluation on every settle. + let (memory, _) = sim.memory_as_mut_ptr(); + // SAFETY: a names an eight-bit input in this allocation. + unsafe { *memory.add(a.offset) = 11 }; + sim.eval_comb().unwrap(); + assert_eq!(sim.get(a2), BigUint::from(14u8)); + assert_eq!( + unsafe { *memory.add(settle.flags_offset + settle.partitions + 1) }, + 1 + ); +} + /// Loading a state file writes every object, so every partition reading one /// must run again; the settle's flags themselves are not state. #[test]