Skip to content

Commit 3959b3b

Browse files
committed
[Utils][NFC] Clean up update_mc_test_checks.py.
Refine the code a bit to make it easier to comprehend the logic.
1 parent 6f52ae6 commit 3959b3b

File tree

1 file changed

+20
-36
lines changed

1 file changed

+20
-36
lines changed

llvm/utils/update_mc_test_checks.py

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,6 @@ def update_test(ti: common.TestInfo):
223223
testlines = list(dict.fromkeys(testlines))
224224
common.debug("Valid test line found: ", len(testlines))
225225

226-
run_list_size = len(run_list)
227-
testnum = len(testlines)
228-
229226
raw_output = []
230227
raw_prefixes = []
231228
for (
@@ -267,60 +264,47 @@ def update_test(ti: common.TestInfo):
267264
prefix_set = set([prefix for p in run_list for prefix in p[0]])
268265
common.debug("Rewriting FileCheck prefixes:", str(prefix_set))
269266

270-
for test_id in range(testnum):
271-
input_line = testlines[test_id]
272-
267+
for test_id, input_line in enumerate(testlines):
273268
# a {prefix : output, [runid] } dict
274269
# insert output to a prefix-key dict, and do a max sorting
275270
# to select the most-used prefix which share the same output string
276271
p_dict = {}
277-
for run_id in range(run_list_size):
272+
for run_id in range(len(run_list)):
278273
out = raw_output[run_id][test_id]
279274

280275
if hasErr(out):
281276
o = getErrString(out)
282277
else:
283278
o = getOutputString(out)
284279

285-
prefixes = raw_prefixes[run_id]
286-
287-
for p in prefixes:
280+
for p in raw_prefixes[run_id]:
288281
if p not in p_dict:
289282
p_dict[p] = o, [run_id]
290-
else:
291-
if p_dict[p] == (None, []):
292-
continue
283+
continue
293284

294-
prev_o, run_ids = p_dict[p]
295-
if o == prev_o:
296-
run_ids.append(run_id)
297-
p_dict[p] = o, run_ids
298-
else:
299-
# conflict, discard
300-
p_dict[p] = None, []
285+
if p_dict[p] == (None, []):
286+
continue
301287

302-
p_dict_sorted = dict(sorted(p_dict.items(), key=lambda item: -len(item[1][1])))
288+
prev_o, run_ids = p_dict[p]
289+
if o == prev_o:
290+
run_ids.append(run_id)
291+
p_dict[p] = o, run_ids
292+
else:
293+
# conflict, discard
294+
p_dict[p] = None, []
303295

304296
# prefix is selected and generated with most shared output lines
305297
# each run_id can only be used once
306-
used_runid = set()
307-
298+
used_run_ids = set()
308299
selected_prefixes = set()
309-
for prefix, tup in p_dict_sorted.items():
310-
o, run_ids = tup
311-
312-
if len(run_ids) == 0:
313-
continue
314-
315-
skip = False
316-
for i in run_ids:
317-
if i in used_runid:
318-
skip = True
319-
else:
320-
used_runid.add(i)
321-
if not skip:
300+
get_num_runs = lambda item: len(item[1][1])
301+
p_dict_sorted = sorted(p_dict.items(), key=get_num_runs, reverse=True)
302+
for prefix, (o, run_ids) in p_dict_sorted:
303+
if run_ids and used_run_ids.isdisjoint(run_ids):
322304
selected_prefixes.add(prefix)
323305

306+
used_run_ids.update(run_ids)
307+
324308
# Generate check lines in alphabetical order.
325309
check_lines = []
326310
for prefix in sorted(selected_prefixes):

0 commit comments

Comments
 (0)