llvm-project
50 строк · 1.5 Кб
1#!/usr/bin/env python
2
3# This script extracts the VPlan digraphs from the vectoriser debug messages
4# and saves them in individual dot files (one for each plan). Optionally, and
5# providing 'dot' is installed, it can also render the dot into a PNG file.
6
7from __future__ import print_function
8
9import sys
10import re
11import argparse
12import shutil
13import subprocess
14
15parser = argparse.ArgumentParser()
16parser.add_argument("--png", action="store_true")
17args = parser.parse_args()
18
19dot = shutil.which("dot")
20if args.png and not dot:
21raise RuntimeError("Can't export to PNG without 'dot' in the system")
22
23pattern = re.compile(r"(digraph VPlan {.*?\n})", re.DOTALL)
24matches = re.findall(pattern, sys.stdin.read())
25
26for vplan in matches:
27m = re.search("graph \[.+(VF=.+,UF.+)", vplan)
28if not m:
29raise ValueError("Can't get the right VPlan name")
30name = re.sub("[^a-zA-Z0-9]", "", m.group(1))
31
32if args.png:
33filename = "VPlan" + name + ".png"
34print("Exporting " + name + " to PNG via dot: " + filename)
35p = subprocess.Popen(
36[dot, "-Tpng", "-o", filename],
37encoding="utf-8",
38stdin=subprocess.PIPE,
39stdout=subprocess.PIPE,
40stderr=subprocess.PIPE,
41)
42out, err = p.communicate(input=vplan)
43if err:
44raise RuntimeError("Error running dot: " + err)
45
46else:
47filename = "VPlan" + name + ".dot"
48print("Exporting " + name + " to DOT: " + filename)
49with open(filename, "w") as out:
50out.write(vplan)
51