Skip to content

Commit 09b41a8

Browse files
authored
Refactors "to be deleted" and "to be added" compilation flags and enables training with AFDO profile reingestion.
1 parent bd76ae4 commit 09b41a8

File tree

7 files changed

+61
-25
lines changed

7 files changed

+61
-25
lines changed

compiler_opt/rl/compilation_runner.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,12 @@ def _overwrite_trajectory_reward(sequence_example: tf.train.SequenceExample,
7575
return sequence_example
7676

7777

78-
def get_command_line_for_bundle(cmd_file: str,
79-
ir_file: str,
80-
thinlto: Optional[str] = None) -> List[str]:
78+
def get_command_line_for_bundle(
79+
cmd_file: str,
80+
ir_file: str,
81+
thinlto: Optional[str] = None,
82+
additional_flags: Tuple[str, ...] = (),
83+
delete_flags: Tuple[str, ...] = ()) -> List[str]:
8184
"""Cleans up base command line.
8285
8386
Remove certain unnecessary flags, and add the .bc file to compile and, if
@@ -87,21 +90,19 @@ def get_command_line_for_bundle(cmd_file: str,
8790
cmd_file: Path to a .cmd file (from corpus).
8891
ir_file: The path to the ir file to compile.
8992
thinlto: The path to the thinlto index, or None.
93+
additional_flags: Tuple of clang flags to add.
94+
delete_flags: Tuple of clang flags to remove.
9095
9196
Returns:
9297
The argument list to pass to the compiler process.
9398
"""
9499
cmdline = []
95-
flags_to_remove = [
96-
'-split-dwarf-file', '-split-dwarf-output', '-fthinlto-index',
97-
'-fprofile-sample-use'
98-
]
99100

100101
with open(cmd_file, encoding='utf-8') as f:
101102
option_iterator = iter(f.read().split('\0'))
102103
option = next(option_iterator, None)
103104
while option:
104-
if any(option.startswith(flag) for flag in flags_to_remove):
105+
if any([option.startswith(flag) for flag in delete_flags]):
105106
if '=' not in option:
106107
next(option_iterator, None)
107108
else:
@@ -110,6 +111,7 @@ def get_command_line_for_bundle(cmd_file: str,
110111
cmdline.extend(['-x', 'ir', ir_file])
111112
if thinlto:
112113
cmdline.append('-fthinlto-index=' + thinlto)
114+
cmdline.extend(additional_flags)
113115
return cmdline
114116

115117

@@ -281,17 +283,23 @@ def _get_pool():
281283
def __init__(self,
282284
clang_path: Optional[str] = None,
283285
launcher_path: Optional[str] = None,
284-
moving_average_decay_rate: float = 1):
286+
moving_average_decay_rate: float = 1,
287+
additional_flags: Tuple[str, ...] = (),
288+
delete_flags: Tuple[str, ...] = ()):
285289
"""Initialization of CompilationRunner class.
286290
287291
Args:
288292
clang_path: path to the clang binary.
289293
launcher_path: path to the launcher binary.
290294
moving_average_decay_rate: moving average decay rate during training.
295+
additional_flags: tuple of clang flags to add.
296+
delete_flags: tuple of clang flags to remove.
291297
"""
292298
self._clang_path = clang_path
293299
self._launcher_path = launcher_path
294300
self._moving_average_decay_rate = moving_average_decay_rate
301+
self._additional_flags = additional_flags
302+
self._delete_flags = delete_flags
295303
self._compilation_timeout = _COMPILATION_TIMEOUT.value
296304

297305
def _get_cancellation_manager(

compiler_opt/rl/compilation_runner_test.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,18 +224,24 @@ def test_command_line_file(self):
224224
])
225225

226226
def test_command_line_correction(self):
227+
delete_compilation_flags = ('-split-dwarf-file', '-split-dwarf-output',
228+
'-fthinlto-index', '-fprofile-sample-use',
229+
'-fprofile-remapping-file')
227230
data = [
228231
'-cc1', '-fthinlto-index=bad', '-split-dwarf-file', '/tmp/foo.dwo',
229232
'-split-dwarf-output', 'somepath/some.dwo'
230233
]
231234
argfile = self.create_tempfile(content='\0'.join(data))
232235
self.assertEqual(
233-
compilation_runner.get_command_line_for_bundle(argfile.full_path,
234-
'hi.bc'),
236+
compilation_runner.get_command_line_for_bundle(
237+
argfile.full_path, 'hi.bc', delete_flags=delete_compilation_flags),
235238
['-cc1', '-x', 'ir', 'hi.bc'])
236239
self.assertEqual(
237-
compilation_runner.get_command_line_for_bundle(argfile.full_path,
238-
'hi.bc', 'index.bc'),
240+
compilation_runner.get_command_line_for_bundle(
241+
argfile.full_path,
242+
'hi.bc',
243+
'index.bc',
244+
delete_flags=delete_compilation_flags),
239245
['-cc1', '-x', 'ir', 'hi.bc', '-fthinlto-index=index.bc'])
240246

241247
def test_start_subprocess_output(self):

