Instantly share code, notes, and snippets.

Embed
What would you like to do?
View LLVM optimizer pass diffs, inspired by John Regehr's blog post https://blog.regehr.org/archives/1603
import sys
import subprocess
import tempfile
if len(sys.argv) < 2:
print(f"Usage: {sys.argv[0]} <llvm args...>")
sys.exit(1)
script = sys.argv[1:]
result = subprocess.run(
['opt', '-print-before-all', '-print-after-all'] + script,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
)
def run_diff(name, before, after):
before = b'\n'.join(b.rstrip() for b in before).strip()
after = b'\n'.join(a.rstrip() for a in after).strip()
with tempfile.NamedTemporaryFile() as b, tempfile.NamedTemporaryFile() as a:
b.write(before)
a.write(after)
b.flush()
a.flush()
result = subprocess.run(['icdiff', '-W', b.name, a.name], stdout=subprocess.PIPE)
if result.stdout:
print('\n\n\n')
print(f"Step: {name}")
if result.stdout:
sys.stdout.buffer.write(result.stdout)
input("\n\nPress enter to continue:")
lines = result.stderr.split(b'\n')
name = None
before = []
after = []
mode = None
for line in lines:
if line.startswith(b'***'):
if mode is None or b'IR Dump After' in mode:
if mode is not None:
run_diff(name, before, after)
name = line.decode('utf8').strip('* ')
before = []
else:
after = []
mode = line
continue
if b'IR Dump Before' in mode:
before.append(line)
else:
after.append(line)
run_diff(name, before, after)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment