Last active
September 7, 2018 13:11
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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