Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit cd5b9c6

Browse files
committed
[update_cc_test_checks] Support 'clang | opt | FileCheck'
Some clang lit tests use a pipeline of the form // RUN: %clang [args] -O0 %s | opt [specific optimizations] | FileCheck %s to make the expected test output depend on as few optimization phases as possible, for stability. But when you write a RUN line of this form, you lose the ability to use update_cc_test_checks.py to automatically generate the expected output, because it only supports two-stage pipelines consisting of '%clang | FileCheck' (or %clang_cc1). This change extends the set of supported RUN lines so that pipelines with an invocation of `opt` in the middle can still be automatically handled. To implement it, I've adjusted `get_function_body()` so that it can cope with an arbitrary sequence of intermediate pipeline commands. But the code that decides which RUN lines to consider is more conservative: it only adds clang | opt | FileCheck to the set of supported lines, because I didn't want to accidentally include some other kind of line that doesn't output IR at all. (Also in this commit is the minimal change to make this script work at all, after r373912 added an extra parameter to `add_ir_checks`.) Reviewers: MaskRay, xbolva00 Subscribers: llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D68406 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@374287 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 45cd9c2 commit cd5b9c6

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

utils/update_cc_test_checks.py

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ def config():
9494
help='Space-separated extra args to clang, e.g. --clang-args=-v')
9595
parser.add_argument('--c-index-test',
9696
help='"c-index-test" executable, defaults to $llvm_bin/c-index-test')
97+
parser.add_argument('--opt',
98+
help='"opt" executable, defaults to $llvm_bin/opt')
9799
parser.add_argument(
98100
'--functions', nargs='+', help='A list of function name regexes. '
99101
'If specified, update CHECK lines for functions matching at least one regex')
@@ -114,6 +116,18 @@ def config():
114116
if not distutils.spawn.find_executable(args.clang):
115117
print('Please specify --llvm-bin or --clang', file=sys.stderr)
116118
sys.exit(1)
119+
120+
if args.opt is None:
121+
if args.llvm_bin is None:
122+
args.opt = 'opt'
123+
else:
124+
args.opt = os.path.join(args.llvm_bin, 'opt')
125+
if not distutils.spawn.find_executable(args.opt):
126+
# Many uses of this tool will not need an opt binary, because it's only
127+
# needed for updating a test that runs clang | opt | FileCheck. So we
128+
# defer this error message until we find that opt is actually needed.
129+
args.opt = None
130+
117131
if args.c_index_test is None:
118132
if args.llvm_bin is None:
119133
args.c_index_test = 'c-index-test'
@@ -126,10 +140,23 @@ def config():
126140
return args
127141

128142

129-
def get_function_body(args, filename, clang_args, prefixes, triple_in_cmd, func_dict):
143+
def get_function_body(args, filename, clang_args, extra_commands, prefixes, triple_in_cmd, func_dict):
130144
# TODO Clean up duplication of asm/common build_function_body_dictionary
131145
# Invoke external tool and extract function bodies.
132146
raw_tool_output = common.invoke_tool(args.clang, clang_args, filename)
147+
for extra_command in extra_commands:
148+
extra_args = shlex.split(extra_command)
149+
with tempfile.NamedTemporaryFile() as f:
150+
f.write(raw_tool_output.encode())
151+
f.flush()
152+
if extra_args[0] == 'opt':
153+
if args.opt is None:
154+
print(filename, 'needs to run opt. '
155+
'Please specify --llvm-bin or --opt', file=sys.stderr)
156+
sys.exit(1)
157+
extra_args[0] = args.opt
158+
raw_tool_output = common.invoke_tool(extra_args[0],
159+
extra_args[1:], f.name)
133160
if '-emit-llvm' in clang_args:
134161
common.build_function_body_dictionary(
135162
common.OPT_FUNCTION_RE, common.scrub_body, [],
@@ -178,7 +205,7 @@ def main():
178205
run_list = []
179206
line2spell_and_mangled_list = collections.defaultdict(list)
180207
for l in run_lines:
181-
commands = [cmd.strip() for cmd in l.split('|', 1)]
208+
commands = [cmd.strip() for cmd in l.split('|')]
182209

183210
triple_in_cmd = None
184211
m = common.TRIPLE_ARG_RE.search(commands[0])
@@ -193,6 +220,11 @@ def main():
193220
clang_args[0:1] = SUBST[clang_args[0]]
194221
clang_args = [filename if i == '%s' else i for i in clang_args] + args.clang_args
195222

223+
# Permit piping the output through opt
224+
if not (len(commands) == 2 or
225+
(len(commands) == 3 and commands[1].startswith('opt'))):
226+
print('WARNING: Skipping non-clang RUN line: ' + l, file=sys.stderr)
227+
196228
# Extract -check-prefix in FileCheck args
197229
filecheck_cmd = commands[-1]
198230
common.verify_filecheck_prefixes(filecheck_cmd)
@@ -203,7 +235,7 @@ def main():
203235
for item in m.group(1).split(',')]
204236
if not check_prefixes:
205237
check_prefixes = ['CHECK']
206-
run_list.append((check_prefixes, clang_args, triple_in_cmd))
238+
run_list.append((check_prefixes, clang_args, commands[1:-1], triple_in_cmd))
207239

208240
# Strip CHECK lines which are in `prefix_set`, update test file.
209241
prefix_set = set([prefix for p in run_list for prefix in p[0]])
@@ -223,12 +255,12 @@ def main():
223255
prefixes = p[0]
224256
for prefix in prefixes:
225257
func_dict.update({prefix: dict()})
226-
for prefixes, clang_args, triple_in_cmd in run_list:
258+
for prefixes, clang_args, extra_commands, triple_in_cmd in run_list:
227259
if args.verbose:
228260
print('Extracted clang cmd: clang {}'.format(clang_args), file=sys.stderr)
229261
print('Extracted FileCheck prefixes: {}'.format(prefixes), file=sys.stderr)
230262

231-
get_function_body(args, filename, clang_args, prefixes, triple_in_cmd, func_dict)
263+
get_function_body(args, filename, clang_args, extra_commands, prefixes, triple_in_cmd, func_dict)
232264

233265
# Invoke c-index-test to get mapping from start lines to mangled names.
234266
# Forward all clang args for now.
@@ -254,7 +286,7 @@ def main():
254286
if added:
255287
output_lines.append('//')
256288
added.add(mangled)
257-
common.add_ir_checks(output_lines, '//', run_list, func_dict, mangled)
289+
common.add_ir_checks(output_lines, '//', run_list, func_dict, mangled, False)
258290
output_lines.append(line.rstrip('\n'))
259291

260292
# Update the test file.

0 commit comments

Comments
 (0)