Skip to content

Commit

Permalink
[UpdateTestChecks] Update tests option
Browse files Browse the repository at this point in the history
Summary:
Port of new feature introduced https://reviews.llvm.org/D65610 to other update scripts.

- update_*_checks.py: add an alias -u for --update-only
- port --update-only to other update_*_test_checks.py scripts
- update script aborts if the test file was generated by another update_*_test_checks.py utility

Reviewers: lebedev.ri, RKSimon, MaskRay, reames, gbedwell

Reviewed By: MaskRay

Subscribers: llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D65793

llvm-svn: 368174
  • Loading branch information
davidbolvansky committed Aug 7, 2019
1 parent 762bc33 commit 7169ea3
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 57 deletions.
23 changes: 16 additions & 7 deletions llvm/utils/UpdateTestChecks/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ def invoke_tool(exe, cmd_args, ir):
SCRUB_LOOP_COMMENT_RE = re.compile(
r'# =>This Inner Loop Header:.*|# in Loop:.*', flags=re.M)


def error(msg, test_file=None):
if test_file:
msg = '{}: {}'.format(msg, test_file)
print('ERROR: {}'.format(msg), file=sys.stderr)

def warn(msg, test_file=None):
if test_file:
msg = '{}: {}'.format(msg, test_file)
print('WARNING: {}'.format(msg), file=sys.stderr)

