blob: 952797c9ba0abff44b3e3d180c56ef1ba83b3f57 [file] [log] [blame]
Scott Todd10460ea2021-07-16 12:11:06 -07001#!/usr/bin/env python3
2
3# Copyright 2021 The IREE Authors
4#
5# Licensed under the Apache License v2.0 with LLVM Exceptions.
6# See https://llvm.org/LICENSE.txt for license information.
7# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8"""Converts a dump of IR text to a markdown file with formatting.
9
10Example usage:
11 # Get a dump of IR from a compiler tool:
12 $ iree-opt \
13 -iree-transformation-pipeline \
14 -iree-hal-target-backends=vmvx \
15 -mlir-disable-threading \
16 -print-ir-after-all \
17 -print-ir-after-change \
18 -mlir-elide-elementsattrs-if-larger=8 \
19 $PWD/iree/samples/models/simple_abs.mlir \
20 2> /tmp/simple_abs_vmvx_pipeline.mlir
21 > /dev/null
22
23 # Convert the IR dump to markdown:
24 $ python3 ir_to_markdown.py \
25 /tmp/simple_abs_vmvx_pipeline.mlir \
26 -o /tmp/simple_abs_vmvx_pipeline.md
27"""
28
29import argparse
30import re
31
32MLIR_START_SEQUENCE = "// -----//"
33MLIR_END_SEQUENCE = "//----- //"
34
35
36def parse_arguments():
37 """Parses command line arguments."""
38
39 parser = argparse.ArgumentParser()
40 parser.add_argument('input_file_path',
41 type=str,
42 nargs='?',
43 metavar="<input_file_path>",
44 help='Input IR dump (.mlir from -print-ir-after-all)')
45 parser.add_argument('-o,',
46 '--output',
47 type=str,
48 required=True,
49 metavar="<output>",
50 help='Output file path (e.g. translation_ir.md)')
51 # TODO(scotttodd): flags for original IR path and compilation command line
52 # .md could then show original IR + flags -> output
53 # TODO(scotttodd): flag for markdown flavor (mkdocs, github, etc.)
54 # TODO(scotttodd): flag for diff view (correlate IR before and IR after)?
55
56 return parser.parse_args()
57
58
59def main(args):
60 input_file_path = args.input_file_path
61 output_file_path = args.output
62 print("Converting input file '%s'" % (input_file_path))
63 print(" into output file '%s'" % (output_file_path))
64
65 with open(input_file_path, "r") as input_file:
66 with open(output_file_path, "w") as output_file:
67
68 # Iterate line by line through the input file, collecting text into
69 # blocks and writing them into the output file with markdown formatting
70 # as we go.
71 #
72 # Note: we could parse through and find/replace within the file using
73 # regex (or sed), but iterating this way is easier to understand and
74 # uses a predictable amount of memory.
75
76 current_block_lines = []
77 dump_after_regex = re.compile(MLIR_START_SEQUENCE + "\s(.*)\s" +
78 MLIR_END_SEQUENCE)
79
80 def finish_block():
81 nonlocal current_block_lines
82 if len(current_block_lines) != 0:
83 current_block_lines.append("```\n\n")
84 output_file.writelines(current_block_lines)
85 current_block_lines = []
86
87 for input_line in input_file:
88 if input_line == "\n":
89 continue
90
91 if input_line.startswith(MLIR_START_SEQUENCE):
92 finish_block()
93 header_text = dump_after_regex.match(input_line).group(1)
94 current_block_lines.append("### " + header_text + "\n\n")
95 current_block_lines.append("```mlir\n")
96 else:
97 current_block_lines.append(input_line)
98
99 finish_block()
100
101
102if __name__ == '__main__':
103 main(parse_arguments())