diff --git a/crates/celox-frontend-core/src/symbolic/assembly.rs b/crates/celox-frontend-core/src/symbolic/assembly.rs index 8c11b502974..9f14b9f63ac 100644 --- a/crates/celox-frontend-core/src/symbolic/assembly.rs +++ b/crates/celox-frontend-core/src/symbolic/assembly.rs @@ -2401,6 +2401,29 @@ fn extend_boundaries( target.len() != old_len } +/// Add the boundaries of `source` within its range to `target`, mapping the +/// range of `source` onto the equally wide range of `target`. +fn extend_shifted_boundaries( + boundaries: &mut HashMap>, + (source, source_access): (AbsoluteAddr, BitAccess), + (target, target_access): (AbsoluteAddr, BitAccess), +) -> bool { + let shifted: Vec = boundaries + .get(&source) + .into_iter() + .flatten() + .filter(|&&boundary| source_access.lsb <= boundary && boundary <= source_access.msb + 1) + .map(|&boundary| boundary - source_access.lsb + target_access.lsb) + .collect(); + if shifted.is_empty() || (source == target && source_access == target_access) { + return false; + } + let target = boundaries.entry(target).or_default(); + let old_len = target.len(); + target.extend(shifted); + target.len() != old_len +} + fn propagate_boundaries( bindings: &PackageBindings, expanded: &HashMap, @@ -2453,6 +2476,26 @@ fn propagate_boundaries( child_abs, ); } + + // Child -> Parent: where the child reads the + // port in parts, the connected range of a parent + // variable is read in the same parts, as in + // `a = {x[7:4], ...}` within one module. Only a + // connection of one range as wide as the port maps + // bit for bit. + let mut sources = child_addr.sources.iter(); + if let (Some(source), None) = (sources.next(), sources.next()) + && let GlueAddr::Parent(parent_var) = source.id + && source.access.msb - source.access.lsb + == target.access.msb - target.access.lsb + { + let parent_abs = bindings.locate(*id, parent_var); + changed |= extend_shifted_boundaries( + &mut current_boundaries, + (child_abs, target.access), + (parent_abs, source.access), + ); + } } } diff --git a/crates/celox-test-suite/src/sv/cases/hierarchy.vtest b/crates/celox-test-suite/src/sv/cases/hierarchy.vtest index 6538509dbce..307729ae1be 100644 --- a/crates/celox-test-suite/src/sv/cases/hierarchy.vtest +++ b/crates/celox-test-suite/src/sv/cases/hierarchy.vtest @@ -1462,3 +1462,57 @@ (top Top) (eval) (assert_eq y (+ (<< 1 128) (+ (<< 2 43) 1))) (assert_eq width 129)) + +; A child's output port depends only on the bits of its input port that +; the child reads, so a parent may feed other bits of that output back into +; the port without a combinational loop (#1955). +(case child_ports_keep_the_bit_ranges_they_read + (source "port_bit_ranges.sv" #" + module Btb(input logic [7:0] ifpc_in, output logic [7:0] tar); + logic [3:0] tgt = 4'h9; + assign tar = {ifpc_in[7:4], tgt}; + endmodule + module Top; + logic clk = 0; + logic [7:0] tar, ifpc_out, fetch; + logic [7:0] ifpc = 8'h50; + assign fetch = tar; + always @(posedge clk) ifpc <= fetch; + assign ifpc_out = {ifpc[7:4], fetch[3:0]}; + Btb b(.ifpc_in(ifpc_out), .tar(tar)); + initial begin + #1 $display("%h", fetch); + $finish; + end + endmodule + "#) + (top Top) + (run_to_finish) + (expect_output "59\n")) + +; The same through a part-select of a wider parent variable connected to the +; input port: the bits the child reads map onto the selected range. +(case child_ports_keep_the_bit_ranges_of_selected_connections + (source "port_select_ranges.sv" #" + module Btb(input logic [7:0] ifpc_in, output logic [7:0] tar); + logic [3:0] tgt = 4'h9; + assign tar = {ifpc_in[7:4], tgt}; + endmodule + module Top; + logic clk = 0; + logic [7:0] tar, fetch; + logic [11:0] ifpc_out; + logic [7:0] ifpc = 8'h50; + assign fetch = tar; + always @(posedge clk) ifpc <= fetch; + assign ifpc_out = {ifpc[7:4], fetch[3:0], 4'h3}; + Btb b(.ifpc_in(ifpc_out[11:4]), .tar(tar)); + initial begin + #1 $display("%h %h", fetch, ifpc_out); + $finish; + end + endmodule + "#) + (top Top) + (run_to_finish) + (expect_output "59 593\n")) diff --git a/crates/celox/tests/frontends/systemverilog/hierarchy.rs b/crates/celox/tests/frontends/systemverilog/hierarchy.rs index 94f3838738b..ca63ad3c551 100644 --- a/crates/celox/tests/frontends/systemverilog/hierarchy.rs +++ b/crates/celox/tests/frontends/systemverilog/hierarchy.rs @@ -698,3 +698,12 @@ fn array_parameter_elements_check_unselected_conditional_branch_types() { ); } } + +sv_backends! { + fn child_ports_keep_the_bit_ranges_they_read(sim) { + @case "hierarchy::child_ports_keep_the_bit_ranges_they_read"; + } + fn child_ports_keep_the_bit_ranges_of_selected_connections(sim) { + @case "hierarchy::child_ports_keep_the_bit_ranges_of_selected_connections"; + } +}