def scrub_body(body):
# Scrub runs of whitespace out of the assembly, but leave the leading
# whitespace in place.
Expand Down Expand Up @@ -108,7 +119,7 @@ def build_function_body_dictionary(function_re, scrubber, scrubber_args, raw_too
if 'analysis' in m.groupdict():
analysis = m.group('analysis')
if analysis.lower() != 'cost model analysis':
print('WARNING: Unsupported analysis mode: %r!' % (analysis,), file=sys.stderr)
warn('Unsupported analysis mode: %r!' % (analysis,))
if func.startswith('stress'):
# We only use the last line of the function body for stress tests.
scrubbed_body = '\n'.join(scrubbed_body.splitlines()[-1:])
Expand All @@ -123,8 +134,7 @@ def build_function_body_dictionary(function_re, scrubber, scrubber_args, raw_too
continue
else:
if prefix == prefixes[-1]:
print('WARNING: Found conflicting asm under the '
'same prefix: %r!' % (prefix,), file=sys.stderr)
warn('Found conflicting asm under the same prefix: %r!' % (prefix,))
else:
func_dict[prefix][func] = None
continue
Expand Down Expand Up @@ -272,8 +282,8 @@ def check_prefix(prefix):
hint = ""
if ',' in prefix:
hint = " Did you mean '--check-prefixes=" + prefix + "'?"
print(("WARNING: Supplied prefix '%s' is invalid. Prefix must contain only alphanumeric characters, hyphens and underscores." + hint) %
(prefix), file=sys.stderr)
warn(("Supplied prefix '%s' is invalid. Prefix must contain only alphanumeric characters, hyphens and underscores." + hint) %
(prefix))


def verify_filecheck_prefixes(fc_cmd):
Expand All @@ -287,5 +297,4 @@ def verify_filecheck_prefixes(fc_cmd):
for prefix in prefixes:
check_prefix(prefix)
if prefixes.count(prefix) > 1:
print("WARNING: Supplied prefix '%s' is not unique in the prefix list." %
(prefix,), file=sys.stderr)
warn("Supplied prefix '%s' is not unique in the prefix list." % (prefix,))
25 changes: 21 additions & 4 deletions llvm/utils/update_analyze_test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,17 @@ def main():
help='The opt binary used to generate the test case')
parser.add_argument(
'--function', help='The function in the test file to update')
parser.add_argument('-u', '--update-only', action='store_true',
help='Only update test if it was already autogened')
parser.add_argument('tests', nargs='+')
args = parser.parse_args()

autogenerated_note = (ADVERT + 'utils/' + os.path.basename(__file__))
script_name = os.path.basename(__file__)
autogenerated_note = (ADVERT + 'utils/' + script_name)

opt_basename = os.path.basename(args.opt_binary)
if (opt_basename != "opt"):
print('ERROR: Unexpected opt name: ' + opt_basename, file=sys.stderr)
common.error('Unexpected opt name: ' + opt_basename)
sys.exit(1)

test_paths = [test for pattern in args.tests for test in glob.glob(pattern)]
Expand All @@ -75,6 +78,16 @@ def main():
with open(test) as f:
input_lines = [l.rstrip() for l in f]

first_line = input_lines[0] if input_lines else ""
if 'autogenerated' in first_line and script_name not in first_line:
common.warn("Skipping test which wasn't autogenerated by " + script_name + ": " + test)
continue

if args.update_only:
if not first_line or 'autogenerated' not in first_line:
common.warn("Skipping test which isn't autogenerated: " + test)
continue

raw_lines = [m.group(1)
for m in [common.RUN_LINE_RE.match(l) for l in input_lines] if m]
run_lines = [raw_lines[0]] if len(raw_lines) > 0 else []
Expand All @@ -91,15 +104,19 @@ def main():

prefix_list = []
for l in run_lines:
if '|' not in l:
common.warn('Skipping unparseable RUN line: ' + l)
continue

(tool_cmd, filecheck_cmd) = tuple([cmd.strip() for cmd in l.split('|', 1)])
common.verify_filecheck_prefixes(filecheck_cmd)

if not tool_cmd.startswith(opt_basename + ' '):
print('WARNING: Skipping non-%s RUN line: %s' % (opt_basename, l), file=sys.stderr)
common.warn('WSkipping non-%s RUN line: %s' % (opt_basename, l))
continue

if not filecheck_cmd.startswith('FileCheck '):
print('WARNING: Skipping non-FileChecked RUN line: ' + l, file=sys.stderr)
common.warn('Skipping non-FileChecked RUN line: ' + l)
continue

tool_cmd_args = tool_cmd[len(opt_basename):].strip()
Expand Down
15 changes: 14 additions & 1 deletion llvm/utils/update_cc_test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ def config():
parser.add_argument(
'--x86_extra_scrub', action='store_true',
help='Use more regex for x86 matching to reduce diffs between various subtargets')
parser.add_argument('-u', '--update-only', action='store_true',
help='Only update test if it was already autogened')
parser.add_argument('tests', nargs='+')
args = parser.parse_args()
args.clang_args = shlex.split(args.clang_args or '')
Expand Down Expand Up @@ -126,11 +128,22 @@ def get_function_body(args, filename, clang_args, prefixes, triple_in_cmd, func_

def main():
args = config()
autogenerated_note = (ADVERT + 'utils/' + os.path.basename(__file__))
script_name = os.path.basename(__file__)
autogenerated_note = (ADVERT + 'utils/' + script_name)

for filename in args.tests:
with open(filename) as f:
input_lines = [l.rstrip() for l in f]

first_line = input_lines[0] if input_lines else ""
if 'autogenerated' in first_line and script_name not in first_line:
common.warn("Skipping test which wasn't autogenerated by " + script_name, filename)
continue

if args.update_only:
if not first_line or 'autogenerated' not in first_line:
common.warn("Skipping test which isn't autogenerated: " + filename)
continue

# Extract RUN lines.
raw_lines = [m.group(1)
Expand Down
23 changes: 20 additions & 3 deletions llvm/utils/update_llc_test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,30 @@ def main():
help='Use more regex for x86 matching to reduce diffs between various subtargets')
parser.add_argument(
'--no_x86_scrub_rip', action='store_false', dest='x86_scrub_rip')
parser.add_argument('-u', '--update-only', action='store_true',
help='Only update test if it was already autogened')
parser.add_argument('tests', nargs='+')
args = parser.parse_args()

autogenerated_note = (ADVERT + 'utils/' + os.path.basename(__file__))
script_name = os.path.basename(__file__)
autogenerated_note = (ADVERT + 'utils/' + script_name)

test_paths = [test for pattern in args.tests for test in glob.glob(pattern)]
for test in test_paths:
if args.verbose:
print('Scanning for RUN lines in test file: %s' % (test,), file=sys.stderr)
with open(test) as f:
input_lines = [l.rstrip() for l in f]

first_line = input_lines[0] if input_lines else ""
if 'autogenerated' in first_line and script_name not in first_line:
common.warn("Skipping test which wasn't autogenerated by " + script_name, test)
continue

if args.update_only:
if not first_line or 'autogenerated' not in first_line:
common.warn("Skipping test which isn't autogenerated: " + test)
continue

triple_in_ir = None
for l in input_lines:
Expand All @@ -73,6 +86,10 @@ def main():

run_list = []
for l in run_lines:
if '|' not in l:
common.warn('Skipping unparseable RUN line: ' + l)
continue

commands = [cmd.strip() for cmd in l.split('|', 1)]
llc_cmd = commands[0]

Expand All @@ -91,11 +108,11 @@ def main():
filecheck_cmd = commands[1]
common.verify_filecheck_prefixes(filecheck_cmd)
if not llc_cmd.startswith('llc '):
print('WARNING: Skipping non-llc RUN line: ' + l, file=sys.stderr)
common.warn('Skipping non-llc RUN line: ' + l)
continue

if not filecheck_cmd.startswith('FileCheck '):
print('WARNING: Skipping non-FileChecked RUN line: ' + l, file=sys.stderr)
common.warn('Skipping non-FileChecked RUN line: ' + l)
continue

llc_cmd_args = llc_cmd[len('llc'):].strip()
Expand Down
76 changes: 43 additions & 33 deletions llvm/utils/update_mir_test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,6 @@ def log(msg, verbose=True):
print(msg, file=sys.stderr)


def warn(msg, test_file=None):
if test_file:
msg = '{}: {}'.format(test_file, msg)
print('WARNING: {}'.format(msg), file=sys.stderr)


def find_triple_in_ir(lines, verbose=False):
for l in lines:
m = common.TRIPLE_IR_RE.match(l)
Expand Down Expand Up @@ -119,16 +113,20 @@ def build_run_list(test, run_lines, verbose=False):
run_list = []
all_prefixes = []
for l in run_lines:
if '|' not in l:
common.warn('Skipping unparseable RUN line: ' + l)
continue

commands = [cmd.strip() for cmd in l.split('|', 1)]
llc_cmd = commands[0]
filecheck_cmd = commands[1] if len(commands) > 1 else ''
common.verify_filecheck_prefixes(filecheck_cmd)

if not llc_cmd.startswith('llc '):
warn('Skipping non-llc RUN line: {}'.format(l), test_file=test)
common.warn('Skipping non-llc RUN line: {}'.format(l), test_file=test)
continue
if not filecheck_cmd.startswith('FileCheck '):
warn('Skipping non-FileChecked RUN line: {}'.format(l),
common.warn('Skipping non-FileChecked RUN line: {}'.format(l),
test_file=test)
continue

Expand Down Expand Up @@ -193,7 +191,7 @@ def build_function_body_dictionary(test, raw_tool_output, triple, prefixes,
log(' {}'.format(l))
for prefix in prefixes:
if func in func_dict[prefix] and func_dict[prefix][func] != body:
warn('Found conflicting asm for prefix: {}'.format(prefix),
common.warn('Found conflicting asm for prefix: {}'.format(prefix),
test_file=test)
func_dict[prefix][func] = body

Expand Down Expand Up @@ -225,7 +223,7 @@ def add_check_lines(test, output_lines, prefix, func_name, single_bb,
func_body.pop(0)

if not func_body:
warn('Function has no instructions to check: {}'.format(func_name),
common.warn('Function has no instructions to check: {}'.format(func_name),
test_file=test)
return

Expand Down Expand Up @@ -294,49 +292,60 @@ def should_add_line_to_output(input_line, prefix_set):
return True


def update_test_file(llc, test, remove_common_prefixes=False, verbose=False):
log('Scanning for RUN lines in test file: {}'.format(test), verbose)
def update_test_file(args, test):
log('Scanning for RUN lines in test file: {}'.format(test), args.verbose)
with open(test) as fd:
input_lines = [l.rstrip() for l in fd]

triple_in_ir = find_triple_in_ir(input_lines, verbose)
run_lines = find_run_lines(test, input_lines, verbose)
run_list, common_prefixes = build_run_list(test, run_lines, verbose)
script_name = os.path.basename(__file__)
first_line = input_lines[0] if input_lines else ""
if 'autogenerated' in first_line and script_name not in first_line:
common.warn("Skipping test which wasn't autogenerated by " +
script_name + ": " + test)
return

if args.update_only:
if not first_line or 'autogenerated' not in first_line:
common.warn("Skipping test which isn't autogenerated: " + test)
return

triple_in_ir = find_triple_in_ir(input_lines, args.verbose)
run_lines = find_run_lines(test, input_lines, args.verbose)
run_list, common_prefixes = build_run_list(test, run_lines, args.verbose)

simple_functions = find_functions_with_one_bb(input_lines, verbose)
simple_functions = find_functions_with_one_bb(input_lines, args.verbose)

func_dict = {}
for run in run_list:
for prefix in run.prefixes:
func_dict.update({prefix: dict()})
for prefixes, llc_args, triple_in_cmd in run_list:
log('Extracted LLC cmd: llc {}'.format(llc_args), verbose)
log('Extracted FileCheck prefixes: {}'.format(prefixes), verbose)
log('Extracted LLC cmd: llc {}'.format(llc_args), args.verbose)
log('Extracted FileCheck prefixes: {}'.format(prefixes), args.verbose)

raw_tool_output = llc(llc_args, test)
raw_tool_output = args.llc(llc_args, test)
if not triple_in_cmd and not triple_in_ir:
warn('No triple found: skipping file', test_file=test)
common.warn('No triple found: skipping file', test_file=test)
return

build_function_body_dictionary(test, raw_tool_output,
triple_in_cmd or triple_in_ir,
prefixes, func_dict, verbose)
prefixes, func_dict, args.verbose)

state = 'toplevel'
func_name = None
prefix_set = set([prefix for run in run_list for prefix in run.prefixes])
log('Rewriting FileCheck prefixes: {}'.format(prefix_set), verbose)
log('Rewriting FileCheck prefixes: {}'.format(prefix_set), args.verbose)

if remove_common_prefixes:
if args.remove_common_prefixes:
prefix_set.update(common_prefixes)
elif common_prefixes:
warn('Ignoring common prefixes: {}'.format(common_prefixes),
common.warn('Ignoring common prefixes: {}'.format(common_prefixes),
test_file=test)

comment_char = '#' if test.endswith('.mir') else ';'
autogenerated_note = ('{} NOTE: Assertions have been autogenerated by '
'utils/{}'.format(comment_char,
os.path.basename(__file__)))
'utils/{}'.format(comment_char, script_name))
output_lines = []
output_lines.append(autogenerated_note)

Expand Down Expand Up @@ -374,14 +383,14 @@ def update_test_file(llc, test, remove_common_prefixes=False, verbose=False):
state = 'mir function body'
add_checks_for_function(test, output_lines, run_list,
func_dict, func_name, single_bb=False,
verbose=verbose)
verbose=args.verbose)
elif state == 'mir function prefix':
m = MIR_PREFIX_DATA_RE.match(input_line)
if not m:
state = 'mir function body'
add_checks_for_function(test, output_lines, run_list,
func_dict, func_name, single_bb=True,
verbose=verbose)
verbose=args.verbose)

if should_add_line_to_output(input_line, prefix_set):
output_lines.append(input_line)
Expand All @@ -397,7 +406,7 @@ def update_test_file(llc, test, remove_common_prefixes=False, verbose=False):
state = 'ir function body'
add_checks_for_function(test, output_lines, run_list,
func_dict, func_name, single_bb=False,
verbose=verbose)
verbose=args.verbose)

if should_add_line_to_output(input_line, prefix_set):
output_lines.append(input_line)
Expand All @@ -409,7 +418,7 @@ def update_test_file(llc, test, remove_common_prefixes=False, verbose=False):
output_lines.append(input_line)


log('Writing {} lines to {}...'.format(len(output_lines), test), verbose)
log('Writing {} lines to {}...'.format(len(output_lines), test), args.verbose)

with open(test, 'wb') as fd:
fd.writelines(['{}\n'.format(l).encode('utf-8') for l in output_lines])
Expand All @@ -425,16 +434,17 @@ def main():
parser.add_argument('--remove-common-prefixes', action='store_true',
help='Remove existing check lines whose prefixes are '
'shared between multiple commands')
parser.add_argument('-u', '--update-only', action='store_true',
help='Only update test if it was already autogened')
parser.add_argument('tests', nargs='+')
args = parser.parse_args()

test_paths = [test for pattern in args.tests for test in glob.glob(pattern)]
for test in test_paths:
try:
update_test_file(args.llc, test, args.remove_common_prefixes,
verbose=args.verbose)
update_test_file(args, test)
except Exception:
warn('Error processing file', test_file=test)
common.warn('Error processing file', test_file=test)
raise


Expand Down
Loading

0 comments on commit 7169ea3

Please sign in to comment.