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
90 changes: 90 additions & 0 deletions crates/celox-frontend-sv/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7237,6 +7237,19 @@ fn packed_index_offset(variable: &SvVariable, index: i128) -> Option<usize> {
.filter(|offset| *offset < variable.width)
}

/// The bit position of declared index `index` of `variable`, which may lie
/// outside it; see [`packed_index_offset`].
fn packed_index_position(variable: &SvVariable, index: i128) -> Option<i128> {
if !variable.array_dims.is_empty() {
return Some(index);
}
match variable.packed_ranges.as_slice() {
[(left, right)] if left >= right => index.checked_sub(*right),
[(_, right)] => right.checked_sub(index),
_ => Some(index),
}
}

fn unpacked_element_width(variable: &SvVariable) -> Option<usize> {
if variable.array_dims.is_empty() {
return None;
Expand Down Expand Up @@ -7395,6 +7408,83 @@ fn sv_memory_offset(variable: &SvVariable, bit_offset: usize, width: usize) -> S
}
}

/// The bits of `variable` that a constant select from declared index `msb`
/// to `lsb` writes, and the number of low bits of the assigned value that
/// fall below the variable. A select partly outside the variable writes only
/// the bits inside, one wholly outside writes none (IEEE 1800-2023 11.5.1).
fn constant_select_write(
variable: &SvVariable,
msb: i128,
lsb: i128,
) -> Option<(Option<BitAccess>, usize)> {
let (msb, lsb) = (
packed_index_position(variable, msb)?,
packed_index_position(variable, lsb)?,
);
let (low, high) = (msb.min(lsb), msb.max(lsb));
let top = i128::try_from(variable.width).ok()? - 1;
let (start, end) = (low.max(0), high.min(top));
if start > end {
return Some((None, 0));
}
Some((
Some(BitAccess::new(
usize::try_from(start).ok()?,
usize::try_from(end).ok()?,
)),
usize::try_from(start - low).ok()?,
))
}

/// `folded`, the select `original` with the values its position reads
/// substituted, unless that turned a run-time select within one array
/// element into a constant one reaching past the element: the run-time form
/// keeps its write within the element (see `dynamic_packed_write`), while
/// the constant position counts from bit 0 of the whole array.
fn keep_element_window(
original: &sv::ir::LValue,
folded: sv::ir::LValue,
variables: &HashMap<SourceVarId, SvVariable>,
name_to_id: &HashMap<String, SourceVarId>,
constants: &HashMap<String, i128>,
parameter_types: &HashMap<String, (usize, bool)>,
) -> sv::ir::LValue {
let constant =
|expr| sv::typecheck::eval_const_expr_with_types(expr, constants, parameter_types);
let (
sv::ir::LValue::Select {
msb: original_msb,
lsb: original_lsb,
..
},
sv::ir::LValue::Select { msb, lsb, .. },
) = (original, &folded)
else {
return folded;
};
if constant(original_msb).is_some() && constant(original_lsb).is_some() {
return folded;
}
let (Some(msb), Some(lsb)) = (constant(msb), constant(lsb)) else {
return folded;
};
let Some(window) =
dynamic_packed_write(original, variables, name_to_id, constants, parameter_types)
.and_then(|write| write.window)
else {
return folded;
};
let inside = |position: i128| {
usize::try_from(position)
.is_ok_and(|position| (window.lsb..=window.msb).contains(&position))
};
if inside(msb) && inside(lsb) {
folded
} else {
original.clone()
}
}

