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
28 changes: 18 additions & 10 deletions crates/celox-frontend-sv/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4803,6 +4803,23 @@ struct SvSignalType {
type_kind: PortTypeKind,
}

fn unpacked_range_bounds(
range: &sv::ir::UnpackedRange,
constants: &HashMap<String, i128>,
parameter_types: &HashMap<String, (usize, bool)>,
) -> Option<(i128, i128)> {
// User constant functions belong to the analyzer session, which has
// ended by lowering. Retain its evaluated bounds as a fallback while
// keeping the symbolic declaration contract available to other consumers.
sv::typecheck::eval_const_expr_with_types(range.left(), constants, parameter_types)
.zip(sv::typecheck::eval_const_expr_with_types(
range.right(),
constants,
parameter_types,
))
.or_else(|| range.resolved_bounds())

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Avoid multiplying cached unpacked widths twice

When both a packed range and an unpacked range call user constant functions (for example, logic [word_width()-1:0] data [depth()]), this fallback lets the unpacked range proceed, but signal_type_from_sv still falls back to typ.resolved_width() for the unresolved packed range. That value already includes every unpacked dimension, and the function subsequently multiplies it by array_dims again, so an 8-bit, depth-2 array is lowered as 32 bits instead of 16 and can silently corrupt layout and indexing. Preserve a packed-only resolved width or resolved packed bounds rather than using the type's total width.

Useful? React with 👍 / 👎.

}

