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
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2023-2025, The OpenROAD Authors

#include <cstdio>
#include <fstream>
#include <iterator>
#include <memory>
#include <string>

#include "db_sta/dbSta.hh"
#include "gtest/gtest.h"
#include "odb/db.h"
#include "sta/NetworkClass.hh"
#include "sta/VerilogWriter.hh"
#include "tst/IntegratedFixture.h"
#include "utl/Logger.h"

namespace rsz {
class BufRemTest3 : public tst::IntegratedFixture
{
 protected:
  BufRemTest3()
      : tst::IntegratedFixture(tst::IntegratedFixture::Technology::kNangate45,
                               "_main/src/rsz/test/")
  {
    if (debug_) {
      logger_.setDebugLevel(utl::ODB, "DB_EDIT", 2);
      logger_.setDebugLevel(utl::RSZ, "remove_buffer", 3);
    }
  }

  bool debug_ = false;  // Set to true to generate debug output
};

TEST_F(BufRemTest3, RemoveBufferCase9)
{
  std::string test_name = "TestBufferRemoval3_9";
  readVerilogAndSetup(test_name + ".v");

  // Netlist before buffer removal:
  //  (undriven input) -> buf1 -> out

  // Dump pre ECO state
  if (debug_) {
    dumpVerilogAndOdb(test_name + "_pre_eco");
  }

  odb::dbDatabase::beginEco(block_);

  // Pre sanity check
  sta_->updateTiming(true);
  // Do not call checkAxioms() because there is an undriven buffer.

  //----------------------------------------------------
  // Remove buffer
  //----------------------------------------------------
  auto insts = std::make_unique<sta::InstanceSeq>();
  odb::dbInst* buf_inst = block_->findInst("buf1");
  ASSERT_NE(buf_inst, nullptr);
  sta::Instance* sta_buf = db_network_->dbToSta(buf_inst);
  insts->emplace_back(sta_buf);
  resizer_.removeBuffers(*insts);

  // Post sanity check
  sta_->updateTiming(true);
  // Do not call checkAxioms() because there is an undriven buffer.

  // Write verilog and check the content after buffer removal
  const std::string after_vlog_path = test_name + "_after.v";
  sta::writeVerilog(after_vlog_path.c_str(), false, {}, sta_->network());

  std::ifstream file_after(after_vlog_path);
  std::string content_after((std::istreambuf_iterator<char>(file_after)),
                            std::istreambuf_iterator<char>());

  // Netlist after buffer removal:
  // in -> mod_inst/mod_in -> assign -> mod_inst/mod_out -> out
  const std::string expected_after_vlog = R"(module top (clk,
    in,
    out);
 input clk;
 input in;
 output out;


endmodule
)";

  EXPECT_EQ(content_after, expected_after_vlog);

  odb::dbDatabase::undoEco(block_);

  // Dump undo ECO state
  if (debug_) {
    dumpVerilogAndOdb(test_name + "_undo_eco");
  }

  // Clean up
  removeFile(after_vlog_path);
}

TEST_F(BufRemTest3, RemoveBufferCase8)
{
  std::string test_name = "TestBufferRemoval3_8";
  readVerilogAndSetup(test_name + ".v");

  // Netlist before buffer removal:
  //  (undriven input) -> buf1 -> out

  // Dump pre ECO state
  if (debug_) {
    dumpVerilogAndOdb(test_name + "_pre_eco");
  }

  odb::dbDatabase::beginEco(block_);

  // Pre sanity check
  sta_->updateTiming(true);
  // Do not call checkAxioms() because there is an undriven buffer.

  //----------------------------------------------------
  // Remove buffer
  //----------------------------------------------------
  auto insts = std::make_unique<sta::InstanceSeq>();
  odb::dbInst* buf_inst = block_->findInst("buf1");
  ASSERT_NE(buf_inst, nullptr);
  sta::Instance* sta_buf = db_network_->dbToSta(buf_inst);
  insts->emplace_back(sta_buf);
  resizer_.removeBuffers(*insts);

  // Post sanity check
  sta_->updateTiming(true);
  // Do not call checkAxioms() because there is an undriven buffer.

  // Write verilog and check the content after buffer removal
  const std::string after_vlog_path = test_name + "_after.v";
  sta::writeVerilog(after_vlog_path.c_str(), false, {}, sta_->network());

  std::ifstream file_after(after_vlog_path);
  std::string content_after((std::istreambuf_iterator<char>(file_after)),
                            std::istreambuf_iterator<char>());

  // Netlist after buffer removal:
  // in -> mod_inst/mod_in -> assign -> mod_inst/mod_out -> out
  const std::string expected_after_vlog = R"(module top (clk,
    in,
    out);
 input clk;
 input in;
 output out;


 BUF_X1 buf1 (.Z(out));