fn packed_expr_select_offsets(
expr: &sv::ir::Expr,
msb: i128,
Expand Down
87 changes: 57 additions & 30 deletions crates/celox-frontend-sv/src/lowering/comb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,13 +557,20 @@ impl<'p, 'a> Comb<'p, 'a> {
let sv::ir::Expr::Select { msb, lsb, .. } = self.propagate(store, &probe) else {
unreachable!()
};
sv::ir::LValue::Select {
name: name.clone(),
msb,
lsb,
array_slice_width: array_slice_width.clone(),
array_slice_reversed: *array_slice_reversed,
}
keep_element_window(
lvalue,
sv::ir::LValue::Select {
name: name.clone(),
msb,
lsb,
array_slice_width: array_slice_width.clone(),
array_slice_reversed: *array_slice_reversed,
},
self.m.variables,
self.m.name_to_id,
self.m.constants,
self.m.parameter_types,
)
}
}
}
Expand Down Expand Up @@ -1529,8 +1536,7 @@ impl<'p, 'a> Comb<'p, 'a> {
lvalue: &sv::ir::LValue,
) -> Result<usize, sv::AnalyzerError> {
Ok(match self.lvalue_target(store, lvalue)? {
Target::Static { access, .. } => access.msb - access.lsb + 1,
Target::Dynamic { width, .. } => width,
Target::Static { width, .. } | Target::Dynamic { width, .. } => width,
})
}

