diff --git a/src/rsz/src/RepairSetup.cc b/src/rsz/src/RepairSetup.cc index 25898361a89..0a5dc52f827 100644 --- a/src/rsz/src/RepairSetup.cc +++ b/src/rsz/src/RepairSetup.cc @@ -243,6 +243,9 @@ bool RepairSetup::repairSetup(const float setup_slack_margin, float initial_tns = sta_->totalNegativeSlack(max_); float prev_tns = initial_tns; + // Capture the initial WNS once per repair_timing call; the WNS-stagnation + // gate scales its threshold against |initial_wns_|. + initial_wns_ = sta_->worstSlack(max_); max_end_repairs_ = violator_collector_->getMaxEndpointCount() * repair_tns_end_percent; max_end_repairs_ = std::max(max_end_repairs_, 1); @@ -982,9 +985,42 @@ void RepairSetup::printProgressFooter() const "---------------------------------------------------------------"); } +void RepairSetup::resetStagnationTracking() +{ + // Initialize best_wns_ to the most-negative finite float so the very first + // worstSlack() sample always wins the max(). + best_wns_ = std::numeric_limits::lowest(); + wns_stagnation_tripped_ = false; + wns_stagnation_iteration_ = 0; +} + +std::string RepairSetup::wnsStagnationReport(const int iteration) const +{ + if (!wns_stagnation_tripped_) { + return {}; + } + const int digits = 3; + return fmt::format( + "repair_timing: obviously futile - WNS only reached {} after {} passes " + "(initial {}, improvement {}). This is probably an exploration run, " + "not a timing closure run. Aborting with best-effort result.", + delayAsString(best_wns_, digits, sta_), + wns_stagnation_iteration_, + delayAsString(initial_wns_, digits, sta_), + delayAsString(best_wns_ - initial_wns_, digits, sta_)); +} + // Terminate progress if incremental fix rate within an opto interval falls // below the threshold. Bump up the threshold after each large opto // interval. +// A second, independent "WNS-stagnation" gate also returns true when, after +// the warmup, the best-so-far WNS has failed to improve by a deterministic +// threshold relative to the initial WNS. That detects designs where WNS +// essentially never moves from its starting value — the real signature of +// an "obviously futile" run. Plateaus on legitimate designs (WNS drops by +// orders of magnitude early, then flat-lines while TNS keeps improving) do +// not trip this gate, because best_wns_ has already moved far from +// initial_wns_ by the time the plateau starts. bool RepairSetup::terminateProgress(const int iteration, const float initial_tns, float& prev_tns, @@ -995,6 +1031,36 @@ bool RepairSetup::terminateProgress(const int iteration, const char* phase_name, const char phase_marker) { + // WNS-stagnation gate. Sampled every call so the "best so far" tracker + // advances at the same rate as the caller's inner loop (deterministic). + best_wns_ = std::max(best_wns_, sta_->worstSlack(max_)); + if (iteration > wns_stagnation_warmup_iterations_) { + const float threshold + = std::max(wns_stagnation_abs_tol_, + wns_stagnation_rel_tol_ * std::fabs(initial_wns_)); + if (best_wns_ - initial_wns_ < threshold) { + debugPrint(logger_, + RSZ, + "repair_setup", + 1, + "{}{} Phase: Exiting at iteration {} because WNS best-so-far " + "{} only improved {} from initial {} (threshold {}) " + "[endpoint {}/{}]", + phase_name, + phase_marker, + iteration, + delayAsString(best_wns_, 3, sta_), + delayAsString(best_wns_ - initial_wns_, 3, sta_), + delayAsString(initial_wns_, 3, sta_), + delayAsString(threshold, 3, sta_), + endpt_index, + num_endpts); + wns_stagnation_tripped_ = true; + wns_stagnation_iteration_ = iteration; + return true; + } + } + if (iteration % opto_large_interval_ == 0) { fix_rate_threshold *= 2.0; } @@ -1070,6 +1136,7 @@ void RepairSetup::repairSetup_Legacy(const float setup_slack_margin, bool two_cons_terminations = false; printProgress(opto_iteration, false, false, phase_marker); float fix_rate_threshold = inc_fix_rate_threshold_; + resetStagnationTracking(); const auto& violating_ends = violator_collector_->getViolatingEndpoints(); min_viol_ = -violating_ends.back().second; @@ -1340,6 +1407,10 @@ void RepairSetup::repairSetup_Legacy(const float setup_slack_margin, "LEGACY{} Phase: Exiting due to no TNS progress " "for two opto cycles", phase_marker); + const std::string wns_msg = wnsStagnationReport(opto_iteration); + if (!wns_msg.empty()) { + logger_->info(RSZ, 234, "{}", wns_msg); + } break; } } // for each violating endpoint @@ -1453,6 +1524,7 @@ void RepairSetup::repairSetup_Wns(const float setup_slack_margin, overall_no_progress_count_ = 0; float fix_rate_threshold = inc_fix_rate_threshold_; + resetStagnationTracking(); constexpr int max_no_progress = 4; const int phase1_start_iteration = opto_iteration; bool journal_open = false; @@ -1654,6 +1726,10 @@ void RepairSetup::repairSetup_Wns(const float setup_slack_margin, "WNS{} Phase: No TNS progress for {} cycles, exiting", phase_marker, overall_no_progress_count_); + const std::string wns_msg = wnsStagnationReport(opto_iteration); + if (!wns_msg.empty()) { + logger_->info(RSZ, 235, "{}", wns_msg); + } break; } } @@ -2904,6 +2980,7 @@ void RepairSetup::repairSetup_LastGasp(const OptoParams& params, bool prev_termination = false; bool two_cons_terminations = false; float fix_rate_threshold = inc_fix_rate_threshold_; + resetStagnationTracking(); while (violator_collector_->hasMoreEndpoints()) { if (max_iterations > 0 && opto_iteration >= max_iterations) { @@ -3075,6 +3152,10 @@ void RepairSetup::repairSetup_LastGasp(const OptoParams& params, "LAST_GASP{} Phase: No TNS progress for two opto cycles, " "exiting", phase_marker); + const std::string wns_msg = wnsStagnationReport(opto_iteration); + if (!wns_msg.empty()) { + logger_->info(RSZ, 236, "{}", wns_msg); + } break; } } // for each violating endpoint diff --git a/src/rsz/src/RepairSetup.hh b/src/rsz/src/RepairSetup.hh index 6aa4e792af9..02acba2fcb7 100644 --- a/src/rsz/src/RepairSetup.hh +++ b/src/rsz/src/RepairSetup.hh @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -141,6 +142,11 @@ class RepairSetup : public sta::dbStaState int num_endpts, const char* phase_name, char phase_marker); + // Reset the per-phase WNS-stagnation ring buffer. + void resetStagnationTracking(); + // Return the info-log message for a phase-level abort caused by the + // WNS-stagnation gate (empty if that gate did not fire). + std::string wnsStagnationReport(int iteration) const; bool swapVTCritCells(const OptoParams& params, int& num_viols); void traverseFaninCone(sta::Vertex* endpoint, std::unordered_map& crit_insts, @@ -245,6 +251,35 @@ class RepairSetup : public sta::dbStaState static constexpr float inc_fix_rate_threshold_ = 0.0001; // default fix rate threshold = 0.01% static constexpr int max_last_gasp_passes_ = 10; + + // WNS-stagnation gate. Compares the best-so-far WNS against the WNS + // captured at the start of repair_timing (initial_wns_). If after the + // warmup the best WNS has improved by less than + // max(wns_stagnation_abs_tol_, wns_stagnation_rel_tol_ * |initial_wns_|), + // terminateProgress() returns true. "No improvement from initial" is the + // real signature of an obviously-futile design; plateaus on legitimate + // designs (WNS bottoms out at a topology limit while TNS keeps + // improving) are *not* caught, because best_wns_ has already moved far + // from initial_wns_. Deterministic (iteration-count based, no wallclock). + static constexpr float wns_stagnation_abs_tol_ = 1.0e-12f; // 1 ps + // 5% of |initial_wns|. Empirically: aes improves WNS by ~50%, clone_flat + // by ~95%, repair_fanout by ~90% of initial; the hopeless.v synthetic only + // moves WNS by ~2% because buffer insertion on a grossly-over-clocked + // design still chips off a little. 5% sits safely in that gap. + static constexpr float wns_stagnation_rel_tol_ = 0.05f; + // Warmup: skip the gate until this many passes have elapsed, so normal + // repair progress on ordinary designs is not short-circuited. + static constexpr int wns_stagnation_warmup_iterations_ = 1000; + + sta::Slack initial_wns_ = 0; + // Best (largest / least-negative) WNS observed since the most recent + // resetStagnationTracking(). Reset at the start of each repair phase. + sta::Slack best_wns_ = 0; + // Set by terminateProgress() when the WNS-stagnation gate fires. Consumed + // by the phase-level abort sites to emit a loud info log. Reset by + // resetStagnationTracking(). + bool wns_stagnation_tripped_ = false; + int wns_stagnation_iteration_ = 0; }; } // namespace rsz diff --git a/src/rsz/test/BUILD b/src/rsz/test/BUILD index 60f21f3f660..48f8f46e282 100644 --- a/src/rsz/test/BUILD +++ b/src/rsz/test/BUILD @@ -123,6 +123,7 @@ TESTS = [ "repair_setup8", "repair_setup9", "repair_setup9_hier", + "repair_setup_hopeless", "repair_setup_sizedown", "repair_setup_sizeup_match", "repair_setup_undo1", diff --git a/src/rsz/test/CMakeLists.txt b/src/rsz/test/CMakeLists.txt index f203de6c6a0..88f21ba98f1 100644 --- a/src/rsz/test/CMakeLists.txt +++ b/src/rsz/test/CMakeLists.txt @@ -86,6 +86,7 @@ or_integration_tests( repair_hold14 repair_hold15 repair_hold_multi_output_load + repair_setup_hopeless repair_setup_sizedown repair_setup_sizeup_match repair_setup1 diff --git a/src/rsz/test/repair_setup_hopeless.ok b/src/rsz/test/repair_setup_hopeless.ok new file mode 100644 index 00000000000..616bd5e2dfd --- /dev/null +++ b/src/rsz/test/repair_setup_hopeless.ok @@ -0,0 +1,16 @@ +[INFO ODB-0227] LEF file: Nangate45/Nangate45.lef, created 22 layers, 27 vias, 135 library cells +[INFO RSZ-0100] Repair move sequence: SizeUpMove +[INFO RSZ-0094] Found 1200 endpoints with setup violations. +[INFO RSZ-0099] Repairing 1200 out of 1200 (100.00%) violating endpoints... + Iter | Removed | Resized | Inserted | Cloned | Pin | Area | WNS | StTNS | EnTNS | Viol | Worst + | Buffers | Gates | Buffers | Gates | Swaps | | | | | Endpts | St/EnPt +------------------------------------------------------------------------------------------------------------------------------ + 0* | 0 | 0 | 0 | 0 | 0 | +0.0% | -0.142 | -170.6 | -170.6 | 1200 | dst0/D + 1* | 0 | 0 | 0 | 0 | 0 | +0.0% | -0.142 | -170.6 | -170.6 | 1200 | dst0/D +[INFO RSZ-0234] repair_timing: obviously futile - WNS only reached -0.142 after 1002 passes (initial -0.142, improvement 0.000). This is probably an exploration run, not a timing closure run. Aborting with best-effort result. + 1002* | 0 | 0 | 0 | 0 | 0 | +0.0% | -0.142 | -170.6 | -170.6 | 1200 | dst0/D +[INFO RSZ-0236] repair_timing: obviously futile - WNS only reached -0.142 after 1004 passes (initial -0.142, improvement 0.000). This is probably an exploration run, not a timing closure run. Aborting with best-effort result. + final | 0 | 0 | 0 | 0 | 0 | +0.0% | -0.142 | -170.6 | -170.6 | 1200 | dst0/D +------------------------------------------------------------------------------------------------------------------------------ +[WARNING RSZ-0062] Unable to repair all setup violations. +worst slack max -0.14 diff --git a/src/rsz/test/repair_setup_hopeless.tcl b/src/rsz/test/repair_setup_hopeless.tcl new file mode 100644 index 00000000000..ebc01255078 --- /dev/null +++ b/src/rsz/test/repair_setup_hopeless.tcl @@ -0,0 +1,63 @@ +# repair_timing on a hopelessly under-clocked design must terminate +# without grinding through the entire endpoint list one futile pass at a +# time. The block below has many independent register-to-register pairs, +# a clock period below clk-to-Q + setup, and dont_use + skip_* tuned so +# no move can improve WNS. Without the WNS-stagnation gate, repair_timing +# walks the whole 1200-endpoint list; with the gate it exits shortly +# after the warmup. The .ok pins the bounded behaviour. + +source "helpers.tcl" +read_liberty Nangate45/Nangate45_typ.lib +read_lef Nangate45/Nangate45.lef + +set db [ord::get_db] +set tech [$db getTech] +set chip [odb::dbChip_create $db $tech] +set block [odb::dbBlock_create $chip top] +$block setDefUnits 2000 +set m1 [$tech findLayer metal1] +set dff [$db findMaster DFF_X2] +set dff_d [$dff findMTerm D] +set dff_q [$dff findMTerm Q] +set dff_ck [$dff findMTerm CK] + +set clk_net [odb::dbNet_create $block clk] +set clk_bt [odb::dbBTerm_create $clk_net clk] +$clk_bt setIoType INPUT +set bp [odb::dbBPin_create $clk_bt] +odb::dbBox_create $bp $m1 0 0 100 100 +$bp setPlacementStatus LOCKED + +set num_pairs 1200 +for { set i 0 } { $i < $num_pairs } { incr i } { + set y [expr { $i * 2000 }] + set src [odb::dbInst_create $block $dff "src$i"] + $src setLocation 0 $y + $src setPlacementStatus LOCKED + [$src getITerm $dff_ck] connect $clk_net + set dst [odb::dbInst_create $block $dff "dst$i"] + $dst setLocation 2000 $y + $dst setPlacementStatus LOCKED + [$dst getITerm $dff_ck] connect $clk_net + set n [odb::dbNet_create $block "n$i"] + [$src getITerm $dff_q] connect $n + [$dst getITerm $dff_d] connect $n +} +ord::design_created + +create_clock -period 0.001 clk +source Nangate45/Nangate45.rc +set_wire_rc -layer metal3 +estimate_parasitics -placement + +# DFF_X2 is the largest DFF in Nangate45; with everything but _X1/_X2 +# off-limits, SizeUpMove has nothing to size up. +foreach lib_cell [get_lib_cells *] { + if { ![regexp {_X[12]$} [get_name $lib_cell]] } { + set_dont_use $lib_cell + } +} + +repair_timing -setup -skip_buffer_removal -skip_pin_swap -skip_gate_cloning \ + -skip_buffering +report_worst_slack -max diff --git a/test/orfs/hopeless/BUILD b/test/orfs/hopeless/BUILD new file mode 100644 index 00000000000..cce3a87d47c --- /dev/null +++ b/test/orfs/hopeless/BUILD @@ -0,0 +1,52 @@ +load("@bazel-orfs//:sweep.bzl", "orfs_sweep") +load("@rules_shell//shell:sh_test.bzl", "sh_test") +load("//test/orfs:check_same.bzl", "check_same") + +package(features = ["layering_check"]) + +orfs_sweep( + name = "hopeless", + # buildifier: disable=unsorted-dict-items + arguments = { + "CORE_AREA": "2.16 2.16 45.36 45.36", + "DIE_AREA": "0 0 47.52 47.52", + "PLACE_DENSITY": "0.50", + # Clock period far below what the 8-lane × 8-deep add/xor chain can + # ever meet. This is the whole point of the test: the flow must bail + # on an obviously-infeasible target without grinding forever. + "ABC_CLOCK_PERIOD_IN_PS": "50.0", + }, + sources = { + "RULES_JSON": [":rules-base.json"], + "SDC_FILE": [":constraint.sdc"], + }, + stage = "final", + sweep = { + "b": {}, + "base": {}, + }, + tags = ["manual"], + test_kwargs = { + "tags": ["orfs"], + }, + verilog_files = ["hopeless.v"], +) + +check_same( + name = "hopeless_idempotent_test", + flow = "hopeless", + tags = ["orfs"], + variant_a = "base", + variant_b = "b", +) + +# Asserts the WNS-stagnation gate actually fired on this infeasible design. +# Without this, the flow could "pass" simply because some other termination +# mechanism stopped repair_timing, and the real gate would quietly bit-rot. +sh_test( + name = "hopeless_stagnation_test", + srcs = ["check_stagnation.sh"], + args = ["$(locations :hopeless_base_logs)"], + data = [":hopeless_base_logs"], + tags = ["orfs"], +) diff --git a/test/orfs/hopeless/README.md b/test/orfs/hopeless/README.md new file mode 100644 index 00000000000..f3a99e18b9d --- /dev/null +++ b/test/orfs/hopeless/README.md @@ -0,0 +1,19 @@ +# hopeless + +Regression test for the WNS-stagnation gate in `repair_timing` +(`src/rsz/src/RepairSetup.cc`). + +The design is a deliberately deep combinational chain clocked impossibly fast +(see `constraint.sdc` + `ABC_CLOCK_PERIOD_IN_PS` in `BUILD`). With the old +defaults, `repair_timing` would grind forever producing tiny TNS twitches +without moving WNS. With the gate, the phase aborts deterministically after +~1200 iterations of no WNS movement and logs `repair_timing: WNS stuck at ...`. + +The test asserts only that the flow completes within the bazel timeout. +QoR is intentionally unchecked — the point is bounded runtime and determinism. + +Run: + +``` +bazelisk test //test/orfs/hopeless:hopeless_final_base_test +``` diff --git a/test/orfs/hopeless/check_stagnation.sh b/test/orfs/hopeless/check_stagnation.sh new file mode 100755 index 00000000000..96ac46063d9 --- /dev/null +++ b/test/orfs/hopeless/check_stagnation.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash +# Asserts that the repair_timing WNS-stagnation gate fired somewhere in the +# flow logs. The hopeless test design is specifically engineered so that +# repair_timing will iterate past the 1000-pass warmup on an infeasible +# clock target; if the gate stops firing for any reason (design got too +# small, warmup got longer, etc.) this test fails loudly instead of silently +# losing coverage. +set -euo pipefail + +needle="obviously futile" +hits=0 +for log in "$@"; do + if grep -q "$needle" "$log"; then + echo "FOUND in $log:" + grep "$needle" "$log" + hits=$((hits + 1)) + fi +done + +if [[ "$hits" -eq 0 ]]; then + echo "ERROR: repair_timing WNS-stagnation gate did not fire in any log." >&2 + echo "Searched $# log file(s) for '$needle'." >&2 + echo "This means either:" >&2 + echo " (a) the hopeless.v design is no longer pathological enough," >&2 + echo " (b) the gate's warmup/threshold changed, or" >&2 + echo " (c) the log message was renamed." >&2 + exit 1 +fi + +echo "OK: WNS-stagnation gate fired in $hits log file(s)." diff --git a/test/orfs/hopeless/constraint.sdc b/test/orfs/hopeless/constraint.sdc new file mode 100644 index 00000000000..ca8fc65cc44 --- /dev/null +++ b/test/orfs/hopeless/constraint.sdc @@ -0,0 +1,15 @@ +current_design hopeless + +set clk_name core_clock +set clk_port_name clk +set clk_period $::env(ABC_CLOCK_PERIOD_IN_PS) +set clk_io_pct 0.2 + +set clk_port [get_ports $clk_port_name] + +create_clock -name $clk_name -period $clk_period $clk_port + +set non_clock_inputs [lsearch -inline -all -not -exact [all_inputs] $clk_port] + +set_input_delay [expr $clk_period * $clk_io_pct] -clock $clk_name $non_clock_inputs +set_output_delay [expr $clk_period * $clk_io_pct] -clock $clk_name [all_outputs] diff --git a/test/orfs/hopeless/hopeless.v b/test/orfs/hopeless/hopeless.v new file mode 100644 index 00000000000..960940b93ee --- /dev/null +++ b/test/orfs/hopeless/hopeless.v @@ -0,0 +1,46 @@ +// Parallel pipelines with deliberately deep combinational paths, clocked +// impossibly fast by constraint.sdc. Used by the repair_timing WNS-stagnation +// regression: the flow must bail deterministically instead of grinding +// forever on a gap it cannot close. More pipelines => more endpoints => +// repair_timing iterates long enough for the stagnation gate to trip. + +`define LANES 8 +`define DEPTH 8 + +module hopeless ( + input wire clk, + input wire reset, + input wire [31:0] a, + input wire [31:0] b, + output reg [`LANES*32-1:0] out +); + + reg [31:0] a_r, b_r; + always @(posedge clk) begin + a_r <= a; + b_r <= b; + end + + genvar g; + generate + for (g = 0; g < `LANES; g = g + 1) begin : lane + wire [31:0] c0 = a_r ^ (b_r + g); + wire [31:0] c1 = c0 + (32'hdeadbeef ^ g); + wire [31:0] c2 = c1 ^ (32'h55555555 + g); + wire [31:0] c3 = c2 + (32'h12345678 ^ g); + wire [31:0] c4 = c3 ^ (32'haaaaaaaa + g); + wire [31:0] c5 = c4 + (32'h0f0f0f0f ^ g); + wire [31:0] c6 = c5 ^ (32'hf0f0f0f0 + g); + wire [31:0] c7 = c6 + (32'hcafebabe ^ g); + + always @(posedge clk) begin + if (reset) begin + out[32*g +: 32] <= 32'b0; + end else begin + out[32*g +: 32] <= c7; + end + end + end + endgenerate + +endmodule diff --git a/test/orfs/hopeless/rules-base.json b/test/orfs/hopeless/rules-base.json new file mode 100644 index 00000000000..5448aca70c6 --- /dev/null +++ b/test/orfs/hopeless/rules-base.json @@ -0,0 +1,22 @@ +{ + "constraints__clocks__count": { + "value": 1, + "compare": "==" + }, + "detailedplace__design__violations": { + "value": 0, + "compare": "==" + }, + "finish__timing__setup__ws": { + "value": -1000000.0, + "compare": ">=" + }, + "finish__timing__setup__tns": { + "value": -1000000000.0, + "compare": ">=" + }, + "finish__timing__hold__ws": { + "value": -1000000.0, + "compare": ">=" + } +}