endmodule
)";

  EXPECT_EQ(content_after, expected_after_vlog);

  odb::dbDatabase::undoEco(block_);

  // Dump undo ECO state
  if (debug_) {
    dumpVerilogAndOdb(test_name + "_undo_eco");
  }

  // Clean up
  removeFile(after_vlog_path);
}

TEST_F(BufRemTest3, RemoveBufferCase7)
{
  std::string test_name = "TestBufferRemoval3_7";
  readVerilogAndSetup(test_name + ".v");

  // Netlist before buffer removal:
  //  (undriven input) -> buf1 -> buf2 -> out

  // Dump pre ECO state
  if (debug_) {
    dumpVerilogAndOdb(test_name + "_pre_eco");
  }

  odb::dbDatabase::beginEco(block_);

  // Pre sanity check
  sta_->updateTiming(true);
  // Do not call checkAxioms() because there is an undriven buffer.

  //----------------------------------------------------
  // Remove buffer
  //----------------------------------------------------
  auto insts = std::make_unique<sta::InstanceSeq>();
  odb::dbInst* buf_inst = block_->findInst("buf1");
  ASSERT_NE(buf_inst, nullptr);
  sta::Instance* sta_buf = db_network_->dbToSta(buf_inst);
  insts->emplace_back(sta_buf);
  resizer_.removeBuffers(*insts);

  // Post sanity check
  sta_->updateTiming(true);
  // Do not call checkAxioms() because there is an undriven buffer.

  // Write verilog and check the content after buffer removal
  const std::string after_vlog_path = test_name + "_after.v";
  sta::writeVerilog(after_vlog_path.c_str(), false, {}, sta_->network());

  std::ifstream file_after(after_vlog_path);
  std::string content_after((std::istreambuf_iterator<char>(file_after)),
                            std::istreambuf_iterator<char>());

  // Netlist after buffer removal:
  // in -> mod_inst/mod_in -> assign -> mod_inst/mod_out -> out
  const std::string expected_after_vlog = R"(module top (clk,
    in,
    out);
 input clk;
 input in;
 output out;

 wire n1;

 BUF_X1 buf1 (.Z(n1));
 BUF_X1 buf2 (.A(n1),
    .Z(out));
endmodule
)";

  EXPECT_EQ(content_after, expected_after_vlog);

  odb::dbDatabase::undoEco(block_);

  // Dump undo ECO state
  if (debug_) {
    dumpVerilogAndOdb(test_name + "_undo_eco");
  }

  // Clean up
  removeFile(after_vlog_path);
}

