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.

2 changes: 1 addition & 1 deletion crates/celox-backend-arm64/src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ pub fn emit_prepared_eu_with_arena(
}
};
sir_eu.verify_result().map_err(PrepareError::Sir)?;
let tick_loop = label == "eval_comb_apply_ff" && native_tick_loop;
let tick_loop = celox_backend_common::loops_ticks(label) && native_tick_loop;
let check_runtime_events = tick_loop
&& sir_eu.blocks.values().any(|block| {
block.instructions.iter().any(|instruction| {
Expand Down
11 changes: 11 additions & 0 deletions crates/celox-backend-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,14 @@
pub mod regalloc;

pub mod cranelift_options;

/// The compile label of a generated derived-edge unit, which runs periods
/// of its event in the tick loop.
pub const DERIVED_EDGE_LABEL: &str = "derived_edge";

/// Whether a function compiled under `label` runs in the tick loop when the
/// backend's native tick loop is on: the fused tick (`eval_comb_apply_ff`)
/// and the derived-edge units.
pub fn loops_ticks(label: &str) -> bool {
label == "eval_comb_apply_ff" || label == DERIVED_EDGE_LABEL
}
4 changes: 2 additions & 2 deletions crates/celox-backend-x86/src/backend/native/emit/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ fn emit_prepared_eu_inner(
);
}
dump_native_block_context(label, "after_isel", &sir_eu, &mfunc, diagnostics);
let check_runtime_events = label == "eval_comb_apply_ff"
let check_runtime_events = celox_backend_common::loops_ticks(label)
&& options.native_tick_loop
&& sir_eu.blocks.values().any(|block| {
block.instructions.iter().any(|instruction| {
Expand Down Expand Up @@ -347,7 +347,7 @@ fn emit_prepared_eu_inner(
} else {
&[]
};
let result = if label == "eval_comb_apply_ff" && options.native_tick_loop {
let result = if celox_backend_common::loops_ticks(label) && options.native_tick_loop {
emit_with_plan_tick_loop(
&mfunc,
&ra.assignment,
Expand Down
2 changes: 1 addition & 1 deletion crates/celox-backend-x86/src/backend/native/regalloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ fn run_regalloc_in_place(
is_cancelled,
)?,
};
let tick_loop = label == "eval_comb_apply_ff" && native_tick_loop;
let tick_loop = celox_backend_common::loops_ticks(label) && native_tick_loop;
let vector_allocation = super::x86_slp::allocate(func, spill_frame_size, tick_loop);
for (value, location) in vector_allocation.assignments {
assignment.set_x86_vector(value, location);
Expand Down
23 changes: 16 additions & 7 deletions crates/celox-design/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1390,19 +1390,28 @@ pub struct SettlePartitions<A> {
pub marks: Vec<(A, Vec<u32>)>,
}

/// The process units that run the edges of one event together with its
/// derived clock domains, the signal having already moved. Each reads
/// [`SchedulerSlots::dirty`] to know whether combinational logic must
/// settle before the domains evaluate.
/// The process unit that runs edges of one event together with its derived
/// clock domains. It runs the edges [`SchedulerSlots::cont`] selects
/// ([`DERIVED_EDGE_RISING`], [`DERIVED_EDGE_FALLING`], or both, a period, in
/// that order), moving the signal itself unless [`DERIVED_EDGE_MOVED`] says
/// the host moved it for a single edge. [`SchedulerSlots::dirty`] says
/// whether combinational logic has pending changes, before and after.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct DerivedEdgeUnits<A> {
/// The canonical event.
pub event: A,
/// Indices among the process units of the rising and the falling edge.
pub rising: usize,
pub falling: usize,
/// The unit's index among the process units.
pub unit: usize,
}

/// A [`DerivedEdgeUnits`] mode bit: run the rising edge.
pub const DERIVED_EDGE_RISING: u8 = 1;
/// A [`DerivedEdgeUnits`] mode bit: run the falling edge.
pub const DERIVED_EDGE_FALLING: u8 = 2;
/// A [`DerivedEdgeUnits`] mode bit: the host already moved the signal
/// through the one edge selected.
pub const DERIVED_EDGE_MOVED: u8 = 4;

/// Width of [`SchedulerSlots::limit`], [`SchedulerSlots::now`],
/// [`SchedulerSlots::exit_arg`], [`SchedulerSlots::count`],
/// [`SchedulerSlots::current`], [`SchedulerSlots::levels`],
Expand Down
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
15 changes: 15 additions & 0 deletions crates/celox-runtime/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ pub trait SimBackend {
})
}

/// Run process unit `index` up to `count` times in a row, as
/// [`Self::eval_comb_apply_ff_many_at`] runs ticks, and return how many
/// runs completed: generated code may loop inside the unit, returning
/// after a runtime event; backends without that loop run it once.
fn run_process_many(
&mut self,
index: usize,
count: u64,
) -> (u64, Result<(), SimulatorErrorCode>) {
if count == 0 {
return (0, Ok(()));
}
(1, self.run_process(index))
}

// ── signal access ───────────────────────────────────────────
fn resolve_signal(&self, addr: &AbsoluteAddr) -> SignalRef;
fn resolve_event(&self, addr: &AbsoluteAddr) -> Self::Event;
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
Loading
Loading