Skip to content

Commit 8b95fed

Browse files
authored
Merge pull request #9877 from The-OpenROAD-Project-staging/secure-fix-feedthrough-by-rmbuf
rsz: fix feedthrough buffer removal breaking write_verilog assign
2 parents cfd36d4 + 41c082d commit 8b95fed

5 files changed

Lines changed: 158 additions & 7 deletions

File tree

‎src/rsz/src/UnbufferMove.cc‎

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "UnbufferMove.hh"
55

6+
#include <algorithm>
67
#include <cmath>
78
#include <optional>
89
#include <string>
@@ -14,6 +15,7 @@
1415
#include "SplitLoadMove.hh"
1516
#include "SwapPinsMove.hh"
1617
#include "odb/db.h"
18+
#include "odb/dbSet.h"
1719
#include "rsz/Resizer.hh"
1820
#include "sta/ArcDelayCalc.hh"
1921
#include "sta/Delay.hh"
@@ -373,13 +375,19 @@ bool UnbufferMove::removeBuffer(Instance* buffer)
373375
odb::dbModNet* ip_modnet = db_network_->hierNet(in_pin);
374376
odb::dbModNet* survivor_modnet = ip_modnet;
375377
odb::dbModNet* removed_modnet = op_modnet;
376-
bool in_net_has_port = db_network_->hasPort(in_net);
377-
bool out_net_has_port = db_network_->hasPort(out_net);
378-
if (!in_net_has_port && out_net_has_port) {
379-
survivor = out_net;
380-
removed = in_net;
381-
survivor_modnet = op_modnet;
382-
removed_modnet = ip_modnet;
378+
379+
// When the buffer removal creates a feedthrough (input port directly
380+
// wired to output port), the input ModNet must survive so that
381+
// VerilogWriter sees port_name != net_name and emits the assign.
382+
if (!bufferRemovalCreatesFeedthrough(ip_modnet, op_modnet)) {
383+
bool in_net_has_port = db_network_->hasPort(in_net);
384+
bool out_net_has_port = db_network_->hasPort(out_net);
385+
if (!in_net_has_port && out_net_has_port) {
386+
survivor = out_net;
387+
removed = in_net;
388+
survivor_modnet = op_modnet;
389+
removed_modnet = ip_modnet;
390+
}
383391
}
384392

385393
// Disconnect the buffer and handle the nets
@@ -453,4 +461,29 @@ bool UnbufferMove::removeBuffer(Instance* buffer)
453461
return true;
454462
}
455463

464+
// Check if removing a buffer creates a feedthrough: input port directly
465+
// wired to output port within the same module.
466+
bool UnbufferMove::bufferRemovalCreatesFeedthrough(
467+
odb::dbModNet* ip_modnet,
468+
odb::dbModNet* op_modnet) const
469+
{
470+
if (!ip_modnet || !op_modnet
471+
|| ip_modnet->getParent() != op_modnet->getParent()) {
472+
return false;
473+
}
474+
odb::dbSet<odb::dbModBTerm> ip_bterms = ip_modnet->getModBTerms();
475+
const bool ip_has_input_port
476+
= std::ranges::any_of(ip_bterms, [](odb::dbModBTerm* bt) {
477+
return bt->getIoType() == odb::dbIoType::INPUT;
478+
});
479+
480+
odb::dbSet<odb::dbModBTerm> op_bterms = op_modnet->getModBTerms();
481+
const bool op_has_output_port
482+
= std::ranges::any_of(op_bterms, [](odb::dbModBTerm* bt) {
483+
return bt->getIoType() == odb::dbIoType::OUTPUT;
484+
});
485+
486+
return ip_has_input_port && op_has_output_port;
487+
}
488+
456489
} // namespace rsz

‎src/rsz/src/UnbufferMove.hh‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
#include "sta/Path.hh"
88
#include "sta/PathExpanded.hh"
99

10+
namespace odb {
11+
class dbModNet;
12+
}
13+
1014
namespace rsz {
1115

1216
class UnbufferMove : public BaseMove
@@ -28,6 +32,10 @@ class UnbufferMove : public BaseMove
2832

2933
private:
3034
bool bufferBetweenPorts(sta::Instance* buffer);
35+
// Returns true if removing buffer creates a feedthrough (input port
36+
// directly wired to output port within the same module).
37+
bool bufferRemovalCreatesFeedthrough(odb::dbModNet* ip_modnet,
38+
odb::dbModNet* op_modnet) const;
3139

3240
static constexpr int buffer_removal_max_fanout_ = 10;
3341
};

‎src/rsz/test/cpp/TestBufferRemoval3.cc‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,4 +851,46 @@ endmodule
851851
removeFile(after_vlog_path);
852852
}
853853