TEST_F(BufRemTest3, RemoveBufferCase6)
{
  std::string test_name = "TestBufferRemoval3_6";
  readVerilogAndSetup(test_name + ".v");

  // Netlist before buffer removal:
  // in -> mod_inst/mod_in -> mod_inst/buf0 -> mod_inst/mod_out -> out

  // Dump pre ECO state
  if (debug_) {
    dumpVerilogAndOdb(test_name + "_pre_eco");
  }

  odb::dbDatabase::beginEco(block_);

  // Pre sanity check
  sta_->updateTiming(true);
  db_network_->checkAxioms();
  sta_->checkSanity();

  //----------------------------------------------------
  // Remove buffer
  //----------------------------------------------------
  auto insts = std::make_unique<sta::InstanceSeq>();
  resizer_.removeBuffers(*insts);

  // Post sanity check
  sta_->updateTiming(true);
  db_network_->checkAxioms();
  sta_->checkSanity();

  // Write verilog and check the content after buffer removal
  const std::string after_vlog_path = test_name + "_after.v";
  sta::writeVerilog(after_vlog_path.c_str(), false, {}, sta_->network());

  std::ifstream file_after(after_vlog_path);
  std::string content_after((std::istreambuf_iterator<char>(file_after)),
                            std::istreambuf_iterator<char>());

  // Netlist after buffer removal:
  // in -> mod_inst/mod_in -> assign -> mod_inst/mod_out -> out
  const std::string expected_after_vlog = R"(module top (clk,
    in,
    out);
 input clk;
 input in;
 output out;


 MOD mod_inst (.mod_in(in),
    .mod_out(out));
endmodule
module MOD (mod_in,
    mod_out);
 input mod_in;
 output mod_out;


 assign mod_out = mod_in;
endmodule
)";

  EXPECT_EQ(content_after, expected_after_vlog);

  odb::dbDatabase::undoEco(block_);

  // Dump undo ECO state
  if (debug_) {
    dumpVerilogAndOdb(test_name + "_undo_eco");
  }

  // Clean up
  removeFile(after_vlog_path);
}

TEST_F(BufRemTest3, RemoveBufferCase5)
{
  std::string test_name = "TestBufferRemoval3_5";
  readVerilogAndSetup(test_name + ".v");

  // Netlist before buffer removal:
  // in -> buf0 -> out

  // Dump pre ECO state
  if (debug_) {
    dumpVerilogAndOdb(test_name + "_pre_eco");
  }

  odb::dbDatabase::beginEco(block_);

  // Pre sanity check
  sta_->updateTiming(true);
  db_network_->checkAxioms();
  sta_->checkSanity();

  //----------------------------------------------------
  // Remove buffer
  //----------------------------------------------------
  auto insts = std::make_unique<sta::InstanceSeq>();
  resizer_.removeBuffers(*insts);

  // removeBuffers will do nothing since buf0 is connecting two ports.

  // Post sanity check
  sta_->updateTiming(true);
  db_network_->checkAxioms();
  sta_->checkSanity();

  // Write verilog and check the content after buffer removal
  const std::string after_vlog_path = test_name + "_after.v";
  sta::writeVerilog(after_vlog_path.c_str(), false, {}, sta_->network());

  std::ifstream file_after(after_vlog_path);
  std::string content_after((std::istreambuf_iterator<char>(file_after)),
                            std::istreambuf_iterator<char>());

  // Netlist after buffer removal:
  // in -> out
  const std::string expected_after_vlog = R"(module top (clk,
    in,
    out);
 input clk;
 input in;
 output out;


 assign out = in;
endmodule
)";

  EXPECT_EQ(content_after, expected_after_vlog);

  odb::dbDatabase::undoEco(block_);

  // Dump undo ECO state
  if (debug_) {
    dumpVerilogAndOdb(test_name + "_undo_eco");
  }

  // Clean up
  removeFile(after_vlog_path);
}

