2018-12-01 19:54:26 +08:00
|
|
|
/*
|
|
|
|
* nextpnr -- Next Generation Place and Route
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018 David Shah <david@symbioticeda.com>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Timing-optimised detailed placement algorithm
|
|
|
|
* Based on "An Effective Timing-Driven Detailed Placement Algorithm for FPGAs"
|
|
|
|
* https://www.cerc.utexas.edu/utda/publications/C205.pdf
|
|
|
|
*/
|
|
|
|
|
2018-12-01 21:43:12 +08:00
|
|
|
#include "timing.h"
|
2018-12-01 19:54:26 +08:00
|
|
|
#include "timing_opt.h"
|
|
|
|
#include "nextpnr.h"
|
2018-12-01 21:43:12 +08:00
|
|
|
#include "util.h"
|
2018-12-01 19:54:26 +08:00
|
|
|
NEXTPNR_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
class TimingOptimiser
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TimingOptimiser(Context *ctx) : ctx(ctx){};
|
|
|
|
bool optimise() {}
|
|
|
|
|
|
|
|
private:
|
2018-12-01 21:22:57 +08:00
|
|
|
// Ratio of available to already-candidates to begin borrowing
|
|
|
|
const float borrow_thresh = 0.2;
|
|
|
|
|
2018-12-01 21:43:12 +08:00
|
|
|
void setup_delay_limits() {
|
|
|
|
for (auto net : sorted(ctx->nets)) {
|
|
|
|
NetInfo *ni = net.second;
|
|
|
|
max_net_delay[ni].clear();
|
|
|
|
max_net_delay[ni].resize(ni->users.size(), std::numeric_limits<delay_t>::max());
|
|
|
|
if (!net_crit.count(net.first))
|
|
|
|
continue;
|
|
|
|
auto &nc = net_crit.at(net.first);
|
|
|
|
if (nc.slack.empty())
|
|
|
|
continue;
|
|
|
|
for (size_t i = 0; i < ni->users.size(); i++) {
|
|
|
|
delay_t net_delay = ctx->getNetinfoRouteDelay(ni, ni->users.at(i));
|
|
|
|
max_net_delay[ni].at(i) = net_delay + ((nc.slack.at(i) - nc.cd_worst_slack) / nc.max_path_length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:22:57 +08:00
|
|
|
bool check_cell_delay_limits(CellInfo *cell) {
|
2018-12-01 21:43:12 +08:00
|
|
|
for (const auto &port : cell->ports) {
|
|
|
|
int nc;
|
|
|
|
if (ctx->getPortTimingClass(cell, port.first, nc) == TMG_IGNORE)
|
|
|
|
continue;
|
|
|
|
NetInfo *net = port.second.net;
|
|
|
|
if (net == nullptr)
|
|
|
|
continue;
|
|
|
|
if (port.second.type == PORT_IN) {
|
|
|
|
if (net->driver.cell == nullptr || net->driver.cell->bel == BelId())
|
|
|
|
continue;
|
|
|
|
BelId srcBel = net->driver.cell->bel;
|
|
|
|
if (ctx->estimateDelay(ctx->getBelPinWire(srcBel, net->driver.port),
|
|
|
|
ctx->getBelPinWire(cell->bel, port.first)) > max_net_delay.at(std::make_pair(cell->name, port.first)))
|
|
|
|
return false;
|
|
|
|
} else if (port.second.type == PORT_OUT) {
|
|
|
|
for (auto user : net->users) {
|
|
|
|
// This could get expensive for high-fanout nets??
|
|
|
|
BelId dstBel = user.cell->bel;
|
|
|
|
if (dstBel == BelId())
|
|
|
|
continue;
|
|
|
|
if (ctx->estimateDelay(ctx->getBelPinWire(cell->bel, port.first),
|
|
|
|
ctx->getBelPinWire(dstBel, user.port)) > max_net_delay.at(std::make_pair(user.cell->name, user.port)))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return true;
|
2018-12-01 21:22:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool acceptable_bel_candidate(CellInfo *cell, BelId newBel) {
|
|
|
|
bool result = true;
|
|
|
|
// At the moment we have to actually do the swap to get an accurate legality result
|
|
|
|
// Switching to macro swaps might help with this
|
|
|
|
BelId oldBel = cell->bel;
|
|
|
|
CellInfo *other_cell = ctx->getBoundBelCell(newBel);
|
|
|
|
if (other_cell != nullptr && other_cell->belStrength > STRENGTH_WEAK) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->bindBel(newBel, cell, STRENGTH_WEAK);
|
|
|
|
if (other_cell != nullptr) {
|
|
|
|
ctx->bindBel(oldBel, other_cell, STRENGTH_WEAK);
|
|
|
|
}
|
|
|
|
if (!ctx->isBelLocationValid(newBel) || ((other_cell != nullptr && !ctx->isBelLocationValid(oldBel)))) {
|
|
|
|
result = false;
|
|
|
|
goto unbind;
|
|
|
|
}
|
|
|
|
|
2018-12-01 22:06:51 +08:00
|
|
|
if (!check_cell_delay_limits(cell) || (other_cell != nullptr && !check_cell_delay_limits(other_cell))) {
|
|
|
|
result = false;
|
|
|
|
goto unbind;
|
|
|
|
}
|
2018-12-01 21:22:57 +08:00
|
|
|
|
|
|
|
unbind:
|
|
|
|
ctx->unbindBel(newBel);
|
|
|
|
if (other_cell != nullptr)
|
|
|
|
ctx->unbindBel(oldBel);
|
|
|
|
// Undo the swap
|
|
|
|
ctx->bindBel(oldBel, cell, STRENGTH_WEAK);
|
|
|
|
if (other_cell != nullptr) {
|
|
|
|
ctx->bindBel(newBel, other_cell, STRENGTH_WEAK);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-12-01 22:06:51 +08:00
|
|
|
int find_neighbours(CellInfo *cell, IdString prev_cell, int d, bool allow_swap) {
|
2018-12-01 21:22:57 +08:00
|
|
|
BelId curr = cell->bel;
|
|
|
|
Loc curr_loc = ctx->getBelLocation(curr);
|
2018-12-01 22:06:51 +08:00
|
|
|
int found_count = 0;
|
2018-12-01 21:22:57 +08:00
|
|
|
for (int dy = -d; dy <= d; dy++) {
|
|
|
|
for (int dx = -d; dx <= d; dx++) {
|
|
|
|
if (dx == 0 && dy == 0)
|
|
|
|
continue;
|
|
|
|
// Go through all the Bels at this location
|
|
|
|
// First, find all bels of the correct type that are either unbound or bound normally
|
|
|
|
// Strongly bound bels are ignored
|
|
|
|
// FIXME: This means that we cannot touch carry chains or similar relatively constrained macros
|
|
|
|
std::vector<BelId> free_bels_at_loc;
|
|
|
|
std::vector<BelId> bound_bels_at_loc;
|
|
|
|
for (auto bel : ctx->getBelsByTile(curr_loc.x + dx, curr_loc.y + dy)) {
|
|
|
|
if (ctx->getBelType(bel) != cell->type)
|
|
|
|
continue;
|
|
|
|
CellInfo *bound = ctx->getBoundBelCell(bel);
|
|
|
|
if (bound == nullptr) {
|
|
|
|
free_bels_at_loc.push_back(bel);
|
|
|
|
} else if (bound->belStrength <= STRENGTH_WEAK) {
|
|
|
|
bound_bels_at_loc.push_back(bel);
|
|
|
|
}
|
|
|
|
}
|
2018-12-01 22:06:51 +08:00
|
|
|
BelId candidate;
|
|
|
|
|
|
|
|
while (!free_bels_at_loc.empty() && !bound_bels_at_loc.empty()) {
|
|
|
|
BelId try_bel;
|
|
|
|
if (!free_bels_at_loc.empty()) {
|
|
|
|
int try_idx = ctx->rng(int(free_bels_at_loc.size()));
|
|
|
|
try_bel = free_bels_at_loc.at(try_idx);
|
|
|
|
free_bels_at_loc.erase(free_bels_at_loc.begin() + try_idx);
|
|
|
|
} else {
|
|
|
|
int try_idx = ctx->rng(int(bound_bels_at_loc.size()));
|
|
|
|
try_bel = bound_bels_at_loc.at(try_idx);
|
|
|
|
bound_bels_at_loc.erase(bound_bels_at_loc.begin() + try_idx);
|
|
|
|
}
|
|
|
|
if (bel_candidate_cells.count(try_bel) && !allow_swap) {
|
|
|
|
// Overlap is only allowed if it is with the previous cell (this is handled by removing those
|
|
|
|
// edges in the graph), or if allow_swap is true to deal with cases where overlap means few neighbours
|
|
|
|
// are identified
|
|
|
|
if (bel_candidate_cells.at(try_bel).size() > 1 || (bel_candidate_cells.at(try_bel).size() == 0 ||
|
|
|
|
*(bel_candidate_cells.at(try_bel).begin()) != prev_cell))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (acceptable_bel_candidate(cell, try_bel)) {
|
|
|
|
candidate = try_bel;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-12-01 21:22:57 +08:00
|
|
|
|
2018-12-01 22:06:51 +08:00
|
|
|
if (candidate != BelId()) {
|
|
|
|
cell_neighbour_bels[cell->name].insert(candidate);
|
|
|
|
bel_candidate_cells[candidate].insert(cell->name);
|
|
|
|
// Work out if we need to delete any overlap
|
|
|
|
std::vector<IdString> overlap;
|
|
|
|
for (auto other : bel_candidate_cells[candidate])
|
|
|
|
if (other != cell->name && other != prev_cell)
|
|
|
|
overlap.push_back(other);
|
|
|
|
if (overlap.size() > 0)
|
|
|
|
NPNR_ASSERT(allow_swap);
|
|
|
|
for (auto ov : overlap) {
|
|
|
|
bel_candidate_cells[candidate].erase(ov);
|
|
|
|
cell_neighbour_bels[ov].erase(candidate);
|
|
|
|
}
|
|
|
|
}
|
2018-12-01 21:22:57 +08:00
|
|
|
}
|
|
|
|
}
|
2018-12-01 22:06:51 +08:00
|
|
|
return found_count;
|
2018-12-01 21:22:57 +08:00
|
|
|
}
|
|
|
|
|
2018-12-01 23:22:32 +08:00
|
|
|
std::vector<std::vector<PortRef*>> find_crit_paths(float crit_thresh, int max_count) {
|
|
|
|
std::vector<std::vector<PortRef*>> crit_paths;
|
|
|
|
std::vector<std::pair<NetInfo *, int>> crit_nets;
|
|
|
|
std::vector<IdString> netnames;
|
|
|
|
std::transform(ctx->nets.begin(), ctx->nets.end(), std::back_inserter(netnames),
|
|
|
|
[](const std::pair<IdString, std::unique_ptr<NetInfo>> &kv){
|
|
|
|
return kv.first;
|
|
|
|
});
|
|
|
|
ctx->sorted_shuffle(netnames);
|
|
|
|
for (auto net : netnames) {
|
|
|
|
if (crit_nets.size() >= max_count)
|
|
|
|
break;
|
|
|
|
if (!net_crit.count(net))
|
|
|
|
continue;
|
|
|
|
auto crit_user = std::max_element(net_crit[net].criticality.begin(),
|
|
|
|
net_crit[net].criticality.end());
|
|
|
|
if (*crit_user > crit_thresh)
|
|
|
|
crit_nets.push_back(std::make_pair(ctx->nets[net].get(), crit_user - net_crit[net].criticality.begin()));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto port_user_index = [](CellInfo *cell, PortInfo &port) -> size_t {
|
|
|
|
NPNR_ASSERT(port.net != nullptr);
|
|
|
|
for (size_t i = 0; i < port.net->users.size(); i++) {
|
|
|
|
auto &usr = port.net->users.at(i);
|
|
|
|
if (usr.cell == cell && usr.port == port.name)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
NPNR_ASSERT_FALSE("port user not found on net");
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto crit_net : crit_nets) {
|
|
|
|
std::deque<PortRef*> crit_path;
|
|
|
|
|
|
|
|
// FIXME: This will fail badly on combinational loops
|
|
|
|
|
|
|
|
// Iterate backwards following greatest criticality
|
|
|
|
NetInfo* back_cursor = crit_net.first;
|
|
|
|
while (back_cursor != nullptr) {
|
|
|
|
float max_crit = 0;
|
|
|
|
std::pair<NetInfo *, size_t> crit_sink{nullptr, 0};
|
|
|
|
CellInfo *cell = back_cursor->driver.cell;
|
|
|
|
if (cell == nullptr)
|
|
|
|
break;
|
|
|
|
for (auto port : cell->ports) {
|
|
|
|
if (port.second.type != PORT_IN)
|
|
|
|
continue;
|
|
|
|
NetInfo *pn = port.second.net;
|
|
|
|
if (pn == nullptr)
|
|
|
|
continue;
|
|
|
|
if (!net_crit.count(pn->name) || net_crit.at(pn->name).criticality.empty())
|
|
|
|
continue;
|
|
|
|
int ccount;
|
|
|
|
DelayInfo combDelay;
|
|
|
|
TimingPortClass tpclass = ctx->getPortTimingClass(cell, port.first, ccount);
|
|
|
|
if (tpclass != TMG_COMB_INPUT && tpclass != TMG_REGISTER_INPUT)
|
|
|
|
continue;
|
|
|
|
bool is_path = ctx->getCellDelay(cell, port.first, back_cursor->driver.port, combDelay);
|
|
|
|
if (!is_path)
|
|
|
|
continue;
|
|
|
|
size_t user_idx = port_user_index(cell, port.second);
|
|
|
|
float usr_crit = net_crit.at(pn->name).criticality.at(user_idx);
|
|
|
|
if (usr_crit >= max_crit) {
|
|
|
|
max_crit = usr_crit;
|
|
|
|
crit_sink = std::make_pair(pn, user_idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (crit_sink.first != nullptr) {
|
|
|
|
crit_path.push_front(&(crit_sink.first->users.at(crit_sink.second)));
|
|
|
|
}
|
|
|
|
back_cursor = crit_sink.first;
|
|
|
|
}
|
|
|
|
// Iterate forwards following greatest criticiality
|
|
|
|
PortRef *fwd_cursor = &(crit_net.first->users.at(crit_net.second));
|
|
|
|
while (fwd_cursor != nullptr) {
|
|
|
|
crit_path.push_back(fwd_cursor);
|
|
|
|
float max_crit = 0;
|
|
|
|
std::pair<NetInfo *, size_t> crit_sink{nullptr, 0};
|
|
|
|
CellInfo *cell = fwd_cursor->cell;
|
|
|
|
for (auto port : cell->ports) {
|
|
|
|
if (port.second.type != PORT_OUT)
|
|
|
|
continue;
|
|
|
|
NetInfo *pn = port.second.net;
|
|
|
|
if (pn == nullptr)
|
|
|
|
continue;
|
|
|
|
if (!net_crit.count(pn->name) || net_crit.at(pn->name).criticality.empty())
|
|
|
|
continue;
|
|
|
|
int ccount;
|
|
|
|
DelayInfo combDelay;
|
|
|
|
TimingPortClass tpclass = ctx->getPortTimingClass(cell, port.first, ccount);
|
|
|
|
if (tpclass != TMG_COMB_OUTPUT && tpclass != TMG_REGISTER_OUTPUT)
|
|
|
|
continue;
|
|
|
|
auto &crits = net_crit.at(pn->name).criticality;
|
|
|
|
auto most_crit_usr = std::max_element(crits.begin(), crits.end());
|
|
|
|
if (*most_crit_usr >= max_crit) {
|
|
|
|
max_crit = *most_crit_usr;
|
|
|
|
crit_sink = std::make_pair(pn, std::distance(crits.begin(), most_crit_usr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (crit_sink.first != nullptr) {
|
|
|
|
fwd_cursor = &(crit_sink.first->users.at(crit_sink.second));
|
|
|
|
} else {
|
|
|
|
fwd_cursor = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<PortRef*> crit_path_vec;
|
|
|
|
std::copy(crit_path.begin(), crit_path.end(), std::back_inserter(crit_path_vec));
|
|
|
|
crit_paths.push_back(crit_path_vec);
|
|
|
|
}
|
|
|
|
|
|
|
|
return crit_paths;
|
|
|
|
}
|
|
|
|
|
2018-12-01 21:22:57 +08:00
|
|
|
// Current candidate Bels for cells (linked in both direction>
|
|
|
|
std::vector<IdString> path_cells;
|
|
|
|
std::unordered_map<IdString, std::unordered_set<BelId>> cell_neighbour_bels;
|
|
|
|
std::unordered_map<BelId, std::unordered_set<IdString>> bel_candidate_cells;
|
2018-12-01 21:43:12 +08:00
|
|
|
// Map cell ports to net delay limit
|
|
|
|
std::unordered_map<std::pair<IdString, IdString>, delay_t> max_net_delay;
|
|
|
|
// Criticality data from timing analysis
|
|
|
|
NetCriticalityMap net_crit;
|
|
|
|
|
2018-12-01 19:54:26 +08:00
|
|
|
Context *ctx;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool timing_opt(Context *ctx, TimingOptCfg cfg) { return TimingOptimiser(ctx).optimise(); }
|
|
|
|
|
|
|
|
NEXTPNR_NAMESPACE_END
|