Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 76 additions & 26 deletions openfpga/src/vpr_wrapper/vpr_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <cstdio>
#include <cstring>
#include <ctime>
#include <exception>
#include <memory>

#include "command_exit_codes.h"
#include "globals.h"
Expand All @@ -23,6 +25,49 @@

namespace vpr {

namespace {

/* Data of a VPR run, which has to outlive the function that ran VPR:
* later OpenFPGA commands read it through the global VPR contexts (g_vpr_ctx),
* which point to the architecture. */
struct VprRun {
t_options options = t_options();
t_arch arch = t_arch();
t_vpr_setup setup = t_vpr_setup();
};

/* The last successful run of command 'vpr'. Like g_vpr_ctx, it is global to
* the process */
std::unique_ptr<VprRun> last_vpr_run;

/* Free the data of a run, from the global VPR contexts as well */
void free_vpr_run(VprRun& run) {
try {
vpr_free_all(run.arch, run.setup);
/* vpr_free_all() leaves the packing of the circuit in the clustering
* context: the next circuit would be checked against it */
ClusteringContext& cluster_ctx = g_vpr_ctx.mutable_clustering();
cluster_ctx.atoms_lookup.clear();
cluster_ctx.post_routing_clb_pin_nets.clear();
cluster_ctx.pre_routing_net_pin_mapping.clear();
} catch (const std::exception& exc) {
VTR_LOG_ERROR("Fail to free VPR data: %s\n", exc.what());
}
}

/* VPR keeps its data in global contexts, and loading a new design on top of
* the data of a previous run crashes. Therefore, release the last run before
* starting a new one */
void release_last_vpr_run() {
if (last_vpr_run) {
VTR_LOG("Free the data of the previous VPR run\n");
free_vpr_run(*last_vpr_run);
last_vpr_run.reset();
}
}

} /* End anonymous namespace */

/**
* VPR program without clean up
* VPR program
Expand All @@ -34,65 +79,68 @@ namespace vpr {
* 1. Initialization
* 2. Pack
* 3. Place-and-route and timing analysis
* 4. Clean up
* 4. Clean up: skipped, as the data is needed by the commands that follow.
* It is freed when VPR is run again, or fails
*/
static int vpr(int argc, char** argv) {
vtr::ScopedFinishTimer t("The entire flow of VPR");

t_options Options = t_options();
/* Arch should NOT be freed once this function is done */
t_arch* Arch = new t_arch;
t_vpr_setup vpr_setup = t_vpr_setup();
release_last_vpr_run();

/* The run is kept (see last_vpr_run) only when VPR succeeds. When it fails,
* its data is freed, so that the next run starts from a clean state */
std::unique_ptr<VprRun> run = std::make_unique<VprRun>();
int exit_code = SUCCESS_EXIT_CODE;

try {
vpr_install_signal_handler();

/* Read options, architecture, and circuit netlist */
vpr_init(argc, const_cast<const char**>(argv), &Options, &vpr_setup, Arch);
vpr_init(argc, const_cast<const char**>(argv), &run->options, &run->setup,
&run->arch);

if (Options.show_version) {
if (run->options.show_version) {
free_vpr_run(*run);
return SUCCESS_EXIT_CODE;
}

bool flow_succeeded = vpr_flow(vpr_setup, *Arch);
bool flow_succeeded = vpr_flow(run->setup, run->arch);
if (!flow_succeeded) {
VTR_LOG("VPR failed to implement circuit\n");
return UNIMPLEMENTABLE_EXIT_CODE;
}

auto& timing_ctx = g_vpr_ctx.timing();
print_timing_stats("Flow", timing_ctx.stats);

/* TODO: move this to the end of flow
* free data structures
*/
/* vpr_free_all(Arch, vpr_setup); */
exit_code = UNIMPLEMENTABLE_EXIT_CODE;
} else {
auto& timing_ctx = g_vpr_ctx.timing();
print_timing_stats("Flow", timing_ctx.stats);

VTR_LOG("VPR succeeded\n");
VTR_LOG("VPR succeeded\n");
}

} catch (const tatum::Error& tatum_error) {
VTR_LOG_ERROR("%s\n", format_tatum_error(tatum_error).c_str());

return ERROR_EXIT_CODE;
exit_code = ERROR_EXIT_CODE;

} catch (const VprError& vpr_error) {
vpr_print_error(vpr_error);

if (vpr_error.type() == VPR_ERROR_INTERRUPTED) {
return INTERRUPTED_EXIT_CODE;
exit_code = INTERRUPTED_EXIT_CODE;
} else {
return ERROR_EXIT_CODE;
exit_code = ERROR_EXIT_CODE;
}

} catch (const vtr::VtrError& vtr_error) {
VTR_LOG_ERROR("%s:%d %s\n", vtr_error.filename_c_str(), vtr_error.line(),
vtr_error.what());
exit_code = ERROR_EXIT_CODE;
}

return ERROR_EXIT_CODE;
if (SUCCESS_EXIT_CODE == exit_code) {
last_vpr_run = std::move(run);
} else {
free_vpr_run(*run);
}

/* Signal success to scripts */
return SUCCESS_EXIT_CODE;
return exit_code;
}

/* A wrapper to return proper codes for openfpga shell */
Expand All @@ -109,6 +157,8 @@ int vpr_wrapper(int argc, char** argv) {
static int vpr_standalone(int argc, char** argv) {
vtr::ScopedFinishTimer t("The entire flow of VPR");

release_last_vpr_run();

t_options Options = t_options();
t_arch Arch = t_arch();
t_vpr_setup vpr_setup = t_vpr_setup();
Expand Down
17 changes: 17 additions & 0 deletions openfpga_flow/tasks/python_binding_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ def and2_files(flow_dir):
"vpr_arch": flow_dir / "vpr_arch" / "k6_frac_N10_tileable_40nm.xml",
"openfpga_arch": flow_dir / "openfpga_arch" / "k6_frac_N10_40nm_openfpga.xml",
"blif": flow_dir / "benchmarks" / "ubm" / "simple_gates" / "and2" / "and2.blif",
# Needs a model that the architecture does not have: VPR fails right
# after loading the architecture
"unsupported_blif": flow_dir
/ "benchmarks"
/ "ubm"
/ "ram"
/ "pipelined_8bit_adder"
/ "pipelined_8bit_adder.blif",
"activity": flow_dir
/ "benchmarks"
/ "ubm"
/ "simple_gates"
/ "and2"
/ "and2.act",
"sim_setting": flow_dir
/ "openfpga_simulation_settings"
/ "auto_sim_openfpga.xml",
}
missing = [str(p) for p in files.values() if not p.exists()]
if missing:
Expand Down
3 changes: 1 addition & 2 deletions openfpga_flow/tasks/python_binding_tests/test_log_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ def test_failed_command_log_has_stderr(shell):


def test_vpr_flow_log(shell, and2_files):
# vpr_standalone, as a second call of 'vpr' in one process crashes (it keeps
# its context alive for the commands that follow). test_smoke.py covers 'vpr'
# vpr_standalone frees its data at the end. test_smoke.py covers 'vpr'
result = shell.run(
f"vpr_standalone {and2_files['vpr_arch']} {and2_files['blif']} "
"--clock_modeling route --device 2x2 --route_chan_width 20"
Expand Down
100 changes: 100 additions & 0 deletions openfpga_flow/tasks/python_binding_tests/test_vpr_rerun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
"""Running VPR several times in one process.

VPR keeps its data in process-wide contexts. 'vpr' keeps it alive for the
commands that follow, so it used to crash when run a second time.
"""

import pytest

FREED_PREVIOUS = "Free the data of the previous VPR run"


@pytest.fixture
def vpr_cmd(and2_files):
def make(command="vpr", chan_width=20):
return (
f"{command} {and2_files['vpr_arch']} {and2_files['blif']} "
f"--clock_modeling route --device 2x2 --route_chan_width {chan_width}"
)

return make


def test_vpr_twice(shell, vpr_cmd):
shell.run(vpr_cmd())
second = shell.run(vpr_cmd())
assert "VPR succeeded" in second.log
assert FREED_PREVIOUS in second.log


def test_vpr_three_times(shell, vpr_cmd):
for _ in range(3):
assert "VPR succeeded" in shell.run(vpr_cmd()).log


def test_vpr_after_standalone(shell, vpr_cmd):
shell.run(vpr_cmd("vpr_standalone"))
assert "VPR succeeded" in shell.run(vpr_cmd()).log


def test_standalone_after_vpr(shell, vpr_cmd):
shell.run(vpr_cmd())
result = shell.run(vpr_cmd("vpr_standalone"))
assert "VPR succeeded" in result.log
assert FREED_PREVIOUS in result.log


def test_vpr_after_failed_vpr(shell, vpr_cmd, openfpga_module):
with pytest.raises(openfpga_module.OpenfpgaError):
shell.run("vpr /nonexistent/arch.xml /nonexistent/design.blif")
assert "VPR succeeded" in shell.run(vpr_cmd()).log


def test_vpr_after_failure_with_arch_loaded(shell, vpr_cmd, and2_files):
# VPR fails once the architecture is loaded, leaving data in the contexts
bad = vpr_cmd().replace(
str(and2_files["blif"]), str(and2_files["unsupported_blif"])
)
failed = shell.run(bad, check=False)
assert failed.status != 0
assert "no such model exists in the architecture" in failed.log
assert "VPR succeeded" in shell.run(vpr_cmd()).log


def test_failed_vpr_releases_previous_run(shell, vpr_cmd, and2_files):
shell.run(vpr_cmd())
bad = vpr_cmd().replace(
str(and2_files["blif"]), str(and2_files["unsupported_blif"])
)
failed = shell.run(bad, check=False)
assert failed.status != 0
assert FREED_PREVIOUS in failed.log
assert "VPR succeeded" in shell.run(vpr_cmd()).log


def test_commands_after_rerun(shell, vpr_cmd, and2_files):
"""The data of the second run is what the next commands work on"""
shell.run(vpr_cmd())
shell.run(vpr_cmd())
shell.run(f"read_openfpga_arch -f {and2_files['openfpga_arch']}")
shell.run(f"read_openfpga_simulation_setting -f {and2_files['sim_setting']}")
shell.run(
f"link_openfpga_arch --activity_file {and2_files['activity']} "
"--sort_gsb_chan_node_in_edges"
)
shell.run("build_fabric --compress_routing")


def test_vpr_with_another_circuit(shell, and2_files, flow_dir):
# The packing of the first circuit must not be checked against the second
other = (
flow_dir / "benchmarks" / "ubm" / "simple_gates" / "and2_or2" / "and2_or2.blif"
)
if not other.exists():
pytest.skip(f"missing {other}")
for blif in (and2_files["blif"], other, and2_files["blif"]):
command = (
f"vpr {and2_files['vpr_arch']} {blif} --clock_modeling route "
"--device 2x2 --route_chan_width 20 --pack --place --route"
)
assert "VPR succeeded" in shell.run(command).log
Loading