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
71 changes: 66 additions & 5 deletions crates/celox-frontend-sv/src/lowering/ff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5565,10 +5565,28 @@ impl<'p, 'a> Ff<'p, 'a> {
let mut changed = self.b.alloc_bit(1, false);
self.b
.emit(SIRInstruction::Imm(changed, SIRValue::new(0u8)));
// A large sensitivity list need not retain every old and current
// value until the final comparison. Check bounded groups, stop at
// the first change, and refresh the snapshots only when needed.
// Occurrence counters also observe transient changes; retain their
// existing path until they can share the same grouped check.
let grouped = sources.len() >= 4 && !self.input_occurrences;
let capture = grouped.then(|| self.b.new_block());
let execute = self.b.new_block();
let done = self.b.new_block();
if let Some(capture) = capture {
let check = self.b.new_block();
self.b.seal_block(SIRTerminator::Branch {
cond: init,
true_block: (check, vec![]),
false_block: (capture, vec![]),
});
self.b.switch_to_block(check);
}
let mut snapshots = Vec::new();
let mut sensitivity_counters = Vec::new();
let mut counter_snapshots = Vec::new();
for source in &sources {
for (ordinal, source) in sources.iter().enumerate() {
let width = source.access.msb - source.access.lsb + 1;
let (saved, _) = self.m.temp(
"comb_input",
Expand Down Expand Up @@ -5603,6 +5621,26 @@ impl<'p, 'a> Ff<'p, 'a> {
.emit(SIRInstruction::Binary(next, changed, BinaryOp::Or, differs));
changed = next;
snapshots.push((saved, width, current));
if let Some(capture) = capture
&& ((ordinal + 1).is_multiple_of(4) || ordinal + 1 == sources.len())
{
let next = if ordinal + 1 == sources.len() {
done
} else {
self.b.new_block()
};
self.b.seal_block(SIRTerminator::Branch {
cond: changed,
true_block: (capture, vec![]),
false_block: (next, vec![]),
});
if next != done {
self.b.switch_to_block(next);
changed = self.b.alloc_bit(1, false);
self.b
.emit(SIRInstruction::Imm(changed, SIRValue::new(0u8)));
}
}
if !self.input_occurrences {
continue;
}
Expand Down Expand Up @@ -5641,8 +5679,18 @@ impl<'p, 'a> Ff<'p, 'a> {
sensitivity_counters.push((*source, counter));
counter_snapshots.push((counter, saved_counter));
}
if let Some(capture) = capture {
self.b.switch_to_block(capture);
}
let run = self.b.alloc_bit(1, false);
if runs_at_time_zero {
if grouped && runs_at_time_zero {
self.b.emit(SIRInstruction::Imm(run, SIRValue::new(1u8)));
} else if grouped {
// Ordinary event controls establish their initial baseline
// without executing; always_comb executes on that first visit.
self.b
.emit(SIRInstruction::Binary(run, init, BinaryOp::Or, init));
} else if runs_at_time_zero {
let first = self.b.alloc_bit(1, false);
self.b
.emit(SIRInstruction::Unary(first, UnaryOp::LogicNot, init));
Expand All @@ -5653,14 +5701,27 @@ impl<'p, 'a> Ff<'p, 'a> {
.emit(SIRInstruction::Binary(run, init, BinaryOp::And, changed));
}
// Snapshot before executing: writes in the body do not retrigger it.
for &(saved, width, current) in &snapshots {
for (source, &(saved, width, current)) in sources.iter().zip(&snapshots) {
let current = if grouped {
// The checks do not mutate input state. Load each value at
// its store, avoiding a live snapshot set spanning the list.
let current = self.b.alloc_logic(width);
let offset = sv_memory_offset(self.m.var(source.id), source.access.lsb, width);
self.b.emit(SIRInstruction::Load(
current,
stable(source.id),
offset,
width,
));
current
} else {
current
};
self.store(saved, SIROffset::Static(0), width, current);
}
let one = self.b.alloc_bit(1, false);
self.b.emit(SIRInstruction::Imm(one, SIRValue::new(1u8)));
self.store(initialized, SIROffset::Static(0), 1, one);
let execute = self.b.new_block();
let done = self.b.new_block();
self.b.seal_block(SIRTerminator::Branch {
cond: run,
true_block: (execute, vec![]),
Expand Down
133 changes: 79 additions & 54 deletions crates/celox-slt/src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3675,9 +3675,27 @@ fn plan_settle_partitions<Addr: Clone + Eq + Ord + Hash + Debug + Copy + Display
copy_source.insert(target, source);
}
}
// WorkLowering executes an acyclic process only at its first
// projection: that execution writes every output. Later projections
// emit no code and must not acquire duplicate reads, writes or watches.
// Cyclic components and guarded regions retain their existing plan.
let mut run_processes = HashSet::default();
let mut skipped_work = HashSet::default();
for (index, work) in prepared.scheduled_work.iter().enumerate() {
if let ScheduledWork::CombPath(path) = work
&& !prepared.adj[*path].contains(path)
&& let LogicPathTarget::Process { process, .. } = &input[*path].target
&& !run_processes.insert(*process)
{
skipped_work.insert(index);
}
}
let mut first_work = Vec::new();
let mut cost = 0;
for (index, work) in prepared.scheduled_work.iter().enumerate() {
if skipped_work.contains(&index) {
continue;
}
if first_work.is_empty() || cost >= partitioning.target_cost {
first_work.push(index);
cost = 0;
Expand All @@ -3704,6 +3722,9 @@ fn plan_settle_partitions<Addr: Clone + Eq + Ord + Hash + Debug + Copy + Display
let mut previous = vec![BTreeSet::new(); count];
let mut pinned = vec![false; count];
for (index, work) in prepared.scheduled_work.iter().enumerate() {
if skipped_work.contains(&index) {
continue;
}
let partition = partition_of_work[index];
for &path_index in work_paths(work) {
let path = &input[path_index];
Expand Down Expand Up @@ -3930,31 +3951,11 @@ impl<'a, Addr: Clone + Eq + Ord + Hash + Debug + Copy + Display> WorkLowering<'a
}
}

/// Start a settle of `count` partitions. Record whether all flags are set
/// 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.
/// Start a settle of `count` partitions. An untracked raw view requires
/// every partition to run. When all individual flags are already set,
/// their own guards suffice; counting them would scan the complete flag
/// array on every settle without changing which partitions execute.
fn begin_settle(&mut self, flags: Addr, count: usize) {
let all = self.builder.alloc_bit(count * 8, false);
self.builder.emit(SIRInstruction::Load(
all,
flags,
SIROffset::Static(0),
count * 8,
));
let count_width = UnaryOp::PopCount.result_width(count * 8);
let dirty = self.builder.alloc_bit(count_width, false);
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 as u64)));
let busy = self.builder.alloc_bit(1, false);
self.builder.emit(SIRInstruction::Binary(
busy,
dirty,
BinaryOp::GeU,
threshold,
));
let untracked = self.builder.alloc_bit(8, false);
self.builder.emit(SIRInstruction::Load(
untracked,
Expand All @@ -3968,15 +3969,11 @@ impl<'a, Addr: Clone + Eq + Ord + Hash + Debug + Copy + Display> WorkLowering<'a
let raw = self.builder.alloc_bit(1, false);
self.builder
.emit(SIRInstruction::Binary(raw, untracked, BinaryOp::Ne, zero));
let full = self.builder.alloc_bit(1, false);
self.builder
.emit(SIRInstruction::Binary(full, busy, BinaryOp::Or, raw));
let one = self.builder.alloc_bit(8, false);
self.builder
.emit(SIRInstruction::Imm(one, SIRValue::new(1u64)));
let byte = self.builder.alloc_bit(8, false);
self.builder
.emit(SIRInstruction::Mux(byte, full, one, zero));
self.builder.emit(SIRInstruction::Mux(byte, raw, one, zero));
self.builder.emit(SIRInstruction::Store(
flags,
SIROffset::Static((count + 1) * 8),
Expand Down Expand Up @@ -6468,31 +6465,59 @@ mod tests {
.chain(outputs.map(|output| output + 100))
.map(|variable| (variable, if variable == PROCESS { 1 } else { 8 }))
.collect();
let result = sort(
paths,
&arena,
&crate::HashSet::default(),
&crate::HashMap::default(),
false,
&widths,
1,
)
.unwrap();
let stores = |variable| {
result
.execution_units
.iter()
.flat_map(|unit| unit.blocks.values())
.flat_map(|block| &block.instructions)
.filter(|instruction| {
matches!(instruction, SIRInstruction::Store(target, ..) if *target == variable)
})
.count()
};
assert_eq!(stores(PROCESS), 1, "the unit runs once");
for output in outputs {
assert_eq!(stores(output), 1);
assert_eq!(stores(output + 100), 1, "each consumer is emitted");
for partitioning in [
None,
Some(super::SettlePartitioning {
flags: 99,
target_cost: 1,
}),
] {
let result = super::sort_with_unpacked_element_widths(
paths.clone(),
&arena,
&crate::HashSet::default(),
&crate::HashMap::default(),
false,
&widths,
&crate::HashMap::default(),
1,
partitioning,
)
.unwrap();
let stores = |variable| {
result.execution_units.iter()
.flat_map(|unit| unit.blocks.values())
.flat_map(|block| &block.instructions)
.filter(|instruction| matches!(instruction, SIRInstruction::Store(target, ..) if *target == variable))
.count()
};
assert_eq!(stores(PROCESS), 1, "the unit runs once");
for output in outputs {
assert_eq!(stores(output), 1);
assert_eq!(stores(output + 100), 1, "each consumer is emitted");
}
if !result.partitions.is_empty() {
assert_eq!(result.partitions.len(), 4, "one body and three consumers");
assert_eq!(
result
.partitions
.iter()
.filter(|part| part.reads.contains(&INPUT))
.count(),
1
);
for output in outputs.into_iter().chain([PROCESS]) {
assert_eq!(
result
.partitions
.iter()
.filter(|part| part.writes.contains(&output))
.count(),
1,
"only the executing partition watches output {output}"
);
}
}
}
}
}
41 changes: 41 additions & 0 deletions crates/celox-test-suite/src/sv/cases/always_comb.vtest
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,44 @@
(modify (set a 0xe318)) (assert_eq y 15)
(modify (set a 0xe680)) (assert_eq y 14)
(modify (set a 0x1000)) (assert_eq y 0))

; A long event list must refresh every baseline after an early difference,
; observe a last-input X/Z-only change, and stay dormant for repeated values.
; The intermediate pulse makes executing the body unnecessarily observable.
(case large_sensitivity_list_preserves_initial_wait_and_four_state_changes
(four_state)
(source "large_sensitivity.sv" #"
module Top;
logic a, b, c, d, e, f, g, h, i;
logic [8:0] observed = 9'h155;
logic [8:0] combinational;
bit pulse = 0;
int count = 0;
always_comb combinational = {a,b,c,d,e,f,g,h,i};
always @(a or b or c or d or e or f or g or h or i) begin
pulse = 0;
observed = {a,b,c,d,e,f,g,h,i};
pulse = 1;
end
always @(posedge pulse) count = count + 1;
initial begin
#1 $display("%b %b %0d", observed, combinational, count);
a = 0;
#1 $display("%b %b %0d", observed, combinational, count);
i = 1;
#1 $display("%b %b %0d", observed, combinational, count);
i = 1;
#1 $display("%b %b %0d", observed, combinational, count);
i = 1'bx;
#1 $display("%b %b %0d", observed, combinational, count);
i = 1'bz;
#1 $display("%b %b %0d", observed, combinational, count);
a = 1;
#1 $display("%b %b %0d", observed, combinational, count);
$finish;
end
endmodule
"#)
(top Top)
(run_to_finish)
(expect_output "101010101 xxxxxxxxx 0\n0xxxxxxxx 0xxxxxxxx 1\n0xxxxxxx1 0xxxxxxx1 2\n0xxxxxxx1 0xxxxxxx1 2\n0xxxxxxxx 0xxxxxxxx 3\n0xxxxxxxz 0xxxxxxxz 4\n1xxxxxxxz 1xxxxxxxz 5\n"))
Loading
Loading