Skip to content
Closed
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
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 28 additions & 6 deletions crates/celox-frontend-core/src/symbolic/assembly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,25 @@ pub fn schedule_symbolic_rtl_reporting_loops(
.values()
.map(|module| modules[module].processes.len())
.sum();
let scheduler_slots = declare_scheduler_slots(&mut modules, root_id, process_count);
// At most one event per trigger signal of each instance's domains;
// instances connected to one clock share its event. A design with more
// events than its slots hold runs without the compiled scheduler.
let expected_events = instance_modules
.values()
.map(|module| {
let module = &modules[module];
module
.eval_apply_ff_blocks
.keys()
.chain(module.eval_only_ff_blocks.keys())
.chain(module.apply_ff_blocks.keys())
.flat_map(|triggers| std::iter::once(&triggers.clock).chain(&triggers.resets))
.collect::<HashSet<_>>()
.len()
})
.sum::<usize>();
let scheduler_slots =
declare_scheduler_slots(&mut modules, root_id, process_count, expected_events);
// Each package with variables is one instance outside the hierarchy,
// named by a path segment no instance name can take.
let mut package_instances = HashMap::default();
Expand Down Expand Up @@ -2754,12 +2772,15 @@ fn declare_settle_flags(

/// Declare the hidden objects a compiled scheduler exchanges with the
/// runtime (`RuntimeSchema::scheduler`) as variables of the top module, for
/// a design with `process_count` process kernels. A design without kernels
/// has them too: host clocks may run in a compiled scheduler.
/// a design with `process_count` process kernels and about
/// `expected_events` events, whose edge and level masks hold one bit per
/// event in whole 64-bit words. A design without kernels has them too: host
/// clocks may run in a compiled scheduler.
fn declare_scheduler_slots(
modules: &mut HashMap<ModuleId, SimModule>,
root: ModuleId,
process_count: usize,
expected_events: usize,
) -> Option<celox_design::SchedulerSlots<SourceVarId>> {
let variables = &mut modules.get_mut(&root)?.variables;
let mut next = variables.keys().map(|id| id.0 + 1).max().unwrap_or(0);
Expand Down Expand Up @@ -2791,19 +2812,20 @@ fn declare_scheduler_slots(
use celox_design::{
SCHEDULER_CLOCKS as CLOCKS, SCHEDULER_FLAG_WIDTH as FLAG, SCHEDULER_TIME_WIDTH as TIME,
};
let events = expected_events.next_multiple_of(TIME).max(TIME);
Some(celox_design::SchedulerSlots {
limit: declare("limit".into(), TIME),
now: declare("now".into(), TIME),
exit: declare("exit".into(), FLAG),
exit_arg: declare("exit_arg".into(), TIME),
exit_arg: declare("exit_arg".into(), events),
finish: declare("finish".into(), FLAG),
cont: declare("cont".into(), FLAG),
dirty: declare("dirty".into(), FLAG),
defer_limit_settle: declare("defer_limit_settle".into(), FLAG),
count: declare("count".into(), TIME),
current: declare("current".into(), TIME),
levels: declare("levels".into(), TIME),
unknown_levels: declare("unknown_levels".into(), TIME),
levels: declare("levels".into(), events),
unknown_levels: declare("unknown_levels".into(), events),
ready: declare("ready".into(), TIME),
wake: (0..process_count)
.map(|index| declare(format!("wake[{index}]"), TIME))
Expand Down
88 changes: 72 additions & 16 deletions crates/celox-runtime/src/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,52 @@ pub struct ResolvedSchedule<B: SimBackend> {

/// A design's compiled scheduler resolved against a backend: its process
/// unit and the slots it exchanges a run with.
/// A set of the compiled scheduler's events, by their bit in `levels`: one
/// 64-bit word per 64 events.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(crate) struct EventMask(Vec<u64>);

impl EventMask {
/// The set of the events at `bits`.
pub(crate) fn of(bits: impl IntoIterator<Item = usize>) -> Self {
let mut mask = Self::default();
for bit in bits {
let word = bit / 64;
if mask.0.len() <= word {
mask.0.resize(word + 1, 0);
}
mask.0[word] |= 1 << (bit % 64);
}
mask
}

pub(crate) fn union(&self, other: &Self) -> Self {
let (long, short) = if self.0.len() >= other.0.len() {
(self, other)
} else {
(other, self)
};
let mut union = long.clone();
for (word, other) in union.0.iter_mut().zip(&short.0) {
*word |= other;
}
union
}

/// The events of `self` not in `other`.
pub(crate) fn difference(&self, other: &Self) -> Self {
let mut difference = self.clone();
for (word, other) in difference.0.iter_mut().zip(&other.0) {
*word &= !other;
}
difference
}

pub(crate) fn intersects(&self, other: &Self) -> bool {
self.0.iter().zip(&other.0).any(|(a, b)| a & b != 0)
}
}

pub(crate) struct SchedulerRefs {
pub(crate) unit: usize,
pub(crate) limit: SignalRef,
Expand Down Expand Up @@ -93,19 +139,29 @@ pub(crate) struct SchedulerRefs {
/// in the order of its clock slots.
pub(crate) clocks: Vec<(usize, SignalRef)>,
/// Per process kernel, the bits of `levels` of the events it writes.
pub(crate) kernel_events: Vec<u64>,
pub(crate) kernel_events: Vec<EventMask>,
/// Per clock slot, the bit of `levels` of its event.
pub(crate) clock_events: Vec<u64>,
pub(crate) clock_events: Vec<EventMask>,
/// The bits of `levels` of clocks derived from the others, which any
/// slot may change.
pub(crate) derived_events: u64,
pub(crate) derived_events: EventMask,
pub(crate) clock_next: SignalRef,
pub(crate) clock_period: SignalRef,
pub(crate) clock_value: SignalRef,
pub(crate) clock_armed: SignalRef,
}

impl SchedulerRefs {
/// The low word of `exit_arg`, where an exit that names a clock stores
/// its index. The slot is wider when it holds the edges of more than 64
/// events.
pub(crate) fn exit_index(&self) -> SignalRef {
SignalRef {
width: self.exit_arg.width.min(celox_design::SCHEDULER_TIME_WIDTH),
..self.exit_arg
}
}

/// Element `bit` of the per-clock slot `slots`.
pub(crate) fn clock_element(slots: SignalRef, bit: usize) -> SignalRef {
let width = celox_design::SCHEDULER_TIME_WIDTH;
Expand Down Expand Up @@ -479,20 +535,22 @@ impl<B: SimBackend> ResolvedSchedule<B> {
.collect::<Option<_>>()?,
kernel_events: (0..scheduler.fallback.len())
.map(|index| {
scheduler
.kernel_events
.get(index)
.into_iter()
.flatten()
.fold(0, |mask, &bit| mask | 1 << bit)
EventMask::of(
scheduler
.kernel_events
.get(index)
.into_iter()
.flatten()
.copied(),
)
})
.collect(),
clock_events: scheduler
.clocks
.iter()
.map(|clock| {
let bit = scheduler.events.iter().position(|event| event == clock)?;
Some(1 << bit)
Some(EventMask::of([bit]))
})
.collect::<Option<_>>()?,
clocks: scheduler
Expand All @@ -507,18 +565,16 @@ impl<B: SimBackend> ResolvedSchedule<B> {
clock_period: resolve(&slots.clock_period),
clock_value: resolve(&slots.clock_value),
clock_armed: resolve(&slots.clock_armed),
derived_events: 0,
derived_events: EventMask::default(),
};
// What no kernel and no clock writes itself derives from them.
let all = 1u64
.checked_shl(refs.events.len() as u32)
.map_or(u64::MAX, |bit| bit - 1);
let all = EventMask::of(0..refs.events.len());
let driven = refs
.kernel_events
.iter()
.chain(&refs.clock_events)
.fold(0, |mask, &bits| mask | bits);
refs.derived_events = all & !driven;
.fold(EventMask::default(), |mask, bits| mask.union(bits));
refs.derived_events = all.difference(&driven);
Some(refs)
}),
kernel_fallback: model
Expand Down
32 changes: 21 additions & 11 deletions crates/celox-runtime/src/simulation/compiled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl<B: SimBackend> SimulationState<B> {
|| !pending.nba_events.is_empty()
// A derived clock changes with the slots of its sources, which
// the scheduler does not tell apart.
|| self.observed_events & scheduler.derived_events != 0
|| self.observed_events.intersects(&scheduler.derived_events)
|| pending.waiters.iter().any(|(process, _)| {
scheduler.waits.get(process).is_none_or(Option::is_none)
|| self.writes_observed(scheduler, process)
Expand Down Expand Up @@ -109,7 +109,7 @@ impl<B: SimBackend> SimulationState<B> {
for event in queue {
if clock_slot(scheduler, event, clocks).is_some_and(|bit| {
compiled >> bit & 1 != 0
&& scheduler.clock_events[bit] & self.observed_events == 0
&& !scheduler.clock_events[bit].intersects(&self.observed_events)
}) {
first = first.min(event.time);
} else {
Expand Down Expand Up @@ -141,7 +141,7 @@ impl<B: SimBackend> SimulationState<B> {
scheduler
.kernel_events
.get(process)
.is_some_and(|&events| events & self.observed_events != 0)
.is_some_and(|events| events.intersects(&self.observed_events))
}

/// Run the compiled scheduler for the slots up to `limit`. Returns, like
Expand Down Expand Up @@ -293,7 +293,7 @@ impl<B: SimBackend> SimulationState<B> {
let slot_complete = complete || exit == Some(SchedulerExit::ClockOverflow);
let overflowed = scheduler
.clocks
.get(backend.get_as::<u64>(scheduler.exit_arg) as usize)
.get(backend.get_as::<u64>(scheduler.exit_index()) as usize)
.map(|&(event, _)| event);
// Armed clocks wait for their next edge in the event queue again; a
// clock whose edge applied in a slot the run left unfinished is gone,
Expand Down Expand Up @@ -393,12 +393,19 @@ impl<B: SimBackend> SimulationState<B> {
} else if count > 0 {
// The baseline is the levels slot `now` started from; events the
// kernels do not write kept theirs.
let levels: u64 = executor.backend().get_as(scheduler.levels);
let unknown: u64 = if self.four_state {
executor.backend().get_as(scheduler.unknown_levels)
} else {
0
// The frontend sizes the slots from an estimate, so they can be
// wider than the events need.
let plane = |slot: crate::SignalRef| -> Box<dyn Fn(usize) -> bool> {
if slot.width <= 64 {
let bits: u64 = executor.backend().get_as(slot);
Box::new(move |bit| bits >> bit & 1 != 0)
} else {
let bits = executor.backend().get(slot);
Box::new(move |bit| bits.bit(bit as u64))
}
};
let level = plane(scheduler.levels);
let unknown = self.four_state.then(|| plane(scheduler.unknown_levels));
// A run without clocks records no level of their events, which
// only the clocks change.
let scheduler = self
Expand All @@ -414,8 +421,11 @@ impl<B: SimBackend> SimulationState<B> {
clocked || scheduler.clocks.iter().all(|&(clock, _)| clock != id)
})
.map(|(bit, &id)| {
let level = (levels >> bit & 1) as u8;
(id, level | ((unknown >> bit & 1) as u8 * LEVEL_UNKNOWN))
let unknown = unknown.as_ref().is_some_and(|unknown| unknown(bit));
(
id,
u8::from(level(bit)) | (u8::from(unknown) * LEVEL_UNKNOWN),
)
})
.collect();
for (id, level) in recorded {
Expand Down
15 changes: 8 additions & 7 deletions crates/celox-runtime/src/simulation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ pub struct SimulationState<B: SimBackend> {
/// The bits of the compiled scheduler's events whose changes the host
/// observes inside a slot: the kernels and host clocks that write them
/// run in the reference loop.
observed_events: u64,
observed_events: crate::schedule::EventMask,
}

impl<B: SimBackend> SimulationState<B> {
Expand Down Expand Up @@ -234,7 +234,7 @@ impl<B: SimBackend> SimulationState<B> {
return_budget: SLOT_RETURN_BUDGET,
slot_passes: 0,
stats: SchedulerStats::default(),
observed_events: 0,
observed_events: Default::default(),
};
state.stats.kernels = (0..state.model.processes.len())
.map(|index| match state.model.kernel_fallback.get(index) {
Expand Down Expand Up @@ -427,11 +427,12 @@ impl<B: SimBackend> SimulationState<B> {
let Some(scheduler) = &self.model.scheduler else {
return;
};
self.observed_events = signals
.into_iter()
.filter_map(|signal| self.model.signal_to_id.get(&signal))
.filter_map(|event| scheduler.events.iter().position(|id| id == event))
.fold(0, |mask, bit| mask | 1 << bit);
self.observed_events = crate::schedule::EventMask::of(
signals
.into_iter()
.filter_map(|signal| self.model.signal_to_id.get(&signal))
.filter_map(|event| scheduler.events.iter().position(|id| id == event)),
);
}

/// [`Self::step_until`], showing the step to `observer`. An error of
Expand Down
Loading
Loading