gisel-ab 0.9.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. gisel_ab/__init__.py +120 -0
  2. gisel_ab/__main__.py +8 -0
  3. gisel_ab/baseline.py +145 -0
  4. gisel_ab/bugreport.py +113 -0
  5. gisel_ab/cli.py +150 -0
  6. gisel_ab/cohort.py +704 -0
  7. gisel_ab/cpp/aarch64_generated_ctor.cpp +23 -0
  8. gisel_ab/cpp/aarch64_query_runner.cpp +380 -0
  9. gisel_ab/cpp/arm_generated_ctor.cpp +18 -0
  10. gisel_ab/cpp/arm_query_runner.cpp +58 -0
  11. gisel_ab/cpp/m68k_generated_ctor.cpp +21 -0
  12. gisel_ab/cpp/m68k_query_runner.cpp +43 -0
  13. gisel_ab/cpp/mips_generated_ctor.cpp +16 -0
  14. gisel_ab/cpp/mips_query_runner.cpp +52 -0
  15. gisel_ab/cpp/ppc_generated_ctor.cpp +16 -0
  16. gisel_ab/cpp/ppc_query_runner.cpp +46 -0
  17. gisel_ab/cpp/query-recovery.h +229 -0
  18. gisel_ab/cpp/query_runner.cpp +419 -0
  19. gisel_ab/cpp/query_runner_common.h +417 -0
  20. gisel_ab/cpp/riscv_generated_ctor.cpp +25 -0
  21. gisel_ab/cpp/riscv_query_runner.cpp +379 -0
  22. gisel_ab/cpp/spirv_generated_ctor.cpp +24 -0
  23. gisel_ab/cpp/spirv_query_runner.cpp +98 -0
  24. gisel_ab/cpp/wasm_generated_ctor.cpp +17 -0
  25. gisel_ab/cpp/wasm_query_runner.cpp +52 -0
  26. gisel_ab/cpp/x86_generated_ctor.cpp +24 -0
  27. gisel_ab/data/aarch64-contract.json +102 -0
  28. gisel_ab/data/baseline-2026-11-12.json +1684 -0
  29. gisel_ab/data/closure/aarch64-D.json +156 -0
  30. gisel_ab/data/closure/aarch64-G.json +75 -0
  31. gisel_ab/data/closure/arm-D.json +50 -0
  32. gisel_ab/data/closure/arm-G.json +57 -0
  33. gisel_ab/data/closure/m68k-D.json +46 -0
  34. gisel_ab/data/closure/m68k-G.json +54 -0
  35. gisel_ab/data/closure/mips-D.json +50 -0
  36. gisel_ab/data/closure/mips-G.json +87 -0
  37. gisel_ab/data/closure/ppc-D.json +49 -0
  38. gisel_ab/data/closure/ppc-G.json +56 -0
  39. gisel_ab/data/closure/riscv-D.json +59 -0
  40. gisel_ab/data/closure/riscv-G.json +66 -0
  41. gisel_ab/data/closure/spirv-D.json +58 -0
  42. gisel_ab/data/closure/spirv-G.json +58 -0
  43. gisel_ab/data/closure/wasm-D.json +56 -0
  44. gisel_ab/data/closure/wasm-G.json +62 -0
  45. gisel_ab/data/closure/x86-D.json +60 -0
  46. gisel_ab/data/closure/x86-G.json +66 -0
  47. gisel_ab/data/domain.json +139 -0
  48. gisel_ab/data/mir-configs.json +444 -0
  49. gisel_ab/data/mir-shapes.jsonc +1168 -0
  50. gisel_ab/data/riscv-contract.json +102 -0
  51. gisel_ab/data/suite/README.md +81 -0
  52. gisel_ab/data/suite/aarch64/official.answers.jsonl +12559 -0
  53. gisel_ab/data/suite/aarch64/queries.jsonl +12559 -0
  54. gisel_ab/data/suite/aarch64/suite.json +43 -0
  55. gisel_ab/data/suite/m68k/official.answers.jsonl +35 -0
  56. gisel_ab/data/suite/m68k/queries.jsonl +35 -0
  57. gisel_ab/data/suite/m68k/suite.json +24 -0
  58. gisel_ab/data/suite/riscv/official.answers.jsonl +6086 -0
  59. gisel_ab/data/suite/riscv/queries.jsonl +6086 -0
  60. gisel_ab/data/suite/riscv/suite.json +41 -0
  61. gisel_ab/data/suite/x86/official.answers.jsonl +3014 -0
  62. gisel_ab/data/suite/x86/queries.jsonl +3014 -0
  63. gisel_ab/data/suite/x86/suite.json +33 -0
  64. gisel_ab/data/targets/aarch64.json +39 -0
  65. gisel_ab/data/targets/arm.json +39 -0
  66. gisel_ab/data/targets/m68k.json +39 -0
  67. gisel_ab/data/targets/mips.json +39 -0
  68. gisel_ab/data/targets/ppc.json +39 -0
  69. gisel_ab/data/targets/riscv.json +39 -0
  70. gisel_ab/data/targets/schema.json +266 -0
  71. gisel_ab/data/targets/spirv.json +39 -0
  72. gisel_ab/data/targets/wasm.json +39 -0
  73. gisel_ab/data/targets/x86-ctor.json +39 -0
  74. gisel_ab/data/targets/x86.json +39 -0
  75. gisel_ab/data/x86-contract.json +93 -0
  76. gisel_ab/doctor.py +320 -0
  77. gisel_ab/domain_plan.py +130 -0
  78. gisel_ab/evidence.py +892 -0
  79. gisel_ab/l0/__init__.py +0 -0
  80. gisel_ab/l0/attribute.py +286 -0
  81. gisel_ab/l0/closure.py +255 -0
  82. gisel_ab/l0/diff.py +388 -0
  83. gisel_ab/l0/g_attribution.py +93 -0
  84. gisel_ab/l0/inject_opcode.py +123 -0
  85. gisel_ab/l0/inject_predicate.py +150 -0
  86. gisel_ab/l0/queries.py +215 -0
  87. gisel_ab/l0/run.py +688 -0
  88. gisel_ab/l0/swap.py +257 -0
  89. gisel_ab/l0/u_attribution.py +192 -0
  90. gisel_ab/l3.py +271 -0
  91. gisel_ab/mir/__init__.py +0 -0
  92. gisel_ab/mir/config_check.py +144 -0
  93. gisel_ab/mir/from_query.py +717 -0
  94. gisel_ab/mir/libs.py +252 -0
  95. gisel_ab/mir/replay.py +548 -0
  96. gisel_ab/paths.py +151 -0
  97. gisel_ab/refresh_counts.py +80 -0
  98. gisel_ab/replay_frozen.py +154 -0
  99. gisel_ab/setup.py +92 -0
  100. gisel_ab/suite.py +390 -0
  101. gisel_ab/targets.py +287 -0
  102. gisel_ab/vertical.py +654 -0
  103. gisel_ab-0.9.0.dist-info/METADATA +349 -0
  104. gisel_ab-0.9.0.dist-info/RECORD +107 -0
  105. gisel_ab-0.9.0.dist-info/WHEEL +5 -0
  106. gisel_ab-0.9.0.dist-info/entry_points.txt +2 -0
  107. gisel_ab-0.9.0.dist-info/top_level.txt +1 -0
