Scott Todd | 10460ea | 2021-07-16 12:11:06 -0700 | [diff] [blame] | 1 | #!/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 | |
| 10 | Example 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 | |
| 29 | import argparse |
| 30 | import re |
| 31 | |
| 32 | MLIR_START_SEQUENCE = "// -----//" |
| 33 | MLIR_END_SEQUENCE = "//----- //" |
| 34 | |
| 35 | |
| 36 | def 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 | |
| 59 | def 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 | |
| 102 | if __name__ == '__main__': |
| 103 | main(parse_arguments()) |