2018-06-07 21:38:14 +08:00
|
|
|
/*
|
|
|
|
* nextpnr -- Next Generation Place and Route
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018 Clifford Wolf <clifford@clifford.at>
|
|
|
|
*
|
|
|
|
* 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 23:07:42 +08:00
|
|
|
#include "place.h"
|
2018-06-14 21:21:00 +08:00
|
|
|
#include <cmath>
|
2018-06-07 21:38:14 +08:00
|
|
|
#include <iostream>
|
2018-06-13 23:07:42 +08:00
|
|
|
#include <limits>
|
2018-06-08 03:38:24 +08:00
|
|
|
#include <list>
|
|
|
|
#include <map>
|
2018-06-07 21:38:14 +08:00
|
|
|
#include <ostream>
|
2018-06-13 23:07:42 +08:00
|
|
|
#include <queue>
|
2018-06-08 03:38:24 +08:00
|
|
|
#include <set>
|
|
|
|
#include <stdarg.h>
|
2018-06-07 21:38:14 +08:00
|
|
|
#include <stdio.h>
|
2018-06-08 03:38:24 +08:00
|
|
|
#include <stdlib.h>
|
2018-06-07 21:38:14 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <vector>
|
2018-06-15 15:27:02 +08:00
|
|
|
#include <random>
|
|
|
|
#include <algorithm>
|
2018-06-12 19:45:59 +08:00
|
|
|
#include "arch_place.h"
|
2018-06-08 03:38:24 +08:00
|
|
|
#include "log.h"
|
2018-06-07 21:38:14 +08:00
|
|
|
|
2018-06-12 20:24:59 +08:00
|
|
|
NEXTPNR_NAMESPACE_BEGIN
|
|
|
|
|
2018-06-08 03:38:24 +08:00
|
|
|
void place_design(Design *design)
|
|
|
|
{
|
|
|
|
std::set<IdString> types_used;
|
|
|
|
std::set<IdString>::iterator not_found, element;
|
|
|
|
std::set<BelType> used_bels;
|
2018-06-07 21:38:14 +08:00
|
|
|
|
2018-06-13 22:52:21 +08:00
|
|
|
log_info("Placing..\n");
|
|
|
|
|
2018-06-10 01:38:37 +08:00
|
|
|
// Initial constraints placer
|
2018-06-08 03:38:24 +08:00
|
|
|
for (auto cell_entry : design->cells) {
|
|
|
|
CellInfo *cell = cell_entry.second;
|
2018-06-10 01:52:22 +08:00
|
|
|
auto loc = cell->attrs.find("BEL");
|
2018-06-10 01:38:37 +08:00
|
|
|
if (loc != cell->attrs.end()) {
|
|
|
|
std::string loc_name = loc->second;
|
|
|
|
BelId bel = design->chip.getBelByName(IdString(loc_name));
|
|
|
|
if (bel == BelId()) {
|
|
|
|
log_error("No Bel named \'%s\' located for "
|
2018-06-10 01:52:22 +08:00
|
|
|
"this chip (processing BEL attribute on \'%s\')\n",
|
2018-06-10 01:38:37 +08:00
|
|
|
loc_name.c_str(), cell->name.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
BelType bel_type = design->chip.getBelType(bel);
|
|
|
|
if (bel_type != belTypeFromId(cell->type)) {
|
|
|
|
log_error("Bel \'%s\' of type \'%s\' does not match cell "
|
|
|
|
"\'%s\' of type \'%s\'",
|
|
|
|
loc_name.c_str(), belTypeToId(bel_type).c_str(),
|
|
|
|
cell->name.c_str(), cell->type.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
cell->bel = bel;
|
|
|
|
design->chip.bindBel(bel, cell->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto cell_entry : design->cells) {
|
|
|
|
CellInfo *cell = cell_entry.second;
|
|
|
|
// Ignore already placed cells
|
|
|
|
if (cell->bel != BelId())
|
|
|
|
continue;
|
|
|
|
|
2018-06-08 03:38:24 +08:00
|
|
|
BelType bel_type;
|
2018-06-07 21:38:14 +08:00
|
|
|
|
2018-06-08 03:38:24 +08:00
|
|
|
element = types_used.find(cell->type);
|
|
|
|
if (element != types_used.end()) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-06-07 21:38:14 +08:00
|
|
|
|
2018-06-08 03:38:24 +08:00
|
|
|
bel_type = belTypeFromId(cell->type);
|
|
|
|
if (bel_type == BelType()) {
|
|
|
|
log_error("No Bel of type \'%s\' defined for "
|
|
|
|
"this chip\n",
|
|
|
|
cell->type.c_str());
|
|
|
|
}
|
|
|
|
types_used.insert(cell->type);
|
|
|
|
}
|
2018-06-07 21:38:14 +08:00
|
|
|
|
2018-06-08 03:38:24 +08:00
|
|
|
for (auto bel_type_name : types_used) {
|
|
|
|
auto blist = design->chip.getBels();
|
|
|
|
BelType bel_type = belTypeFromId(bel_type_name);
|
|
|
|
auto bi = blist.begin();
|
2018-06-07 21:38:14 +08:00
|
|
|
|
2018-06-08 03:38:24 +08:00
|
|
|
for (auto cell_entry : design->cells) {
|
|
|
|
CellInfo *cell = cell_entry.second;
|
2018-06-07 21:38:14 +08:00
|
|
|
|
2018-06-10 01:38:37 +08:00
|
|
|
// Ignore already placed cells
|
|
|
|
if (cell->bel != BelId())
|
|
|
|
continue;
|
2018-06-08 03:38:24 +08:00
|
|
|
// Only place one type of Bel at a time
|
2018-06-12 21:08:01 +08:00
|
|
|
if (cell->type != bel_type_name)
|
2018-06-08 03:38:24 +08:00
|
|
|
continue;
|
2018-06-07 21:38:14 +08:00
|
|
|
|
2018-06-08 03:38:24 +08:00
|
|
|
while ((bi != blist.end()) &&
|
2018-06-10 01:38:37 +08:00
|
|
|
((design->chip.getBelType(*bi) != bel_type ||
|
2018-06-12 19:45:59 +08:00
|
|
|
!design->chip.checkBelAvail(*bi)) ||
|
|
|
|
!isValidBelForCell(design, cell, *bi)))
|
2018-06-08 03:38:24 +08:00
|
|
|
bi++;
|
|
|
|
if (bi == blist.end())
|
|
|
|
log_error("Too many \'%s\' used in design\n",
|
|
|
|
cell->type.c_str());
|
|
|
|
cell->bel = *bi++;
|
2018-06-10 01:38:37 +08:00
|
|
|
design->chip.bindBel(cell->bel, cell->name);
|
|
|
|
|
|
|
|
// Back annotate location
|
2018-06-12 21:08:01 +08:00
|
|
|
cell->attrs["BEL"] = design->chip.getBelName(cell->bel).str();
|
2018-06-08 03:38:24 +08:00
|
|
|
}
|
|
|
|
}
|
2018-06-07 21:38:14 +08:00
|
|
|
}
|
2018-06-12 20:24:59 +08:00
|
|
|
|
2018-06-15 15:27:02 +08:00
|
|
|
static void place_cell(Design *design, CellInfo *cell, std::mt19937 &rnd)
|
2018-06-13 23:07:42 +08:00
|
|
|
{
|
2018-06-15 15:27:02 +08:00
|
|
|
std::uniform_real_distribution<float> random_wirelength(0.0, 30.0);
|
2018-06-13 23:07:42 +08:00
|
|
|
float best_distance = std::numeric_limits<float>::infinity();
|
|
|
|
BelId best_bel = BelId();
|
|
|
|
Chip &chip = design->chip;
|
2018-06-15 02:25:35 +08:00
|
|
|
if(cell->bel != BelId()) {
|
|
|
|
chip.unbindBel(cell->bel);
|
|
|
|
cell->bel = BelId();
|
|
|
|
}
|
2018-06-13 23:07:42 +08:00
|
|
|
BelType targetType = belTypeFromId(cell->type);
|
|
|
|
for (auto bel : chip.getBels()) {
|
|
|
|
if (chip.getBelType(bel) == targetType && chip.checkBelAvail(bel) &&
|
|
|
|
isValidBelForCell(design, cell, bel)) {
|
2018-06-14 01:10:12 +08:00
|
|
|
float distance = 0;
|
2018-06-14 21:21:00 +08:00
|
|
|
float belx, bely;
|
2018-06-15 15:27:02 +08:00
|
|
|
bool has_conns = false;
|
2018-06-14 21:21:00 +08:00
|
|
|
chip.estimatePosition(bel, belx, bely);
|
2018-06-14 01:10:12 +08:00
|
|
|
for (auto port : cell->ports) {
|
|
|
|
const PortInfo &pi = port.second;
|
2018-06-15 02:25:35 +08:00
|
|
|
if (pi.net != nullptr) {
|
2018-06-14 01:10:12 +08:00
|
|
|
CellInfo *drv = pi.net->driver.cell;
|
2018-06-15 15:27:02 +08:00
|
|
|
float pin_wirelength = std::numeric_limits<float>::infinity();
|
2018-06-14 21:21:00 +08:00
|
|
|
if (drv != nullptr && drv->bel != BelId()) {
|
|
|
|
float otherx, othery;
|
|
|
|
chip.estimatePosition(drv->bel, otherx, othery);
|
2018-06-15 15:27:02 +08:00
|
|
|
float local_wl = std::abs(belx - otherx) +
|
2018-06-15 02:25:35 +08:00
|
|
|
std::abs(bely - othery);
|
2018-06-15 15:27:02 +08:00
|
|
|
if (local_wl < pin_wirelength)
|
|
|
|
pin_wirelength = local_wl;
|
|
|
|
has_conns = true;
|
2018-06-15 02:25:35 +08:00
|
|
|
}
|
|
|
|
if (pi.net->users.size() < 5) {
|
|
|
|
for (auto user : pi.net->users) {
|
|
|
|
CellInfo *uc = user.cell;
|
|
|
|
if (uc != nullptr && uc->bel != BelId()) {
|
|
|
|
float otherx, othery;
|
|
|
|
chip.estimatePosition(uc->bel, otherx, othery);
|
2018-06-15 15:27:02 +08:00
|
|
|
float local_wl = std::abs(belx - otherx) +
|
2018-06-15 02:25:35 +08:00
|
|
|
std::abs(bely - othery);
|
2018-06-15 15:27:02 +08:00
|
|
|
if (local_wl < pin_wirelength)
|
|
|
|
pin_wirelength = local_wl;
|
|
|
|
has_conns = true;
|
2018-06-15 02:25:35 +08:00
|
|
|
}
|
|
|
|
}
|
2018-06-14 21:21:00 +08:00
|
|
|
}
|
2018-06-15 15:27:02 +08:00
|
|
|
if (!std::isinf(pin_wirelength))
|
|
|
|
distance += pin_wirelength;
|
2018-06-13 23:07:42 +08:00
|
|
|
}
|
|
|
|
}
|
2018-06-15 15:27:02 +08:00
|
|
|
if (!has_conns)
|
|
|
|
distance = random_wirelength(rnd);
|
2018-06-15 02:25:35 +08:00
|
|
|
if (distance <= best_distance) {
|
2018-06-14 01:10:12 +08:00
|
|
|
best_distance = distance;
|
|
|
|
best_bel = bel;
|
|
|
|
}
|
2018-06-13 23:07:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (best_bel == BelId()) {
|
|
|
|
log_error("failed to place cell '%s' of type '%s'\n",
|
|
|
|
cell->name.c_str(), cell->type.c_str());
|
|
|
|
}
|
|
|
|
cell->bel = best_bel;
|
|
|
|
chip.bindBel(cell->bel, cell->name);
|
|
|
|
|
|
|
|
// Back annotate location
|
|
|
|
cell->attrs["BEL"] = chip.getBelName(cell->bel).str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void place_design_heuristic(Design *design)
|
|
|
|
{
|
|
|
|
size_t total_cells = design->cells.size(), placed_cells = 0;
|
|
|
|
std::queue<CellInfo *> visit_cells;
|
|
|
|
// Initial constraints placer
|
|
|
|
for (auto cell_entry : design->cells) {
|
|
|
|
CellInfo *cell = cell_entry.second;
|
|
|
|
auto loc = cell->attrs.find("BEL");
|
|
|
|
if (loc != cell->attrs.end()) {
|
|
|
|
std::string loc_name = loc->second;
|
|
|
|
BelId bel = design->chip.getBelByName(IdString(loc_name));
|
|
|
|
if (bel == BelId()) {
|
|
|
|
log_error("No Bel named \'%s\' located for "
|
|
|
|
"this chip (processing BEL attribute on \'%s\')\n",
|
|
|
|
loc_name.c_str(), cell->name.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
BelType bel_type = design->chip.getBelType(bel);
|
|
|
|
if (bel_type != belTypeFromId(cell->type)) {
|
|
|
|
log_error("Bel \'%s\' of type \'%s\' does not match cell "
|
|
|
|
"\'%s\' of type \'%s\'",
|
|
|
|
loc_name.c_str(), belTypeToId(bel_type).c_str(),
|
|
|
|
cell->name.c_str(), cell->type.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
cell->bel = bel;
|
|
|
|
design->chip.bindBel(bel, cell->name);
|
|
|
|
placed_cells++;
|
|
|
|
visit_cells.push(cell);
|
|
|
|
}
|
|
|
|
}
|
2018-06-14 00:53:58 +08:00
|
|
|
log_info("place_constraints placed %d\n", placed_cells);
|
2018-06-15 15:27:02 +08:00
|
|
|
std::mt19937 rnd;
|
|
|
|
std::vector<IdString> autoplaced;
|
2018-06-14 01:10:12 +08:00
|
|
|
for (auto cell : design->cells) {
|
|
|
|
CellInfo *ci = cell.second;
|
2018-06-15 02:25:35 +08:00
|
|
|
if (ci->bel == BelId()) {
|
2018-06-15 15:27:02 +08:00
|
|
|
place_cell(design, ci, rnd);
|
|
|
|
autoplaced.push_back(cell.first);
|
2018-06-15 02:25:35 +08:00
|
|
|
placed_cells++;
|
|
|
|
}
|
2018-06-13 23:07:42 +08:00
|
|
|
log_info("placed %d/%d\n", placed_cells, total_cells);
|
|
|
|
}
|
2018-06-15 15:27:02 +08:00
|
|
|
for (int i = 0 ; i < 2; i ++) {
|
2018-06-15 02:25:35 +08:00
|
|
|
int replaced_cells = 0;
|
2018-06-15 15:27:02 +08:00
|
|
|
std::shuffle(autoplaced.begin(), autoplaced.end(), rnd);
|
2018-06-15 02:25:35 +08:00
|
|
|
for (auto cell : autoplaced) {
|
|
|
|
CellInfo *ci = design->cells[cell];
|
2018-06-15 15:27:02 +08:00
|
|
|
place_cell(design, ci, rnd);
|
2018-06-15 02:25:35 +08:00
|
|
|
replaced_cells++;
|
|
|
|
log_info("replaced %d/%d\n", replaced_cells, autoplaced.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-13 23:07:42 +08:00
|
|
|
}
|
|
|
|
|
2018-06-12 20:24:59 +08:00
|
|
|
NEXTPNR_NAMESPACE_END
|