Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions crates/celox-frontend-core/src/symbolic/assembly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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))
}
}
}
Expand Down Expand Up @@ -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);
}
}
}
19 changes: 6 additions & 13 deletions crates/celox-slt/src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3513,12 +3513,6 @@ struct PlannedPartition<Addr> {
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<Addr> {
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand Down
66 changes: 66 additions & 0 deletions crates/celox/tests/settle_partitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Loading