TEST_F(BufRemTest3, RemoveBufferCase4)
{
  std::string test_name = "TestBufferRemoval3_4";
  readVerilogAndSetup(test_name + ".v");

  // Netlist before buffer removal:
  // DFF_X1/Q -> buf_top1 -> load_top1 -> out1
  // DFF_X1/Q -> buf_top1 -> load_top2 -> out2
  // DFF_X1/Q -> buf_top1 -> mod3_inst/load_mod3_1 -> out5
  // DFF_X1/Q -> buf_top2 -> load_top3 -> out3
  // DFF_X1/Q -> buf_top2 -> mod3_inst/load_mod3_2 -> out6
  // DFF_X1/Q -> mod2_inst/buf_mod2 -> load_top4 -> out4
  // DFF_X1/Q -> mod2_inst/buf_mod2 -> mod3_inst/load_mod3_3

  // Pre sanity check
  sta_->updateTiming(true);
  db_network_->checkAxioms();
  sta_->checkSanity();

  // Dump pre ECO state
  if (debug_) {
    dumpVerilogAndOdb(test_name + "_pre_eco");
  }

  odb::dbDatabase::beginEco(block_);

  //----------------------------------------------------
  // Remove buffer
  //----------------------------------------------------
  auto insts = std::make_unique<sta::InstanceSeq>();
  resizer_.removeBuffers(*insts);

  // Post sanity check
  sta_->updateTiming(true);
  db_network_->checkAxioms();
  sta_->checkSanity();

  // Write verilog and check the content after buffer removal
  const std::string after_vlog_path = test_name + "_after.v";
  sta::writeVerilog(after_vlog_path.c_str(), false, {}, sta_->network());

  std::ifstream file_after(after_vlog_path);
  std::string content_after((std::istreambuf_iterator<char>(file_after)),
                            std::istreambuf_iterator<char>());

  // Netlist after buffer removal:
  // DFF_X1/Q -> out1, out2, out3, out4, out5, out6
  const std::string expected_after_vlog = R"(module top (clk,
    in1,
    out1,
    out2,
    out3,
    out4,
    out5,
    out6);
 input clk;
 input in1;
 output out1;
 output out2;
 output out3;
 output out4;
 output out5;
 output out6;


 MOD1 mod1_inst (.clk_in(clk),
    .d_in(in1),
    .q_out(out1));
 MOD2 mod2_inst (.in(out1),
    .out(out4));
 MOD3 mod3_inst (.in1(out1),
    .in2(out1),
    .in3(out4),
    .out1(out5),
    .out2(out6));
 assign out2 = out1;
 assign out3 = out1;
endmodule
module MOD1 (clk_in,
    d_in,
    q_out);
 input clk_in;
 input d_in;
 output q_out;


 DFF_X1 drvr (.D(d_in),
    .CK(clk_in),
    .Q(q_out));
endmodule
module MOD2 (in,
    out);
 input in;
 output out;


 assign out = in;
endmodule
module MOD3 (in1,
    in2,
    in3,
    out1,
    out2);
 input in1;
 input in2;
 input in3;
 output out1;
 output out2;


 assign out2 = in2;
 assign out1 = in1;
endmodule
)";

  EXPECT_EQ(content_after, expected_after_vlog);

  odb::dbDatabase::undoEco(block_);

  // Dump undo ECO state
  if (debug_) {
    dumpVerilogAndOdb(test_name + "_undo_eco");
  }

  // Clean up
  removeFile(after_vlog_path);
}

TEST_F(BufRemTest3, RemoveBufferCase3)
{
  std::string test_name = "TestBufferRemoval3_3";
  readVerilogAndSetup(test_name + ".v");

  // Netlist before buffer removal:
  // DFF_X1/Q -> buf0 -> mod_inst/buf1 -> buf2 -> out1

  odb::dbInst* buf_inst = block_->findInst("mod_inst/buf1");
  ASSERT_NE(buf_inst, nullptr);
  sta::Instance* sta_buf = db_network_->dbToSta(buf_inst);

  // Dump pre ECO state
  if (debug_) {
    dumpVerilogAndOdb(test_name + "_pre_eco");
  }

  odb::dbDatabase::beginEco(block_);

  // Pre sanity check
  sta_->updateTiming(true);
  db_network_->checkAxioms();
  sta_->checkSanity();

  //----------------------------------------------------
  // Remove buffer
  //----------------------------------------------------
  auto insts = std::make_unique<sta::InstanceSeq>();
  insts->emplace_back(sta_buf);
  resizer_.removeBuffers(*insts);

  // Post sanity check
  sta_->updateTiming(true);
  db_network_->checkAxioms();
  sta_->checkSanity();

  // Write verilog and check the content after buffer removal
  const std::string after_vlog_path = test_name + "_after.v";
  sta::writeVerilog(after_vlog_path.c_str(), false, {}, sta_->network());

  std::ifstream file_after(after_vlog_path);
  std::string content_after((std::istreambuf_iterator<char>(file_after)),
                            std::istreambuf_iterator<char>());

  // Netlist after buffer removal:
  // DFF_X1/Q -> buf0 -> buf2 -> out1
  const std::string expected_after_vlog = R"(module top (clk,
    in1,
    out1);
 input clk;
 input in1;
 output out1;

 wire buf1_out;
 wire buf0_out;
 wire dff_q;

 BUF_X1 buf0 (.A(dff_q),
    .Z(buf0_out));
 BUF_X1 buf2 (.A(buf1_out),
    .Z(out1));
 DFF_X1 dff_inst (.D(in1),
    .CK(clk),
    .Q(dff_q));
 MOD mod_inst (.in(buf0_out),
    .out(buf1_out));
