Skip to content
Merged
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));
Comment on lines +7427 to +7428

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Exclude no-op slices from driver overlap analysis

For two assignments to the same wholly out-of-range slice, such as two continuous assignments to q[11:8] when q is 8 bits, this helper makes both writes no-ops during lowering, but continuous_driver_ranges and local_driver_ranges still collect the raw [8:11] ranges through net_lvalue_range. Their overlap check therefore rejects the module as having multiple drivers before lowering reaches this return. The driver scans need to clip ranges to the destination and omit ranges with no in-bounds bits.

Useful? React with 👍 / 👎.

}
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),
Comment on lines +219 to +220

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Skip wholly out-of-range targets in the process scan

When an always_comb body only contains a now-supported no-op such as q[11:8] = v for an 8-bit q, constant_select_write returns Some((None, _)), but this and_then converts it to None and unwrap_or(whole) records all of q as written. The generated body contains no store, so retention analysis can report an inferred latch for a statement that writes no bits; edge-process metadata likewise contains a phantom whole-variable target. Handle the wholly-outside result separately and avoid adding a target.

Useful? React with 👍 / 👎.

};
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
2 changes: 1 addition & 1 deletion crates/celox-test-suite/LIMITATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ four-state expectations; the assertions remain enabled on all Celox variants.
| `sv_verilator_short_circuit_calls` | execute | verilator | 1 | Verilator 5.052 runs a function call in the right operand of && in a select index when the left operand is false, so its output argument is written; IEEE 1800-2023 11.4.7 does not evaluate that operand. Icarus cannot compile the design (sv_icarus_function_outputs). |
| `sv_verilator_function_outputs` | compile | verilator | 1 | Verilator 5.052 fails with an internal error (V3DfgSynthesize: Non-ReadOnly reference) on output and inout function arguments. |
| `sv_verilator_generate_name_collision` | compile | verilator | 1 | Verilator 5.052 rejects a generate block named like a port with an "Unsupported" diagnostic instead of a duplicate-name error, so the run cannot count it as a language rejection. The design is invalid (IEEE 1800-2023 5.6.1, 23.9) and Icarus rejects it as a duplicate declaration. |
| `sv_verilator_part_select_write_overhang` | execute | verilator | 1 | Verilator 5.052 writes the out-of-range bit of a partially out-of-range indexed part-select (minus_write[i -: 2] with i = 0) into bit 7 instead of discarding it; IEEE 1800-2023 11.5.1 ignores writes to out-of-range bits. Icarus agrees with the expected value. |
| `sv_verilator_part_select_write_overhang` | execute | verilator | 4 | Verilator 5.052 writes bits that IEEE 1800-2023 11.5.1 discards: the out-of-range bit of a partially out-of-range indexed part-select (minus_write[i -: 2] with i = 0) lands in bit 7, a constant part-select wholly outside the vector (comb_out[11:8]) writes its low bits, a partly outside one of an ascending vector (comb_up[6:9]) takes the wrong end of the value, and run-time selects within one array element whose positions are known at compile time reach past it. Icarus agrees with the expected values. |
| `sv_verilator_unpacked_array_extension` | compile | verilator | 2 | Verilator 5.052 rejects an unpacked array argument or assignment pattern item with a narrower element type through "EXTEND unexpected in assignment to unpacked array" (EXTENDS for a signed element), a diagnostic about its own width extension rather than the element types, so the run cannot count it as a language rejection. The designs are invalid (IEEE 1800-2023 7.6, 10.8). |
| `sv_icarus_dimensions_type_queries` | compile | icarus | 2 | Icarus 13.0 rejects data type arguments to `$dimensions`, with syntax/type-name errors and an assertion for a multidimensional cast. IEEE 1800-2023 20.7 permits these queries. Verilator passes the integral/string/real type case. |
| `sv_verilator_chandle_dimensions` | execute | verilator | 1 | Verilator 5.052 returns 1 for `$dimensions(chandle)`; IEEE 1800-2023 6.14 and 20.7 require 0 for this nonarray pointer type. |
Expand Down
Loading
Loading