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
43 changes: 43 additions & 0 deletions crates/celox-frontend-core/src/symbolic/assembly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AbsoluteAddr, BTreeSet<usize>>,
(source, source_access): (AbsoluteAddr, BitAccess),
(target, target_access): (AbsoluteAddr, BitAccess),
) -> bool {
let shifted: Vec<usize> = 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<InstancePath, InstanceId>,
Expand Down Expand Up @@ -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),
);
}
}
}

Expand Down
54 changes: 54 additions & 0 deletions crates/celox-test-suite/src/sv/cases/hierarchy.vtest
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
9 changes: 9 additions & 0 deletions crates/celox/tests/frontends/systemverilog/hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
Loading