endmodule
module MOD (in,
    out);
 input in;
 output out;


 assign out = in;
endmodule
)";

  EXPECT_EQ(content_after, expected_after_vlog);

  odb::dbDatabase::undoEco(block_);

  // Dump undo ECO state
  if (debug_) {
    dumpVerilogAndOdb(test_name + "_undo_eco");
  }

  // Clean up
  removeFile(after_vlog_path);
}

TEST_F(BufRemTest3, RemoveBufferCase2)
{
  std::string test_name = "TestBufferRemoval3_2";
  readVerilogAndSetup(test_name + ".v");

  odb::dbInst* buf_inst = block_->findInst("sub_inst/buf");
  ASSERT_NE(buf_inst, nullptr);
  sta::Instance* sta_buf = db_network_->dbToSta(buf_inst);

  // Dump pre ECO state
  if (debug_) {
    dumpVerilogAndOdb(test_name + "_pre_eco");
  }

  odb::dbDatabase::beginEco(block_);

  // Pre sanity check
  sta_->updateTiming(true);
  db_network_->checkAxioms();
  sta_->checkSanity();

  //----------------------------------------------------
  // Remove buffer
  //----------------------------------------------------
  auto insts = std::make_unique<sta::InstanceSeq>();
  insts->emplace_back(sta_buf);
  resizer_.removeBuffers(*insts);

  // Post sanity check
  sta_->updateTiming(true);
  db_network_->checkAxioms();
  sta_->checkSanity();

  // Write verilog and check the content after buffer removal
  const std::string after_vlog_path = test_name + "_after.v";
  sta::writeVerilog(after_vlog_path.c_str(), false, {}, sta_->network());

  std::ifstream file_after(after_vlog_path);
  std::string content_after((std::istreambuf_iterator<char>(file_after)),
                            std::istreambuf_iterator<char>());

  const std::string expected_after_vlog = R"(module top (clk,
    in1,
    out1);
 input clk;
 input in1;
 output out1;

 wire sub_out;

 BUF_X4 load (.A(sub_out),
    .Z(out1));
 SUBMOD sub_inst (.in(in1),
    .out(sub_out));
endmodule
module SUBMOD (in,
    out);
 input in;
 output out;


 BUF_X1 load0 (.A(in),
    .Z(out));
endmodule
)";

  EXPECT_EQ(content_after, expected_after_vlog);

  odb::dbDatabase::undoEco(block_);

  // Dump undo ECO state
  if (debug_) {
    dumpVerilogAndOdb(test_name + "_undo_eco");
  }

  // Clean up
  removeFile(after_vlog_path);
}

