|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import re |
| 3 | +import subprocess |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +REPO = Path(__file__).resolve().parents[1] |
| 7 | +ORA_BIN = REPO / "zig-out" / "bin" / "ora" |
| 8 | +CHECKS_DIR = REPO / "tests" / "mlir" |
| 9 | + |
| 10 | +SKIP_MARKERS = ["/invalid/", "/negative_tests/"] |
| 11 | + |
| 12 | + |
| 13 | +def should_skip(path: Path) -> bool: |
| 14 | + s = str(path) |
| 15 | + if any(m in s for m in SKIP_MARKERS): |
| 16 | + return True |
| 17 | + name = path.name |
| 18 | + if name.startswith("fail_") or "/fail_" in s or "fail_region_err" in s: |
| 19 | + return True |
| 20 | + if "/logs/log_fail_" in s: |
| 21 | + return True |
| 22 | + return False |
| 23 | + |
| 24 | + |
| 25 | +def main() -> int: |
| 26 | + if not ORA_BIN.exists(): |
| 27 | + print(f"error: ora binary not found at {ORA_BIN}. Run 'zig build' first.") |
| 28 | + return 1 |
| 29 | + |
| 30 | + CHECKS_DIR.mkdir(parents=True, exist_ok=True) |
| 31 | + |
| 32 | + # collect already covered inputs |
| 33 | + covered = set() |
| 34 | + for check in CHECKS_DIR.glob("*.check"): |
| 35 | + try: |
| 36 | + text = check.read_text() |
| 37 | + except Exception: |
| 38 | + continue |
| 39 | + m = re.search(r"^// INPUT:\s*(.+)$", text, re.M) |
| 40 | + if m: |
| 41 | + covered.add(m.group(1).strip()) |
| 42 | + |
| 43 | + ora_files = sorted((REPO / "ora-example").rglob("*.ora")) |
| 44 | + |
| 45 | + created = 0 |
| 46 | + failed = [] |
| 47 | + |
| 48 | + for path in ora_files: |
| 49 | + rel = path.relative_to(REPO).as_posix() |
| 50 | + if should_skip(path): |
| 51 | + continue |
| 52 | + if rel in covered: |
| 53 | + continue |
| 54 | + |
| 55 | + try: |
| 56 | + proc = subprocess.run( |
| 57 | + [str(ORA_BIN), "--emit-mlir", str(path)], |
| 58 | + cwd=REPO, |
| 59 | + stdout=subprocess.PIPE, |
| 60 | + stderr=subprocess.PIPE, |
| 61 | + text=True, |
| 62 | + timeout=30, |
| 63 | + ) |
| 64 | + except Exception as e: |
| 65 | + failed.append((rel, f"exception {e}")) |
| 66 | + continue |
| 67 | + if proc.returncode != 0: |
| 68 | + failed.append((rel, f"exit {proc.returncode}")) |
| 69 | + continue |
| 70 | + |
| 71 | + out = proc.stdout |
| 72 | + marker = "Ora MLIR (before conversion)" |
| 73 | + if marker not in out: |
| 74 | + failed.append((rel, "missing Ora MLIR marker")) |
| 75 | + continue |
| 76 | + |
| 77 | + ora = out.split(marker, 1)[1] |
| 78 | + func_names = re.findall(r"func\.func\s+@([A-Za-z0-9_]+)", ora) |
| 79 | + |
| 80 | + check_name = "auto__" + rel.replace("/", "__").replace(".ora", ".check") |
| 81 | + check_path = CHECKS_DIR / check_name |
| 82 | + |
| 83 | + lines = [] |
| 84 | + lines.append("// AUTO-GENERATED. Do not edit by hand.") |
| 85 | + lines.append(f"// INPUT: {rel}") |
| 86 | + lines.append("// CHECK-NOT: builtin.unrealized_conversion_cast") |
| 87 | + if func_names: |
| 88 | + for name in func_names: |
| 89 | + lines.append(f"// CHECK-LABEL: func.func @{name}") |
| 90 | + else: |
| 91 | + lines.append("// CHECK: module") |
| 92 | + |
| 93 | + check_path.write_text("\n".join(lines) + "\n") |
| 94 | + created += 1 |
| 95 | + |
| 96 | + print(f"created {created} checks") |
| 97 | + if failed: |
| 98 | + print("failed:") |
| 99 | + for rel, reason in failed: |
| 100 | + print(f" {rel}: {reason}") |
| 101 | + return 0 |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == "__main__": |
| 105 | + raise SystemExit(main()) |
0 commit comments