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
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
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
74 changes: 63 additions & 11 deletions crates/celox/src/backend/native/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ pub struct SharedNativeCode {
comb_unit_funcs: Vec<NativeSimFunc>,
/// One function per process kernel.
process_funcs: Vec<NativeSimFunc>,
/// Per process, whether it was compiled in the tick loop: the
/// derived-edge units when the loop is on.
process_loops: Vec<bool>,
/// Lane-partitioned kernels and their task functions.
lane_kernels: Option<super::lanes::NativeLaneKernels>,
/// Keep the combined executable image alive so every entry pointer remains
Expand Down Expand Up @@ -332,6 +335,14 @@ impl SharedNativeCode {
.copied()
.map(|offset| native_function_at(&jit_image, offset))
.collect::<Result<Vec<_>, _>>()?;
let mut process_loops = vec![false; process_funcs.len()];
if program_image.options.native_tick_loop {
for units in &program_image.runtime_schema.derived_edges {
if let Some(loops) = process_loops.get_mut(units.unit) {
*loops = true;
}
}
}
let event_map = materialize_map(&program_image.event_map)?;
let eval_only_event_map = materialize_map(&program_image.eval_only_event_map)?;
let apply_event_map = materialize_map(&program_image.apply_event_map)?;
Expand Down Expand Up @@ -375,6 +386,7 @@ impl SharedNativeCode {
clock_alias_funcs,
comb_unit_funcs,
process_funcs,
process_loops,
lane_kernels,
_jit_image: jit_image,
event_map,
Expand Down Expand Up @@ -884,11 +896,13 @@ impl NativeProgramImage {
usize::MAX
}
});
// Derived-edge units follow process and scheduler entries.
let derived_units = 2 * self.runtime_schema.derived_edges.len();
let derived_in_range = self.runtime_schema.derived_edges.iter().all(|units| {
units.rising < self.process_offsets.len() && units.falling < self.process_offsets.len()
});
// The derived-edge units follow process and scheduler entries.
let derived_units = self.runtime_schema.derived_edges.len();
let derived_in_range = self
.runtime_schema
.derived_edges
.iter()
.all(|units| units.unit < self.process_offsets.len());
if !derived_in_range
|| self.process_offsets.len()
!= self
Expand Down Expand Up @@ -2788,6 +2802,14 @@ fn compile_program_with_outlining_threshold(
// registers, scratch values, or extern call area.
process_options = process_options.with_native_callees([comb_jit.required_state_size]);
}
// The derived-edge units run periods of their events in a loop.
let derived_units: crate::HashSet<usize> = sir
.runtime()
.runtime_schema
.derived_edges
.iter()
.map(|units| units.unit)
.collect();
let derived_model = (!sir.deferred_derived_edges.is_empty()).then(|| {
celox_design::schedule::ScheduleModel::derive(&sir.design, &sir.runtime().runtime_schema)
});
Expand All @@ -2809,13 +2831,14 @@ fn compile_program_with_outlining_threshold(
},
|edge| {
if outline_comb {
// The recipe estimate includes two complete comb
// copies. Both now use a single call; its fixed CFG
// allowance already covers those call instructions.
// The recipe estimate includes four complete comb
// copies, two per edge. All now use a single call;
// its fixed CFG allowance already covers those call
// instructions.
(
edge.blocks.saturating_sub(comb_blocks.saturating_mul(2)),
edge.blocks.saturating_sub(comb_blocks.saturating_mul(4)),
edge.instructions
.saturating_sub(comb_instructions.saturating_mul(2)),
.saturating_sub(comb_instructions.saturating_mul(4)),
)
} else {
(edge.blocks, edge.instructions)
Expand Down Expand Up @@ -2862,11 +2885,16 @@ fn compile_program_with_outlining_threshold(
})
.transpose()?;
let unit = generated.as_ref().unwrap_or(unit);
let label = if derived_units.contains(&index) {
celox_backend_common::DERIVED_EDGE_LABEL.to_string()
} else {
format!("process[{index}]")
};
compile_unit_refs(
&[unit],
layout,
options.four_state,
&format!("process[{index}]"),
&label,
NativeMergeKind::Complete,
None,
&process_options,
Expand Down Expand Up @@ -4091,9 +4119,33 @@ impl super::super::SimBackend for NativeBackend {
.process_funcs
.get(index)
.ok_or(SimulatorErrorCode::InternalError)?;
// A unit compiled in the tick loop runs as many times as the header
// says, which a batch that returned early leaves above one.
if self.compiled.process_loops[index] {
return self.call_func_many_timed(func, 1).1;
}
self.call_func_timed(func)
}

/// The derived-edge units are compiled with the tick loop when it is on
/// (see [`celox_backend_common::loops_ticks`]); any other unit runs once.
fn run_process_many(
&mut self,
index: usize,
count: u64,
) -> (u64, Result<(), SimulatorErrorCode>) {
let Some(&func) = self.compiled.process_funcs.get(index) else {
return (0, Err(SimulatorErrorCode::InternalError));
};
if self.compiled.process_loops[index] {
self.call_func_many_timed(func, count)
} else if count == 0 {
(0, Ok(()))
} else {
(1, self.call_func_timed(func))
}
}

fn resolve_signal(&self, addr: &AbsoluteAddr) -> SignalRef {
let layout = &self.compiled.layout;
let offset = layout.offsets.get(addr).copied().unwrap_or(0);
Expand Down
4 changes: 2 additions & 2 deletions crates/celox/src/backend/native/image_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use std::{
use super::backend::NativeProgramImage;

const TRAILER_MAGIC: &[u8; 8] = b"CELOXNPI";
// Images retain the scheduler's slots and the reads of TB settle requests.
const CONTAINER_VERSION: u16 = 56;
// Derived-edge units are one per event and run periods in the tick loop.
const CONTAINER_VERSION: u16 = 57;
const TRAILER_SIZE: usize = 32;
// This optional prefix descriptor does not change the Postcard payload. Older
// readers ignore framing flags and continue to locate the payload at EOF.
Expand Down
32 changes: 32 additions & 0 deletions crates/celox/src/backend/tiered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,23 @@ impl CompiledTier {
}
}

fn run_process_many(
&mut self,
index: usize,
count: u64,
) -> (u64, Result<(), SimulatorErrorCode>) {
match self {
#[cfg(feature = "cranelift")]
Self::Jit(jit) => jit.run_process_many(index, count),
#[cfg(any(
target_arch = "x86_64",
feature = "arm64-codegen",
target_arch = "aarch64"
))]
Self::Native(native) => native.run_process_many(index, count),
}
}

/// Resolve the phase-specific apply event for a shared trigger address
/// and execute it.
fn apply_ff_at_addr(&mut self, addr: &AbsoluteAddr) -> Result<(), SimulatorErrorCode> {
Expand Down Expand Up @@ -1640,6 +1657,21 @@ impl SimBackend for TieredBackend {
}
}

fn run_process_many(
&mut self,
index: usize,
count: u64,
) -> (u64, Result<(), SimulatorErrorCode>) {
self.maybe_promote();
self.maybe_upgrade();
self.count_evaluation_for_current_tier();
match &mut self.phase {
Phase::Interpreting(Some(interp)) => interp.run_process_many(index, count),
Phase::Compiled(compiled) => compiled.run_process_many(index, count),
Phase::Interpreting(None) => unreachable!("promoted backend left no interpreter"),
}
}

fn resolve_signal(&self, addr: &AbsoluteAddr) -> SignalRef {
match &self.phase {
Phase::Interpreting(Some(interp)) => interp.resolve_signal(addr),
Expand Down
Loading
Loading