TEST_F(BufRemTest3, RemoveBufferCase1)
{
  std::string test_name = "TestBufferRemoval3_1";
  readVerilogAndSetup(test_name + ".v");

  odb::dbInst* buf_inst = block_->findInst("buf");
  ASSERT_NE(buf_inst, nullptr);
  sta::Instance* sta_buf = db_network_->dbToSta(buf_inst);

  // Dump pre ECO state
  if (debug_) {
    dumpVerilogAndOdb(test_name + "_pre_eco");
  }

  odb::dbDatabase::beginEco(block_);

  // Pre sanity check
  sta_->updateTiming(true);
  db_network_->checkAxioms();
  sta_->checkSanity();

  //----------------------------------------------------
  // Remove buffer
  //----------------------------------------------------
  auto insts = std::make_unique<sta::InstanceSeq>();
  insts->emplace_back(sta_buf);
  resizer_.removeBuffers(*insts);

  // Post sanity check
  sta_->updateTiming(true);
  db_network_->checkAxioms();
  sta_->checkSanity();

  // Write verilog and check the content after buffer removal
  const std::string after_vlog_path = test_name + "_after.v";
  sta::writeVerilog(after_vlog_path.c_str(), false, {}, sta_->network());

  std::ifstream file_after(after_vlog_path);
  std::string content_after((std::istreambuf_iterator<char>(file_after)),
                            std::istreambuf_iterator<char>());

  const std::string expected_after_vlog = R"(module top (clk,
    in1,
    out1,
    out2);
 input clk;
 input in1;
 output out1;
 output out2;

 wire net1;

 BUF_X1 drvr (.A(in1),
    .Z(net1));
 BUF_X4 load (.A(net1),
    .Z(out1));
 SUBMOD sub_inst (.in(net1),
    .out(out2));
endmodule
module SUBMOD (in,
    out);
 input in;
 output out;


 BUF_X1 load0 (.A(in),
    .Z(out));
endmodule
)";

  EXPECT_EQ(content_after, expected_after_vlog);

  odb::dbDatabase::undoEco(block_);

  // Dump undo ECO state
  if (debug_) {
    dumpVerilogAndOdb(test_name + "_undo_eco");
  }

  // Clean up
  removeFile(after_vlog_path);
}

TEST_F(BufRemTest3, RemoveBufferCase0)
{
  std::string test_name = "TestBufferRemoval3_0";
  readVerilogAndSetup(test_name + ".v");

  odb::dbModNet* modnet = block_->findModNet("mem/A0");
  ASSERT_NE(modnet, nullptr);

  odb::dbInst* buf_inst = block_->findInst("buf");
  ASSERT_NE(buf_inst, nullptr);
  sta::Instance* sta_buf = db_network_->dbToSta(buf_inst);

  // Dump pre ECO state
  if (debug_) {
    dumpVerilogAndOdb(test_name + "_pre_eco");
  }

  odb::dbDatabase::beginEco(block_);

  // Pre sanity check
  sta_->updateTiming(true);
  db_network_->checkAxioms();
  sta_->checkSanity();

  //----------------------------------------------------
  // Remove buffer
  //----------------------------------------------------
  auto insts = std::make_unique<sta::InstanceSeq>();
  insts->emplace_back(sta_buf);
  resizer_.removeBuffers(*insts);

  // Post sanity check
  sta_->updateTiming(true);
  db_network_->checkAxioms();
  sta_->checkSanity();

  // Write verilog and check the content after buffer removal
  const std::string after_vlog_path = test_name + "_after.v";
  sta::writeVerilog(after_vlog_path.c_str(), false, {}, sta_->network());

  std::ifstream file_after(after_vlog_path);
  std::string content_after((std::istreambuf_iterator<char>(file_after)),
                            std::istreambuf_iterator<char>());

  const std::string expected_after_vlog = R"(module top (clk,
    in1,
    out1,
    out2);
 input clk;
 input in1;
 output out1;
 output out2;

 wire net1;

 BUF_X1 drvr (.A(in1),
    .Z(net1));
 BUF_X4 load (.A(net1),
    .Z(out1));
 MEM mem (.Z1(out2),
    .A1(net1),
    .A0(net1));
endmodule
module MEM (Z1,
    A1,
    A0);
 output Z1;
 input A1;
 input A0;


 BUF_X1 load0 (.A(A0));
 BUF_X1 load1 (.A(A1),
    .Z(Z1));
