Skip to content

Commit

Permalink
add lit_to_bazel helper script
Browse files Browse the repository at this point in the history
  • Loading branch information
j2kun committed Mar 19, 2024
1 parent 1dc27ac commit abca6c8
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ tests/tfhe_rust_bool/end_to_end_fpga/

# vscode
.vscode/**

# for python files
__pycache__
1 change: 1 addition & 0 deletions requirements-dev.txt
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 added scripts/__init__.py
Empty file.
105 changes: 105 additions & 0 deletions scripts/lit_to_bazel.py
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)
78 changes: 78 additions & 0 deletions scripts/test_lit_to_bazel.py
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",
]

0 comments on commit abca6c8

Please sign in to comment.