OpenSTA/search/WorstSlack.cc

328 lines
8.9 KiB
C++
Raw Permalink Normal View History

2018-09-28 17:54:21 +02:00
// OpenSTA, Static Timing Analyzer
// Copyright (c) 2024, Parallax Software, Inc.
2018-09-28 17:54:21 +02:00
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2018-09-28 17:54:21 +02:00
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-09-28 17:54:21 +02:00
2020-04-06 04:44:41 +02:00
#include "WorstSlack.hh"
2020-04-05 23:53:44 +02:00
#include "Debug.hh"
#include "Report.hh"
#include "Mutex.hh"
#include "Graph.hh"
#include "Corner.hh"
#include "Search.hh"
#include "PathAnalysisPt.hh"
2020-04-05 20:35:51 +02:00
2018-09-28 17:54:21 +02:00
namespace sta {
using std::min;
2018-12-05 23:18:41 +01:00
WorstSlacks::WorstSlacks(StaState *sta) :
worst_slacks_(sta->corners()->pathAnalysisPtCount(), sta),
2018-12-05 23:18:41 +01:00
sta_(sta)
2018-09-28 17:54:21 +02:00
{
}
void
2018-12-05 23:18:41 +01:00
WorstSlacks::worstSlack(const MinMax *min_max,
// Return values.
Slack &worst_slack,
Vertex *&worst_vertex)
2018-09-28 17:54:21 +02:00
{
2018-12-05 23:18:41 +01:00
worst_slack = MinMax::min()->initValue();
2019-03-13 01:25:53 +01:00
worst_vertex = nullptr;
2019-07-18 15:19:00 +02:00
for (auto corner : *sta_->corners()) {
2018-12-05 23:18:41 +01:00
PathAPIndex path_ap_index = corner->findPathAnalysisPt(min_max)->index();
Slack worst_slack1;
Vertex *worst_vertex1;
worst_slacks_[path_ap_index].worstSlack(path_ap_index,
2018-12-05 23:18:41 +01:00
worst_slack1, worst_vertex1);
2020-07-12 02:43:30 +02:00
if (delayLess(worst_slack1, worst_slack, sta_)) {
2018-12-05 23:18:41 +01:00
worst_slack = worst_slack1;
worst_vertex = worst_vertex1;
}
}
2018-09-28 17:54:21 +02:00
}
2018-12-05 23:18:41 +01:00
void
WorstSlacks::worstSlack(const Corner *corner,
const MinMax *min_max,
// Return values.
Slack &worst_slack,
Vertex *&worst_vertex)
2018-09-28 17:54:21 +02:00
{
2018-12-05 23:18:41 +01:00
PathAPIndex path_ap_index = corner->findPathAnalysisPt(min_max)->index();
worst_slacks_[path_ap_index].worstSlack(path_ap_index,
2018-12-05 23:18:41 +01:00
worst_slack, worst_vertex);
2018-09-28 17:54:21 +02:00
}
void
WorstSlacks::updateWorstSlacks(Vertex *vertex,
2018-12-05 23:18:41 +01:00
SlackSeq &slacks)
2018-09-28 17:54:21 +02:00
{
2018-12-05 23:18:41 +01:00
PathAPIndex path_ap_count = sta_->corners()->pathAnalysisPtCount();
for (PathAPIndex i = 0; i < path_ap_count; i++)
worst_slacks_[i].updateWorstSlack(vertex, slacks, i);
2018-09-28 17:54:21 +02:00
}
void
WorstSlacks::worstSlackNotifyBefore(Vertex *vertex)
{
2018-12-05 23:18:41 +01:00
WorstSlackSeq::Iterator worst_iter(worst_slacks_);
while (worst_iter.hasNext()) {
WorstSlack &worst_slack = worst_iter.next();
worst_slack.deleteVertexBefore(vertex);
}
2018-09-28 17:54:21 +02:00
}
////////////////////////////////////////////////////////////////
WorstSlack::WorstSlack(StaState *sta) :
StaState(sta),
2018-09-28 17:54:21 +02:00
slack_init_(MinMax::min()->initValue()),
2019-03-13 01:25:53 +01:00
worst_vertex_(nullptr),
2018-09-28 17:54:21 +02:00
worst_slack_(slack_init_),
slack_threshold_(slack_init_),
queue_(new VertexSet(graph_)),
2018-09-28 17:54:21 +02:00
min_queue_size_(10),
2018-12-05 23:18:41 +01:00
max_queue_size_(20)
2018-09-28 17:54:21 +02:00
{
}
WorstSlack::~WorstSlack()
{
delete queue_;
}
WorstSlack::WorstSlack(const WorstSlack &worst_slack) :
StaState(worst_slack),
2018-12-05 23:18:41 +01:00
slack_init_(MinMax::min()->initValue()),
2019-03-13 01:25:53 +01:00
worst_vertex_(nullptr),
2018-12-05 23:18:41 +01:00
worst_slack_(slack_init_),
slack_threshold_(slack_init_),
queue_(new VertexSet(graph_)),
2018-12-05 23:18:41 +01:00
min_queue_size_(10),
max_queue_size_(20)
2018-09-28 17:54:21 +02:00
{
}
void
WorstSlack::deleteVertexBefore(Vertex *vertex)
{
LockGuard lock(lock_);
2018-09-28 17:54:21 +02:00
if (vertex == worst_vertex_) {
2019-03-13 01:25:53 +01:00
worst_vertex_ = nullptr;
2018-09-28 17:54:21 +02:00
worst_slack_ = slack_init_;
}
queue_->erase(vertex);
2018-09-28 17:54:21 +02:00
}
2018-12-05 23:18:41 +01:00
void
WorstSlack::worstSlack(PathAPIndex path_ap_index,
// Return values.
Slack &worst_slack,
Vertex *&worst_vertex)
2018-09-28 17:54:21 +02:00
{
findWorstSlack(path_ap_index);
2018-12-05 23:18:41 +01:00
worst_slack = worst_slack_;
worst_vertex = worst_vertex_;
2018-09-28 17:54:21 +02:00
}
void
WorstSlack::findWorstSlack(PathAPIndex path_ap_index)
2018-09-28 17:54:21 +02:00
{
2019-03-13 01:25:53 +01:00
if (worst_vertex_ == nullptr) {
if (queue_->empty())
initQueue(path_ap_index);
2018-09-28 17:54:21 +02:00
else
findWorstInQueue(path_ap_index);
2018-09-28 17:54:21 +02:00
}
}
void
WorstSlack::initQueue(PathAPIndex path_ap_index)
2018-09-28 17:54:21 +02:00
{
debugPrint(debug_, "wns", 3, "init queue");
2018-09-28 17:54:21 +02:00
queue_->clear();
2019-03-13 01:25:53 +01:00
worst_vertex_ = nullptr;
2018-09-28 17:54:21 +02:00
worst_slack_ = slack_init_;
slack_threshold_ = slack_init_;
for(Vertex *vertex : *search_->endpoints()) {
Slack slack = search_->wnsSlack(vertex, path_ap_index);
2020-07-12 01:24:48 +02:00
if (!delayEqual(slack, slack_init_)) {
if (delayLess(slack, worst_slack_, this))
setWorstSlack(vertex, slack);
if (delayLessEqual(slack, slack_threshold_, this))
queue_->insert(vertex);
int queue_size = queue_->size();
2018-09-28 17:54:21 +02:00
if (queue_size >= max_queue_size_)
sortQueue(path_ap_index);
2018-09-28 17:54:21 +02:00
}
}
debugPrint(debug_, "wns", 3, "threshold %s",
delayAsString(slack_threshold_, this));
2018-09-28 17:54:21 +02:00
// checkQueue();
}
void
WorstSlack::sortQueue(PathAPIndex path_ap_index)
2018-09-28 17:54:21 +02:00
{
if (queue_->size() > 0) {
debugPrint(debug_, "wns", 3, "sort queue");
2020-07-08 17:01:43 +02:00
VertexSeq vertices;
vertices.reserve(queue_->size());
for (Vertex *vertex : *queue_)
2020-07-08 17:01:43 +02:00
vertices.push_back(vertex);
WnsSlackLess slack_less(path_ap_index, this);
2020-07-08 17:01:43 +02:00
sort(vertices, slack_less);
int vertex_count = vertices.size();
int threshold_index = min(min_queue_size_, vertex_count - 1);
Vertex *threshold_vertex = vertices[threshold_index];
slack_threshold_ = search_->wnsSlack(threshold_vertex, path_ap_index);
debugPrint(debug_, "wns", 3, "threshold %s",
delayAsString(slack_threshold_, this));
2020-07-08 17:01:43 +02:00
// Reinsert vertices with slack < threshold.
queue_->clear();
2020-07-08 17:01:43 +02:00
VertexSeq::Iterator queue_iter2(vertices);
while (queue_iter2.hasNext()) {
Vertex *vertex = queue_iter2.next();
Slack slack = search_->wnsSlack(vertex, path_ap_index);
if (delayGreater(slack, slack_threshold_, this))
2020-07-08 17:01:43 +02:00
break;
queue_->insert(vertex);
2020-07-08 17:01:43 +02:00
}
max_queue_size_ = queue_->size() * 2;
2020-07-08 17:01:43 +02:00
Vertex *worst_slack_vertex = vertices[0];
Slack worst_slack_slack = search_->wnsSlack(worst_slack_vertex, path_ap_index);
setWorstSlack(worst_slack_vertex, worst_slack_slack);
2018-09-28 17:54:21 +02:00
}
}
void
WorstSlack::findWorstInQueue(PathAPIndex path_ap_index)
2018-09-28 17:54:21 +02:00
{
debugPrint(debug_, "wns", 3, "find worst in queue");
2018-09-28 17:54:21 +02:00
2019-03-13 01:25:53 +01:00
worst_vertex_ = nullptr;
2018-09-28 17:54:21 +02:00
worst_slack_ = slack_init_;
for (Vertex *vertex : *queue_) {
Slack slack = search_->wnsSlack(vertex, path_ap_index);
if (delayLess(slack, worst_slack_, this))
setWorstSlack(vertex, slack);
2018-09-28 17:54:21 +02:00
}
}
void
WorstSlack::checkQueue(PathAPIndex path_ap_index)
2018-09-28 17:54:21 +02:00
{
VertexSeq ends;
for(Vertex *end : *search_->endpoints()) {
if (delayLessEqual(search_->wnsSlack(end, path_ap_index),
slack_threshold_, this))
2018-09-28 17:54:21 +02:00
ends.push_back(end);
}
WnsSlackLess slack_less(path_ap_index, this);
2018-12-05 23:18:41 +01:00
sort(ends, slack_less);
2018-09-28 17:54:21 +02:00
VertexSet end_set(graph_);
for (Vertex *end : ends) {
2018-09-28 17:54:21 +02:00
end_set.insert(end);
if (!queue_->hasKey(end)
&& delayLessEqual(search_->wnsSlack(end, path_ap_index),
slack_threshold_, this))
report_->reportLine("WorstSlack queue missing %s %s < %s",
end->name(network_),
delayAsString(search_->wnsSlack(end, path_ap_index), this),
delayAsString(slack_threshold_, this));
2018-09-28 17:54:21 +02:00
}
for (Vertex *end : *queue_) {
2018-09-28 17:54:21 +02:00
if (!end_set.hasKey(end))
report_->reportLine("WorstSlack queue extra %s %s > %s",
end->name(network_),
delayAsString(search_->wnsSlack(end, path_ap_index), this),
delayAsString(slack_threshold_, this));
2018-09-28 17:54:21 +02:00
}
}
void
WorstSlack::updateWorstSlack(Vertex *vertex,
2018-12-05 23:18:41 +01:00
SlackSeq &slacks,
PathAPIndex path_ap_index)
2018-09-28 17:54:21 +02:00
{
// Do not touch the state unless queue has been initialized
if (!queue_->empty()) {
Slack slack = slacks[path_ap_index];
// Locking is required because ArrivalVisitor is called by multiple
// threads.
LockGuard lock(lock_);
if (worst_vertex_
&& delayLess(slack, worst_slack_, this))
setWorstSlack(vertex, slack);
else if (vertex == worst_vertex_)
// Mark worst slack as unknown (updated by findWorstSlack().
worst_vertex_ = nullptr;
if (!delayEqual(slack, slack_init_)
&& delayLessEqual(slack, slack_threshold_, this)) {
debugPrint(debug_, "wns", 3, "insert %s %s",
vertex->name(network_),
delayAsString(slack, this));
queue_->insert(vertex);
}
else {
debugPrint(debug_, "wns", 3, "delete %s %s",
vertex->name(network_),
delayAsString(slack, this));
queue_->erase(vertex);
}
//checkQueue(path_ap_index);
2018-09-28 17:54:21 +02:00
}
}
void
WorstSlack::setWorstSlack(Vertex *vertex,
Slack slack)
2018-09-28 17:54:21 +02:00
{
debugPrint(debug_, "wns", 3, "%s %s",
vertex->name(network_),
delayAsString(slack, this));
2018-09-28 17:54:21 +02:00
worst_vertex_ = vertex;
worst_slack_ = slack;
}
2018-12-05 23:18:41 +01:00
////////////////////////////////////////////////////////////////
WnsSlackLess::WnsSlackLess(PathAPIndex path_ap_index,
const StaState *sta) :
path_ap_index_(path_ap_index),
search_(sta->search())
{
}
bool
WnsSlackLess::operator()(Vertex *vertex1,
Vertex *vertex2)
{
2020-07-12 01:24:48 +02:00
return delayLess(search_->wnsSlack(vertex1, path_ap_index_),
2020-07-12 02:43:30 +02:00
search_->wnsSlack(vertex2, path_ap_index_),
search_);
2018-12-05 23:18:41 +01:00
}
2018-09-28 17:54:21 +02:00
} // namespace