endmodule
)";

  EXPECT_EQ(content_after, expected_after_vlog);

  odb::dbDatabase::undoEco(block_);

  // Dump undo ECO state
  if (debug_) {
    dumpVerilogAndOdb(test_name + "_undo_eco");
  }

  // Clean up
  removeFile(after_vlog_path);
}

// Regression test for feedthrough buffer removal
//
// Design: top has a register driving child_mod's data_i (internal wire),
//   and child_mod's data_o connects to a top-level output port.
//   Inside child_mod, data_i -> BUF_X1 -> data_o (feedthrough buffer).
//
// When remove_buffers removes the feedthrough buffer, the buffer
// removal logic detects the feedthrough and keeps the input ModNet
// as the survivor.  This ensures VerilogWriter emits
// "assign data_o = data_i;" correctly.
TEST_F(BufRemTest3, FeedthroughAssign)
{
  std::string test_name = "TestBufferRemoval3_feedthrough";
  readVerilogAndSetup(test_name + ".v", /*init_default_sdc=*/false);

  // Verify the feedthrough buffer exists before removal
  odb::dbModule* child_mod = block_->findModule("child_mod");
  ASSERT_NE(child_mod, nullptr);
  odb::dbInst* buf_inst = block_->findInst("u_child/u_ft");
  ASSERT_NE(buf_inst, nullptr) << "Feedthrough buffer u_child/u_ft not found";

  // Before remove_buffers: two separate ModNets
  odb::dbModBTerm* bt_in = child_mod->findModBTerm("data_i");
  odb::dbModBTerm* bt_out = child_mod->findModBTerm("data_o");
  ASSERT_NE(bt_in, nullptr);
  ASSERT_NE(bt_out, nullptr);
  EXPECT_NE(bt_in->getModNet(), bt_out->getModNet())
      << "ModNets should be separate before remove_buffers";

  // Run remove_buffers — the buffer removal logic detects the
  // feedthrough and forces the input ModNet to survive.
  resizer_.removeBuffers({});

  // After remove_buffers: buffer is gone
  EXPECT_EQ(block_->findInst("u_child/u_ft"), nullptr)
      << "Feedthrough buffer should be removed";

  // write_verilog should emit "assign data_o = data_i;" for the
  // feedthrough since port_name("data_o") != net_name("data_i").
  writeAndCompareVerilogOutputFile(test_name, test_name + "_post.v");
}

