2018-06-10 00:19:20 +08:00
|
|
|
/*
|
|
|
|
* nextpnr -- Next Generation Place and Route
|
|
|
|
*
|
2018-06-22 22:19:17 +08:00
|
|
|
* Copyright (C) 2018 Clifford Wolf <clifford@symbioticeda.com>
|
2018-06-10 00:19:20 +08:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-06-13 22:52:21 +08:00
|
|
|
#include <cmath>
|
2018-06-10 00:19:20 +08:00
|
|
|
#include <queue>
|
|
|
|
|
|
|
|
#include "log.h"
|
2018-07-12 00:04:09 +08:00
|
|
|
#include "router1.h"
|
2018-07-22 04:59:48 +08:00
|
|
|
#include "timing.h"
|
2018-06-10 00:19:20 +08:00
|
|
|
|
2018-06-21 20:16:07 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
USING_NEXTPNR_NAMESPACE
|
|
|
|
|
|
|
|
struct hash_id_wire
|
2018-06-21 20:08:45 +08:00
|
|
|
{
|
2018-06-23 21:28:09 +08:00
|
|
|
std::size_t operator()(const std::pair<IdString, WireId> &arg) const noexcept
|
2018-06-21 20:08:45 +08:00
|
|
|
{
|
2018-06-21 20:16:07 +08:00
|
|
|
std::size_t seed = std::hash<IdString>()(arg.first);
|
2018-06-23 21:28:09 +08:00
|
|
|
seed ^= std::hash<WireId>()(arg.second) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
|
2018-06-21 20:08:45 +08:00
|
|
|
return seed;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-21 20:16:07 +08:00
|
|
|
struct hash_id_pip
|
2018-06-21 20:08:45 +08:00
|
|
|
{
|
2018-06-21 20:16:07 +08:00
|
|
|
std::size_t operator()(const std::pair<IdString, PipId> &arg) const noexcept
|
2018-06-21 20:08:45 +08:00
|
|
|
{
|
2018-06-21 20:16:07 +08:00
|
|
|
std::size_t seed = std::hash<IdString>()(arg.first);
|
2018-06-23 21:28:09 +08:00
|
|
|
seed ^= std::hash<PipId>()(arg.second) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
|
2018-06-21 20:08:45 +08:00
|
|
|
return seed;
|
|
|
|
}
|
|
|
|
};
|
2018-06-12 20:24:59 +08:00
|
|
|
|
2018-06-10 00:19:20 +08:00
|
|
|
struct QueuedWire
|
|
|
|
{
|
|
|
|
WireId wire;
|
|
|
|
PipId pip;
|
2018-06-13 22:52:21 +08:00
|
|
|
|
2018-06-16 21:23:04 +08:00
|
|
|
delay_t delay = 0, togo = 0;
|
2018-06-19 18:49:40 +08:00
|
|
|
int randtag = 0;
|
2018-06-10 00:19:20 +08:00
|
|
|
|
2018-06-12 20:24:59 +08:00
|
|
|
struct Greater
|
2018-06-10 00:19:20 +08:00
|
|
|
{
|
2018-06-23 21:28:09 +08:00
|
|
|
bool operator()(const QueuedWire &lhs, const QueuedWire &rhs) const noexcept
|
2018-06-12 20:24:59 +08:00
|
|
|
{
|
2018-06-19 18:49:40 +08:00
|
|
|
delay_t l = lhs.delay + lhs.togo, r = rhs.delay + rhs.togo;
|
|
|
|
return l == r ? lhs.randtag > rhs.randtag : l > r;
|
2018-06-12 20:24:59 +08:00
|
|
|
}
|
|
|
|
};
|
2018-06-10 00:19:20 +08:00
|
|
|
};
|
|
|
|
|
2018-06-21 20:08:45 +08:00
|
|
|
struct RipupScoreboard
|
|
|
|
{
|
2018-06-23 21:58:24 +08:00
|
|
|
std::unordered_map<WireId, int> wireScores;
|
|
|
|
std::unordered_map<PipId, int> pipScores;
|
|
|
|
std::unordered_map<std::pair<IdString, WireId>, int, hash_id_wire> netWireScores;
|
|
|
|
std::unordered_map<std::pair<IdString, PipId>, int, hash_id_pip> netPipScores;
|
2018-06-21 20:08:45 +08:00
|
|
|
};
|
|
|
|
|
2018-07-15 01:52:56 +08:00
|
|
|
void ripup_net(Context *ctx, IdString net_name)
|
2018-06-10 00:19:20 +08:00
|
|
|
{
|
2018-07-21 23:02:53 +08:00
|
|
|
if (ctx->debug)
|
|
|
|
log("Ripping up all routing for net %s.\n", net_name.c_str(ctx));
|
|
|
|
|
2018-06-26 03:33:48 +08:00
|
|
|
auto net_info = ctx->nets.at(net_name).get();
|
2018-06-23 21:16:24 +08:00
|
|
|
std::vector<PipId> pips;
|
|
|
|
std::vector<WireId> wires;
|
|
|
|
|
|
|
|
pips.reserve(net_info->wires.size());
|
|
|
|
wires.reserve(net_info->wires.size());
|
2018-06-13 22:52:21 +08:00
|
|
|
|
2018-06-14 21:09:13 +08:00
|
|
|
for (auto &it : net_info->wires) {
|
2018-06-23 21:16:24 +08:00
|
|
|
if (it.second.pip != PipId())
|
|
|
|
pips.push_back(it.second.pip);
|
|
|
|
else
|
|
|
|
wires.push_back(it.first);
|
2018-06-14 21:09:13 +08:00
|
|
|
}
|
2018-06-14 00:28:02 +08:00
|
|
|
|
2018-06-23 21:16:24 +08:00
|
|
|
for (auto pip : pips)
|
2018-07-15 01:53:08 +08:00
|
|
|
ctx->unbindPip(pip);
|
2018-06-23 21:16:24 +08:00
|
|
|
|
|
|
|
for (auto wire : wires)
|
2018-07-15 01:53:08 +08:00
|
|
|
ctx->unbindWire(wire);
|
2018-06-23 21:16:24 +08:00
|
|
|
|
2018-07-04 18:15:23 +08:00
|
|
|
NPNR_ASSERT(net_info->wires.empty());
|
2018-06-14 21:09:13 +08:00
|
|
|
}
|
2018-06-10 00:19:20 +08:00
|
|
|
|
2018-06-14 21:09:13 +08:00
|
|
|
struct Router
|
|
|
|
{
|
2018-06-20 19:32:50 +08:00
|
|
|
Context *ctx;
|
2018-08-02 19:58:23 +08:00
|
|
|
const Router1Cfg &cfg;
|
2018-06-21 20:08:45 +08:00
|
|
|
RipupScoreboard scores;
|
2018-06-20 19:32:50 +08:00
|
|
|
IdString net_name;
|
2018-06-21 20:08:45 +08:00
|
|
|
|
2018-06-20 19:32:50 +08:00
|
|
|
bool ripup;
|
|
|
|
delay_t ripup_penalty;
|
|
|
|
|
2018-06-14 21:09:13 +08:00
|
|
|
std::unordered_set<IdString> rippedNets;
|
2018-06-20 19:32:50 +08:00
|
|
|
std::unordered_map<WireId, QueuedWire> visited;
|
2018-06-22 01:17:04 +08:00
|
|
|
int visitCnt = 0, revisitCnt = 0, overtimeRevisitCnt = 0;
|
2018-06-14 21:09:13 +08:00
|
|
|
bool routedOkay = false;
|
2018-06-16 21:23:04 +08:00
|
|
|
delay_t maxDelay = 0.0;
|
2018-06-18 17:58:37 +08:00
|
|
|
WireId failedDest;
|
2018-06-10 00:19:20 +08:00
|
|
|
|
2018-07-15 01:52:56 +08:00
|
|
|
void route(const std::unordered_map<WireId, delay_t> &src_wires, WireId dst_wire)
|
2018-06-20 19:32:50 +08:00
|
|
|
{
|
2018-06-23 21:28:09 +08:00
|
|
|
std::priority_queue<QueuedWire, std::vector<QueuedWire>, QueuedWire::Greater> queue;
|
2018-06-20 19:32:50 +08:00
|
|
|
|
|
|
|
visited.clear();
|
|
|
|
|
|
|
|
for (auto &it : src_wires) {
|
|
|
|
QueuedWire qw;
|
|
|
|
qw.wire = it.first;
|
|
|
|
qw.pip = PipId();
|
2018-08-01 23:05:30 +08:00
|
|
|
qw.delay = it.second - (it.second / 16);
|
2018-06-20 19:32:50 +08:00
|
|
|
qw.togo = ctx->estimateDelay(qw.wire, dst_wire);
|
|
|
|
qw.randtag = ctx->rng();
|
|
|
|
|
|
|
|
queue.push(qw);
|
|
|
|
visited[qw.wire] = qw;
|
|
|
|
}
|
|
|
|
|
2018-06-22 01:17:04 +08:00
|
|
|
int thisVisitCnt = 0;
|
|
|
|
int thisVisitCntLimit = 0;
|
|
|
|
|
2018-06-23 21:28:09 +08:00
|
|
|
while (!queue.empty() && (thisVisitCntLimit == 0 || thisVisitCnt < thisVisitCntLimit)) {
|
2018-06-20 19:32:50 +08:00
|
|
|
QueuedWire qw = queue.top();
|
|
|
|
queue.pop();
|
|
|
|
|
2018-06-22 01:17:04 +08:00
|
|
|
if (thisVisitCntLimit == 0 && visited.count(dst_wire))
|
2018-06-22 01:36:20 +08:00
|
|
|
thisVisitCntLimit = (thisVisitCnt * 3) / 2;
|
2018-06-22 01:17:04 +08:00
|
|
|
|
2018-06-20 19:32:50 +08:00
|
|
|
for (auto pip : ctx->getPipsDownhill(qw.wire)) {
|
2018-07-21 19:52:59 +08:00
|
|
|
delay_t next_delay = qw.delay + ctx->getPipDelay(pip).maxDelay();
|
2018-06-21 20:08:45 +08:00
|
|
|
WireId next_wire = ctx->getPipDstWire(pip);
|
|
|
|
bool foundRipupNet = false;
|
2018-06-22 01:17:04 +08:00
|
|
|
thisVisitCnt++;
|
2018-06-20 19:32:50 +08:00
|
|
|
|
2018-07-21 19:52:59 +08:00
|
|
|
next_delay += ctx->getWireDelay(next_wire).maxDelay();
|
2018-07-21 19:38:44 +08:00
|
|
|
|
2018-07-15 01:53:08 +08:00
|
|
|
if (!ctx->checkWireAvail(next_wire)) {
|
2018-06-20 19:32:50 +08:00
|
|
|
if (!ripup)
|
|
|
|
continue;
|
2018-07-15 01:53:08 +08:00
|
|
|
IdString ripupWireNet = ctx->getConflictingWireNet(next_wire);
|
2018-06-21 20:08:45 +08:00
|
|
|
if (ripupWireNet == net_name || ripupWireNet == IdString())
|
2018-06-20 19:32:50 +08:00
|
|
|
continue;
|
2018-06-23 21:58:24 +08:00
|
|
|
|
|
|
|
auto it1 = scores.wireScores.find(next_wire);
|
|
|
|
if (it1 != scores.wireScores.end())
|
|
|
|
next_delay += (it1->second * ripup_penalty) / 8;
|
|
|
|
|
|
|
|
auto it2 = scores.netWireScores.find(std::make_pair(ripupWireNet, next_wire));
|
|
|
|
if (it2 != scores.netWireScores.end())
|
|
|
|
next_delay += it2->second * ripup_penalty;
|
|
|
|
|
2018-06-21 20:08:45 +08:00
|
|
|
foundRipupNet = true;
|
2018-06-20 19:32:50 +08:00
|
|
|
}
|
|
|
|
|
2018-07-15 01:53:08 +08:00
|
|
|
if (!ctx->checkPipAvail(pip)) {
|
2018-06-20 19:32:50 +08:00
|
|
|
if (!ripup)
|
|
|
|
continue;
|
2018-07-15 01:53:08 +08:00
|
|
|
IdString ripupPipNet = ctx->getConflictingPipNet(pip);
|
2018-06-21 20:08:45 +08:00
|
|
|
if (ripupPipNet == net_name || ripupPipNet == IdString())
|
2018-06-20 19:32:50 +08:00
|
|
|
continue;
|
2018-06-23 21:58:24 +08:00
|
|
|
|
|
|
|
auto it1 = scores.pipScores.find(pip);
|
|
|
|
if (it1 != scores.pipScores.end())
|
|
|
|
next_delay += (it1->second * ripup_penalty) / 8;
|
|
|
|
|
|
|
|
auto it2 = scores.netPipScores.find(std::make_pair(ripupPipNet, pip));
|
|
|
|
if (it2 != scores.netPipScores.end())
|
|
|
|
next_delay += it2->second * ripup_penalty;
|
|
|
|
|
2018-06-21 20:08:45 +08:00
|
|
|
foundRipupNet = true;
|
2018-06-20 19:32:50 +08:00
|
|
|
}
|
|
|
|
|
2018-06-21 20:08:45 +08:00
|
|
|
if (foundRipupNet)
|
2018-06-20 19:32:50 +08:00
|
|
|
next_delay += ripup_penalty;
|
2018-06-21 20:08:45 +08:00
|
|
|
|
2018-07-04 18:15:23 +08:00
|
|
|
NPNR_ASSERT(next_delay >= 0);
|
2018-06-20 19:32:50 +08:00
|
|
|
|
|
|
|
if (visited.count(next_wire)) {
|
2018-06-23 21:28:09 +08:00
|
|
|
if (visited.at(next_wire).delay <= next_delay + ctx->getDelayEpsilon())
|
2018-06-20 19:32:50 +08:00
|
|
|
continue;
|
|
|
|
#if 0 // FIXME
|
2018-06-21 20:08:45 +08:00
|
|
|
if (ctx->debug)
|
2018-08-01 23:05:30 +08:00
|
|
|
log("Found better route to %s. Old vs new delay estimate: %.3f %.3f\n",
|
2018-06-20 19:32:50 +08:00
|
|
|
ctx->getWireName(next_wire).c_str(),
|
|
|
|
ctx->getDelayNS(visited.at(next_wire).delay),
|
|
|
|
ctx->getDelayNS(next_delay));
|
|
|
|
#endif
|
2018-06-22 01:17:04 +08:00
|
|
|
if (thisVisitCntLimit == 0)
|
|
|
|
revisitCnt++;
|
|
|
|
else
|
|
|
|
overtimeRevisitCnt++;
|
2018-06-20 19:32:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QueuedWire next_qw;
|
|
|
|
next_qw.wire = next_wire;
|
|
|
|
next_qw.pip = pip;
|
|
|
|
next_qw.delay = next_delay;
|
|
|
|
next_qw.togo = ctx->estimateDelay(next_wire, dst_wire);
|
2018-07-29 18:30:11 +08:00
|
|
|
next_qw.randtag = ctx->rng();
|
2018-06-20 19:32:50 +08:00
|
|
|
|
|
|
|
visited[next_qw.wire] = next_qw;
|
|
|
|
queue.push(next_qw);
|
|
|
|
}
|
|
|
|
}
|
2018-06-22 01:17:04 +08:00
|
|
|
|
|
|
|
visitCnt += thisVisitCnt;
|
2018-06-20 19:32:50 +08:00
|
|
|
}
|
|
|
|
|
2018-08-02 19:58:23 +08:00
|
|
|
Router(Context *ctx, const Router1Cfg &cfg, RipupScoreboard &scores, WireId src_wire, WireId dst_wire,
|
|
|
|
bool ripup = false, delay_t ripup_penalty = 0)
|
|
|
|
: ctx(ctx), cfg(cfg), scores(scores), ripup(ripup), ripup_penalty(ripup_penalty)
|
2018-06-20 19:32:50 +08:00
|
|
|
{
|
|
|
|
std::unordered_map<WireId, delay_t> src_wires;
|
2018-07-21 19:52:59 +08:00
|
|
|
src_wires[src_wire] = ctx->getWireDelay(src_wire).maxDelay();
|
2018-07-15 01:52:56 +08:00
|
|
|
route(src_wires, dst_wire);
|
2018-06-20 20:04:10 +08:00
|
|
|
routedOkay = visited.count(dst_wire);
|
2018-06-21 01:22:03 +08:00
|
|
|
|
2018-06-21 20:08:45 +08:00
|
|
|
if (ctx->debug) {
|
2018-06-21 01:22:03 +08:00
|
|
|
log("Route (from destination to source):\n");
|
|
|
|
|
|
|
|
WireId cursor = dst_wire;
|
|
|
|
|
|
|
|
while (1) {
|
2018-06-23 21:28:09 +08:00
|
|
|
log(" %8.3f %s\n", ctx->getDelayNS(visited[cursor].delay), ctx->getWireName(cursor).c_str(ctx));
|
2018-06-21 01:22:03 +08:00
|
|
|
|
|
|
|
if (cursor == src_wire)
|
|
|
|
break;
|
|
|
|
|
|
|
|
cursor = ctx->getPipSrcWire(visited[cursor].pip);
|
|
|
|
}
|
|
|
|
}
|
2018-06-20 19:32:50 +08:00
|
|
|
}
|
|
|
|
|
2018-08-02 19:58:23 +08:00
|
|
|
Router(Context *ctx, const Router1Cfg &cfg, RipupScoreboard &scores, IdString net_name, int user_idx = -1,
|
|
|
|
bool reroute = false, bool ripup = false, delay_t ripup_penalty = 0)
|
|
|
|
: ctx(ctx), cfg(cfg), scores(scores), net_name(net_name), ripup(ripup), ripup_penalty(ripup_penalty)
|
2018-06-14 21:09:13 +08:00
|
|
|
{
|
2018-06-26 03:33:48 +08:00
|
|
|
auto net_info = ctx->nets.at(net_name).get();
|
2018-06-10 00:19:20 +08:00
|
|
|
|
2018-06-21 20:08:45 +08:00
|
|
|
if (ctx->debug)
|
2018-06-18 21:53:18 +08:00
|
|
|
log("Routing net %s.\n", net_name.c_str(ctx));
|
2018-06-10 00:19:20 +08:00
|
|
|
|
2018-06-21 20:08:45 +08:00
|
|
|
if (ctx->debug)
|
2018-06-23 21:28:09 +08:00
|
|
|
log(" Source: %s.%s.\n", net_info->driver.cell->name.c_str(ctx), net_info->driver.port.c_str(ctx));
|
2018-06-10 00:19:20 +08:00
|
|
|
|
2018-07-22 08:16:03 +08:00
|
|
|
auto src_wire = ctx->getNetinfoSourceWire(net_info);
|
2018-06-10 00:19:20 +08:00
|
|
|
|
|
|
|
if (src_wire == WireId())
|
2018-07-23 18:45:31 +08:00
|
|
|
log_error("No wire found for port %s on source cell %s.\n", net_info->driver.port.c_str(ctx),
|
|
|
|
net_info->driver.cell->name.c_str(ctx));
|
2018-06-10 00:19:20 +08:00
|
|
|
|
2018-06-21 20:08:45 +08:00
|
|
|
if (ctx->debug)
|
2018-06-18 21:53:18 +08:00
|
|
|
log(" Source wire: %s\n", ctx->getWireName(src_wire).c_str(ctx));
|
2018-06-10 00:19:20 +08:00
|
|
|
|
2018-06-20 19:32:50 +08:00
|
|
|
std::unordered_map<WireId, delay_t> src_wires;
|
2018-08-01 23:05:30 +08:00
|
|
|
std::vector<std::pair<delay_t, int>> users_array;
|
2018-07-21 23:02:53 +08:00
|
|
|
|
|
|
|
if (user_idx < 0) {
|
2018-08-01 23:05:30 +08:00
|
|
|
// route all users, from worst to best slack
|
|
|
|
for (int user_idx = 0; user_idx < int(net_info->users.size()); user_idx++) {
|
|
|
|
auto dst_wire = ctx->getNetinfoSinkWire(net_info, net_info->users[user_idx]);
|
|
|
|
delay_t slack = net_info->users[user_idx].budget - ctx->estimateDelay(src_wire, dst_wire);
|
|
|
|
users_array.push_back(std::pair<delay_t, int>(slack, user_idx));
|
|
|
|
}
|
|
|
|
std::sort(users_array.begin(), users_array.end());
|
2018-07-21 23:02:53 +08:00
|
|
|
} else {
|
|
|
|
// route only the selected user
|
2018-08-01 23:05:30 +08:00
|
|
|
users_array.push_back(std::pair<delay_t, int>(delay_t(), user_idx));
|
2018-07-21 23:02:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (reroute) {
|
|
|
|
// complete ripup
|
|
|
|
ripup_net(ctx, net_name);
|
|
|
|
ctx->bindWire(src_wire, net_name, STRENGTH_WEAK);
|
|
|
|
src_wires[src_wire] = ctx->getWireDelay(src_wire).maxDelay();
|
|
|
|
} else {
|
|
|
|
// re-use existing routes as much as possible
|
2018-07-22 01:36:48 +08:00
|
|
|
if (net_info->wires.count(src_wire) == 0)
|
|
|
|
ctx->bindWire(src_wire, net_name, STRENGTH_WEAK);
|
2018-07-21 23:02:53 +08:00
|
|
|
src_wires[src_wire] = ctx->getWireDelay(src_wire).maxDelay();
|
2018-06-23 21:58:24 +08:00
|
|
|
|
2018-07-22 08:16:03 +08:00
|
|
|
for (int user_idx = 0; user_idx < int(net_info->users.size()); user_idx++) {
|
2018-08-01 10:31:54 +08:00
|
|
|
auto dst_wire = ctx->getNetinfoSinkWire(net_info, net_info->users[user_idx]);
|
2018-07-21 23:02:53 +08:00
|
|
|
|
|
|
|
if (dst_wire == WireId())
|
2018-07-22 08:16:03 +08:00
|
|
|
log_error("No wire found for port %s on destination cell %s.\n",
|
2018-07-23 18:45:31 +08:00
|
|
|
net_info->users[user_idx].port.c_str(ctx),
|
|
|
|
net_info->users[user_idx].cell->name.c_str(ctx));
|
2018-07-21 23:02:53 +08:00
|
|
|
|
2018-07-22 06:50:49 +08:00
|
|
|
std::function<delay_t(WireId)> register_existing_path =
|
|
|
|
[ctx, net_info, &src_wires, ®ister_existing_path](WireId wire) -> delay_t {
|
2018-07-21 23:02:53 +08:00
|
|
|
auto it = src_wires.find(wire);
|
|
|
|
if (it != src_wires.end())
|
|
|
|
return it->second;
|
|
|
|
|
|
|
|
PipId pip = net_info->wires.at(wire).pip;
|
|
|
|
delay_t delay = register_existing_path(ctx->getPipSrcWire(pip));
|
|
|
|
delay += ctx->getPipDelay(pip).maxDelay();
|
|
|
|
delay += ctx->getWireDelay(wire).maxDelay();
|
|
|
|
src_wires[wire] = delay;
|
|
|
|
|
|
|
|
return delay;
|
|
|
|
};
|
|
|
|
|
|
|
|
WireId cursor = dst_wire;
|
|
|
|
while (src_wires.count(cursor) == 0) {
|
|
|
|
auto it = net_info->wires.find(cursor);
|
|
|
|
if (it == net_info->wires.end())
|
|
|
|
goto check_next_user_for_existing_path;
|
|
|
|
NPNR_ASSERT(it->second.pip != PipId());
|
|
|
|
cursor = ctx->getPipSrcWire(it->second.pip);
|
|
|
|
}
|
|
|
|
|
|
|
|
register_existing_path(dst_wire);
|
2018-07-22 06:50:49 +08:00
|
|
|
check_next_user_for_existing_path:;
|
2018-07-21 23:02:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<WireId> ripup_wires;
|
|
|
|
for (auto &it : net_info->wires)
|
|
|
|
if (src_wires.count(it.first) == 0)
|
|
|
|
ripup_wires.push_back(it.first);
|
|
|
|
|
|
|
|
for (auto &it : ripup_wires) {
|
|
|
|
if (ctx->debug)
|
2018-07-22 06:50:49 +08:00
|
|
|
log(" Unbind dangling wire for net %s: %s\n", net_name.c_str(ctx),
|
|
|
|
ctx->getWireName(it).c_str(ctx));
|
2018-07-21 23:02:53 +08:00
|
|
|
ctx->unbindWire(it);
|
|
|
|
}
|
|
|
|
}
|
2018-06-19 18:49:40 +08:00
|
|
|
|
2018-08-01 23:05:30 +08:00
|
|
|
for (auto user_idx_it : users_array) {
|
|
|
|
int user_idx = user_idx_it.second;
|
|
|
|
|
2018-06-21 20:08:45 +08:00
|
|
|
if (ctx->debug)
|
2018-07-23 18:45:31 +08:00
|
|
|
log(" Route to: %s.%s.\n", net_info->users[user_idx].cell->name.c_str(ctx),
|
|
|
|
net_info->users[user_idx].port.c_str(ctx));
|
2018-06-13 23:52:18 +08:00
|
|
|
|
2018-08-01 10:31:54 +08:00
|
|
|
auto dst_wire = ctx->getNetinfoSinkWire(net_info, net_info->users[user_idx]);
|
2018-06-10 00:19:20 +08:00
|
|
|
|
|
|
|
if (dst_wire == WireId())
|
2018-07-22 08:16:03 +08:00
|
|
|
log_error("No wire found for port %s on destination cell %s.\n",
|
|
|
|
net_info->users[user_idx].port.c_str(ctx), net_info->users[user_idx].cell->name.c_str(ctx));
|
2018-06-10 00:19:20 +08:00
|
|
|
|
2018-06-21 20:08:45 +08:00
|
|
|
if (ctx->debug) {
|
2018-06-23 21:28:09 +08:00
|
|
|
log(" Destination wire: %s\n", ctx->getWireName(dst_wire).c_str(ctx));
|
|
|
|
log(" Path delay estimate: %.2f\n", float(ctx->estimateDelay(src_wire, dst_wire)));
|
2018-06-14 18:43:00 +08:00
|
|
|
}
|
2018-06-10 00:19:20 +08:00
|
|
|
|
2018-07-15 01:52:56 +08:00
|
|
|
route(src_wires, dst_wire);
|
2018-06-10 00:19:20 +08:00
|
|
|
|
2018-06-14 00:28:02 +08:00
|
|
|
if (visited.count(dst_wire) == 0) {
|
2018-06-21 20:08:45 +08:00
|
|
|
if (ctx->debug)
|
2018-06-23 21:28:09 +08:00
|
|
|
log("Failed to route %s -> %s.\n", ctx->getWireName(src_wire).c_str(ctx),
|
2018-06-18 21:53:18 +08:00
|
|
|
ctx->getWireName(dst_wire).c_str(ctx));
|
2018-06-14 21:09:13 +08:00
|
|
|
else if (ripup)
|
2018-06-23 21:28:09 +08:00
|
|
|
log_info("Failed to route %s -> %s.\n", ctx->getWireName(src_wire).c_str(ctx),
|
2018-06-18 21:53:18 +08:00
|
|
|
ctx->getWireName(dst_wire).c_str(ctx));
|
2018-07-15 01:52:56 +08:00
|
|
|
ripup_net(ctx, net_name);
|
2018-06-18 17:58:37 +08:00
|
|
|
failedDest = dst_wire;
|
2018-06-14 21:09:13 +08:00
|
|
|
return;
|
2018-06-14 00:28:02 +08:00
|
|
|
}
|
2018-06-10 00:19:20 +08:00
|
|
|
|
2018-06-21 20:08:45 +08:00
|
|
|
if (ctx->debug)
|
2018-06-23 21:28:09 +08:00
|
|
|
log(" Final path delay: %.3f\n", ctx->getDelayNS(visited[dst_wire].delay));
|
2018-06-13 22:52:21 +08:00
|
|
|
maxDelay = fmaxf(maxDelay, visited[dst_wire].delay);
|
|
|
|
|
2018-06-21 20:08:45 +08:00
|
|
|
if (ctx->debug)
|
2018-06-13 22:52:21 +08:00
|
|
|
log(" Route (from destination to source):\n");
|
2018-06-10 00:19:20 +08:00
|
|
|
|
|
|
|
WireId cursor = dst_wire;
|
|
|
|
|
|
|
|
while (1) {
|
2018-06-21 20:08:45 +08:00
|
|
|
if (ctx->debug)
|
2018-06-23 21:28:09 +08:00
|
|
|
log(" %8.3f %s\n", ctx->getDelayNS(visited[cursor].delay), ctx->getWireName(cursor).c_str(ctx));
|
2018-06-10 00:19:20 +08:00
|
|
|
|
|
|
|
if (src_wires.count(cursor))
|
|
|
|
break;
|
|
|
|
|
2018-07-15 01:53:08 +08:00
|
|
|
IdString conflicting_wire_net = ctx->getConflictingWireNet(cursor);
|
2018-06-14 21:09:13 +08:00
|
|
|
|
2018-06-21 20:08:45 +08:00
|
|
|
if (conflicting_wire_net != IdString()) {
|
2018-07-04 18:15:23 +08:00
|
|
|
NPNR_ASSERT(ripup);
|
|
|
|
NPNR_ASSERT(conflicting_wire_net != net_name);
|
2018-06-23 21:58:24 +08:00
|
|
|
|
2018-07-15 01:53:08 +08:00
|
|
|
ctx->unbindWire(cursor);
|
|
|
|
if (!ctx->checkWireAvail(cursor))
|
2018-07-15 01:52:56 +08:00
|
|
|
ripup_net(ctx, conflicting_wire_net);
|
2018-06-23 21:58:24 +08:00
|
|
|
|
2018-06-21 20:08:45 +08:00
|
|
|
rippedNets.insert(conflicting_wire_net);
|
2018-06-23 21:58:24 +08:00
|
|
|
scores.wireScores[cursor]++;
|
|
|
|
scores.netWireScores[std::make_pair(net_name, cursor)]++;
|
|
|
|
scores.netWireScores[std::make_pair(conflicting_wire_net, cursor)]++;
|
2018-06-14 21:09:13 +08:00
|
|
|
}
|
|
|
|
|
2018-06-23 21:58:24 +08:00
|
|
|
PipId pip = visited[cursor].pip;
|
2018-07-15 01:53:08 +08:00
|
|
|
IdString conflicting_pip_net = ctx->getConflictingPipNet(pip);
|
2018-06-23 21:58:24 +08:00
|
|
|
|
2018-06-21 20:08:45 +08:00
|
|
|
if (conflicting_pip_net != IdString()) {
|
2018-07-04 18:15:23 +08:00
|
|
|
NPNR_ASSERT(ripup);
|
|
|
|
NPNR_ASSERT(conflicting_pip_net != net_name);
|
2018-06-23 21:58:24 +08:00
|
|
|
|
2018-07-15 01:53:08 +08:00
|
|
|
ctx->unbindPip(pip);
|
|
|
|
if (!ctx->checkPipAvail(pip))
|
2018-07-15 01:52:56 +08:00
|
|
|
ripup_net(ctx, conflicting_pip_net);
|
2018-06-23 21:58:24 +08:00
|
|
|
|
|
|
|
rippedNets.insert(conflicting_pip_net);
|
|
|
|
scores.pipScores[visited[cursor].pip]++;
|
|
|
|
scores.netPipScores[std::make_pair(net_name, visited[cursor].pip)]++;
|
|
|
|
scores.netPipScores[std::make_pair(conflicting_pip_net, visited[cursor].pip)]++;
|
2018-06-14 21:09:13 +08:00
|
|
|
}
|
|
|
|
|
2018-07-15 01:53:08 +08:00
|
|
|
ctx->bindPip(visited[cursor].pip, net_name, STRENGTH_WEAK);
|
2018-06-20 19:32:50 +08:00
|
|
|
src_wires[cursor] = visited[cursor].delay;
|
2018-06-18 20:06:37 +08:00
|
|
|
cursor = ctx->getPipSrcWire(visited[cursor].pip);
|
2018-06-10 00:19:20 +08:00
|
|
|
}
|
|
|
|
}
|
2018-06-14 21:09:13 +08:00
|
|
|
|
|
|
|
routedOkay = true;
|
2018-06-10 00:19:20 +08:00
|
|
|
}
|
2018-06-14 21:09:13 +08:00
|
|
|
};
|
|
|
|
|
2018-07-21 23:02:53 +08:00
|
|
|
struct RouteJob
|
|
|
|
{
|
|
|
|
IdString net;
|
|
|
|
int user_idx = -1;
|
|
|
|
delay_t slack = 0;
|
|
|
|
int randtag = 0;
|
2018-06-14 21:09:13 +08:00
|
|
|
|
2018-07-21 23:02:53 +08:00
|
|
|
struct Greater
|
|
|
|
{
|
|
|
|
bool operator()(const RouteJob &lhs, const RouteJob &rhs) const noexcept
|
|
|
|
{
|
|
|
|
return lhs.slack == rhs.slack ? lhs.randtag > rhs.randtag : lhs.slack > rhs.slack;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
2018-06-14 21:09:13 +08:00
|
|
|
|
2018-08-02 20:51:09 +08:00
|
|
|
void addFullNetRouteJob(Context *ctx, const Router1Cfg &cfg, IdString net_name,
|
|
|
|
std::unordered_map<IdString, std::vector<bool>> &cache,
|
2018-07-22 06:50:49 +08:00
|
|
|
std::priority_queue<RouteJob, std::vector<RouteJob>, RouteJob::Greater> &queue)
|
2018-07-21 17:52:41 +08:00
|
|
|
{
|
2018-07-21 23:02:53 +08:00
|
|
|
NetInfo *net_info = ctx->nets.at(net_name).get();
|
2018-06-14 21:09:13 +08:00
|
|
|
|
2018-07-21 23:02:53 +08:00
|
|
|
if (net_info->driver.cell == nullptr)
|
|
|
|
return;
|
2018-06-14 21:09:13 +08:00
|
|
|
|
2018-07-22 08:16:03 +08:00
|
|
|
auto src_wire = ctx->getNetinfoSourceWire(net_info);
|
2018-06-15 01:13:14 +08:00
|
|
|
|
2018-07-21 23:02:53 +08:00
|
|
|
if (src_wire == WireId())
|
2018-07-23 18:45:31 +08:00
|
|
|
log_error("No wire found for port %s on source cell %s.\n", net_info->driver.port.c_str(ctx),
|
|
|
|
net_info->driver.cell->name.c_str(ctx));
|
2018-07-21 23:02:53 +08:00
|
|
|
|
|
|
|
auto &net_cache = cache[net_name];
|
|
|
|
|
|
|
|
if (net_cache.empty())
|
|
|
|
net_cache.resize(net_info->users.size());
|
|
|
|
|
|
|
|
RouteJob job;
|
|
|
|
job.net = net_name;
|
|
|
|
job.user_idx = -1;
|
|
|
|
job.slack = 0;
|
|
|
|
job.randtag = ctx->rng();
|
|
|
|
|
|
|
|
bool got_slack = false;
|
|
|
|
|
2018-07-22 06:50:49 +08:00
|
|
|
for (int user_idx = 0; user_idx < int(net_info->users.size()); user_idx++) {
|
2018-07-21 23:02:53 +08:00
|
|
|
if (net_cache[user_idx])
|
|
|
|
continue;
|
|
|
|
|
2018-08-01 10:31:54 +08:00
|
|
|
auto dst_wire = ctx->getNetinfoSinkWire(net_info, net_info->users[user_idx]);
|
2018-07-21 23:02:53 +08:00
|
|
|
|
|
|
|
if (dst_wire == WireId())
|
2018-07-23 18:45:31 +08:00
|
|
|
log_error("No wire found for port %s on destination cell %s.\n", net_info->users[user_idx].port.c_str(ctx),
|
|
|
|
net_info->users[user_idx].cell->name.c_str(ctx));
|
2018-07-21 23:02:53 +08:00
|
|
|
|
|
|
|
if (user_idx == 0)
|
2018-07-22 08:16:03 +08:00
|
|
|
job.slack = net_info->users[user_idx].budget - ctx->estimateDelay(src_wire, dst_wire);
|
2018-07-21 23:02:53 +08:00
|
|
|
else
|
2018-07-22 08:16:03 +08:00
|
|
|
job.slack = std::min(job.slack, net_info->users[user_idx].budget - ctx->estimateDelay(src_wire, dst_wire));
|
2018-07-21 23:02:53 +08:00
|
|
|
|
|
|
|
WireId cursor = dst_wire;
|
|
|
|
while (src_wire != cursor) {
|
|
|
|
auto it = net_info->wires.find(cursor);
|
|
|
|
if (it == net_info->wires.end()) {
|
|
|
|
if (!got_slack)
|
2018-07-22 08:16:03 +08:00
|
|
|
job.slack = net_info->users[user_idx].budget - ctx->estimateDelay(src_wire, dst_wire);
|
2018-07-21 23:02:53 +08:00
|
|
|
else
|
2018-07-23 18:45:31 +08:00
|
|
|
job.slack = std::min(job.slack,
|
|
|
|
net_info->users[user_idx].budget - ctx->estimateDelay(src_wire, dst_wire));
|
2018-07-21 23:02:53 +08:00
|
|
|
got_slack = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
NPNR_ASSERT(it->second.pip != PipId());
|
|
|
|
cursor = ctx->getPipSrcWire(it->second.pip);
|
2018-07-21 17:52:41 +08:00
|
|
|
}
|
|
|
|
}
|
2018-06-15 01:13:14 +08:00
|
|
|
|
2018-07-21 23:02:53 +08:00
|
|
|
queue.push(job);
|
2018-06-15 01:13:14 +08:00
|
|
|
|
2018-07-21 23:02:53 +08:00
|
|
|
for (int user_idx = 0; user_idx < int(net_info->users.size()); user_idx++)
|
|
|
|
net_cache[user_idx] = true;
|
2018-07-21 17:52:41 +08:00
|
|
|
}
|
|
|
|
|
2018-08-02 20:51:09 +08:00
|
|
|
void addNetRouteJobs(Context *ctx, const Router1Cfg &cfg, IdString net_name,
|
|
|
|
std::unordered_map<IdString, std::vector<bool>> &cache,
|
2018-07-21 23:02:53 +08:00
|
|
|
std::priority_queue<RouteJob, std::vector<RouteJob>, RouteJob::Greater> &queue)
|
2018-06-14 21:09:13 +08:00
|
|
|
{
|
2018-07-21 23:02:53 +08:00
|
|
|
NetInfo *net_info = ctx->nets.at(net_name).get();
|
2018-06-14 21:09:13 +08:00
|
|
|
|
2018-07-21 23:02:53 +08:00
|
|
|
if (net_info->driver.cell == nullptr)
|
|
|
|
return;
|
2018-06-14 21:09:13 +08:00
|
|
|
|
2018-07-22 08:16:03 +08:00
|
|
|
auto src_wire = ctx->getNetinfoSourceWire(net_info);
|
2018-06-15 01:13:14 +08:00
|
|
|
|
2018-07-21 23:02:53 +08:00
|
|
|
if (src_wire == WireId())
|
2018-07-23 18:45:31 +08:00
|
|
|
log_error("No wire found for port %s on source cell %s.\n", net_info->driver.port.c_str(ctx),
|
|
|
|
net_info->driver.cell->name.c_str(ctx));
|
2018-06-15 01:13:14 +08:00
|
|
|
|
2018-07-21 23:02:53 +08:00
|
|
|
auto &net_cache = cache[net_name];
|
2018-06-15 01:13:14 +08:00
|
|
|
|
2018-07-21 23:02:53 +08:00
|
|
|
if (net_cache.empty())
|
|
|
|
net_cache.resize(net_info->users.size());
|
2018-06-15 01:13:14 +08:00
|
|
|
|
2018-07-22 06:50:49 +08:00
|
|
|
for (int user_idx = 0; user_idx < int(net_info->users.size()); user_idx++) {
|
2018-07-21 23:02:53 +08:00
|
|
|
if (net_cache[user_idx])
|
|
|
|
continue;
|
2018-06-15 01:13:14 +08:00
|
|
|
|
2018-08-01 10:31:54 +08:00
|
|
|
auto dst_wire = ctx->getNetinfoSinkWire(net_info, net_info->users[user_idx]);
|
2018-06-15 01:13:14 +08:00
|
|
|
|
2018-07-21 23:02:53 +08:00
|
|
|
if (dst_wire == WireId())
|
2018-07-23 18:45:31 +08:00
|
|
|
log_error("No wire found for port %s on destination cell %s.\n", net_info->users[user_idx].port.c_str(ctx),
|
|
|
|
net_info->users[user_idx].cell->name.c_str(ctx));
|
2018-06-15 01:13:14 +08:00
|
|
|
|
2018-07-21 23:02:53 +08:00
|
|
|
WireId cursor = dst_wire;
|
|
|
|
while (src_wire != cursor) {
|
|
|
|
auto it = net_info->wires.find(cursor);
|
|
|
|
if (it == net_info->wires.end()) {
|
|
|
|
RouteJob job;
|
|
|
|
job.net = net_name;
|
|
|
|
job.user_idx = user_idx;
|
2018-07-22 08:16:03 +08:00
|
|
|
job.slack = net_info->users[user_idx].budget - ctx->estimateDelay(src_wire, dst_wire);
|
2018-07-21 23:02:53 +08:00
|
|
|
job.randtag = ctx->rng();
|
|
|
|
queue.push(job);
|
|
|
|
net_cache[user_idx] = true;
|
|
|
|
break;
|
2018-06-21 23:56:45 +08:00
|
|
|
}
|
2018-07-21 23:02:53 +08:00
|
|
|
NPNR_ASSERT(it->second.pip != PipId());
|
|
|
|
cursor = ctx->getPipSrcWire(it->second.pip);
|
2018-06-21 23:56:45 +08:00
|
|
|
}
|
2018-07-21 23:02:53 +08:00
|
|
|
}
|
|
|
|
}
|
2018-06-15 01:13:14 +08:00
|
|
|
|
2018-08-02 20:51:09 +08:00
|
|
|
void cleanupReroute(Context *ctx, const Router1Cfg &cfg, RipupScoreboard &scores,
|
|
|
|
std::unordered_set<IdString> &cleanupQueue,
|
2018-08-02 19:39:37 +08:00
|
|
|
std::priority_queue<RouteJob, std::vector<RouteJob>, RouteJob::Greater> &jobQueue,
|
|
|
|
int &totalVisitCnt, int &totalRevisitCnt, int &totalOvertimeRevisitCnt)
|
2018-08-01 23:05:30 +08:00
|
|
|
{
|
|
|
|
std::priority_queue<RouteJob, std::vector<RouteJob>, RouteJob::Greater> cleanupJobs;
|
2018-08-02 20:51:09 +08:00
|
|
|
std::vector<NetInfo *> allNetinfos;
|
2018-08-01 23:05:30 +08:00
|
|
|
|
|
|
|
for (auto net_name : cleanupQueue) {
|
|
|
|
NetInfo *net_info = ctx->nets.at(net_name).get();
|
|
|
|
auto src_wire = ctx->getNetinfoSourceWire(net_info);
|
|
|
|
|
2018-08-02 19:39:37 +08:00
|
|
|
if (ctx->verbose)
|
|
|
|
allNetinfos.push_back(net_info);
|
|
|
|
|
2018-08-02 20:47:07 +08:00
|
|
|
std::unordered_map<WireId, int> useCounters;
|
|
|
|
std::vector<int> candidateArcs;
|
|
|
|
|
2018-08-01 23:05:30 +08:00
|
|
|
for (int user_idx = 0; user_idx < int(net_info->users.size()); user_idx++) {
|
|
|
|
auto dst_wire = ctx->getNetinfoSinkWire(net_info, net_info->users[user_idx]);
|
2018-08-02 20:47:07 +08:00
|
|
|
|
|
|
|
if (dst_wire == src_wire)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto cursor = dst_wire;
|
|
|
|
useCounters[cursor]++;
|
|
|
|
|
|
|
|
while (cursor != src_wire) {
|
|
|
|
auto it = net_info->wires.find(cursor);
|
|
|
|
if (it == net_info->wires.end())
|
|
|
|
break;
|
|
|
|
cursor = ctx->getPipSrcWire(it->second.pip);
|
|
|
|
useCounters[cursor]++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cursor != src_wire)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
candidateArcs.push_back(user_idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int user_idx : candidateArcs) {
|
|
|
|
auto dst_wire = ctx->getNetinfoSinkWire(net_info, net_info->users[user_idx]);
|
|
|
|
|
|
|
|
if (useCounters.at(dst_wire) != 1)
|
|
|
|
continue;
|
|
|
|
|
2018-08-01 23:05:30 +08:00
|
|
|
RouteJob job;
|
|
|
|
job.net = net_name;
|
|
|
|
job.user_idx = user_idx;
|
|
|
|
job.slack = net_info->users[user_idx].budget - ctx->estimateDelay(src_wire, dst_wire);
|
|
|
|
job.randtag = ctx->rng();
|
|
|
|
cleanupJobs.push(job);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-02 20:51:09 +08:00
|
|
|
log_info("running cleanup re-route of %d nets (%d arcs).\n", int(cleanupQueue.size()), int(cleanupJobs.size()));
|
2018-08-01 23:05:30 +08:00
|
|
|
|
|
|
|
cleanupQueue.clear();
|
|
|
|
|
|
|
|
int visitCnt = 0, revisitCnt = 0, overtimeRevisitCnt = 0;
|
2018-08-02 19:39:37 +08:00
|
|
|
int totalWireCountDelta = 0;
|
|
|
|
|
|
|
|
if (ctx->verbose) {
|
|
|
|
for (auto it : allNetinfos)
|
|
|
|
totalWireCountDelta -= it->wires.size();
|
|
|
|
}
|
2018-08-01 23:05:30 +08:00
|
|
|
|
|
|
|
while (!cleanupJobs.empty()) {
|
|
|
|
RouteJob job = cleanupJobs.top();
|
|
|
|
cleanupJobs.pop();
|
|
|
|
|
|
|
|
auto net_name = job.net;
|
|
|
|
auto user_idx = job.user_idx;
|
|
|
|
|
|
|
|
NetInfo *net_info = ctx->nets.at(net_name).get();
|
|
|
|
auto dst_wire = ctx->getNetinfoSinkWire(net_info, net_info->users[user_idx]);
|
|
|
|
|
|
|
|
ctx->unbindWire(dst_wire);
|
|
|
|
|
2018-08-02 19:58:23 +08:00
|
|
|
Router router(ctx, cfg, scores, net_name, user_idx, false, false);
|
2018-08-01 23:05:30 +08:00
|
|
|
|
2018-08-02 19:39:37 +08:00
|
|
|
if (!router.routedOkay)
|
|
|
|
log_error("Failed to re-route arc %d of net %s.\n", user_idx, net_name.c_str(ctx));
|
|
|
|
|
2018-08-01 23:05:30 +08:00
|
|
|
visitCnt += router.visitCnt;
|
|
|
|
revisitCnt += router.revisitCnt;
|
|
|
|
overtimeRevisitCnt += router.overtimeRevisitCnt;
|
|
|
|
}
|
|
|
|
|
2018-08-02 19:39:37 +08:00
|
|
|
if (ctx->verbose) {
|
|
|
|
for (auto it : allNetinfos)
|
|
|
|
totalWireCountDelta += it->wires.size();
|
|
|
|
|
2018-08-02 20:51:09 +08:00
|
|
|
log_info(" visited %d PIPs (%.2f%% revisits, %.2f%% overtime), %+d wires.\n", visitCnt,
|
|
|
|
(100.0 * revisitCnt) / visitCnt, (100.0 * overtimeRevisitCnt) / visitCnt, totalWireCountDelta);
|
2018-08-02 19:39:37 +08:00
|
|
|
}
|
2018-08-01 23:05:30 +08:00
|
|
|
|
|
|
|
totalVisitCnt += visitCnt;
|
|
|
|
totalRevisitCnt += revisitCnt;
|
|
|
|
totalOvertimeRevisitCnt += overtimeRevisitCnt;
|
|
|
|
}
|
|
|
|
|
2018-07-21 23:02:53 +08:00
|
|
|
} // namespace
|
2018-06-18 01:43:07 +08:00
|
|
|
|
2018-07-21 23:02:53 +08:00
|
|
|
NEXTPNR_NAMESPACE_BEGIN
|
2018-06-14 21:09:13 +08:00
|
|
|
|
2018-08-02 19:58:23 +08:00
|
|
|
bool router1(Context *ctx, const Router1Cfg &cfg)
|
2018-07-21 23:02:53 +08:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
int totalVisitCnt = 0, totalRevisitCnt = 0, totalOvertimeRevisitCnt = 0;
|
|
|
|
delay_t ripup_penalty = ctx->getRipupDelayPenalty();
|
|
|
|
RipupScoreboard scores;
|
2018-06-14 21:09:13 +08:00
|
|
|
|
2018-07-21 23:02:53 +08:00
|
|
|
log_break();
|
|
|
|
log_info("Routing..\n");
|
2018-07-22 03:00:42 +08:00
|
|
|
ctx->lock();
|
2018-06-21 20:08:45 +08:00
|
|
|
|
2018-08-01 23:05:30 +08:00
|
|
|
std::unordered_set<IdString> cleanupQueue;
|
2018-07-21 23:02:53 +08:00
|
|
|
std::unordered_map<IdString, std::vector<bool>> jobCache;
|
|
|
|
std::priority_queue<RouteJob, std::vector<RouteJob>, RouteJob::Greater> jobQueue;
|
2018-06-18 01:27:48 +08:00
|
|
|
|
2018-07-21 23:02:53 +08:00
|
|
|
for (auto &net_it : ctx->nets)
|
2018-08-02 19:58:23 +08:00
|
|
|
addNetRouteJobs(ctx, cfg, net_it.first, jobCache, jobQueue);
|
2018-07-21 23:02:53 +08:00
|
|
|
|
|
|
|
if (jobQueue.empty()) {
|
|
|
|
log_info("found no unrouted source-sink pairs. no routing necessary.\n");
|
2018-08-01 18:53:52 +08:00
|
|
|
ctx->unlock();
|
2018-07-21 23:02:53 +08:00
|
|
|
return true;
|
2018-06-21 23:56:45 +08:00
|
|
|
}
|
2018-06-18 01:27:48 +08:00
|
|
|
|
2018-07-21 23:02:53 +08:00
|
|
|
log_info("found %d unrouted source-sink pairs. starting routing procedure.\n", int(jobQueue.size()));
|
2018-06-14 21:09:13 +08:00
|
|
|
|
2018-06-21 23:56:45 +08:00
|
|
|
int iterCnt = 0;
|
2018-06-14 21:09:13 +08:00
|
|
|
|
2018-07-21 23:02:53 +08:00
|
|
|
while (!jobQueue.empty()) {
|
2018-08-02 19:58:23 +08:00
|
|
|
if (iterCnt == cfg.maxIterCnt) {
|
2018-06-21 23:56:45 +08:00
|
|
|
log_warning("giving up after %d iterations.\n", iterCnt);
|
|
|
|
log_info("Checksum: 0x%08x\n", ctx->checksum());
|
2018-06-23 21:16:24 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
ctx->check();
|
|
|
|
#endif
|
2018-08-01 18:53:52 +08:00
|
|
|
ctx->unlock();
|
2018-06-21 23:56:45 +08:00
|
|
|
return false;
|
2018-06-14 21:09:13 +08:00
|
|
|
}
|
|
|
|
|
2018-06-21 23:56:45 +08:00
|
|
|
iterCnt++;
|
|
|
|
if (ctx->verbose)
|
|
|
|
log_info("-- %d --\n", iterCnt);
|
2018-06-21 20:08:45 +08:00
|
|
|
|
2018-07-21 23:02:53 +08:00
|
|
|
int visitCnt = 0, revisitCnt = 0, overtimeRevisitCnt = 0, jobCnt = 0, failedCnt = 0;
|
2018-06-21 20:08:45 +08:00
|
|
|
|
2018-07-21 23:02:53 +08:00
|
|
|
std::unordered_set<IdString> normalRouteNets, ripupQueue;
|
2018-06-14 21:09:13 +08:00
|
|
|
|
2018-07-24 10:25:00 +08:00
|
|
|
if (ctx->verbose || iterCnt == 1)
|
2018-07-24 09:58:57 +08:00
|
|
|
log_info("routing queue contains %d jobs.\n", int(jobQueue.size()));
|
2018-08-01 12:56:30 +08:00
|
|
|
else if (ctx->slack_redist_iter > 0 && iterCnt % ctx->slack_redist_iter == 0)
|
2018-08-01 10:07:39 +08:00
|
|
|
assign_budget(ctx, true /* quiet */);
|
2018-06-18 01:27:48 +08:00
|
|
|
|
2018-07-21 23:02:53 +08:00
|
|
|
bool printNets = ctx->verbose && (jobQueue.size() < 10);
|
2018-06-14 21:09:13 +08:00
|
|
|
|
2018-07-21 23:02:53 +08:00
|
|
|
while (!jobQueue.empty()) {
|
2018-07-22 06:50:49 +08:00
|
|
|
if (ctx->debug)
|
2018-07-21 23:02:53 +08:00
|
|
|
log("Next job slack: %f\n", double(jobQueue.top().slack));
|
2018-06-19 18:49:40 +08:00
|
|
|
|
2018-07-21 23:02:53 +08:00
|
|
|
auto net_name = jobQueue.top().net;
|
|
|
|
auto user_idx = jobQueue.top().user_idx;
|
|
|
|
jobQueue.pop();
|
2018-06-18 01:27:48 +08:00
|
|
|
|
2018-08-02 19:58:23 +08:00
|
|
|
if (cfg.fullCleanupReroute)
|
|
|
|
cleanupQueue.insert(net_name);
|
|
|
|
|
2018-07-21 23:02:53 +08:00
|
|
|
if (printNets) {
|
|
|
|
if (user_idx < 0)
|
2018-07-22 06:50:49 +08:00
|
|
|
log_info(" routing all %d users of net %s\n", int(ctx->nets.at(net_name)->users.size()),
|
|
|
|
net_name.c_str(ctx));
|
2018-07-21 23:02:53 +08:00
|
|
|
else
|
|
|
|
log_info(" routing user %d of net %s\n", user_idx, net_name.c_str(ctx));
|
|
|
|
}
|
2018-06-14 21:09:13 +08:00
|
|
|
|
2018-08-02 19:58:23 +08:00
|
|
|
Router router(ctx, cfg, scores, net_name, user_idx, false, false);
|
2018-06-14 21:09:13 +08:00
|
|
|
|
2018-07-21 23:02:53 +08:00
|
|
|
jobCnt++;
|
2018-06-14 21:09:13 +08:00
|
|
|
visitCnt += router.visitCnt;
|
|
|
|
revisitCnt += router.revisitCnt;
|
2018-06-22 01:17:04 +08:00
|
|
|
overtimeRevisitCnt += router.overtimeRevisitCnt;
|
2018-06-14 21:09:13 +08:00
|
|
|
|
2018-06-21 23:56:45 +08:00
|
|
|
if (!router.routedOkay) {
|
|
|
|
if (printNets)
|
2018-06-23 21:28:09 +08:00
|
|
|
log_info(" failed to route to %s.\n", ctx->getWireName(router.failedDest).c_str(ctx));
|
2018-06-21 23:56:45 +08:00
|
|
|
ripupQueue.insert(net_name);
|
2018-07-21 23:02:53 +08:00
|
|
|
failedCnt++;
|
|
|
|
} else {
|
|
|
|
normalRouteNets.insert(net_name);
|
2018-06-18 01:27:48 +08:00
|
|
|
}
|
|
|
|
|
2018-07-22 03:00:42 +08:00
|
|
|
if ((ctx->verbose || iterCnt == 1) && !printNets && (jobCnt % 100 == 0)) {
|
2018-07-21 23:02:53 +08:00
|
|
|
log_info(" processed %d jobs. (%d routed, %d failed)\n", jobCnt, jobCnt - failedCnt, failedCnt);
|
2018-07-21 01:34:59 +08:00
|
|
|
ctx->yield();
|
|
|
|
}
|
2018-06-14 21:09:13 +08:00
|
|
|
}
|
|
|
|
|
2018-07-21 23:02:53 +08:00
|
|
|
NPNR_ASSERT(jobQueue.empty());
|
|
|
|
jobCache.clear();
|
2018-06-21 23:56:45 +08:00
|
|
|
|
2018-07-22 03:00:42 +08:00
|
|
|
if ((ctx->verbose || iterCnt == 1) && (jobCnt % 100 != 0)) {
|
2018-07-21 23:02:53 +08:00
|
|
|
log_info(" processed %d jobs. (%d routed, %d failed)\n", jobCnt, jobCnt - failedCnt, failedCnt);
|
2018-07-22 03:00:42 +08:00
|
|
|
ctx->yield();
|
|
|
|
}
|
2018-06-16 21:23:04 +08:00
|
|
|
|
2018-06-21 20:08:45 +08:00
|
|
|
if (ctx->verbose)
|
2018-08-02 20:51:09 +08:00
|
|
|
log_info(" visited %d PIPs (%.2f%% revisits, %.2f%% overtime revisits).\n", visitCnt,
|
|
|
|
(100.0 * revisitCnt) / visitCnt, (100.0 * overtimeRevisitCnt) / visitCnt);
|
2018-06-14 21:09:13 +08:00
|
|
|
|
2018-06-21 23:56:45 +08:00
|
|
|
if (!ripupQueue.empty()) {
|
|
|
|
if (ctx->verbose || iterCnt == 1)
|
2018-08-02 20:51:09 +08:00
|
|
|
log_info("failed to route %d nets. re-routing in ripup mode.\n", int(ripupQueue.size()));
|
2018-06-21 23:56:45 +08:00
|
|
|
|
|
|
|
printNets = ctx->verbose && (ripupQueue.size() < 10);
|
|
|
|
|
|
|
|
visitCnt = 0;
|
|
|
|
revisitCnt = 0;
|
2018-06-22 01:31:50 +08:00
|
|
|
overtimeRevisitCnt = 0;
|
2018-07-21 23:02:53 +08:00
|
|
|
int netCnt = 0;
|
2018-06-21 23:56:45 +08:00
|
|
|
int ripCnt = 0;
|
|
|
|
|
2018-06-23 21:28:09 +08:00
|
|
|
std::vector<IdString> ripupArray(ripupQueue.begin(), ripupQueue.end());
|
2018-06-21 23:56:45 +08:00
|
|
|
ctx->sorted_shuffle(ripupArray);
|
|
|
|
|
|
|
|
for (auto net_name : ripupArray) {
|
2018-08-02 19:58:23 +08:00
|
|
|
if (cfg.cleanupReroute)
|
|
|
|
cleanupQueue.insert(net_name);
|
2018-08-01 23:05:30 +08:00
|
|
|
|
2018-06-21 23:56:45 +08:00
|
|
|
if (printNets)
|
2018-06-23 21:28:09 +08:00
|
|
|
log_info(" routing net %s. (%d users)\n", net_name.c_str(ctx),
|
2018-06-21 23:56:45 +08:00
|
|
|
int(ctx->nets.at(net_name)->users.size()));
|
|
|
|
|
2018-08-02 19:58:23 +08:00
|
|
|
Router router(ctx, cfg, scores, net_name, -1, false, true, ripup_penalty);
|
2018-06-21 23:56:45 +08:00
|
|
|
|
|
|
|
netCnt++;
|
|
|
|
visitCnt += router.visitCnt;
|
|
|
|
revisitCnt += router.revisitCnt;
|
2018-06-22 01:31:50 +08:00
|
|
|
overtimeRevisitCnt += router.overtimeRevisitCnt;
|
2018-06-21 23:56:45 +08:00
|
|
|
|
|
|
|
if (!router.routedOkay)
|
2018-06-23 21:28:09 +08:00
|
|
|
log_error("Net %s is impossible to route.\n", net_name.c_str(ctx));
|
2018-06-21 23:56:45 +08:00
|
|
|
|
2018-08-01 23:05:30 +08:00
|
|
|
for (auto it : router.rippedNets) {
|
2018-08-02 19:58:23 +08:00
|
|
|
addFullNetRouteJob(ctx, cfg, it, jobCache, jobQueue);
|
|
|
|
if (cfg.cleanupReroute)
|
|
|
|
cleanupQueue.insert(it);
|
2018-08-01 23:05:30 +08:00
|
|
|
}
|
2018-06-21 23:56:45 +08:00
|
|
|
|
|
|
|
if (printNets) {
|
|
|
|
if (router.rippedNets.size() < 10) {
|
2018-06-23 21:28:09 +08:00
|
|
|
log_info(" ripped up %d other nets:\n", int(router.rippedNets.size()));
|
2018-06-21 23:56:45 +08:00
|
|
|
for (auto n : router.rippedNets)
|
2018-06-23 21:28:09 +08:00
|
|
|
log_info(" %s (%d users)\n", n.c_str(ctx), int(ctx->nets.at(n)->users.size()));
|
2018-06-21 23:56:45 +08:00
|
|
|
} else {
|
2018-06-23 21:28:09 +08:00
|
|
|
log_info(" ripped up %d other nets.\n", int(router.rippedNets.size()));
|
2018-06-21 23:56:45 +08:00
|
|
|
}
|
|
|
|
}
|
2018-06-21 20:08:45 +08:00
|
|
|
|
2018-06-21 23:56:45 +08:00
|
|
|
ripCnt += router.rippedNets.size();
|
2018-06-21 20:08:45 +08:00
|
|
|
|
2018-07-21 01:34:59 +08:00
|
|
|
if ((ctx->verbose || iterCnt == 1) && !printNets && (netCnt % 100 == 0)) {
|
2018-06-23 21:28:09 +08:00
|
|
|
log_info(" routed %d nets, ripped %d nets.\n", netCnt, ripCnt);
|
2018-07-21 01:34:59 +08:00
|
|
|
ctx->yield();
|
|
|
|
}
|
2018-06-21 23:56:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((ctx->verbose || iterCnt == 1) && (netCnt % 100 != 0))
|
2018-06-23 21:28:09 +08:00
|
|
|
log_info(" routed %d nets, ripped %d nets.\n", netCnt, ripCnt);
|
2018-06-14 00:28:02 +08:00
|
|
|
|
2018-06-21 23:56:45 +08:00
|
|
|
if (ctx->verbose)
|
2018-08-02 20:51:09 +08:00
|
|
|
log_info(" visited %d PIPs (%.2f%% revisits, %.2f%% overtime revisits).\n", visitCnt,
|
|
|
|
(100.0 * revisitCnt) / visitCnt, (100.0 * overtimeRevisitCnt) / visitCnt);
|
2018-06-21 23:56:45 +08:00
|
|
|
|
2018-07-21 23:02:53 +08:00
|
|
|
if (ctx->verbose && !jobQueue.empty())
|
2018-08-02 20:51:09 +08:00
|
|
|
log_info(" ripped up %d previously routed nets. continue routing.\n", int(jobQueue.size()));
|
2018-06-21 23:56:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!ctx->verbose)
|
2018-07-22 06:50:49 +08:00
|
|
|
log_info("iteration %d: routed %d nets without ripup, routed %d nets with ripup.\n", iterCnt,
|
|
|
|
int(normalRouteNets.size()), int(ripupQueue.size()));
|
2018-06-21 23:56:45 +08:00
|
|
|
|
2018-06-22 02:41:20 +08:00
|
|
|
totalVisitCnt += visitCnt;
|
|
|
|
totalRevisitCnt += revisitCnt;
|
|
|
|
totalOvertimeRevisitCnt += overtimeRevisitCnt;
|
|
|
|
|
2018-06-23 21:28:09 +08:00
|
|
|
if (iterCnt == 8 || iterCnt == 16 || iterCnt == 32 || iterCnt == 64 || iterCnt == 128)
|
2018-06-21 23:56:45 +08:00
|
|
|
ripup_penalty += ctx->getRipupDelayPenalty();
|
2018-07-17 23:27:50 +08:00
|
|
|
|
2018-08-02 20:47:07 +08:00
|
|
|
if (jobQueue.empty() || (iterCnt % 5) == 0 || (cfg.fullCleanupReroute && iterCnt == 1))
|
2018-08-02 20:51:09 +08:00
|
|
|
cleanupReroute(ctx, cfg, scores, cleanupQueue, jobQueue, totalVisitCnt, totalRevisitCnt,
|
|
|
|
totalOvertimeRevisitCnt);
|
2018-08-01 23:05:30 +08:00
|
|
|
|
2018-07-21 01:34:59 +08:00
|
|
|
ctx->yield();
|
2018-06-21 23:56:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
log_info("routing complete after %d iterations.\n", iterCnt);
|
2018-06-22 02:41:20 +08:00
|
|
|
|
2018-08-02 20:51:09 +08:00
|
|
|
log_info("visited %d PIPs (%.2f%% revisits, %.2f%% overtime revisits).\n", totalVisitCnt,
|
|
|
|
(100.0 * totalRevisitCnt) / totalVisitCnt, (100.0 * totalOvertimeRevisitCnt) / totalVisitCnt);
|
2018-06-22 02:41:20 +08:00
|
|
|
|
2018-07-22 08:16:03 +08:00
|
|
|
{
|
|
|
|
float tns = 0;
|
|
|
|
int tns_net_count = 0;
|
|
|
|
int tns_arc_count = 0;
|
|
|
|
for (auto &net_it : ctx->nets) {
|
|
|
|
bool got_negative_slack = false;
|
|
|
|
NetInfo *net_info = ctx->nets.at(net_it.first).get();
|
|
|
|
for (int user_idx = 0; user_idx < int(net_info->users.size()); user_idx++) {
|
2018-08-01 10:31:54 +08:00
|
|
|
delay_t arc_delay = ctx->getNetinfoRouteDelay(net_info, net_info->users[user_idx]);
|
2018-07-22 08:16:03 +08:00
|
|
|
delay_t arc_budget = net_info->users[user_idx].budget;
|
|
|
|
delay_t arc_slack = arc_budget - arc_delay;
|
|
|
|
if (arc_slack < 0) {
|
|
|
|
if (!got_negative_slack) {
|
|
|
|
if (ctx->verbose)
|
|
|
|
log_info("net %s has negative slack arcs:\n", net_info->name.c_str(ctx));
|
|
|
|
tns_net_count++;
|
|
|
|
}
|
|
|
|
if (ctx->verbose)
|
|
|
|
log_info(" arc %s -> %s has %f ns slack (delay %f, budget %f)\n",
|
2018-07-23 18:45:31 +08:00
|
|
|
ctx->getWireName(ctx->getNetinfoSourceWire(net_info)).c_str(ctx),
|
2018-08-01 11:57:36 +08:00
|
|
|
ctx->getWireName(ctx->getNetinfoSinkWire(net_info, net_info->users[user_idx]))
|
|
|
|
.c_str(ctx),
|
2018-07-23 18:45:31 +08:00
|
|
|
ctx->getDelayNS(arc_slack), ctx->getDelayNS(arc_delay),
|
|
|
|
ctx->getDelayNS(arc_budget));
|
2018-07-22 08:16:03 +08:00
|
|
|
tns += ctx->getDelayNS(arc_slack);
|
|
|
|
tns_arc_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-23 18:45:31 +08:00
|
|
|
log_info("final tns with respect to arc budgets: %f ns (%d nets, %d arcs)\n", tns, tns_net_count,
|
|
|
|
tns_arc_count);
|
2018-07-22 08:16:03 +08:00
|
|
|
}
|
|
|
|
|
2018-07-21 23:54:47 +08:00
|
|
|
NPNR_ASSERT(jobQueue.empty());
|
|
|
|
jobCache.clear();
|
|
|
|
|
|
|
|
for (auto &net_it : ctx->nets)
|
2018-08-02 19:58:23 +08:00
|
|
|
addNetRouteJobs(ctx, cfg, net_it.first, jobCache, jobQueue);
|
2018-07-21 23:54:47 +08:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
if (!jobQueue.empty()) {
|
|
|
|
log_info("Design strangely still contains unrouted source-sink pairs:\n");
|
|
|
|
while (!jobQueue.empty()) {
|
|
|
|
log_info(" user %d on net %s.\n", jobQueue.top().user_idx, jobQueue.top().net.c_str(ctx));
|
|
|
|
jobQueue.pop();
|
|
|
|
}
|
|
|
|
log_info("Checksum: 0x%08x\n", ctx->checksum());
|
|
|
|
ctx->check();
|
2018-08-01 18:53:52 +08:00
|
|
|
ctx->unlock();
|
2018-07-21 23:54:47 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-06-21 23:56:45 +08:00
|
|
|
log_info("Checksum: 0x%08x\n", ctx->checksum());
|
2018-06-23 21:16:24 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
ctx->check();
|
|
|
|
#endif
|
2018-07-29 03:50:21 +08:00
|
|
|
timing_analysis(ctx, true /* print_fmax */, true /* print_path */);
|
2018-07-26 22:45:57 +08:00
|
|
|
ctx->unlock();
|
2018-06-21 23:56:45 +08:00
|
|
|
return true;
|
|
|
|
} catch (log_execution_error_exception) {
|
2018-06-23 21:16:24 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
ctx->check();
|
|
|
|
#endif
|
2018-07-26 22:45:57 +08:00
|
|
|
ctx->unlock();
|
2018-06-21 23:56:45 +08:00
|
|
|
return false;
|
|
|
|
}
|
2018-06-10 00:19:20 +08:00
|
|
|
}
|
2018-06-12 20:24:59 +08:00
|
|
|
|
2018-07-12 00:04:09 +08:00
|
|
|
bool Context::getActualRouteDelay(WireId src_wire, WireId dst_wire, delay_t &delay)
|
2018-06-20 20:04:10 +08:00
|
|
|
{
|
2018-06-21 20:08:45 +08:00
|
|
|
RipupScoreboard scores;
|
2018-08-02 19:58:23 +08:00
|
|
|
Router router(this, Router1Cfg(), scores, src_wire, dst_wire);
|
2018-06-20 20:04:10 +08:00
|
|
|
if (router.routedOkay)
|
|
|
|
delay = router.visited.at(dst_wire).delay;
|
|
|
|
return router.routedOkay;
|
|
|
|
}
|
|
|
|
|
2018-06-12 20:24:59 +08:00
|
|
|
NEXTPNR_NAMESPACE_END
|