Repository navigation
Keep Yosys set up between yosys commands and report its errors as command failures - #2889
Conversation
|
|
||
| // 4. Verify design integrity after script completion | ||
| design->check(); | ||
| bool is_file(const std::string& path) { |
There was a problem hiding this comment.
Please use std::filesystem API which is cross-platform and standard.
There was a problem hiding this comment.
Done: std::filesystem::is_directory and is_regular_file, with the error_code overloads so they do not throw. <sys/stat.h> is gone.
| Yosys::yosys_setup(); | ||
| Yosys::log_errfile = stderr; | ||
| Yosys::log_streams.push_back(&std::cout); | ||
| namespace { |
There was a problem hiding this comment.
Why a namespace is created here?
There was a problem hiding this comment.
Removed the anonymous namespace. The helpers are static now.
| std::string folder_of_this_code() { | ||
| #if defined(__unix__) || defined(__APPLE__) | ||
| Dl_info info; | ||
| if (dladdr(reinterpret_cast<void*>(&folder_of_this_code), &info) == 0 || |
There was a problem hiding this comment.
You can use std::filesystem::current_path()
There was a problem hiding this comment.
std::filesystem::current_path() is the working directory of the process, which is not where Yosys's files are. So I kept dladdr for this one.
In the openfpga shell nothing more is needed. Yosys finds share/ and yosys-abc next to the executable, where the CMake step puts them.
The Python modules (_openfpga.so and openfpga_shell.so) are a different case. The running executable is python, so Yosys looks next to python and finds nothing, and every yosys command fails. The CMake step already places share/ and yosys-abc in build/openfpga, next to both modules. dladdr gives the folder the module was loaded from, and locate_yosys_files looks there. That uses the layout CMake already creates and adds nothing to it.
The lookup only takes over when Yosys did not find share/ or yosys-abc by itself, so the environment variable ABC and Yosys's own folders still win. It is compiled only for Unix and macOS (unix, APPLE). Elsewhere Yosys keeps its default lookup.
| * itself (the environment variable ABC, its own folders) is kept */ | ||
| void locate_yosys_files() { | ||
| std::string folder; | ||
| if (const char* given = std::getenv("OPENFPGA_YOSYS_DIR")) { |
There was a problem hiding this comment.
The share directory is copied by CMake to ensure the share directory and yosys-abc in the right place, even in the packaged installers.
OpenFPGA/openfpga/CMakeLists.txt
Lines 95 to 105 in cd9edb1
There was a problem hiding this comment.
Agreed, the code relies on that layout. I removed OPENFPGA_YOSYS_DIR, so the only place the code looks is the folder of the loaded module, where that CMake step puts the files.
55e7540 to
87e7f73
Compare
…ailures
The yosys command shut Yosys down after each script, and Yosys cannot
register its passes a second time, so a second call failed with
"No such command: read_verilog". An error reported through log_error()
ended the whole process with exit(1), also when OpenFPGA runs inside Python.
- Set Yosys up once for the life of the process and never shut it down.
- Turn log_cmd_error() and log_error() into exceptions (log_cmd_error_throw
and the log_error_atexit hook), report the message of Yosys as a failed
command, and reset the Yosys log stack the way its own shell does.
- Give each script a design of its own.
- Find share/ and yosys-abc in the folder of the loaded code (or in
OPENFPGA_YOSYS_DIR) when Yosys cannot find them next to the executable,
which is the case for the Python module. Link against ${CMAKE_DL_LIBS}
for dladdr, which is used on Unix and macOS only (other platforms keep
what Yosys finds).
- Add Python tests: a synthesis to BLIF, four kinds of Yosys errors each
followed by a good run in the same process, and a loop of both.
… drop OPENFPGA_YOSYS_DIR
87e7f73 to
849b533
Compare
Motivate of the pull request
The
yosyscommand of the shell can only be used once per process, and a Yosys error ends the wholeprocess. That makes it unusable from a long-running session (the Python module, scripts that run Yosys more
than once, a tool that retries after a failed synthesis).
Describe the technical details
What is currently done?
yosys --file script.ysruns a Yosys script in the process (libyosys is linked into libopenfpga). Twothings go wrong, both reproduced on master (
cd9edb15e):yosys_shutdown()after every script. Yosys cannot register its passes a second time,so the second call fails:
ERROR: No such command: read_verilog.log_error()(a missing file, a syntax error, an error inside a pass)calls
exit(1). Thetry/catchof the wrapper never sees it, so theopenfpgaprocess ends. The samehappens when the shell runs inside Python.
Under Python there is a third problem: Yosys looks for its
share/folder and foryosys-abcnext to therunning executable, which is
python, so a script that reachestechmaporabcfails withinit_share_dirname: unable to determine share/ directory!.What does this pull request change?
log_cmd_error()andlog_error()become exceptions (log_cmd_error_throw, and alog_error_atexithook that throws before
exit(1)). The wrapper reports the message of Yosys as a failed command(
Error N: Yosys failed: <message>, return code non-zero) and resets the Yosys log stack the way theYosys shell does after an error.
share/andyosys-abcare looked for in the folder of the loaded code (found withdladdr), wherethe build already links them, or in the folder named by
OPENFPGA_YOSYS_DIR. What Yosys found by itself(the
ABCenvironment variable, its own folders) is kept.libopenfpgalinks${CMAKE_DL_LIBS}.python_binding_tests/test_yosys_command.py): a synthesis to a BLIF; four kinds of Yosyserrors (missing file, syntax error, unknown command, error inside a pass), each followed by a good run
in the same process; a loop of five rounds of both; a script does not see the design of the one before.
Which part of the code base require a change
Impact of the pull request
The command keeps its name and options. A script that failed with an exit of the process now returns a
failed command; a script that ran once still behaves the same.
How it was tested
openfpga -iwith a sequence of good and failing scripts, and with 60 commands (30 good, 30 failing, halfof the failures inside the
hierarchypass): every failure reported, every later run worked, memory steadyat about 50 MB.
OpenfpgaShell.run_command): the same sequence, and 120 commands in one process(peak 86 MB). The new pytest file and
test_smoke.pypass (12 tests).Notes for review
log_error()leaves Yosys's globals in a state that Yosys itself does not expect afterlog_error(); the design is discarded and the log stack reset, which is what its own shell does after acommand error. I found no leak or crash in the runs above, but a crash or a failed internal assertion in
Yosys still ends the process. A caller that cannot accept that should run the shell in a worker process.
dladdr/<dlfcn.h>only under__unix__or__APPLE__; on other platforms (theMsys2 builds) it is skipped and Yosys keeps looking where it did before. I could not build the Windows
jobs here, so CI is the check for them.
share/andyosys-abclinks that the build makes next to themodule; installing them is not part of this PR.
(
syntax.v:2: ERROR: ...). Returning them as data is a follow-up.