1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
|
/*
* nextpnr -- Next Generation Place and Route
*
* Copyright (C) 2018 Clifford Wolf <clifford@symbioticeda.com>
* Copyright (C) 2018 Miodrag Milanovic <miodrag@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.
*
*/
#ifndef NO_GUI
#include <QApplication>
#include "application.h"
#include "mainwindow.h"
#endif
#ifndef NO_PYTHON
#include "pybindings.h"
#endif
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/filesystem/convenience.hpp>
#include <boost/program_options.hpp>
#include <fstream>
#include <iostream>
#include "command.h"
#include "design_utils.h"
#include "json_frontend.h"
#include "jsonwrite.h"
#include "log.h"
#include "timing.h"
#include "util.h"
#include "version.h"
NEXTPNR_NAMESPACE_BEGIN
CommandHandler::CommandHandler(int argc, char **argv) : argc(argc), argv(argv) { log_streams.clear(); }
bool CommandHandler::parseOptions()
{
options.add(getGeneralOptions()).add(getArchOptions());
try {
po::parsed_options parsed =
po::command_line_parser(argc, argv)
.style(po::command_line_style::default_style ^ po::command_line_style::allow_guessing)
.options(options)
.positional(pos)
.run();
po::store(parsed, vm);
po::notify(vm);
return true;
} catch (std::exception &e) {
std::cout << e.what() << "\n";
return false;
}
}
bool CommandHandler::executeBeforeContext()
{
if (vm.count("help") || argc == 1) {
std::cerr << boost::filesystem::basename(argv[0])
<< " -- Next Generation Place and Route (Version " GIT_DESCRIBE_STR ")\n";
std::cerr << options << "\n";
return argc != 1;
}
if (vm.count("version")) {
std::cerr << boost::filesystem::basename(argv[0])
<< " -- Next Generation Place and Route (Version " GIT_DESCRIBE_STR ")\n";
return true;
}
validate();
if (vm.count("quiet")) {
log_streams.push_back(std::make_pair(&std::cerr, LogLevel::WARNING_MSG));
} else {
log_streams.push_back(std::make_pair(&std::cerr, LogLevel::LOG_MSG));
}
if (vm.count("log")) {
std::string logfilename = vm["log"].as<std::string>();
logfile.open(logfilename);
if (!logfile.is_open())
log_error("Failed to open log file '%s' for writing.\n", logfilename.c_str());
log_streams.push_back(std::make_pair(&logfile, LogLevel::LOG_MSG));
}
return false;
}
po::options_description CommandHandler::getGeneralOptions()
{
po::options_description general("General options");
general.add_options()("help,h", "show help");
general.add_options()("verbose,v", "verbose output");
general.add_options()("quiet,q", "quiet mode, only errors and warnings displayed");
general.add_options()("log,l", po::value<std::string>(),
"log file, all log messages are written to this file regardless of -q");
general.add_options()("debug", "debug output");
general.add_options()("force,f", "keep running after errors");
#ifndef NO_GUI
general.add_options()("gui", "start gui");
general.add_options()("gui-no-aa", "disable anti aliasing (use together with --gui option)");
#endif
#ifndef NO_PYTHON
general.add_options()("run", po::value<std::vector<std::string>>(),
"python file to execute instead of default flow");
pos.add("run", -1);
general.add_options()("pre-pack", po::value<std::vector<std::string>>(), "python file to run before packing");
general.add_options()("pre-place", po::value<std::vector<std::string>>(), "python file to run before placement");
general.add_options()("pre-route", po::value<std::vector<std::string>>(), "python file to run before routing");
general.add_options()("post-route", po::value<std::vector<std::string>>(), "python file to run after routing");
#endif
general.add_options()("json", po::value<std::string>(), "JSON design file to ingest");
general.add_options()("write", po::value<std::string>(), "JSON design file to write");
general.add_options()("seed", po::value<int>(), "seed value for random number generator");
general.add_options()("randomize-seed,r", "randomize seed value for random number generator");
general.add_options()(
"placer", po::value<std::string>(),
std::string("placer algorithm to use; available: " + boost::algorithm::join(Arch::availablePlacers, ", ") +
"; default: " + Arch::defaultPlacer)
.c_str());
general.add_options()(
"router", po::value<std::string>(),
std::string("router algorithm to use; available: " + boost::algorithm::join(Arch::availableRouters, ", ") +
"; default: " + Arch::defaultRouter)
.c_str());
general.add_options()("slack_redist_iter", po::value<int>(), "number of iterations between slack redistribution");
general.add_options()("cstrweight", po::value<float>(), "placer weighting for relative constraint satisfaction");
general.add_options()("starttemp", po::value<float>(), "placer SA start temperature");
general.add_options()("placer-budgets", "use budget rather than criticality in placer timing weights");
general.add_options()("pack-only", "pack design only without placement or routing");
general.add_options()("no-route", "process design without routing");
general.add_options()("no-place", "process design without placement");
general.add_options()("no-pack", "process design without packing");
general.add_options()("ignore-loops", "ignore combinational loops in timing analysis");
general.add_options()("version,V", "show version");
general.add_options()("test", "check architecture database integrity");
general.add_options()("freq", po::value<double>(), "set target frequency for design in MHz");
general.add_options()("timing-allow-fail", "allow timing to fail in design");
general.add_options()("no-tmdriv", "disable timing-driven placement");
general.add_options()("sdf", po::value<std::string>(), "SDF delay back-annotation file to write");
general.add_options()("sdf-cvc", "enable tweaks for SDF file compatibility with the CVC simulator");
general.add_options()("placed-svg", po::value<std::string>(), "write render of placement to SVG file");
general.add_options()("routed-svg", po::value<std::string>(), "write render of routing to SVG file");
return general;
}
void CommandHandler::setupContext(Context *ctx)
{
if (ctx->settings.find(ctx->id("seed")) != ctx->settings.end())
ctx->rngstate = ctx->setting<uint64_t>("seed");
if (vm.count("verbose")) {
ctx->verbose = true;
}
if (vm.count("debug")) {
ctx->verbose = true;
ctx->debug = true;
}
if (vm.count("force")) {
ctx->force = true;
}
if (vm.count("seed")) {
ctx->rngseed(vm["seed"].as<int>());
}
if (vm.count("randomize-seed")) {
srand(time(NULL));
int r;
do {
r = rand();
} while (r == 0);
ctx->rngseed(r);
}
if (vm.count("slack_redist_iter")) {
ctx->settings[ctx->id("slack_redist_iter")] = vm["slack_redist_iter"].as<int>();
if (vm.count("freq") && vm["freq"].as<double>() == 0) {
ctx->settings[ctx->id("auto_freq")] = true;
#ifndef NO_GUI
if (!vm.count("gui"))
#endif
log_warning("Target frequency not specified. Will optimise for max frequency.\n");
}
}
if (vm.count("ignore-loops")) {
ctx->settings[ctx->id("timing/ignoreLoops")] = true;
}
if (vm.count("timing-allow-fail")) {
ctx->settings[ctx->id("timing/allowFail")] = true;
}
if (vm.count("placer")) {
std::string placer = vm["placer"].as<std::string>();
if (std::find(Arch::availablePlacers.begin(), Arch::availablePlacers.end(), placer) ==
Arch::availablePlacers.end())
log_error("Placer algorithm '%s' is not supported (available options: %s)\n", placer.c_str(),
boost::algorithm::join(Arch::availablePlacers, ", ").c_str());
ctx->settings[ctx->id("placer")] = placer;
}
if (vm.count("router")) {
std::string router = vm["router"].as<std::string>();
if (std::find(Arch::availableRouters.begin(), Arch::availableRouters.end(), router) ==
Arch::availableRouters.end())
log_error("Router algorithm '%s' is not supported (available options: %s)\n", router.c_str(),
boost::algorithm::join(Arch::availableRouters, ", ").c_str());
ctx->settings[ctx->id("router")] = router;
}
if (vm.count("cstrweight")) {
ctx->settings[ctx->id("placer1/constraintWeight")] = std::to_string(vm["cstrweight"].as<float>());
}
if (vm.count("starttemp")) {
ctx->settings[ctx->id("placer1/startTemp")] = std::to_string(vm["starttemp"].as<float>());
}
if (vm.count("placer-budgets")) {
ctx->settings[ctx->id("placer1/budgetBased")] = true;
}
if (vm.count("freq")) {
auto freq = vm["freq"].as<double>();
if (freq > 0)
ctx->settings[ctx->id("target_freq")] = std::to_string(freq * 1e6);
}
if (vm.count("no-tmdriv"))
ctx->settings[ctx->id("timing_driven")] = false;
// Setting default values
if (ctx->settings.find(ctx->id("target_freq")) == ctx->settings.end())
ctx->settings[ctx->id("target_freq")] = std::to_string(12e6);
if (ctx->settings.find(ctx->id("timing_driven")) == ctx->settings.end())
ctx->settings[ctx->id("timing_driven")] = true;
if (ctx->settings.find(ctx->id("slack_redist_iter")) == ctx->settings.end())
ctx->settings[ctx->id("slack_redist_iter")] = 0;
if (ctx->settings.find(ctx->id("auto_freq")) == ctx->settings.end())
ctx->settings[ctx->id("auto_freq")] = false;
if (ctx->settings.find(ctx->id("placer")) == ctx->settings.end())
ctx->settings[ctx->id("placer")] = Arch::defaultPlacer;
if (ctx->settings.find(ctx->id("router")) == ctx->settings.end())
ctx->settings[ctx->id("router")] = Arch::defaultRouter;
ctx->settings[ctx->id("arch.name")] = std::string(ctx->archId().c_str(ctx));
ctx->settings[ctx->id("arch.type")] = std::string(ctx->archArgsToId(ctx->archArgs()).c_str(ctx));
ctx->settings[ctx->id("seed")] = ctx->rngstate;
}
int CommandHandler::executeMain(std::unique_ptr<Context> ctx)
{
if (vm.count("test")) {
ctx->archcheck();
return 0;
}
#ifndef NO_GUI
if (vm.count("gui")) {
Application a(argc, argv, (vm.count("gui-no-aa") > 0));
MainWindow w(std::move(ctx), this);
try {
if (vm.count("json")) {
std::string filename = vm["json"].as<std::string>();
std::ifstream f(filename);
if (!parse_json(f, filename, w.getContext()))
log_error("Loading design failed.\n");
customAfterLoad(w.getContext());
w.notifyChangeContext();
w.updateActions();
} else
w.notifyChangeContext();
} catch (log_execution_error_exception) {
// show error is handled by gui itself
}
w.show();
return a.exec();
}
#endif
if (vm.count("json")) {
std::string filename = vm["json"].as<std::string>();
std::ifstream f(filename);
if (!parse_json(f, filename, ctx.get()))
log_error("Loading design failed.\n");
customAfterLoad(ctx.get());
}
#ifndef NO_PYTHON
init_python(argv[0], true);
python_export_global("ctx", *ctx);
if (vm.count("run")) {
std::vector<std::string> files = vm["run"].as<std::vector<std::string>>();
for (auto filename : files)
execute_python_file(filename.c_str());
} else
#endif
if (vm.count("json")) {
bool do_pack = vm.count("pack-only") != 0 || vm.count("no-pack") == 0;
bool do_place = vm.count("pack-only") == 0 && vm.count("no-place") == 0;
bool do_route = vm.count("pack-only") == 0 && vm.count("no-route") == 0;
if (do_pack) {
run_script_hook("pre-pack");
if (!ctx->pack() && !ctx->force)
log_error("Packing design failed.\n");
}
assign_budget(ctx.get());
ctx->check();
print_utilisation(ctx.get());
if (do_place) {
run_script_hook("pre-place");
if (!ctx->place() && !ctx->force)
log_error("Placing design failed.\n");
ctx->check();
if (vm.count("placed-svg"))
ctx->writeSVG(vm["placed-svg"].as<std::string>(), "scale=50 hide_routing");
}
if (do_route) {
run_script_hook("pre-route");
if (!ctx->route() && !ctx->force)
log_error("Routing design failed.\n");
run_script_hook("post-route");
if (vm.count("routed-svg"))
ctx->writeSVG(vm["routed-svg"].as<std::string>(), "scale=500");
}
customBitstream(ctx.get());
}
if (vm.count("write")) {
std::string filename = vm["write"].as<std::string>();
std::ofstream f(filename);
if (!write_json_file(f, filename, ctx.get()))
log_error("Saving design failed.\n");
}
if (vm.count("sdf")) {
std::string filename = vm["sdf"].as<std::string>();
std::ofstream f(filename);
if (!f)
log_error("Failed to open SDF file '%s' for writing.\n", filename.c_str());
ctx->writeSDF(f, vm.count("sdf-cvc"));
}
#ifndef NO_PYTHON
deinit_python();
#endif
return had_nonfatal_error ? 1 : 0;
}
void CommandHandler::conflicting_options(const boost::program_options::variables_map &vm, const char *opt1,
const char *opt2)
{
if (vm.count(opt1) && !vm[opt1].defaulted() && vm.count(opt2) && !vm[opt2].defaulted()) {
std::string msg = "Conflicting options '" + std::string(opt1) + "' and '" + std::string(opt2) + "'.";
log_error("%s\n", msg.c_str());
}
}
void CommandHandler::printFooter()
{
int warning_count = get_or_default(message_count_by_level, LogLevel::WARNING_MSG, 0),
error_count = get_or_default(message_count_by_level, LogLevel::ERROR_MSG, 0);
if (warning_count > 0 || error_count > 0)
log_always("%d warning%s, %d error%s\n", warning_count, warning_count == 1 ? "" : "s", error_count,
error_count == 1 ? "" : "s");
}
int CommandHandler::exec()
{
try {
if (!parseOptions())
return -1;
if (executeBeforeContext())
return 0;
std::unordered_map<std::string, Property> values;
std::unique_ptr<Context> ctx = createContext(values);
setupContext(ctx.get());
setupArchContext(ctx.get());
int rc = executeMain(std::move(ctx));
printFooter();
return rc;
} catch (log_execution_error_exception) {
printFooter();
return -1;
}
}
std::unique_ptr<Context> CommandHandler::load_json(std::string filename)
{
vm.clear();
std::unordered_map<std::string, Property> values;
std::unique_ptr<Context> ctx = createContext(values);
setupContext(ctx.get());
setupArchContext(ctx.get());
{
std::ifstream f(filename);
if (!parse_json(f, filename, ctx.get()))
log_error("Loading design failed.\n");
}
customAfterLoad(ctx.get());
return ctx;
}
void CommandHandler::run_script_hook(const std::string &name)
{
#ifndef NO_PYTHON
if (vm.count(name)) {
std::vector<std::string> files = vm[name].as<std::vector<std::string>>();
for (auto filename : files)
execute_python_file(filename.c_str());
}
#endif
}
NEXTPNR_NAMESPACE_END
|