Skip to content
19 changes: 19 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 @@ -999,6 +1009,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 @@ -1054,6 +1065,12 @@ pub struct EventTopology<A> {
/// a signal needs a settle before its domains evaluate.
#[serde(default)]
pub combinationally_read: BTreeSet<A>,
/// Canonical events whose own domain reads no level of their signal: its
/// flip-flops do not sample the signal as data. With
/// [`Self::combinationally_read`] it says whether a run of the domain
/// observes the signal's level. Absent means it may.
#[serde(default)]
pub level_unread_by_domain: BTreeSet<A>,
}

/// A clock computed combinationally from an event's signal.
Expand Down Expand Up @@ -1149,6 +1166,7 @@ impl<A> Default for EventTopology<A> {
combinational_derived: BTreeMap::new(),
independent_derived: BTreeSet::new(),
combinationally_read: BTreeSet::new(),
level_unread_by_domain: BTreeSet::new(),
}
}
}
Expand Down Expand Up @@ -1797,6 +1815,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 @@ -134,8 +137,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
190 changes: 188 additions & 2 deletions crates/celox-frontend-core/src/symbolic/assembly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,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 @@ -774,6 +775,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 @@ -821,6 +829,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 @@ -867,6 +882,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 @@ -899,15 +935,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 @@ -956,6 +1028,7 @@ pub fn schedule_symbolic_rtl_reporting_loops(
combinational_derived,
independent_derived,
combinationally_read,
level_unread_by_domain,
} = timed_sub!(
"analyze_clock_dependencies",
analyze_clock_dependencies(
Expand Down Expand Up @@ -1653,6 +1726,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 @@ -1802,6 +1880,7 @@ pub fn schedule_symbolic_rtl_reporting_loops(
.collect(),
independent_derived: independent_derived.into_iter().map(project).collect(),
combinationally_read: combinationally_read.into_iter().map(project).collect(),
level_unread_by_domain: level_unread_by_domain.into_iter().map(project).collect(),
combinational_derived: combinational_derived
.into_iter()
.map(|(event, clocks)| {
Expand Down Expand Up @@ -1867,6 +1946,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 @@ -2012,6 +2096,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 @@ -2084,6 +2186,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 @@ -3694,6 +3797,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 Expand Up @@ -4629,6 +4791,8 @@ struct ClockAnalysis {
independent_derived: BTreeSet<AbsoluteAddr>,
/// Events whose signal combinational logic reads.
combinationally_read: BTreeSet<AbsoluteAddr>,
/// Events whose own domain does not read their signal.
level_unread_by_domain: BTreeSet<AbsoluteAddr>,
}

fn analyze_clock_dependencies(
Expand Down Expand Up @@ -5023,6 +5187,27 @@ fn analyze_clock_dependencies(
}
Some((reads, writes))
};
// A domain reads its own clock's level when a flip-flop samples it as
// data. An external call may read anything.
let canonical = |address: AbsoluteAddr| clock_domains.get(&address).copied().unwrap_or(address);
let level_unread_by_domain: BTreeSet<AbsoluteAddr> = eval_apply_ffs
.iter()
.filter(|(domain, units)| {
let domain = canonical(**domain);
!units
.iter()
.flat_map(|unit| unit.blocks.values())
.flat_map(|block| &block.instructions)
.any(|instruction| match instruction {
SIRInstruction::Load(_, address, ..) | SIRInstruction::Commit(address, ..) => {
canonical(address.absolute_addr()) == domain
}
SIRInstruction::ExternCall { .. } => true,
_ => false,
})
})
.map(|(domain, _)| canonical(*domain))
.collect();
let mut independent_derived = BTreeSet::new();
let combinationally_read: BTreeSet<AbsoluteAddr> = comb_deps
.values()
Expand Down Expand Up @@ -5070,6 +5255,7 @@ fn analyze_clock_dependencies(
combinational_derived,
independent_derived,
combinationally_read,
level_unread_by_domain,
}
}

Expand Down
Loading
Loading