Skip to content

Commit 88ed39b

Browse files
Add directory input to extract_ir.py
This patch adds in a directory input to extract_ir.py where the script does a glob for *.o files inside the specified directory. This is useful in cases where it is easy to specify specific flags through a build system (like the necessary -Xclang -fembed-bitcode=all), but the build system produces no manifest of compilation commands like CMake's compile_commands.json. This isn't reccomended to use in place of a proper manifest but is necessary for supporting build systems like Make.
1 parent 6294cf9 commit 88ed39b

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

compiler_opt/tools/extract_ir.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@
4141

4242
flags.DEFINE_string(
4343
'input', None,
44-
'Input file - either compile_commands.json or a linker parameter list')
44+
'Input file or directory - either compile_commands.json, a linker parameter'
45+
'list, or a path to a directory containing object files.')
4546
flags.DEFINE_enum(
46-
'input_type', 'json', ['json', 'params'],
47-
'Input file type - json or params. The latter refers to lld params.')
47+
'input_type', 'json', ['json', 'params', 'directory'],
48+
'Input file type - json, params, or directory. params latter refers to lld'
49+
'params.')
4850
flags.DEFINE_string('output_dir', None, 'Output directory')
4951
flags.DEFINE_integer(
5052
'num_workers', None,
@@ -110,6 +112,13 @@ def main(argv):
110112
objs = extract_ir_lib.load_from_lld_params(
111113
[l.strip() for l in f.readlines()], FLAGS.obj_base_dir,
112114
FLAGS.output_dir)
115+
elif FLAGS.input_type == 'directory':
116+
logging.warning(
117+
'Using the directory input is only reccomended if the build system'
118+
'your project uses does not support any structured output that'
119+
'ml-compiler-opt understands. If your build system provides a'
120+
'structured compilation database, use that instead')
121+
objs = extract_ir_lib.load_from_directory(FLAGS.input, FLAGS.output_dir)
113122
else:
114123
logging.error('Unknown input type: %s', FLAGS.input_type)
115124

compiler_opt/tools/extract_ir_lib.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,19 @@ def make_obj(obj_file: str) -> TrainingIRExtractor:
250250
return [make_obj(obj_file) for obj_file in just_obj_paths]
251251

252252

253+
def load_from_directory(obj_base_dir: str,
254+
output_dir: str) -> List[TrainingIRExtractor]:
255+
paths = [str(p) for p in pathlib.Path(obj_base_dir).glob('**/*.o')]
256+
257+
def make_spec(obj_file: str):
258+
return TrainingIRExtractor(
259+
obj_relative_path=os.path.relpath(obj_file, start=obj_base_dir),
260+
output_base_dir=output_dir,
261+
obj_base_dir=obj_base_dir)
262+
263+
return [make_spec(path) for path in paths]
264+
265+
253266
def load_for_lld_thinlto(obj_base_dir: str,
254267
output_dir: str) -> List[TrainingIRExtractor]:
255268
# .3.import.bc is the suffix attached to post-merge-pre-opt ('postimport')

compiler_opt/tools/extract_ir_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,21 @@ def test_lld_params(self):
120120
'/tmp/out/lib/obj1.o.thinlto.bc')
121121
self.assertEqual(obj[1].input_obj(), '/some/path/lib/dir/obj2.o')
122122

123+
def test_load_from_directory(self):
124+
tempdir = self.create_tempdir()
125+
subdir = tempdir.mkdir(dir_path='subdir')
126+
subdir.create_file(file_path='test1.o')
127+
subdir.create_file(file_path='test2.o')
128+
outdir = self.create_tempdir()
129+
objs = extract_ir_lib.load_from_directory(tempdir.full_path,
130+
outdir.full_path)
131+
self.assertLen(objs, 2)
132+
for index, obj in enumerate(
133+
sorted(objs, key=lambda x: x._obj_relative_path)):
134+
self.assertEqual(obj._obj_relative_path, f'subdir/test{index + 1:d}.o')
135+
self.assertEqual(obj._obj_base_dir, tempdir.full_path)
136+
self.assertEqual(obj._output_base_dir, outdir.full_path)
137+
123138
def test_lld_thinlto_discovery(self):
124139
tempdir = self.create_tempdir()
125140
tempdir.create_file(file_path='1.3.import.bc')

0 commit comments

Comments
 (0)