fn signal_type_from_sv(
typ: &sv::ir::Type,
constants: &HashMap<String, i128>,
Expand Down Expand Up @@ -4837,16 +4854,7 @@ fn signal_type_from_sv(
.unpacked_ranges()
.iter()
.map(|range| {
let left = sv::typecheck::eval_const_expr_with_types(
range.left(),
constants,
parameter_types,
)?;
let right = sv::typecheck::eval_const_expr_with_types(
range.right(),
constants,
parameter_types,
)?;
let (left, right) = unpacked_range_bounds(range, constants, parameter_types)?;
usize::try_from(left.abs_diff(right))
.ok()
.and_then(|width| width.checked_add(1))
Expand Down
15 changes: 1 addition & 14 deletions crates/celox-frontend-sv/src/lowering/procedural.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,7 @@ impl<'a> ProcModule<'a> {
let bounds: Option<Vec<_>> = r#type
.unpacked_ranges()
.iter()
.map(|range| {
Some((
sv::typecheck::eval_const_expr_with_types(
range.left(),
constants,
parameter_types,
)?,
sv::typecheck::eval_const_expr_with_types(
range.right(),
constants,
parameter_types,
)?,
))
})
.map(|range| super::unpacked_range_bounds(range, constants, parameter_types))
.collect();
if let Some(bounds) = bounds.filter(|bounds| !bounds.is_empty()) {
unpacked_bounds.insert(id, bounds);
Expand Down
23 changes: 18 additions & 5 deletions crates/celox-sv-analyzer/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,12 +630,21 @@ fn convert_type(
let unpacked_ranges: Vec<_> = r#type
.unpacked_ranges()
.iter()
.map(|range| UnpackedRange::new(range.left().clone().into(), range.right().clone().into()))
.map(|range| {
let left = range.left().clone().into();
let right = range.right().clone().into();
let resolved_bounds = typecheck::eval_const_expr_in_env(&left, constants)
.zip(typecheck::eval_const_expr_in_env(&right, constants));
UnpackedRange {
left,
right,
resolved_bounds,
}
})
.collect();
let packed_width = typecheck::resolve_packed_width_in_env(&packed_ranges, constants);
let unpacked_width = unpacked_ranges.iter().try_fold(1usize, |acc, range| {
let left = typecheck::eval_const_expr_in_env(range.left(), constants)?;
let right = typecheck::eval_const_expr_in_env(range.right(), constants)?;
let (left, right) = range.resolved_bounds()?;
let width = usize::try_from(left.abs_diff(right)).ok()?.checked_add(1)?;
acc.checked_mul(width)
});
Expand Down Expand Up @@ -664,11 +673,15 @@ pub struct PackedRange {
pub struct UnpackedRange {
left: ConstExpr,
right: ConstExpr,
resolved_bounds: Option<(i128, i128)>,
}

impl UnpackedRange {
pub(crate) fn new(left: ConstExpr, right: ConstExpr) -> Self {
Self { left, right }
/// Bounds evaluated in the declaring scope, including its constant
/// functions. The original expressions remain available for contracts
/// that must be specialized again with different parameters.
pub fn resolved_bounds(&self) -> Option<(i128, i128)> {
self.resolved_bounds
}

pub fn left(&self) -> &ConstExpr {
Expand Down
26 changes: 26 additions & 0 deletions crates/celox-sv-analyzer/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6175,3 +6175,29 @@ fn scoped_timed_task_static_formals_require_shared_storage() {
}
}
}

#[test]
fn unpacked_constant_function_bounds_survive_analysis_without_losing_the_contract() {
let ir = analyze_source(
r#"
module Top #(parameter int N = 3);
function automatic int extent(input int n); return n * 2; endfunction
logic [7:0] memory [extent(N)];
endmodule
"#,
Path::new("unpacked-constant-function-bounds.sv"),
)
.unwrap();
let ty = ir.modules()[0]
.signals()
.iter()
.find(|signal| signal.name() == "memory")
.unwrap()
.r#type();
let range = &ty.unpacked_ranges()[0];
assert_eq!(range.resolved_bounds(), Some((0, 5)));
assert_eq!(ty.resolved_width(), Some(48));
// Call expressions must remain symbolic for interface declaration
// contracts, even though lowering no longer owns their function bodies.
assert!(typecheck::eval_const_expr(range.right(), &HashMap::default()).is_none());
}
77 changes: 77 additions & 0 deletions crates/celox-test-suite/src/sv/cases/types.vtest
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,80 @@
"#)
(top Top)
(expect reject))

(case constant_functions_in_unpacked_dimensions
; IEEE 1800-2023 7.4.2 and 13.4.3: fixed unpacked bounds may
; call module-local or package constant functions.
(source "constant_function_dimensions.sv" #"
package Sizes;
function automatic int count(input int n); return n * 10; endfunction
endpackage
module Top(output int sized_count, ranged_count, imported_count, qualified_count,
inner_count, negative_left, negative_right);
import Sizes::*;
function automatic int f(input int n); return n * 10; endfunction
logic [7:0] sized [f(1)];
logic [7:0] ranged [0:f(2)-1];
logic [7:0] imported [count(3)];
logic [7:0] qualified [Sizes::count(4)];
logic [7:0] matrix [f(1)][Sizes::count(1)];
logic [7:0] negative [f(-1):f(-1)+2];
assign sized_count = $size(sized);
assign ranged_count = $size(ranged);
assign imported_count = $size(imported);
assign qualified_count = $size(qualified);
assign inner_count = $size(matrix, 2);
assign negative_left = $left(negative);
assign negative_right = $right(negative);
endmodule
"#)
(top Top)
(eval)
(assert_eq sized_count 10)
(assert_eq ranged_count 20)
(assert_eq imported_count 30)
(assert_eq qualified_count 40)
(assert_eq inner_count 10)
(assert_eq negative_left (trunc 32 -10))
(assert_eq negative_right (trunc 32 -8)))

(case constant_functions_in_unpacked_port_dimensions
; Each instance must resolve bounds with its own parameter values.
; The loop body cannot be replaced by a single declaration expression.
(source "constant_function_port_dimensions.sv" #"
module Sub #(parameter int N = 2)(output logic [7:0] values [extent(N)],
output int count, left_bound, right_bound);
function automatic int extent(input int n);
int total;
total = 0;
for (int i = 0; i < n; i++) total = total + 2;
return total;
endfunction
always_comb begin
for (int i = 0; i < extent(N); i++) values[i] = N * 10 + i;
end
assign count = $size(values);
assign left_bound = $left(values);
assign right_bound = $right(values);
endmodule
module Top(output logic [7:0] small_last, large_last,
output int small_count, large_count, small_left, large_right);
logic [7:0] small_values [2];
logic [7:0] large_values [6];
int unused_small_right, unused_large_left;
Sub #(.N(1)) small_inst(.values(small_values), .count(small_count),
.left_bound(small_left), .right_bound(unused_small_right));
Sub #(.N(3)) large_inst(.values(large_values), .count(large_count),
.left_bound(unused_large_left), .right_bound(large_right));
assign small_last = small_values[1];
assign large_last = large_values[5];
endmodule
"#)
(top Top)
(eval)
(assert_eq small_last 11)
(assert_eq large_last 35)
(assert_eq small_count 2)
(assert_eq large_count 6)
(assert_eq small_left 0)
(assert_eq large_right 5))
8 changes: 8 additions & 0 deletions crates/celox/tests/frontends/systemverilog/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
sv_backends! {
fn constant_functions_in_unpacked_port_dimensions(sim) {
@case "types::constant_functions_in_unpacked_port_dimensions";
}

fn constant_functions_in_unpacked_dimensions(sim) {
@case "types::constant_functions_in_unpacked_dimensions";
}

fn passes_unpacked_arrays_of_equivalent_element_types(sim) {
@case "types::passes_unpacked_arrays_of_equivalent_element_types";
}
Expand Down
Loading