Expand All @@ -1551,6 +1557,8 @@ impl<'p, 'a> Comb<'p, 'a> {
sv::ir::LValue::Ident(_) => Ok(Target::Static {
id,
access: full(var_width),
width: var_width,
drop: 0,
lvalue,
}),
sv::ir::LValue::Select { msb, lsb, .. } => {
Expand All @@ -1559,21 +1567,22 @@ impl<'p, 'a> Comb<'p, 'a> {
let lsb_value =
sv::typecheck::eval_const_expr_with_types(lsb, constants, parameter_types);
if let (Some(msb), Some(lsb)) = (msb_value, lsb_value) {
let variable = self.m.var(id);
let (Some(msb), Some(lsb)) = (
packed_index_offset(variable, msb),
packed_index_offset(variable, lsb),
) else {
let width = usize::try_from(msb.abs_diff(lsb)).unwrap_or(0) + 1;
let (access, drop) = constant_select_write(self.m.var(id), msb, lsb)
.ok_or_else(|| unsupported("assignment position"))?;
let Some(access) = access else {
// A constant select entirely outside the vector writes nothing.
return Ok(Target::Dynamic {
id,
offset: Offset::Nothing,
width: usize::try_from(msb.abs_diff(lsb)).unwrap_or(0) + 1,
width,
});
};
return Ok(Target::Static {
id,
access: BitAccess::new(lsb.min(msb), lsb.max(msb)),
access,
width,
drop,
lvalue: lvalue.clone(),
});
}
Expand Down Expand Up @@ -1638,11 +1647,14 @@ impl<'p, 'a> Comb<'p, 'a> {
let lhs = &self.hoist_lvalue(store, frames, lhs)?;
let target = self.lvalue_target(store, lhs)?;
let width = match &target {
Target::Static { access, .. } => access.msb - access.lsb + 1,
Target::Dynamic { width, .. } => *width,
Target::Static { width, .. } | Target::Dynamic { width, .. } => *width,
};
if self.continuous
&& matches!(&target, Target::Dynamic { id, .. } if !self.m.is_hidden(*id))
&& matches!(
&target,
Target::Dynamic { id, offset, .. }
if !self.m.is_hidden(*id) && !matches!(offset, Offset::Nothing)
)
{
return Err(unsupported(format!(
"combinational assignment target `{}` at a run-time position",
Expand Down Expand Up @@ -1703,8 +1715,7 @@ impl<'p, 'a> Comb<'p, 'a> {
rhs: &sv::ir::Expr,
) -> Result<(), sv::AnalyzerError> {
let (id, width) = match &target {
Target::Static { id, access, .. } => (*id, access.msb - access.lsb + 1),
Target::Dynamic { id, width, .. } => (*id, *width),
Target::Static { id, width, .. } | Target::Dynamic { id, width, .. } => (*id, *width),
};
let (mut node, mut sources) = value;
node = coerce_node_width(self.arena, node, Some(width), signed).map_err(slt_error)?;
Expand All @@ -1713,8 +1724,14 @@ impl<'p, 'a> Comb<'p, 'a> {
node = self.alloc(SLTNode::Unary(UnaryOp::ToTwoState, node))?;
}
match target {
Target::Static { id, access, lvalue } => {
let node = permute_reversed_lvalue_rhs_slt(
Target::Static {
id,
access,
drop,
lvalue,
..
} => {
let mut node = permute_reversed_lvalue_rhs_slt(
&lvalue,
node,
width,
Expand All @@ -1723,6 +1740,13 @@ impl<'p, 'a> Comb<'p, 'a> {
self.arena,
)
.ok_or_else(|| unsupported("assignment lvalue order"))?;
let written = access.msb - access.lsb + 1;
if written != width {
node = self.alloc(SLTNode::Slice {
expr: node,
access: BitAccess::new(drop, drop + written - 1),
})?;
}
self.write(store, id, access, (node, sources))
}
Target::Dynamic { id, offset, width } => {
Expand Down Expand Up @@ -3900,14 +3924,12 @@ impl Comb<'_, '_> {
this.m.constants,
this.m.parameter_types,
))
.and_then(|(msb, lsb)| {
Some((
packed_index_offset(variable, msb)?,
packed_index_offset(variable, lsb)?,
))
});
.and_then(|(msb, lsb)| constant_select_write(variable, msb, lsb));
match bounds {
Some((msb, lsb)) => BitAccess::new(msb.min(lsb), msb.max(lsb)),
Some((Some(access), _)) => access,
// A constant select entirely outside the vector
// writes nothing.
Some((None, _)) => return,
// A run-time select may write any bit; the written
// value keeps the others, so it defines the whole
// vector, or the one element its constant part names.
Expand Down Expand Up @@ -3981,9 +4003,14 @@ enum Label {
}

enum Target {
/// A constant select: the select's `width` bits of the value from
/// `drop` up go to the bits `access` of the variable, the part of the
/// select inside it.
Static {
id: SourceVarId,
access: BitAccess,
width: usize,
drop: usize,
lvalue: sv::ir::LValue,
},
Dynamic {
Expand Down
56 changes: 31 additions & 25 deletions crates/celox-frontend-sv/src/lowering/ff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,8 @@ impl<'p, 'a> Ff<'p, 'a> {
self.m.constants,
self.m.parameter_types,
))
.and_then(|(msb, lsb)| {
Some((
packed_index_offset(variable, msb)?,
packed_index_offset(variable, lsb)?,
))
})
.map_or(whole, |(msb, lsb)| {
BitAccess::new(msb.min(lsb), msb.max(lsb))
}),
.and_then(|(msb, lsb)| constant_select_write(variable, msb, lsb)?.0)
.unwrap_or(whole),
};
self.targets.push((id, access));
}
Expand Down Expand Up @@ -354,13 +347,20 @@ impl<'p, 'a> Ff<'p, 'a> {
lsb,
array_slice_width,
array_slice_reversed,
} => sv::ir::LValue::Select {
name: name.clone(),
msb: substitute_overlay_const(msb, &self.overlay),
lsb: substitute_overlay_const(lsb, &self.overlay),
array_slice_width: array_slice_width.clone(),
array_slice_reversed: *array_slice_reversed,
},
} => keep_element_window(
lvalue,
sv::ir::LValue::Select {
name: name.clone(),
msb: substitute_overlay_const(msb, &self.overlay),
lsb: substitute_overlay_const(lsb, &self.overlay),
array_slice_width: array_slice_width.clone(),
array_slice_reversed: *array_slice_reversed,
},
self.m.variables,
self.m.name_to_id,
self.m.constants,
self.m.parameter_types,
),
}
}

Expand Down Expand Up @@ -2245,15 +2245,12 @@ impl<'p, 'a> Ff<'p, 'a> {
sv::typecheck::eval_const_expr_with_types(msb, constants, parameter_types),
sv::typecheck::eval_const_expr_with_types(lsb, constants, parameter_types),
) {
let variable = self.m.var(id);
let (Some(msb), Some(lsb)) = (
packed_index_offset(variable, msb),
packed_index_offset(variable, lsb),
) else {
let (access, drop) = constant_select_write(self.m.var(id), msb, lsb)
.ok_or_else(|| unsupported("assignment position"))?;
let Some(access) = access else {
return Ok(());
};
let low = msb.min(lsb);
let node = permute_reversed_lvalue_rhs_slt(
let mut node = permute_reversed_lvalue_rhs_slt(
lhs,
node,
width,
Expand All @@ -2262,9 +2259,18 @@ impl<'p, 'a> Ff<'p, 'a> {
arena,
)
.ok_or_else(|| unsupported("assignment lvalue order"))?;
let written = access.msb - access.lsb + 1;
if written != width {
node = arena
.alloc(SLTNode::Slice {
expr: node,
access: BitAccess::new(drop, drop + written - 1),
})
.map_err(slt_error)?;
}
let value = self.lower_slt(arena, node)?;
let offset = sv_memory_offset(self.m.var(id), low, width);
self.store(id, offset, width, value);
let offset = sv_memory_offset(self.m.var(id), access.lsb, written);
self.store(id, offset, written, value);
return Ok(());
}
if let Some((_, element_width, offset, access)) = dynamic_array_element_lvalue(
Expand Down
9 changes: 9 additions & 0 deletions crates/celox-frontend-sv/src/warning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub enum FrontendWarningKind {
/// IEEE 1800-2023 3.14.2.3 requires consistent explicit timing declarations.
#[error("mixed explicit and default time units or precisions across design elements")]
MixedTimescale,
#[error("`$size` on an interface array is a language extension")]
InterfaceArrayQuery,
/// IEEE 1800-2023 9.2.2.4 restricts writes by other processes.
#[error("multiple variable drivers for `{signal}` in `{module}` include an always_ff process")]
MultipleAlwaysFfDrivers { module: String, signal: String },
Expand Down Expand Up @@ -96,6 +98,9 @@ pub(crate) fn analyzer_warnings(
found: &[celox_sv_analyzer::ir::AnalyzerWarning],
) -> impl Iterator<Item = PendingWarning> + '_ {
found.iter().map(|warning| match warning {
celox_sv_analyzer::ir::AnalyzerWarning::InterfaceArrayQuery { site } => {
(FrontendWarningKind::InterfaceArrayQuery, site.clone())
}
celox_sv_analyzer::ir::AnalyzerWarning::MixedTimescale => {
(FrontendWarningKind::MixedTimescale, None)
}
Expand Down Expand Up @@ -149,6 +154,7 @@ impl miette::Diagnostic for FrontendWarning {
fn code<'a>(&'a self) -> Option<Box<dyn std::fmt::Display + 'a>> {
Some(Box::new(match self.kind {
FrontendWarningKind::MixedTimescale => "mixed_timescale",
FrontendWarningKind::InterfaceArrayQuery => "interface_array_query",
FrontendWarningKind::MultipleAlwaysFfDrivers { .. } => "multiple_always_ff_drivers",
FrontendWarningKind::UnpackedPortDefault { .. } => "unpacked_port_default",
FrontendWarningKind::InferredLatch => "inferred_latch",
Expand All @@ -166,6 +172,9 @@ impl miette::Diagnostic for FrontendWarning {
FrontendWarningKind::InferredLatch => {
"assign every output on every path, or configure diagnostics.inferred_latch as warn or allow"
}
FrontendWarningKind::InterfaceArrayQuery => {
"interface arrays are not data arrays in IEEE 1800-2023 20.7; set interface_array_query=warn or allow for compatibility"
}
FrontendWarningKind::MixedTimescale => {
"declare timeunit/timeprecision consistently; undeclared scopes use 1ns/1ps when this check is relaxed"
}
Expand Down
Loading
Loading