Skip to content

Keep Yosys set up between yosys commands and report its errors as command failures - #2889

Merged
tangxifan merged 2 commits into
masterfrom
gg_yosys_integration
Dec 2, 2026
Merged

tangxifan merged 2 commits into
masterfrom
gg_yosys_integration

Conversation

@ganeshgore

Copy link
Copy Markdown
Collaborator

Motivate of the pull request

  • To address an existing issue. If so, please provide a link to the issue: <issue id, if any>
  • Breaking new feature. If so, please describe details in the description part.

The yosys command of the shell can only be used once per process, and a Yosys error ends the whole
process. 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.ys runs a Yosys script in the process (libyosys is linked into libopenfpga). Two
things go wrong, both reproduced on master (cd9edb15e):

  1. The wrapper calls yosys_shutdown() after every script. Yosys cannot register its passes a second time,
    so the second call fails: ERROR: No such command: read_verilog.
  2. An error that Yosys reports with log_error() (a missing file, a syntax error, an error inside a pass)
    calls exit(1). The try/catch of the wrapper never sees it, so the openfpga process ends. The same
    happens when the shell runs inside Python.

Under Python there is a third problem: Yosys looks for its share/ folder and for yosys-abc next to the
running executable, which is python, so a script that reaches techmap or abc fails with
init_share_dirname: unable to determine share/ directory!.

What does this pull request change?

  • Yosys is set up once for the life of the process and is not shut down after a script.
  • log_cmd_error() and log_error() become exceptions (log_cmd_error_throw, and a log_error_atexit
    hook 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 the
    Yosys shell does after an error.
  • Each script gets a design of its own.
  • share/ and yosys-abc are looked for in the folder of the loaded code (found with dladdr), where
    the build already links them, or in the folder named by OPENFPGA_YOSYS_DIR. What Yosys found by itself
    (the ABC environment variable, its own folders) is kept. libopenfpga links ${CMAKE_DL_LIBS}.
  • Python tests (python_binding_tests/test_yosys_command.py): a synthesis to a BLIF; four kinds of Yosys
    errors (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

  • VPR
  • Tileable routing architecture generator
  • OpenFPGA libraries
  • FPGA-Verilog
  • FPGA-Bitstream
  • FPGA-SDC
  • FPGA-SPICE
  • Flow scripts
  • Architecture library
  • Cell library
  • Documentation
  • Regression tests
  • Continous Integration (CI) scripts

Impact of the pull request

  • Require a change on Quality of Results (QoR)
  • Break back-compatibility. If so, please list who may be influenced.

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 -i with a sequence of good and failing scripts, and with 60 commands (30 good, 30 failing, half
    of the failures inside the hierarchy pass): every failure reported, every later run worked, memory steady
    at about 50 MB.
  • The Python module (OpenfpgaShell.run_command): the same sequence, and 120 commands in one process
    (peak 86 MB). The new pytest file and test_smoke.py pass (12 tests).
  • Formatted with clang-format 14.0.6 (the C++) and black (the test).

Notes for review

  • Throwing out of log_error() leaves Yosys's globals in a state that Yosys itself does not expect after
    log_error(); the design is discarded and the log stack reset, which is what its own shell does after a
    command 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.
  • The folder lookup uses dladdr/<dlfcn.h> only under __unix__ or __APPLE__; on other platforms (the
    Msys2 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.
  • A pip-installed module will not have the share/ and yosys-abc links that the build makes next to the
    module; installing them is not part of this PR.
  • The failure message carries only the reason; the file and line of a Verilog error stay in the log
    (syntax.v:2: ERROR: ...). Returning them as data is a follow-up.


// 4. Verify design integrity after script completion
design->check();
bool is_file(const std::string& path) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use std::filesystem API which is cross-platform and standard.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why a namespace is created here?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ||

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use std::filesystem::current_path()

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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")) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The share directory is copied by CMake to ensure the share directory and yosys-abc in the right place, even in the packaged installers.

# Must copy/link after openfpga executable is built
add_custom_command(
TARGET openfpga
POST_BUILD
# Symlink Rule 1: Share folder directory
COMMAND ${CMAKE_COMMAND} -E create_symlink "${YOSYS_BUILD_SHARE_DIR}" "${OPENFPGA_BUILD_SHARE_DIR}"
# Symlink Rule 2: Yosys-ABC executable engine
COMMAND ${CMAKE_COMMAND} -E create_symlink "${YOSYS_SRC_ABC_EXE}" "${OPENFPGA_BUILD_ABC_EXE}"
COMMENT "Post-build: Creating necessary symlinks for Yosys and ABC environment"
VERBATIM
)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

…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.
@ganeshgore
ganeshgore force-pushed the gg_yosys_integration branch from 87e7f73 to 849b533 Compare December 2, 2026 05:22
@tangxifan
tangxifan merged commit b939993 into master Dec 2, 2026
42 checks passed
@tangxifan
tangxifan deleted the gg_yosys_integration branch December 2, 2026 21:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants