Repository navigation
Keep Yosys set up between yosys commands and report its errors as command failures
#2889
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
openfpga_flow/tasks/python_binding_tests/test_yosys_command.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| """The `yosys` command of the shell: Yosys runs inside the process, stays set up between commands, and an | ||
| error of Yosys is a failed command that does not end the process.""" | ||
|
|
||
| import re | ||
|
|
||
| import pytest | ||
|
|
||
| AND2 = """module and2(input a, input b, output y); | ||
| assign y = a & b; | ||
| endmodule | ||
| """ | ||
|
|
||
| # Maps to 6-input LUTs and writes a BLIF, like the Yosys step of the flow scripts | ||
| SYNTHESIS = """read_verilog and2.v | ||
| hierarchy -top and2 | ||
| proc | ||
| flatten | ||
| opt | ||
| techmap | ||
| opt | ||
| abc -lut 6 | ||
| opt_clean -purge | ||
| write_blif and2.blif | ||
| """ | ||
|
|
||
| BROKEN_VERILOG = """module broken(input a, output y); | ||
| assign y = a &; | ||
| endmodule | ||
| """ | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def scripts(tmp_path): | ||
| """Yosys scripts of a good run and of the ways that Yosys fails, in the current directory""" | ||
| (tmp_path / "and2.v").write_text(AND2) | ||
| (tmp_path / "broken.v").write_text(BROKEN_VERILOG) | ||
| (tmp_path / "good.ys").write_text(SYNTHESIS) | ||
| (tmp_path / "no_file.ys").write_text("read_verilog nothere.v\n") | ||
| (tmp_path / "syntax.ys").write_text("read_verilog broken.v\n") | ||
| (tmp_path / "no_command.ys").write_text("not_a_yosys_command\n") | ||
| # An error that Yosys reports from inside a pass, not from a frontend or a command line | ||
| (tmp_path / "in_pass.ys").write_text( | ||
| "read_verilog and2.v\nhierarchy -top not_there\n" | ||
| ) | ||
| return tmp_path | ||
|
|
||
|
|
||
| FAILURES = ["no_file.ys", "syntax.ys", "no_command.ys", "in_pass.ys"] | ||
|
|
||
|
|
||
| def synthesize(shell, tmp_path): | ||
| (tmp_path / "and2.blif").unlink(missing_ok=True) | ||
| return shell.run_command("yosys --file good.ys") | ||
|
|
||
|
|
||
| def test_yosys_maps_a_design_to_luts_and_writes_a_blif(shell, scripts): | ||
| assert synthesize(shell, scripts) == 0 | ||
| blif = (scripts / "and2.blif").read_text() | ||
| assert ".model and2" in blif and ".inputs a b" in blif and ".outputs y" in blif | ||
| assert re.search( | ||
| r"^\.names (a b|b a) y\n11 1$", blif, re.M | ||
| ) # the AND gate is one LUT | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("script", FAILURES) | ||
| def test_an_error_of_yosys_fails_the_command_and_leaves_the_process_alive( | ||
| shell, scripts, script | ||
| ): | ||
| assert shell.run_command(f"yosys --file {script}") != 0 | ||
| # The same process runs Yosys again: it was neither shut down nor left half way | ||
| assert synthesize(shell, scripts) == 0 | ||
| assert ".model and2" in (scripts / "and2.blif").read_text() | ||
|
|
||
|
|
||
| def test_yosys_runs_many_times_in_one_process_with_failures_between(shell, scripts): | ||
| for _ in range(5): | ||
| assert synthesize(shell, scripts) == 0 | ||
| for script in FAILURES: | ||
| assert shell.run_command(f"yosys --file {script}") != 0 | ||
| assert synthesize(shell, scripts) == 0 | ||
|
|
||
|
|
||
| def test_a_script_does_not_see_the_design_of_the_one_before(shell, scripts): | ||
| # A design of its own for each script: the module that was read before is gone | ||
| (scripts / "second.ys").write_text("hierarchy -top and2\n") | ||
| assert synthesize(shell, scripts) == 0 | ||
| assert shell.run_command("yosys --file second.ys") != 0 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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()There was a problem hiding this comment.
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.