|
| 1 | +# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 2 | +# See https://llvm.org/LICENSE.txt for license information. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | +"""Extract IR for training. |
| 5 | +
|
| 6 | +Extract IR for training, either from a compile_commands.json file produced by |
| 7 | +cmake, or a linker parameter list file. |
| 8 | +
|
| 9 | +Only run with |
| 10 | +'python compiler_opt/tools/extract_ir.py ...' |
| 11 | +
|
| 12 | +The compilation is assumed to have been performed with clang, using |
| 13 | +-fembed-bitcode=all passed to cc1 (i.e. pass clang -Xclang=-fembed-bitcode=all) |
| 14 | +
|
| 15 | +In a distributed ThinLTO case, the compilation is assumed to have been performed |
| 16 | +specifying -mllvm -lto-embed-bitcode=post-merge-pre-opt. |
| 17 | +
|
| 18 | +In a local ThinLTO case, the compilation is assumedto have been performed |
| 19 | +specifying -Wl,--save-temps=import -Wl,--thinlto-emit-index-files |
| 20 | +
|
| 21 | +To change the logging verbosity, pass an integer representing the desired |
| 22 | +verbosity to the --verbosity flag. Use 0 for all logs, status information, |
| 23 | +and detailed debug information, -1 for solely warnings, and -2 to not produce |
| 24 | +any output. |
| 25 | +""" |
| 26 | + |
| 27 | +import json |
| 28 | +import multiprocessing |
| 29 | + |
| 30 | +from absl import app |
| 31 | +from absl import flags |
| 32 | +from absl import logging |
| 33 | + |
| 34 | +from mlgo.corpus import extract_ir_lib |
| 35 | + |
| 36 | +flags.DEFINE_string( |
| 37 | + "input", |
| 38 | + None, |
| 39 | + "Input file or directory - either compile_commands.json, a linker parameter" |
| 40 | + "list, or a path to a directory containing object files.", |
| 41 | +) |
| 42 | +flags.DEFINE_enum( |
| 43 | + "input_type", |
| 44 | + "json", |
| 45 | + ["json", "params", "directory"], |
| 46 | + "Input file type - json, params, or directory. params latter refers to lld" |
| 47 | + "params.", |
| 48 | +) |
| 49 | +flags.DEFINE_string("output_dir", None, "Output directory") |
| 50 | +flags.DEFINE_integer( |
| 51 | + "num_workers", |
| 52 | + None, |
| 53 | + "Number of parallel workers for objcopy. `None` for maximum available.", |
| 54 | +) |
| 55 | +flags.DEFINE_string("llvm_objcopy_path", "llvm-objcopy", "Path to llvm-objcopy") |
| 56 | +flags.DEFINE_string( |
| 57 | + "obj_base_dir", |
| 58 | + "", |
| 59 | + "Base directory for object files. Defaults to current working dir.", |
| 60 | +) |
| 61 | +flags.DEFINE_string( |
| 62 | + "cmd_filter", |
| 63 | + None, |
| 64 | + "Include only those modules with a command line matching this regexp. " |
| 65 | + "Setting it to None for not filtering. Note that the regexp is applied " |
| 66 | + "independently for each separate command line option. For example, ^-Oz$ " |
| 67 | + "will match Oz - built binaries. Does not work with thinlto_build=lld.", |
| 68 | +) |
| 69 | +flags.DEFINE_enum( |
| 70 | + "thinlto_build", |
| 71 | + None, |
| 72 | + ["distributed", "local"], |
| 73 | + "Set if the build was performed with either 'distributed' or " |
| 74 | + "'local' ThinLTO. This ensures the thinlto.bc files are also copied. " |
| 75 | + "The build is assumed to have had " |
| 76 | + "-mllvm -lto-embed-bitcode=post-merge-pre-opt passed in the distributed " |
| 77 | + "case, or -Wl,--save-temps=import and -Wl,--thinlto-emit-index-files " |
| 78 | + "passed in the local case.", |
| 79 | +) |
| 80 | +flags.DEFINE_string( |
| 81 | + "cmd_section_name", |
| 82 | + ".llvmcmd", |
| 83 | + "The section name passed to llvm-objcopy. For ELF object files, the " |
| 84 | + "default .llvmcmd is correct. For Mach-O object files, one should use " |
| 85 | + "something like __LLVM,__cmdline", |
| 86 | +) |
| 87 | +flags.DEFINE_string( |
| 88 | + "bitcode_section_name", |
| 89 | + ".llvmbc", |
| 90 | + "The section name passed to llvm-objcopy. For ELF object files, the " |
| 91 | + "default .llvmbc is correct. For Mach-O object files, one should use " |
| 92 | + "__LLVM,__bitcode", |
| 93 | +) |
| 94 | + |
| 95 | +flags.mark_flag_as_required("output_dir") |
| 96 | + |
| 97 | +FLAGS = flags.FLAGS |
| 98 | + |
| 99 | + |
| 100 | +def main(argv): |
| 101 | + if len(argv) > 1: |
| 102 | + raise app.UsageError("Too many command-line arguments.") |
| 103 | + |
| 104 | + objs = [] |
| 105 | + if FLAGS.input is not None and FLAGS.thinlto_build == "local": |
| 106 | + raise ValueError("--thinlto_build=local cannot be run with --input") |
| 107 | + if FLAGS.input is None: |
| 108 | + if FLAGS.thinlto_build != "local": |
| 109 | + raise ValueError("--input or --thinlto_build=local must be provided") |
| 110 | + objs = extract_ir_lib.load_for_lld_thinlto(FLAGS.obj_base_dir, FLAGS.output_dir) |
| 111 | + elif FLAGS.input_type == "json": |
| 112 | + with open(FLAGS.input, encoding="utf-8") as f: |
| 113 | + objs = extract_ir_lib.load_from_compile_commands( |
| 114 | + json.load(f), FLAGS.output_dir |
| 115 | + ) |
| 116 | + elif FLAGS.input_type == "params": |
| 117 | + if not FLAGS.obj_base_dir: |
| 118 | + logging.info( |
| 119 | + "-obj_base_dir is unspecified, assuming current directory." |
| 120 | + "If no objects are found, use this option to specify the root" |
| 121 | + "directory for the object file paths in the input file." |
| 122 | + ) |
| 123 | + with open(FLAGS.input, encoding="utf-8") as f: |
| 124 | + objs = extract_ir_lib.load_from_lld_params( |
| 125 | + [l.strip() for l in f.readlines()], FLAGS.obj_base_dir, FLAGS.output_dir |
| 126 | + ) |
| 127 | + elif FLAGS.input_type == "directory": |
| 128 | + logging.warning( |
| 129 | + "Using the directory input is only recommended if the build system" |
| 130 | + "your project uses does not support any structured output that" |
| 131 | + "ml-compiler-opt understands. If your build system provides a" |
| 132 | + "structured compilation database, use that instead" |
| 133 | + ) |
| 134 | + objs = extract_ir_lib.load_from_directory(FLAGS.input, FLAGS.output_dir) |
| 135 | + else: |
| 136 | + logging.error("Unknown input type: %s", FLAGS.input_type) |
| 137 | + |
| 138 | + relative_output_paths = extract_ir_lib.run_extraction( |
| 139 | + objs, |
| 140 | + FLAGS.num_workers, |
| 141 | + FLAGS.llvm_objcopy_path, |
| 142 | + FLAGS.cmd_filter, |
| 143 | + FLAGS.thinlto_build, |
| 144 | + FLAGS.cmd_section_name, |
| 145 | + FLAGS.bitcode_section_name, |
| 146 | + ) |
| 147 | + |
| 148 | + extract_ir_lib.write_corpus_manifest( |
| 149 | + FLAGS.thinlto_build, relative_output_paths, FLAGS.output_dir |
| 150 | + ) |
| 151 | + |
| 152 | + logging.info( |
| 153 | + "Converted %d files out of %d", |
| 154 | + len(objs) - relative_output_paths.count(None), |
| 155 | + len(objs), |
| 156 | + ) |
| 157 | + |
| 158 | + |
| 159 | +def entrypoint(): |
| 160 | + multiprocessing.set_start_method("fork") |
| 161 | + app.run(main) |
| 162 | + |
| 163 | + |
| 164 | +if __name__ == "__main__": |
| 165 | + entrypoint() |
0 commit comments