OpenSTA/search/WorstSlack.cc

350 lines
9.7 KiB
C++
Raw Normal View History

2018-09-28 17:54:21 +02:00
// OpenSTA, Static Timing Analyzer
2020-03-07 03:50:37 +01:00
// Copyright (c) 2020, 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
// 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/>.
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) :
sta_(sta)
2018-09-28 17:54:21 +02:00
{
2018-12-05 23:18:41 +01:00
worst_slacks_.resize(sta->corners()->pathAnalysisPtCount());
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, sta_,
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, sta_,
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, sta_);
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
}
////////////////////////////////////////////////////////////////
2018-12-05 23:18:41 +01:00
WorstSlack::WorstSlack() :
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_),
min_queue_size_(10),
2018-12-05 23:18:41 +01:00
max_queue_size_(20)
2018-09-28 17:54:21 +02:00
{
}
2018-12-05 23:18:41 +01:00
WorstSlack::WorstSlack(const WorstSlack &) :
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_),
min_queue_size_(10),
max_queue_size_(20)
2018-09-28 17:54:21 +02:00
{
}
void
WorstSlack::deleteVertexBefore(Vertex *vertex)
{
2019-03-13 01:25:53 +01:00
UniqueLock 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,
const StaState *sta,
// Return values.
Slack &worst_slack,
Vertex *&worst_vertex)
2018-09-28 17:54:21 +02:00
{
2018-12-05 23:18:41 +01:00
findWorstSlack(path_ap_index, sta);
worst_slack = worst_slack_;
worst_vertex = worst_vertex_;
2018-09-28 17:54:21 +02:00
}
void
2018-12-05 23:18:41 +01:00
WorstSlack::findWorstSlack(PathAPIndex path_ap_index,
const StaState *sta)
2018-09-28 17:54:21 +02:00
{
2019-03-13 01:25:53 +01:00
if (worst_vertex_ == nullptr) {
2018-09-28 17:54:21 +02:00
if (queue_.empty())
2018-12-05 23:18:41 +01:00
initQueue(path_ap_index, sta);
2018-09-28 17:54:21 +02:00
else
2018-12-05 23:18:41 +01:00
findWorstInQueue(path_ap_index, sta);
2018-09-28 17:54:21 +02:00
}
}
void
2018-12-05 23:18:41 +01:00
WorstSlack::initQueue(PathAPIndex path_ap_index,
const StaState *sta)
2018-09-28 17:54:21 +02:00
{
2018-12-05 23:18:41 +01:00
Search *search = sta->search();
const Debug *debug = sta->debug();
2021-01-23 00:07:04 +01:00
debugPrint0(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_;
VertexSet::Iterator end_iter(search->endpoints());
while (end_iter.hasNext()) {
Vertex *vertex = end_iter.next();
2018-12-05 23:18:41 +01:00
Slack slack = search->wnsSlack(vertex, path_ap_index);
2020-07-12 01:24:48 +02:00
if (!delayEqual(slack, slack_init_)) {
2020-07-12 02:43:30 +02:00
if (delayLess(slack, worst_slack_, sta))
2018-12-05 23:18:41 +01:00
setWorstSlack(vertex, slack, sta);
2020-07-12 02:43:30 +02:00
if (delayLessEqual(slack, slack_threshold_, sta))
2018-09-28 17:54:21 +02:00
queue_.insert(vertex);
int queue_size = queue_.size();
if (queue_size >= max_queue_size_)
2018-12-05 23:18:41 +01:00
sortQueue(path_ap_index, sta);
2018-09-28 17:54:21 +02:00
}
}
2021-01-01 20:46:51 +01:00
debugPrint(debug, "wns", 3, "threshold %s",
delayAsString(slack_threshold_, sta));
2018-09-28 17:54:21 +02:00
// checkQueue();
}
void
2018-12-05 23:18:41 +01:00
WorstSlack::sortQueue(PathAPIndex path_ap_index,
const StaState *sta)
2018-09-28 17:54:21 +02:00
{
2020-07-08 17:01:43 +02:00
if (queue_.size() > 0) {
Search *search = sta->search();
const Debug *debug = sta->debug();
2021-01-23 00:07:04 +01:00
debugPrint0(debug, "wns", 3, "sort queue");
2020-07-08 17:01:43 +02:00
VertexSeq vertices;
vertices.reserve(queue_.size());
VertexSet::Iterator queue_iter(queue_);
while (queue_iter.hasNext()) {
Vertex *vertex = queue_iter.next();
vertices.push_back(vertex);
}
WnsSlackLess slack_less(path_ap_index, sta);
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);
2021-01-01 20:46:51 +01:00
debugPrint(debug, "wns", 3, "threshold %s",
delayAsString(slack_threshold_, sta));
2020-07-08 17:01:43 +02:00
// Reinsert vertices with slack < threshold.
queue_.clear();
VertexSeq::Iterator queue_iter2(vertices);
while (queue_iter2.hasNext()) {
Vertex *vertex = queue_iter2.next();
Slack slack = search->wnsSlack(vertex, path_ap_index);
2020-07-12 02:43:30 +02:00
if (delayGreater(slack, slack_threshold_, sta))
2020-07-08 17:01:43 +02:00
break;
queue_.insert(vertex);
}
max_queue_size_ = queue_.size() * 2;
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, sta);
2018-09-28 17:54:21 +02:00
}
}
void
2018-12-05 23:18:41 +01:00
WorstSlack::findWorstInQueue(PathAPIndex path_ap_index,
const StaState *sta)
2018-09-28 17:54:21 +02:00
{
2018-12-05 23:18:41 +01:00
Search *search = sta->search();
const Debug *debug = sta->debug();
2021-01-23 00:07:04 +01:00
debugPrint0(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_;
VertexSet::Iterator queue_iter(queue_);
while (queue_iter.hasNext()) {
Vertex *vertex = queue_iter.next();
2018-12-05 23:18:41 +01:00
Slack slack = search->wnsSlack(vertex, path_ap_index);
2020-07-12 02:43:30 +02:00
if (delayLess(slack, worst_slack_, sta))
2018-12-05 23:18:41 +01:00
setWorstSlack(vertex, slack, sta);
2018-09-28 17:54:21 +02:00
}
}
void
2018-12-05 23:18:41 +01:00
WorstSlack::checkQueue(PathAPIndex path_ap_index,
const StaState *sta)
2018-09-28 17:54:21 +02:00
{
2018-12-05 23:18:41 +01:00
Search *search = sta->search();
Report *report = sta->report();
const Network *network = sta->network();
2018-09-28 17:54:21 +02:00
VertexSeq ends;
VertexSet::Iterator end_iter(search->endpoints());
while (end_iter.hasNext()) {
Vertex *end = end_iter.next();
2020-07-12 01:24:48 +02:00
if (delayLessEqual(search->wnsSlack(end, path_ap_index),
2020-07-12 02:43:30 +02:00
slack_threshold_, sta))
2018-09-28 17:54:21 +02:00
ends.push_back(end);
}
2018-12-05 23:18:41 +01:00
WnsSlackLess slack_less(path_ap_index, sta);
sort(ends, slack_less);
2018-09-28 17:54:21 +02:00
VertexSet end_set;
VertexSeq::Iterator end_iter2(ends);
while (end_iter2.hasNext()) {
Vertex *end = end_iter2.next();
end_set.insert(end);
if (!queue_.hasKey(end)
2020-07-12 01:24:48 +02:00
&& delayLessEqual(search->wnsSlack(end, path_ap_index),
2020-07-12 02:43:30 +02:00
slack_threshold_, sta))
2020-12-28 18:04:57 +01:00
report->reportLine("WorstSlack queue missing %s %s < %s",
end->name(network),
delayAsString(search->wnsSlack(end, path_ap_index), sta),
delayAsString(slack_threshold_, sta));
2018-09-28 17:54:21 +02:00
}
VertexSet::Iterator queue_iter(queue_);
while (queue_iter.hasNext()) {
Vertex *end = queue_iter.next();
if (!end_set.hasKey(end))
2020-12-28 18:04:57 +01:00
report->reportLine("WorstSlack queue extra %s %s > %s",
end->name(network),
delayAsString(search->wnsSlack(end, path_ap_index), sta),
delayAsString(slack_threshold_, sta));
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,
const StaState *sta)
2018-09-28 17:54:21 +02:00
{
2018-12-05 23:18:41 +01:00
const Debug *debug = sta->debug();
const Network *network = sta->network();
Slack slack = slacks[path_ap_index];
2018-09-28 17:54:21 +02:00
2018-12-05 23:18:41 +01:00
// Locking is required because ArrivalVisitor is called by multiple
2018-09-28 17:54:21 +02:00
// threads.
2019-03-13 01:25:53 +01:00
UniqueLock lock(lock_);
2018-09-28 17:54:21 +02:00
if (worst_vertex_
2020-07-12 02:43:30 +02:00
&& delayLess(slack, worst_slack_, sta))
2018-12-05 23:18:41 +01:00
setWorstSlack(vertex, slack, sta);
2018-09-28 17:54:21 +02:00
else if (vertex == worst_vertex_)
// Mark worst slack as unknown (updated by findWorstSlack().
2019-03-13 01:25:53 +01:00
worst_vertex_ = nullptr;
2018-09-28 17:54:21 +02:00
2020-07-12 01:24:48 +02:00
if (!delayEqual(slack, slack_init_)
2020-07-12 02:43:30 +02:00
&& delayLessEqual(slack, slack_threshold_, sta)) {
2021-01-01 20:46:51 +01:00
debugPrint(debug, "wns", 3, "insert %s %s",
vertex->name(network),
delayAsString(slack, sta));
2018-09-28 17:54:21 +02:00
queue_.insert(vertex);
}
else {
2021-01-01 20:46:51 +01:00
debugPrint(debug, "wns", 3, "delete %s %s",
vertex->name(network),
delayAsString(slack, sta));
queue_.erase(vertex);
2018-09-28 17:54:21 +02:00
}
// checkQueue();
}
void
WorstSlack::setWorstSlack(Vertex *vertex,
2018-12-05 23:18:41 +01:00
Slack slack,
const StaState *sta)
2018-09-28 17:54:21 +02:00
{
2021-01-01 20:46:51 +01:00
debugPrint(sta->debug(), "wns", 3, "%s %s",
vertex->name(sta->network()),
delayAsString(slack, sta));
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