compiler_opt/rl/inlining/gin_configs/ppo_nn_agent.gin

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,17 @@ train_eval.deploy_policy_name='saved_collect_policy'
1919
train_eval.use_random_network_distillation=False
2020
train_eval.moving_average_decay_rate=0.8
2121

22-
#######################################
23-
# Turn on if using train_with_rpc.py
24-
# train_eval.additional_compilation_flags=()
25-
#######################################
22+
# TODO(b/233935329): The following clang flags need to be tied to a corpus
23+
# rather than to a training tool invocation.
24+
25+
# List of flags to add to clang compilation command. The flag names should
26+
# match the actual flags provided to clang. An example for AFDO reinjection:
27+
# train_eval.flags_to_add=('-fprofile-sample-use=/path/to/gwp.afdo','-fprofile-remapping-file=/path/to/prof_remap.txt')
28+
train_eval.additional_compilation_flags=()
29+
30+
# List of flags to remove from clang compilation command. The flag names
31+
# should match the actual flags provided to clang.'
32+
train_eval.delete_compilation_flags=('-split-dwarf-file','-split-dwarf-output','-fthinlto-index','-fprofile-sample-use','-fprofile-remapping-file')
2633

2734
# RandomNetworkDistillation configs, off if train_eval.use_random_network_distillation=False.
2835
RandomNetworkDistillation.encoding_network = @encoding_network.EncodingNetwork

compiler_opt/rl/inlining/inlining_runner.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ def _compile_fn(
8585
command_line.append(self._launcher_path)
8686
command_line.extend([self._clang_path] +
8787
compilation_runner.get_command_line_for_bundle(
88-
cmd_path, input_ir_path) + [
88+
cmd_path,
89+
input_ir_path,
90+
additional_flags=self._additional_flags,
91+
delete_flags=self._delete_flags) + [
8992
'-mllvm', '-enable-ml-inliner=development',
9093
'-mllvm', '-training-log=' +
9194
log_path, '-o', output_native_path

compiler_opt/rl/regalloc/gin_configs/ppo_nn_agent.gin

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,17 @@ train_eval.deploy_policy_name='saved_collect_policy'
2020
train_eval.moving_average_decay_rate=0.8
2121
train_eval.use_random_network_distillation=False
2222

23-
#######################################
24-
# Turn on if using train_with_rpc.py
25-
# train_eval.additional_compilation_flags=()
26-
#######################################
23+
# TODO(b/233935329): The following clang flags need to be tied to a corpus
24+
# rather than to a training tool invocation.
25+
26+
# List of flags to add to clang compilation command. The flag names should
27+
# match the actual flags provided to clang. An example for AFDO reinjection:
28+
# train_eval.additional_flags=('-fprofile-sample-use=/path/to/gwp.afdo','-fprofile-remapping-file=/path/to/prof_remap.txt')
29+
train_eval.additional_compilation_flags=()
30+
31+
# List of flags to remove from clang compilation command. The flag names
32+
# should match the actual flags provided to clang.'
33+
train_eval.delete_compilation_flags=('-split-dwarf-file','-split-dwarf-output','-fthinlto-index','-fprofile-sample-use','-fprofile-remapping-file')
2734

2835
# RandomNetworkDistillation configs, off if train_eval.use_random_network_distillation=False.
2936
RandomNetworkDistillation.encoding_network = @regalloc_network.RegAllocRNDEncodingNetwork

compiler_opt/rl/regalloc/regalloc_runner.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ def _compile_fn(
8989
command_line.append(self._launcher_path)
9090
command_line.extend([self._clang_path] +
9191
compilation_runner.get_command_line_for_bundle(
92-
cmd_path, input_ir_path, thinlto_index_path) + [
92+
cmd_path, input_ir_path, thinlto_index_path,
93+
self._additional_flags, self._delete_flags) + [
9394
'-mllvm', '-thinlto-assume-merged', '-mllvm',
9495
'-regalloc-enable-advisor=development',
9596
'-mllvm', '-regalloc-training-log=' +

compiler_opt/rl/train_locally.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ def train_eval(agent_name=constant.AgentName.PPO,
6767
train_sequence_length=1,
6868
deploy_policy_name='saved_policy',
6969
use_random_network_distillation=False,
70-
moving_average_decay_rate=1):
70+
moving_average_decay_rate=1,
71+
additional_compilation_flags=(),
72+
delete_compilation_flags=()):
7173
"""Train for LLVM inliner."""
7274
root_dir = FLAGS.root_dir
7375
problem_config = registry.get_configuration()
@@ -112,7 +114,9 @@ def train_eval(agent_name=constant.AgentName.PPO,
112114
for path in module_paths]
113115

114116
runner = problem_config.get_runner_type()(
115-
moving_average_decay_rate=moving_average_decay_rate)
117+
moving_average_decay_rate=moving_average_decay_rate,
118+
additional_flags=additional_compilation_flags,
119+
delete_flags=delete_compilation_flags)
116120

117121
dataset_fn = data_reader.create_sequence_example_dataset_fn(
118122
agent_name=agent_name,

0 commit comments

Comments
 (0)