gisel_ab/mir/libs.py ADDED
@@ -0,0 +1,252 @@
1
+ #!/usr/bin/env python3
2
+ """MIR-layer plumbing: snapshot CodeGen libraries, swap, and run ``llc``.
3
+
4
+ The MIR layer never rebuilds the whole tree. It
5
+
6
+ 1. snapshots the official ``libLLVM<T>CodeGen.so``,
7
+ 2. points the target's legalizer constructor at the generated ``.inc``
8
+ through ``swap-source-ctor.py`` and rebuilds only that shared library,
9
+ 3. snapshots the replaced library,
10
+ 4. restores the source and rebuilds the baseline library.
11
+
12
+ Replay then runs one ``bin/llc`` twice, selecting the library with
13
+ ``LD_LIBRARY_PATH`` (``llc`` uses ``DT_RUNPATH``, so the environment wins).
14
+ """
15
+
16
+ from __future__ import annotations
17
+
18
+ import fcntl
19
+ import hashlib
20
+ import json
21
+ import os
22
+ import re
23
+ import shutil
24
+ import subprocess
25
+ import sys
26
+ from pathlib import Path
27
+
28
+ ROOT = Path(__file__).resolve().parent
29
+ AB_ROOT = ROOT.parents[2]
30
+ SWAP = "gisel_ab.l0.swap"
31
+ INJECT = "gisel_ab.l0.inject_opcode"
32
+ NINJA = os.environ.get("NINJA", "ninja")
33
+ DEFAULT_GISEL = Path(os.environ.get(
34
+ "GISEL_SRC", AB_ROOT.parent / "gisel")).resolve()
35
+
36
+ # Derived from targets/*.json; never add a target table here again.
37
+ from gisel_ab import targets as target_registry # noqa: E402
38
+
39
+ _SPECS = {name: target_registry.load(name)
40
+ for name in target_registry.names()}
41
+ TEST_DIR = {name: doc["test_dir"] for name, doc in _SPECS.items()}
42
+ SWAPPABLE = tuple(name for name, doc in _SPECS.items()
43
+ if doc["mir"]["swappable"])
44
+
45
+
46
+ class MirError(RuntimeError):
47
+ pass
48
+
49
+
50
+ def sha256(path: Path) -> str:
51
+ h = hashlib.sha256()
52
+ with open(path, "rb") as handle:
53
+ for chunk in iter(lambda: handle.read(1 << 20), b""):
54
+ h.update(chunk)
55
+ return h.hexdigest()
56
+
57
+
58
+ def load_target(name: str) -> dict:
59
+ path = target_registry.TARGET_DIR / (name + ".json")
60
+ if not path.exists():
61
+ raise MirError("unknown target %r (no %s)" % (name, path))
62
+ return json.loads(path.read_text())
63
+
64
+
65
+ def codegen_lib(build: Path, spec: dict) -> Path:
66
+ return build / spec["library"]
67
+
68
+
69
+ def ninja_target(spec: dict) -> str:
70
+ if spec.get("ninja_target"):
71
+ return spec["ninja_target"]
72
+ name = Path(spec["library"]).name
73
+ match = re.match(r"(libLLVM.+CodeGen)\.so", name)
74
+ if not match:
75
+ raise MirError("cannot derive a ninja target from %s" % name)
76
+ return match.group(1) + ".so"
77
+
78
+
79
+ def run(cmd, cwd=None, env=None) -> subprocess.CompletedProcess:
80
+ proc = subprocess.run([str(part) for part in cmd], cwd=str(cwd) if cwd else None,
81
+ env=env, capture_output=True, text=True)
82
+ if proc.returncode:
83
+ raise MirError("command failed (%d): %s\n%s"
84
+ % (proc.returncode, " ".join(str(part) for part in cmd),
85
+ (proc.stderr or proc.stdout).strip()))
86
+ return proc
87
+
88
+
89
+ def ninja(build: Path, spec: dict):
90
+ run([NINJA, "-C", build, ninja_target(spec)])
91
+
92
+
93
+ def git_head(tree: Path) -> str:
94
+ return run(["git", "-C", tree, "rev-parse", "HEAD"]).stdout.strip()
95
+
96
+
97
+ def git_dirty_paths(tree: Path, paths) -> list[str]:
98
+ proc = run(["git", "-C", tree, "status", "--porcelain", "--", *paths])
99
+ return [line for line in proc.stdout.splitlines() if line.strip()]
100
+
101
+
102
+ def generate_inc(gisel: Path, spec: dict, out: Path) -> Path:
103
+ dest = out / spec["inc_name"]
104
+ dest.parent.mkdir(parents=True, exist_ok=True)
105
+ env = dict(os.environ)
106
+ env["PYTHONPATH"] = str(gisel) + (
107
+ os.pathsep + env["PYTHONPATH"] if env.get("PYTHONPATH") else "")
108
+ run([sys.executable, "-m", "gisel", "gen", gisel / spec["source"],
109
+ "--emit=inc", "--out", dest], cwd=gisel, env=env)
110
+ if not dest.exists():
111
+ raise MirError("gisel gen did not write %s" % dest)
112
+ return dest
113
+
114
+
115
+ def mutate_inc(inc: Path, opcode: str, out: Path) -> Path:
116
+ dest = out / ("mutant-" + inc.name)
117
+ run([sys.executable, "-m", INJECT, inc, opcode, dest])
118
+ return dest
119
+
120
+
121
+ def swap_apply(target: str, tree: Path, inc: Path):
122
+ if target not in SWAPPABLE:
123
+ raise MirError("target %r has no constructor swap yet" % target)
124
+ run([sys.executable, "-m", SWAP, "apply", "--target", target,
125
+ "--tree", tree, "--inc", inc])
126
+
127
+
128
+ def swap_restore(target: str, tree: Path):
129
+ if target not in SWAPPABLE:
130
+ raise MirError("target %r has no constructor swap yet" % target)
131
+ run([sys.executable, "-m", SWAP, "restore", "--target", target,
132
+ "--tree", tree])
133
+
134
+
135
+ def snapshot_lib(spec: dict, build: Path, dest: Path) -> tuple[Path, str]:
136
+ src = codegen_lib(build, spec)
137
+ if not src.exists():
138
+ raise MirError("library not found: %s" % src)
139
+ dest.mkdir(parents=True, exist_ok=True)
140
+ dst = dest / src.name
141
+ shutil.copyfile(src, dst)
142
+ return dst, sha256(dst)
143
+
144
+
145
+ def prepare_target(target: str, tree: Path, build: Path, out: Path,
146
+ gisel: Path = DEFAULT_GISEL,
147
+ mutate_opcode: str | None = None) -> dict:
148
+ """Snapshot official, build ours, snapshot ours, restore baseline.
149
+
150
+ The source file is byte-compared before and after, and the restore runs in
151
+ ``finally`` so an interrupted build cannot leave the tree patched.
152
+ """
153
+ spec = load_target(target)
154
+ source = tree / spec["touch_source"]
155
+ if not source.exists():
156
+ raise MirError("source not found: %s" % source)
157
+ dirty = git_dirty_paths(tree, [spec["touch_source"]])
158
+ if dirty:
159
+ raise MirError("tree is already dirty at %s; restore it first"
160
+ % spec["touch_source"])
161
+ source_sha = sha256(source)
162
+
163
+ official_dir = out / "official" / target
164
+ official_lib, official_sha = snapshot_lib(spec, build, official_dir)
165
+
166
+ inc = generate_inc(gisel, spec, out / "inc")
167
+ inc_sha = sha256(inc)
168
+ if mutate_opcode:
169
+ inc = mutate_inc(inc, mutate_opcode, out / "inc")
170
+ inc_sha = sha256(inc)
171
+
172
+ side = "ours-negative" if mutate_opcode else "ours"
173
+ ours_lib = None
174
+ ours_sha = None
175
+ try:
176
+ swap_apply(target, tree, inc)
177
+ ninja(build, spec)
178
+ ours_lib, ours_sha = snapshot_lib(spec, build, out / side / target)
179
+ finally:
180
+ swap_restore(target, tree)
181
+ ninja(build, spec)
182
+
183
+ source_after = sha256(source)
184
+ if source_after != source_sha:
185
+ raise MirError("restore did not reproduce %s (%s -> %s)"
186
+ % (source, source_sha[:12], source_after[:12]))
187
+ lib_after = sha256(codegen_lib(build, spec))
188
+
189
+ return {
190
+ "adapter": spec["name"],
191
+ # The target file may describe the L0 runner strategy; the MIR layer
192
+ # records the constructor swap it actually used.
193
+ "strategy": spec.get("mir_strategy", spec["strategy"]),
194
+ "inc": str(inc),
195
+ "inc_sha256": inc_sha,
196
+ "mutate_opcode": mutate_opcode,
197
+ "official_lib": str(official_lib),
198
+ "official_lib_sha256": official_sha,
199
+ "ours_lib": str(ours_lib),
200
+ "ours_lib_sha256": ours_sha,
201
+ "source": str(source),
202
+ "source_sha256": source_sha,
203
+ "baseline_lib_after_restore_sha256": lib_after,
204
+ "baseline_lib_restored_exactly": lib_after == official_sha,
205
+ }
206
+
207
+
208
+ class Lock:
209
+ """One writer per output directory; flock is released on process exit."""
210
+
211
+ def __init__(self, path: Path):
212
+ self.path = path
213
+ self.handle = None
214
+
215
+ def __enter__(self):
216
+ self.path.parent.mkdir(parents=True, exist_ok=True)
217
+ self.handle = open(self.path, "w")
218
+ try:
219
+ fcntl.flock(self.handle, fcntl.LOCK_EX | fcntl.LOCK_NB)
220
+ except BlockingIOError:
221
+ self.handle.close()
222
+ raise MirError("another run holds %s" % self.path)
223
+ self.handle.write(str(os.getpid()))
224
+ self.handle.flush()
225
+ return self
226
+
227
+ def __exit__(self, *exc):
228
+ fcntl.flock(self.handle, fcntl.LOCK_UN)
229
+ self.handle.close()
230
+ return False
231
+
232
+
233
+ def run_llc(llc: Path, lib_dir: Path, args, timeout: float = 60.0,
234
+ cwd: Path | None = None) -> dict:
235
+ env = dict(os.environ)
236
+ prefix = str(lib_dir)
237
+ if env.get("LD_LIBRARY_PATH"):
238
+ prefix += os.pathsep + env["LD_LIBRARY_PATH"]
239
+ env["LD_LIBRARY_PATH"] = prefix
240
+ try:
241
+ proc = subprocess.run([str(llc), *[str(a) for a in args]],
242
+ cwd=str(cwd) if cwd else None, env=env,
243
+ capture_output=True, text=True, timeout=timeout)
244
+ except subprocess.TimeoutExpired as exc:
245
+ def text(value):
246
+ if value is None:
247
+ return ""
248
+ return value.decode(errors="replace") if isinstance(value, bytes) else value
249
+ return {"rc": None, "timeout": True,
250
+ "out": text(exc.stdout), "err": text(exc.stderr)}
251
+ return {"rc": proc.returncode, "timeout": False,
252
+ "out": proc.stdout, "err": proc.stderr}