Skip to content

Instantly share code, notes, and snippets.

@porglezomp
Last active September 7, 2018 13:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save porglezomp/f2dc233f971cf3f30d45e0b501ae5ead to your computer and use it in GitHub Desktop.
Save porglezomp/f2dc233f971cf3f30d45e0b501ae5ead to your computer and use it in GitHub Desktop.
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