Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also reorganize templates into scripts/ base directory
- Loading branch information
Showing
39 changed files
with
265 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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,31 @@ | ||
name: Scripts tests | ||
permissions: read-all | ||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
branches: | ||
- main | ||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.head_ref || github.ref }} | ||
cancel-in-progress: true | ||
jobs: | ||
build-and-test: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
python-version: ["3.11"] | ||
steps: | ||
- uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # pin@v3 | ||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@0a5c61591373683505ea898e09a3ea4f39ef2b9c # pin@v5 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install -r requirements-dev.txt | ||
- name: Run tests | ||
run: | | ||
python -m pytest |
This file contains 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 |
---|---|---|
|
@@ -21,3 +21,6 @@ tests/tfhe_rust_bool/end_to_end_fpga/tfhe-rs | |
|
||
# vscode | ||
.vscode/** | ||
|
||
# for python files | ||
__pycache__ |
This file contains 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 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,15 @@ | ||
[tool.pytest.ini_options] | ||
norecursedirs = [ | ||
".git", | ||
"__pycache__", | ||
"bazel", | ||
"bazel-bin", | ||
"bazel-heir", | ||
"bazel-out", | ||
"bazel-testlogs", | ||
"docs", | ||
"external", | ||
"tests", | ||
"tools", | ||
"venv", | ||
] |
This file contains 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
fire==0.5.0 | ||
jinja2==3.1.3 | ||
pre-commit==v3.3.3 | ||
pytest==8.1.1 |
Empty file.
This file contains 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,105 @@ | ||
import os | ||
import pathlib | ||
from collections import deque | ||
|
||
import fire | ||
|
||
|
||
# a sentinel for a bash pipe | ||
PIPE = "|" | ||
OUT_REDIRECT = ">" | ||
IN_REDIRECT = "<" | ||
RUN_PREFIX = "// RUN:" | ||
|
||
def strip_run_prefix(line): | ||
if RUN_PREFIX in line: | ||
return line.split(RUN_PREFIX)[1] | ||
return line | ||
|
||
|
||
def convert_to_run_commands(run_lines): | ||
run_lines = deque(run_lines) | ||
cmds = [] | ||
current_command = "" | ||
while run_lines: | ||
line = run_lines.popleft() | ||
if RUN_PREFIX not in line: | ||
continue | ||
|
||
line = strip_run_prefix(line) | ||
|
||
if '|' in line: | ||
first, second = line.split('|', maxsplit=1) | ||
current_command += " " + first.strip() | ||
cmds.append(current_command.strip()) | ||
current_command = "" | ||
cmds.append(PIPE) | ||
run_lines.appendleft(RUN_PREFIX + " " + second.strip()) | ||
continue | ||
|
||
# redirecting to a file implicitly ends the command on that line | ||
if OUT_REDIRECT in line or IN_REDIRECT in line: | ||
cmds.append(line.strip()) | ||
current_command = "" | ||
continue | ||
|
||
if line.strip().endswith('\\'): | ||
current_command += " " + line.replace('\\', '').strip() | ||
continue | ||
|
||
current_command += line | ||
cmds.append(current_command.strip()) | ||
current_command = "" | ||
|
||
return cmds | ||
|
||
|
||
def lit_to_bazel( | ||
lit_test_file: str, | ||
): | ||
"""A helper CLI that converts MLIR test files to bazel run commands. | ||
Args: | ||
lit_test_file: The lit test file that should be converted to a bazel run | ||
command. | ||
""" | ||
|
||
git_root = pathlib.Path(__file__).parent.parent | ||
if not os.path.isdir(git_root / ".git"): | ||
raise RuntimeError(f"Could not find git root, looked at {git_root}") | ||
|
||
if not lit_test_file: | ||
raise ValueError("lit_test_file must be provided") | ||
|
||
if not os.path.isfile(lit_test_file): | ||
raise ValueError("Unable to find lit_test_file '%s'" % lit_test_file) | ||
|
||
run_lines = [] | ||
with open(lit_test_file, "r") as f: | ||
for line in f: | ||
if "// RUN:" in line: | ||
run_lines.append(line) | ||
|
||
commands = convert_to_run_commands(run_lines) | ||
commands = [x for x in commands if 'FileCheck' not in x] | ||
# remove consecutive and trailing pipes | ||
if commands[-1] == PIPE: | ||
commands.pop() | ||
deduped_commands = [] | ||
for command in commands: | ||
if command == PIPE and deduped_commands[-1] == PIPE: | ||
continue | ||
deduped_commands.append(command) | ||
|
||
joined = " ".join(deduped_commands) | ||
# I would consider using bazel-bin/tools/heir-opt, but the yosys | ||
# requirement requires additional env vars to be set for the yosys and ABC | ||
# paths, which is not yet worth doing for this script. | ||
joined = joined.replace("heir-opt", "bazel run --noallow_analysis_cache_discard //tools:heir-opt --") | ||
joined = joined.replace("heir-translate", f"{git_root}/bazel-bin/tools/heir-translate") | ||
joined = joined.replace("%s", str(pathlib.Path(lit_test_file).absolute())) | ||
print(joined) | ||
|
||
|
||
if __name__ == "__main__": | ||
fire.Fire(lit_to_bazel) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains 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 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,78 @@ | ||
from scripts.lit_to_bazel import convert_to_run_commands, PIPE | ||
|
||
|
||
def test_convert_to_run_commands_simple(): | ||
run_lines = [ | ||
"// RUN: heir-opt --canonicalize", | ||
] | ||
assert convert_to_run_commands(run_lines) == [ | ||
"heir-opt --canonicalize", | ||
] | ||
|
||
def test_convert_to_run_commands_simple_with_filecheck(): | ||
run_lines = [ | ||
"// RUN: heir-opt --canonicalize | FileCheck %s", | ||
] | ||
assert convert_to_run_commands(run_lines) == [ | ||
"heir-opt --canonicalize", | ||
PIPE, | ||
"FileCheck %s", | ||
] | ||
|
||
def test_convert_to_run_commands_simple_with_line_continuation(): | ||
run_lines = [ | ||
"// RUN: heir-opt \\", | ||
"// RUN: --canonicalize | FileCheck %s", | ||
] | ||
assert convert_to_run_commands(run_lines) == [ | ||
"heir-opt --canonicalize", | ||
PIPE, | ||
"FileCheck %s", | ||
] | ||
|
||
def test_convert_to_run_commands_simple_with_multiple_line_continuations(): | ||
run_lines = [ | ||
"// RUN: heir-opt \\", | ||
"// RUN: --canonicalize \\", | ||
"// RUN: --cse | FileCheck %s", | ||
] | ||
assert convert_to_run_commands(run_lines) == [ | ||
"heir-opt --canonicalize --cse", | ||
PIPE, | ||
"FileCheck %s", | ||
] | ||
|
||
def test_convert_to_run_commands_simple_with_second_command(): | ||
run_lines = [ | ||
"// RUN: heir-opt --canonicalize > %t", | ||
"// RUN: FileCheck %s < %t", | ||
] | ||
assert convert_to_run_commands(run_lines) == [ | ||
"heir-opt --canonicalize > %t", | ||
"FileCheck %s < %t", | ||
] | ||
|
||
def test_convert_to_run_commands_simple_with_non_run_garbage(): | ||
run_lines = [ | ||
"// RUN: heir-opt --canonicalize > %t", | ||
"// wat", | ||
"// RUN: FileCheck %s < %t", | ||
] | ||
assert convert_to_run_commands(run_lines) == [ | ||
"heir-opt --canonicalize > %t", | ||
"FileCheck %s < %t", | ||
] | ||
|
||
def test_convert_to_run_commands_with_multiple_pipes(): | ||
run_lines = [ | ||
"// RUN: heir-opt --canonicalize \\", | ||
"// RUN: | heir-translate --emit-verilog \\", | ||
"// RUN: | FileCheck %s", | ||
] | ||
assert convert_to_run_commands(run_lines) == [ | ||
"heir-opt --canonicalize", | ||
PIPE, | ||
"heir-translate --emit-verilog", | ||
PIPE, | ||
"FileCheck %s", | ||
] |