diff --git a/crates/celox-frontend-sv/src/lowering.rs b/crates/celox-frontend-sv/src/lowering.rs index 66e5d5607c9..d99d1410139 100644 --- a/crates/celox-frontend-sv/src/lowering.rs +++ b/crates/celox-frontend-sv/src/lowering.rs @@ -4803,6 +4803,23 @@ struct SvSignalType { type_kind: PortTypeKind, } +fn unpacked_range_bounds( + range: &sv::ir::UnpackedRange, + constants: &HashMap, + parameter_types: &HashMap, +) -> 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()) +} + fn signal_type_from_sv( typ: &sv::ir::Type, constants: &HashMap, @@ -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)) diff --git a/crates/celox-frontend-sv/src/lowering/procedural.rs b/crates/celox-frontend-sv/src/lowering/procedural.rs index 56ef6cc6520..155ef0cb96c 100644 --- a/crates/celox-frontend-sv/src/lowering/procedural.rs +++ b/crates/celox-frontend-sv/src/lowering/procedural.rs @@ -109,20 +109,7 @@ impl<'a> ProcModule<'a> { let bounds: Option> = 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); diff --git a/crates/celox-sv-analyzer/src/ir.rs b/crates/celox-sv-analyzer/src/ir.rs index a3c3e4ad7e9..fda3a758d39 100644 --- a/crates/celox-sv-analyzer/src/ir.rs +++ b/crates/celox-sv-analyzer/src/ir.rs @@ -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) }); @@ -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 { diff --git a/crates/celox-sv-analyzer/src/tests.rs b/crates/celox-sv-analyzer/src/tests.rs index 067b0824d65..24d99e7046f 100644 --- a/crates/celox-sv-analyzer/src/tests.rs +++ b/crates/celox-sv-analyzer/src/tests.rs @@ -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()); +} diff --git a/crates/celox-test-suite/src/sv/cases/types.vtest b/crates/celox-test-suite/src/sv/cases/types.vtest index a0a4db4cc08..36039be5839 100644 --- a/crates/celox-test-suite/src/sv/cases/types.vtest +++ b/crates/celox-test-suite/src/sv/cases/types.vtest @@ -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)) diff --git a/crates/celox/tests/frontends/systemverilog/types.rs b/crates/celox/tests/frontends/systemverilog/types.rs index 6286cab494c..1de590fbf14 100644 --- a/crates/celox/tests/frontends/systemverilog/types.rs +++ b/crates/celox/tests/frontends/systemverilog/types.rs @@ -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"; }