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
12 changes: 12 additions & 0 deletions crates/celox-design/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ pub struct RuntimeCombObserver<A> {
pub site_id: u32,
pub activation_group: u32,
pub sensitivity: Vec<VarAtomBase<A>>,
/// Occurrence counters of the sensitivity: a counter that moved since
/// the last check activates the observer even when the settled value
/// equals the last one.
#[serde(default = "Vec::new")]
pub occurrence_counters: Vec<A>,
pub written_inputs: Vec<A>,
}

Expand Down Expand Up @@ -826,6 +831,11 @@ pub struct RuntimeSchema<A> {
pub process_origins: Vec<ProcessOrigin>,
/// Counted signals and their occurrence counters.
pub watches: Vec<EventWatch<A>>,
/// Bit ranges whose changes bump an occurrence counter: the sensitivity
/// of change-gated combinational processes and comb observers. Design
/// code counts its own writes; the runtime counts the writes it makes,
/// such as the nonblocking updates it publishes.
pub occurrence_probes: Vec<(VarAtomBase<A>, A)>,
/// Wait sites of the process kernels. A process's sites appear in the
/// order of its local site indices.
pub wait_sites: Vec<WaitSite<A>>,
Expand Down Expand Up @@ -864,6 +874,7 @@ impl<A> Default for RuntimeSchema<A> {
processes: Vec::new(),
process_origins: Vec::new(),
watches: Vec::new(),
occurrence_probes: Vec::new(),
wait_sites: Vec::new(),
nba_sites: Vec::new(),
host_request_sites: Vec::new(),
Expand Down Expand Up @@ -1661,6 +1672,7 @@ mod tests {
id: initial.address,
access: BitAccess { lsb: 3, msb: 7 },
}],
occurrence_counters: Vec::new(),
written_inputs: vec![initial.address],
});
runtime.testbench_read_roots.insert(initial.address);
Expand Down
10 changes: 7 additions & 3 deletions crates/celox-design/src/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ pub struct ScheduleModel<A> {
/// Counted signals with all their counters, and the canonical domains
/// each counter triggers, in [`RuntimeSchema::watches`] order.
pub counted: Vec<CountedModel<A>>,
/// Bit ranges whose changes bump an occurrence counter
/// ([`RuntimeSchema::occurrence_probes`]).
pub probes: Vec<(crate::VarAtomBase<A>, A)>,
/// Process kernels in kernel order.
pub processes: Vec<ProcessModel<A>>,
/// Signals a wait watches, which the host's writes must be logged for.
pub waited: Vec<A>,
/// The design has process kernels or counted signals. Without either,
/// a round of the scheduler is the batch step.
/// The design has process kernels. Without them, a round of the
/// scheduler is the batch step, which also checks counted signals.
pub timed: bool,
/// Some nonblocking site has a pending flag.
pub has_pending_nba_sites: bool,
Expand Down Expand Up @@ -131,8 +134,9 @@ impl<A: Copy + Eq + std::hash::Hash> ScheduleModel<A> {
Self {
events: design.events.clone(),
kinds: design.event_kinds(),
timed: !processes.is_empty() || !counted.is_empty(),
timed: !processes.is_empty(),
counted,
probes: schema.occurrence_probes.clone(),
processes,
waited,
has_pending_nba_sites,
Expand Down
164 changes: 162 additions & 2 deletions crates/celox-frontend-core/src/symbolic/assembly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,7 @@ pub fn schedule_symbolic_rtl_reporting_loops(
})
.collect();
let declaration_probes;
let occurrence_probes: Vec<(VarAtomBase<AbsoluteAddr>, AbsoluteAddr)>;
let counted = {
// Flip-flop event domains and the aliases that resolve to them.
let event_addresses: HashSet<AbsoluteAddr> = eval_apply_ffs
Expand Down Expand Up @@ -769,6 +770,13 @@ pub fn schedule_symbolic_rtl_reporting_loops(
}
}
}
for observer in &comb_observers {
for &(source, counter) in &observer.sensitivity_counters {
if !probes.contains(&(source, counter)) {
probes.push((source, counter));
}
}
}
// A direct wire/port connection carries occurrences as well as its
// settled value. Preserve them when a kernel writes the upstream
// object twice before the copy path is evaluated. Only equal-width,
Expand Down Expand Up @@ -816,6 +824,13 @@ pub fn schedule_symbolic_rtl_reporting_loops(
}
}
declaration_probes = probes.clone();
occurrence_probes = probes.clone();
// Signals a flip-flop evaluation may store twice in its working
// state. Each store is an update event (IEEE 1800-2023 4.3, 10.4.2)
// that the commit of the final value alone would not show; the
// working state is seeded with the stable value first, so a store
// compares with the value its update replaces.
let ff_multi_stored = ff_multi_stored(&eval_apply_ffs, &eval_only_ffs, &parallel_ff_units);
for (source, counter) in probes {
let width = source.access.msb - source.access.lsb + 1;
let four_state = four_state && variables[&source.id].1;
Expand Down Expand Up @@ -862,6 +877,27 @@ pub fn schedule_symbolic_rtl_reporting_loops(
crate::watch::instrument_unit(&mut part.apply, code);
}
}
if ff_multi_stored.contains(&source.id) {
let working =
|address| RegionedAbsoluteAddr::from_absolute_addr(WORKING_REGION, address);
let code = |address: &RegionedAbsoluteAddr| {
(address.region == WORKING_REGION && address.absolute_addr() == source.id).then(
|| crate::watch::WatchCode {
signal: working(source.id),
width,
offset: offset.clone(),
four_state,
counters: vec![(CounterKind::Changes, stable(counter))],
},
)
};
for_each_ff_evaluation(
&mut eval_apply_ffs,
&mut eval_only_ffs,
&mut parallel_ff_units,
|unit| instrument_stores(unit, code),
);
}
for path in &mut comb_blocks {
if let LogicPathTarget::Process { unit, .. } = &mut path.target {
crate::watch::instrument_unit(unit, |address| {
Expand Down Expand Up @@ -894,15 +930,51 @@ pub fn schedule_symbolic_rtl_reporting_loops(
.flatten()
.flat_map(|split| [&mut split.active, &mut split.nba]),
);
instrument_counted_signals(
let counted = instrument_counted_signals(
&timing,
counted,
&variables,
four_state,
&mut processes,
&mut comb_blocks,
ff_units,
)
);
// A counted signal without pending slots (a design without waits)
// is counted at its commit, which sees only the final value. Count
// each store of an evaluation that may store it twice as well.
let pending: HashSet<AbsoluteAddr> = timing
.pending_slots
.iter()
.map(|slots| slots.signal)
.collect();
for (signal, slots) in &counted {
if !ff_multi_stored.contains(signal) || pending.contains(signal) {
continue;
}
let (width, is_4state) = variables[signal];
let regioned = RegionedAbsoluteAddr::from_absolute_addr;
let code = |address: &RegionedAbsoluteAddr| {
(address.region == WORKING_REGION && address.absolute_addr() == *signal).then(
|| crate::watch::WatchCode {
signal: regioned(WORKING_REGION, *signal),
offset: celox_sir::SIROffset::Static(0),
width,
four_state: four_state && is_4state,
counters: CounterKind::ALL
.into_iter()
.map(|kind| (kind, regioned(STABLE_REGION, slots.counter(kind))))
.collect(),
},
)
};
for_each_ff_evaluation(
&mut eval_apply_ffs,
&mut eval_only_ffs,
&mut parallel_ff_units,
|unit| instrument_stores(unit, code),
);
}
counted
};
// Combinational values and the variables they are computed from.
let comb_sources = comb_sources(&comb_blocks, &global_arena);
Expand Down Expand Up @@ -1648,6 +1720,11 @@ pub fn schedule_symbolic_rtl_reporting_loops(
site_id: observer.site_id,
activation_group: observer.activation_group,
sensitivity: observer.sensitivity.clone(),
occurrence_counters: observer
.sensitivity_counters
.iter()
.map(|&(_, counter)| counter)
.collect(),
written_inputs: observer.written_inputs.clone(),
})
.collect();
Expand Down Expand Up @@ -1862,6 +1939,11 @@ pub fn schedule_symbolic_rtl_reporting_loops(
access: atom.access,
})
.collect(),
occurrence_counters: observer
.occurrence_counters
.into_iter()
.map(project)
.collect(),
written_inputs: observer.written_inputs.into_iter().map(project).collect(),
})
.collect();
Expand Down Expand Up @@ -1995,6 +2077,24 @@ pub fn schedule_symbolic_rtl_reporting_loops(
.chain(host_request_sites.iter().flat_map(|site| site.slots()))
.collect();
let declaration_startup = declaration_startup.map(|startup| startup.map(project));
let occurrence_probes: Vec<(VarAtomBase<StateAddr>, StateAddr)> = occurrence_probes
.into_iter()
.map(|(source, counter)| {
(
VarAtomBase {
id: project(source.id),
access: source.access,
},
project(counter),
)
})
.collect();
// The runtime counts the occurrences its own writes cause.
timing_read_roots.extend(
occurrence_probes
.iter()
.flat_map(|(source, counter)| [source.id, *counter]),
);
if let Some(startup) = &declaration_startup {
timing_read_roots.extend(
startup
Expand Down Expand Up @@ -2067,6 +2167,7 @@ pub fn schedule_symbolic_rtl_reporting_loops(
processes: process_slots,
process_origins,
watches,
occurrence_probes,
wait_sites,
nba_sites,
host_request_sites,
Expand Down Expand Up @@ -3618,6 +3719,65 @@ fn relocate_timing(
metadata
}

type FfUnits = HashMap<AbsoluteAddr, Vec<ExecutionUnit<RegionedAbsoluteAddr>>>;
type FfParts = HashMap<AbsoluteAddr, Vec<(InstanceId, FfPart<RegionedAbsoluteAddr>)>>;

/// The signals some flip-flop evaluation may store twice in its working
/// state in one execution.
fn ff_multi_stored(
eval_apply_ffs: &FfUnits,
eval_only_ffs: &FfUnits,
parallel_ff_units: &FfParts,
) -> HashSet<AbsoluteAddr> {
let working = |address: &RegionedAbsoluteAddr| {
(address.region == WORKING_REGION).then(|| address.absolute_addr())
};
eval_apply_ffs
.values()
.chain(eval_only_ffs.values())
.flatten()
.chain(
parallel_ff_units
.values()
.flatten()
.map(|(_, part)| &part.evaluate),
)
.flat_map(|unit| crate::watch::multi_store_only_candidates(unit, working))
.collect()
}

/// [`crate::watch::instrument_unit`] for the stores alone: the commits that
/// seed a flip-flop evaluation's working state are not writes of the
/// process.
fn instrument_stores(
unit: &mut ExecutionUnit<RegionedAbsoluteAddr>,
watch_for: impl Fn(&RegionedAbsoluteAddr) -> Option<crate::watch::WatchCode<RegionedAbsoluteAddr>>,
) {
crate::watch::instrument_writes(unit, |instruction| match instruction {
SIRInstruction::Store(address, ..) => watch_for(address),
_ => None,
});
}

/// Run `f` on every flip-flop evaluation.
fn for_each_ff_evaluation(
eval_apply_ffs: &mut FfUnits,
eval_only_ffs: &mut FfUnits,
parallel_ff_units: &mut FfParts,
mut f: impl FnMut(&mut ExecutionUnit<RegionedAbsoluteAddr>),
) {
for unit in eval_apply_ffs
.values_mut()
.chain(eval_only_ffs.values_mut())
.flatten()
{
f(unit);
}
for (_, part) in parallel_ff_units.values_mut().flatten() {
f(&mut part.evaluate);
}
}

/// Counted signals: those a unit may store twice between two runtime checks
/// (a frontend declared counters for them) and that a flip-flop domain or a
/// wait watches, by signal.
Expand Down
10 changes: 10 additions & 0 deletions crates/celox-frontend-core/src/symbolic/flattening.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,16 @@ fn convert_comb_observer<
.iter()
.map(|v| VarAtomBase::new(f(&v.id), v.access.lsb, v.access.msb))
.collect(),
sensitivity_counters: observer
.sensitivity_counters
.iter()
.map(|(v, counter)| {
(
VarAtomBase::new(f(&v.id), v.access.lsb, v.access.msb),
f(counter),
)
})
.collect(),
local_inputs: observer
.local_inputs
.iter()
Expand Down
13 changes: 13 additions & 0 deletions crates/celox-frontend-core/src/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,19 @@ pub fn multi_store_candidates<A, K: Eq + std::hash::Hash + Clone>(
})
}

/// Like [`multi_store_candidates`], over the stores alone. A flip-flop
/// evaluation seeds its working state with commits; only its stores are
/// writes of the process.
pub fn multi_store_only_candidates<A, K: Eq + std::hash::Hash + Clone>(
unit: &ExecutionUnit<A>,
key: impl Fn(&A) -> Option<K>,
) -> Vec<K> {
multi_write_candidates(unit, |instruction| match instruction {
SIRInstruction::Store(address, ..) => key(address),
_ => None,
})
}

/// [`multi_store_candidates`] with the written key of each instruction
/// given by `key`, for callers that count only some kinds of writes.
pub fn multi_write_candidates<A, K: Eq + std::hash::Hash + Clone>(
Expand Down
35 changes: 34 additions & 1 deletion crates/celox-frontend-sv/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3723,6 +3723,14 @@ pub(crate) fn attach_instance_glue(
));
}
}
// A flip-flop evaluation stores each update in the working state; two
// of one execution are two update events (IEEE 1800-2023 4.3, 10.4.2).
for unit in module.eval_only_ff_blocks.values() {
candidates.extend(celox_frontend_core::watch::multi_store_only_candidates(
unit,
|address| (address.region == WORKING_REGION).then_some(address.var_id),
));
}
module.arena = arena;
// Combinational event sites follow the flip-flop ones, including the
// events run-time loops emit.
Expand Down Expand Up @@ -4420,10 +4428,35 @@ fn lower_comb_processes(
.collect();
}
}
// The sites of one process activate together.
// The sites of one process activate together. Every change of a
// source schedules the process (IEEE 1800-2023 4.3), also one that a
// later write of the same time slot undoes, so each sensitivity atom
// counts its occurrences.
let base = sites.len() as u32;
let mut counters: Vec<(VarAtomBase<SourceVarId>, SourceVarId)> = Vec::new();
for mut observer in std::mem::take(&mut comb.observers) {
observer.activation_group = base;
for atom in &observer.sensitivity {
if !counters.iter().any(|(counted, _)| counted == atom) {
let (counter, _) = comb.m.temp(
"comb_occurrences",
celox_design::EVENT_COUNTER_WIDTH,
false,
false,
);
counters.push((*atom, counter));
}
}
observer.sensitivity_counters = observer
.sensitivity
.iter()
.map(|atom| {
*counters
.iter()
.find(|(counted, _)| counted == atom)
.expect("a counter for every sensitivity atom")
})
.collect();
observers.push(observer);
}
sites.append(&mut comb.sites);
Expand Down
Loading
Loading