OpenSTA/search/WorstSlack.cc

321 lines
9.4 KiB
C++
Raw Normal View History

2018-09-28 17:54:21 +02:00
// OpenSTA, Static Timing Analyzer
// Copyright (c) 2026, Parallax Software, Inc.
2026-03-15 22:35:24 +01:00
//
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.
2026-03-15 22:35:24 +01:00
//
2018-09-28 17:54:21 +02:00
// 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.
2026-03-15 22:35:24 +01:00
//
2018-09-28 17:54:21 +02:00
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2026-03-15 22:35:24 +01:00
//
// The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software.
2026-03-15 22:35:24 +01:00
//
// Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
2026-03-15 22:35:24 +01:00
//
// This notice may not be removed or altered from any source distribution.
2018-09-28 17:54:21 +02:00
2020-04-06 04:44:41 +02:00
#include "WorstSlack.hh"
#include <algorithm>
#include "ContainerHelpers.hh"
2020-04-05 23:53:44 +02:00
#include "Debug.hh"
#include "Report.hh"
#include "Mutex.hh"
#include "Graph.hh"
#include "Scene.hh"
2020-04-05 23:53:44 +02:00
#include "Search.hh"
2020-04-05 20:35:51 +02:00
2018-09-28 17:54:21 +02:00
namespace sta {
2018-12-05 23:18:41 +01:00
WorstSlacks::WorstSlacks(StaState *sta) :
2026-03-15 22:35:24 +01:00
worst_slacks_(sta->scenePathCount(),
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;
for (Scene *scene : sta_->scenes()) {
PathAPIndex path_ap_index = scene->pathIndex(min_max);
2018-12-05 23:18:41 +01:00
Slack worst_slack1;
Vertex *worst_vertex1;
2026-03-15 22:35:24 +01:00
worst_slacks_[path_ap_index].worstSlack(path_ap_index, 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 Scene *scene,
const MinMax *min_max,
// Return values.
Slack &worst_slack,
Vertex *&worst_vertex)
2018-09-28 17:54:21 +02:00
{
PathAPIndex path_ap_index = scene->pathIndex(min_max);
2026-03-15 22:35:24 +01:00
worst_slacks_[path_ap_index].worstSlack(path_ap_index, worst_slack, worst_vertex);
2018-09-28 17:54:21 +02:00
}
void
WorstSlacks::updateWorstSlacks(Vertex *vertex,
SlackSeq &slacks)
2018-09-28 17:54:21 +02:00
{
PathAPIndex path_ap_count = sta_->scenePathCount();
2018-12-05 23:18:41 +01:00
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)
{
for (WorstSlack &worst_slack : worst_slacks_)
2018-12-05 23:18:41 +01:00
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(VertexIdLess(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
{
}
2026-03-15 22:35:24 +01: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(VertexIdLess(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_;
2026-03-15 22:35:24 +01:00
for (Vertex *vertex : search_->endpoints()) {
Slack slack = search_->wnsSlack(vertex, path_ap_index);
if (!delayEqual(slack, slack_init_, this)) {
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
}
}
2026-03-15 22:35:24 +01:00
debugPrint(debug_, "wns", 3, "threshold {}",
delayAsString(slack_threshold_, MinMax::max(), this));
//checkQueue();
2018-09-28 17:54:21 +02:00
}
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 = std::min(min_queue_size_, vertex_count - 1);
2020-07-08 17:01:43 +02:00
Vertex *threshold_vertex = vertices[threshold_index];
slack_threshold_ = search_->wnsSlack(threshold_vertex, path_ap_index);
2026-03-15 22:35:24 +01:00
debugPrint(debug_, "wns", 3, "threshold {}",
delayAsString(slack_threshold_, MinMax::max(), this));
2020-07-08 17:01:43 +02:00
// Reinsert vertices with slack < threshold.
queue_->clear();
for (Vertex *vertex : vertices) {
Slack slack = search_->wnsSlack(vertex, path_ap_index);
if (delayGreater(slack, slack_threshold_, this))
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;
2026-03-15 22:35:24 +01:00
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 = makeVertexSet(this);
for (Vertex *end : ends) {
2018-09-28 17:54:21 +02:00
end_set.insert(end);
if (!queue_->contains(end)
2026-03-15 22:35:24 +01:00
&& delayLessEqual(search_->wnsSlack(end, path_ap_index), slack_threshold_,
this))
report_->report("WorstSlack queue missing {} {} < {}", end->to_string(this),
delayAsString(search_->wnsSlack(end, path_ap_index), this),
delayAsString(slack_threshold_, this));
2018-09-28 17:54:21 +02:00
}
for (Vertex *end : *queue_) {
if (!end_set.contains(end))
2026-03-15 22:35:24 +01:00
report_->report("WorstSlack queue extra {} {} > {}", end->to_string(this),
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,
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_);
2026-03-15 22:35:24 +01:00
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_, this)
&& delayLessEqual(slack, slack_threshold_, this)) {
2026-03-15 22:35:24 +01:00
debugPrint(debug_, "wns", 3, "insert {} {}", vertex->to_string(this),
delayAsString(slack, this));
queue_->insert(vertex);
}
else {
2026-03-15 22:35:24 +01:00
debugPrint(debug_, "wns", 3, "delete {} {}", vertex->to_string(this),
delayAsString(slack, this));
queue_->erase(vertex);
}
2026-03-15 22:35:24 +01:00
// 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
{
2026-03-15 22:35:24 +01:00
debugPrint(debug_, "wns", 3, "{} {}", vertex->to_string(this),
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) :
2018-12-05 23:18:41 +01:00
path_ap_index_(path_ap_index),
search_(sta->search())
{
}
bool
WnsSlackLess::operator()(Vertex *vertex1,
Vertex *vertex2)
2018-12-05 23:18:41 +01:00
{
2020-07-12 01:24:48 +02:00
return delayLess(search_->wnsSlack(vertex1, path_ap_index_),
2026-03-15 22:35:24 +01:00
search_->wnsSlack(vertex2, path_ap_index_), search_);
2018-12-05 23:18:41 +01:00
}
2026-03-15 22:35:24 +01:00
} // namespace sta