// Regression test for a feedthrough buffer whose two sides sit at different
// hierarchy depths, where remove_buffers leaves a kept sub-module output port
// without any driver.
//
// Design:
//   top
//   +- u_wrap (wrap_mod)
//   |    +- u_blk (blk_mod)
//   |         +- u_drv    (drv_mod)  NOR2_X1 g_drv drives drv_o
//   |         +- u_ft_mod (ft_mod)   ft_i -> BUF_X1 u_ft -> ft_o
//   +- u_sink (sink_mod)             sink_i -> OR2_X1 g_sink .A1
//
// Unlike FeedthroughAssign, the buffer output leaves ft_mod, blk_mod and
// wrap_mod before it re-enters sink_mod, so the two flat nets around the
// buffer end up at very different depths:
//   input  side  u_wrap/u_blk/drv_to_ft
//   output side  mid                      (top level)
//
// removeBuffer() correctly keeps the input ModNet as the survivor for the
// feedthrough, but because the surviving flat net is the deeper one it then
// renames the surviving ModNet to the removed one's name, i.e. to the output
// port name "ft_o".  VerilogWriter now sees output port "ft_o" sitting on a
// net also called "ft_o" and emits nothing, so the "assign ft_o = ft_i;"
// bridge is lost and ft_mod comes out with an undriven output port.
//
// The flat dbNet merge itself is correct, which is why placement and routing
// never notice the problem and only LEC or gate level simulation fails.
//
// NOTE: this test fails on current master by design.  The golden file holds
// the netlist that a correct remove_buffers has to produce.
TEST_F(BufRemTest3, HierFeedthroughAcrossLevels)
{
  std::string test_name = "TestBufferRemoval3_hier_feedthrough";
  readVerilogAndSetup(test_name + ".v", /*init_default_sdc=*/false);

  odb::dbModule* ft_mod = block_->findModule("ft_mod");
  ASSERT_NE(ft_mod, nullptr);
  ASSERT_NE(block_->findInst("u_wrap/u_blk/u_ft_mod/u_ft"), nullptr)
      << "Feedthrough buffer u_ft not found";

  // The leaf driver and the leaf load at the two ends of the whole path.
  odb::dbInst* drv_inst = block_->findInst("u_wrap/u_blk/u_drv/g_drv");
  odb::dbInst* sink_inst = block_->findInst("u_sink/g_sink");
  ASSERT_NE(drv_inst, nullptr);
  ASSERT_NE(sink_inst, nullptr);
  odb::dbITerm* drv_iterm = drv_inst->findITerm("ZN");
  odb::dbITerm* sink_iterm = sink_inst->findITerm("A1");
  ASSERT_NE(drv_iterm, nullptr);
  ASSERT_NE(sink_iterm, nullptr);

  odb::dbModBTerm* bt_in = ft_mod->findModBTerm("ft_i");
  odb::dbModBTerm* bt_out = ft_mod->findModBTerm("ft_o");
  ASSERT_NE(bt_in, nullptr);
  ASSERT_NE(bt_out, nullptr);

  // Before removal the buffer separates the two flat nets as well as the two
  // boundary ModNets of ft_mod.  The depth asymmetry is what triggers the
  // survivor rename inside removeBuffer().
  EXPECT_NE(drv_iterm->getNet(), sink_iterm->getNet());
  EXPECT_NE(bt_in->getModNet(), bt_out->getModNet())
      << "ModNets should be separate before remove_buffers";
  EXPECT_EQ(drv_iterm->getNet()->getName(), "u_wrap/u_blk/drv_to_ft");
  EXPECT_EQ(sink_iterm->getNet()->getName(), "mid");

  if (debug_) {
    std::cout << "pre  ft_i modnet: " << bt_in->getModNet()->getName() << "\n";
    std::cout << "pre  ft_o modnet: " << bt_out->getModNet()->getName() << "\n";
  }

  resizer_.removeBuffers({});

  EXPECT_EQ(block_->findInst("u_wrap/u_blk/u_ft_mod/u_ft"), nullptr)
      << "Feedthrough buffer should be removed";

  // Flat view: the merge is correct, the leaf driver and the leaf load share
  // one dbNet.  This part already works today.
  EXPECT_EQ(drv_iterm->getNet(), sink_iterm->getNet())
      << "flat dbNets should be merged after buffer removal";

  // Hierarchical view: both boundary terminals stay bound to one ModNet, so
  // the database itself is consistent.  What breaks is the name that ModNet
  // is given, because VerilogWriter can only express the feedthrough when the
  // net name differs from the output port name.
  odb::dbModNet* in_modnet = bt_in->getModNet();
  odb::dbModNet* out_modnet = bt_out->getModNet();
  ASSERT_NE(in_modnet, nullptr) << "ft_i lost its dbModNet";
  ASSERT_NE(out_modnet, nullptr) << "ft_o lost its dbModNet";
  EXPECT_EQ(in_modnet, out_modnet)
      << "ft_mod boundary ModNets should be merged after buffer removal";
  if (debug_) {
    std::cout << "post ft_i modnet: " << in_modnet->getName() << "\n";
    std::cout << "post ft_o modnet: " << out_modnet->getName() << "\n";
  }
  EXPECT_NE(in_modnet->getName(), std::string("ft_o"))
      << "surviving ModNet must not take the output port name, otherwise "
         "write_verilog drops the feedthrough assign";

  // The emitted netlist must keep ft_mod's output port driven, which requires
  // "assign ft_o = ft_i;" inside ft_mod.
  writeAndCompareVerilogOutputFile(test_name, test_name + "_post.v");
}

}  // namespace rsz