854+
// Regression test for feedthrough buffer removal
855+
//
856+
// Design: top has a register driving child_mod's data_i (internal wire),
857+
// and child_mod's data_o connects to a top-level output port.
858+
// Inside child_mod, data_i -> BUF_X1 -> data_o (feedthrough buffer).
859+
//
860+
// When remove_buffers removes the feedthrough buffer, the fix in
861+
// UnbufferMove.cc detects the feedthrough and keeps the input ModNet
862+
// as the survivor. This ensures VerilogWriter emits
863+
// "assign data_o = data_i;" correctly.
864+
TEST_F(BufRemTest3, FeedthroughAssign)
865+
{
866+
std::string test_name = "TestBufferRemoval3_feedthrough";
867+
readVerilogAndSetup(test_name + ".v", /*init_default_sdc=*/false);
868+
869+
// Verify the feedthrough buffer exists before removal
870+
odb::dbModule* child_mod = block_->findModule("child_mod");
871+
ASSERT_NE(child_mod, nullptr);
872+
odb::dbInst* buf_inst = block_->findInst("u_child/u_ft");
873+
ASSERT_NE(buf_inst, nullptr) << "Feedthrough buffer u_child/u_ft not found";
874+
875+
// Before remove_buffers: two separate ModNets
876+
odb::dbModBTerm* bt_in = child_mod->findModBTerm("data_i");
877+
odb::dbModBTerm* bt_out = child_mod->findModBTerm("data_o");
878+
ASSERT_NE(bt_in, nullptr);
879+
ASSERT_NE(bt_out, nullptr);
880+
EXPECT_NE(bt_in->getModNet(), bt_out->getModNet())
881+
<< "ModNets should be separate before remove_buffers";
882+
883+
// Run remove_buffers — the fix in UnbufferMove.cc detects the
884+
// feedthrough and forces the input ModNet to survive.
885+
resizer_.removeBuffers({});
886+
887+
// After remove_buffers: buffer is gone
888+
EXPECT_EQ(block_->findInst("u_child/u_ft"), nullptr)
889+
<< "Feedthrough buffer should be removed";
890+
891+
// write_verilog should emit "assign data_o = data_i;" for the
892+
// feedthrough since port_name("data_o") != net_name("data_i").
893+
writeAndCompareVerilogOutputFile(test_name, test_name + "_post.v");
894+
}
895+
854896
} // namespace rsz
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Hierarchical design with a feedthrough buffer in child_mod.
2+
// child_mod has data_i -> BUF_X1 -> data_o (feedthrough via buffer).
3+
//
4+
// In top, the child's data_i is driven by an internal wire (not a port),
5+
// while data_o connects to an output port. This means when
6+
// remove_buffers removes the feedthrough buffer:
7+
// - input side net has NO port (internal wire)
8+
// - output side net HAS a port (data_o)
9+
// So the output net becomes the survivor, and the merged ModNet
10+
// takes the output port name — triggering the bug.
11+
module child_mod (data_i, data_o, in1, out1);
12+
input data_i;
13+
output data_o;
14+
input in1;
15+
output out1;
16+
17+
BUF_X1 u_ft (.A(data_i), .Z(data_o));
18+
BUF_X1 u1 (.A(in1), .Z(out1));
19+
endmodule
20+
21+
module top (clk, in1, out1, data_o);
22+
input clk;
23+
input in1;
24+
output out1;
25+
output data_o;
26+
27+
wire data_internal;
28+
29+
// Drive data_internal from a register output (not a port)
30+
DFF_X1 r1 (.D(in1), .CK(clk), .Q(data_internal), .QN());
31+
32+
child_mod u_child (.data_i(data_internal),
33+
.data_o(data_o),
34+
.in1(in1),
35+
.out1(out1));
36+
endmodule
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module top (clk,
2+
data_o,
3+
in1,
4+
out1);
5+
input clk;
6+
output data_o;
7+
input in1;
8+
output out1;
9+
10+
wire data_internal;
11+
12+
DFF_X1 r1 (.D(in1),
13+
.CK(clk),
14+
.Q(data_internal));
15+
child_mod u_child (.data_i(data_internal),
16+
.data_o(data_o),
17+
.in1(in1),
18+
.out1(out1));
19+
endmodule
20+
module child_mod (data_i,
21+
data_o,
22+
in1,
23+
out1);
24+
input data_i;
25+
output data_o;
26+
input in1;
27+
output out1;
28+
29+
30+
assign out1 = in1;
31+
assign data_o = data_i;
32+
endmodule

0 commit comments

Comments
 (0)