Skip to content

Commit 44f5ae3

Browse files
authored
[utils][UpdateTestChecks] Extract MIR functionality into separate mir.py module (#165535)
This commit extracts some MIR-related code from `common.py` and `update_mir_test_checks.py` into a dedicated `mir.py` module to improve code organization. This is a preparation step for #164965 and also moves some pieces already moved by #140296 All code intentionally moved verbatim with minimal necessary adaptations: * `log()` calls converted to `print(..., file=sys.stderr)` at `mir.py` lines 62, 64 due to a `log` locality.
1 parent 67db5fd commit 44f5ae3

File tree

4 files changed

+367
-357
lines changed

4 files changed

+367
-357
lines changed

llvm/utils/UpdateTestChecks/common.py

Lines changed: 0 additions & 238 deletions
Original file line numberDiff line numberDiff line change
@@ -2396,244 +2396,6 @@ def add_analyze_checks(
23962396
)
23972397

23982398

2399-
IR_FUNC_NAME_RE = re.compile(
2400-
r"^\s*define\s+(?:internal\s+)?[^@]*@(?P<func>[A-Za-z0-9_.]+)\s*\("
2401-
)
2402-
IR_PREFIX_DATA_RE = re.compile(r"^ *(;|$)")
2403-
MIR_FUNC_NAME_RE = re.compile(r" *name: *(?P<func>[A-Za-z0-9_.-]+)")
2404-
MIR_BODY_BEGIN_RE = re.compile(r" *body: *\|")
2405-
MIR_BASIC_BLOCK_RE = re.compile(r" *bb\.[0-9]+.*:$")
2406-
MIR_PREFIX_DATA_RE = re.compile(r"^ *(;|bb.[0-9].*: *$|[a-z]+:( |$)|$)")
2407-
2408-
2409-
def find_mir_functions_with_one_bb(lines, verbose=False):
2410-
result = []
2411-
cur_func = None
2412-
bbs = 0
2413-
for line in lines:
2414-
m = MIR_FUNC_NAME_RE.match(line)
2415-
if m:
2416-
if bbs == 1:
2417-
result.append(cur_func)
2418-
cur_func = m.group("func")
2419-
bbs = 0
2420-
m = MIR_BASIC_BLOCK_RE.match(line)
2421-
if m:
2422-
bbs += 1
2423-
if bbs == 1:
2424-
result.append(cur_func)
2425-
return result
2426-
2427-
2428-
def add_mir_checks_for_function(
2429-
test,
2430-
output_lines,
2431-
run_list,
2432-
func_dict,
2433-
func_name,
2434-
single_bb,
2435-
print_fixed_stack,
2436-
first_check_is_next,
2437-
at_the_function_name,
2438-
):
2439-
printed_prefixes = set()
2440-
for run in run_list:
2441-
for prefix in run[0]:
2442-
if prefix in printed_prefixes:
2443-
break
2444-
if not func_dict[prefix][func_name]:
2445-
continue
2446-
if printed_prefixes:
2447-
# Add some space between different check prefixes.
2448-
indent = len(output_lines[-1]) - len(output_lines[-1].lstrip(" "))
2449-
output_lines.append(" " * indent + ";")
2450-
printed_prefixes.add(prefix)
2451-
add_mir_check_lines(
2452-
test,
2453-
output_lines,
2454-
prefix,
2455-
("@" if at_the_function_name else "") + func_name,
2456-
single_bb,
2457-
func_dict[prefix][func_name],
2458-
print_fixed_stack,
2459-
first_check_is_next,
2460-
)
2461-
break
2462-
else:
2463-
warn(
2464-
"Found conflicting asm for function: {}".format(func_name),
2465-
test_file=test,
2466-
)
2467-
return output_lines
2468-
2469-
2470-
def add_mir_check_lines(
2471-
test,
2472-
output_lines,
2473-
prefix,
2474-
func_name,
2475-
single_bb,
2476-
func_info,
2477-
print_fixed_stack,
2478-
first_check_is_next,
2479-
):
2480-
func_body = str(func_info).splitlines()
2481-
if single_bb:
2482-
# Don't bother checking the basic block label for a single BB
2483-
func_body.pop(0)
2484-
2485-
if not func_body:
2486-
warn(
2487-
"Function has no instructions to check: {}".format(func_name),
2488-
test_file=test,
2489-
)
2490-
return
2491-
2492-
first_line = func_body[0]
2493-
indent = len(first_line) - len(first_line.lstrip(" "))
2494-
# A check comment, indented the appropriate amount
2495-
check = "{:>{}}; {}".format("", indent, prefix)
2496-
2497-
output_lines.append("{}-LABEL: name: {}".format(check, func_name))
2498-
2499-
if print_fixed_stack:
2500-
output_lines.append("{}: fixedStack:".format(check))
2501-
for stack_line in func_info.extrascrub.splitlines():
2502-
filecheck_directive = check + "-NEXT"
2503-
output_lines.append("{}: {}".format(filecheck_directive, stack_line))
2504-
2505-
first_check = not first_check_is_next
2506-
for func_line in func_body:
2507-
if not func_line.strip():
2508-
# The mir printer prints leading whitespace so we can't use CHECK-EMPTY:
2509-
output_lines.append(check + "-NEXT: {{" + func_line + "$}}")
2510-
continue
2511-
filecheck_directive = check if first_check else check + "-NEXT"
2512-
first_check = False
2513-
check_line = "{}: {}".format(filecheck_directive, func_line[indent:]).rstrip()
2514-
output_lines.append(check_line)
2515-
2516-
2517-
def should_add_mir_line_to_output(input_line, prefix_set):
2518-
# Skip any check lines that we're handling as well as comments
2519-
m = CHECK_RE.match(input_line)
2520-
if (m and m.group(1) in prefix_set) or input_line.strip() == ";":
2521-
return False
2522-
return True
2523-
2524-
2525-
def add_mir_checks(
2526-
input_lines,
2527-
prefix_set,
2528-
autogenerated_note,
2529-
test,
2530-
run_list,
2531-
func_dict,
2532-
print_fixed_stack,
2533-
first_check_is_next,
2534-
at_the_function_name,
2535-
):
2536-
simple_functions = find_mir_functions_with_one_bb(input_lines)
2537-
2538-
output_lines = []
2539-
output_lines.append(autogenerated_note)
2540-
2541-
func_name = None
2542-
state = "toplevel"
2543-
for input_line in input_lines:
2544-
if input_line == autogenerated_note:
2545-
continue
2546-
2547-
if state == "toplevel":
2548-
m = IR_FUNC_NAME_RE.match(input_line)
2549-
if m:
2550-
state = "ir function prefix"
2551-
func_name = m.group("func")
2552-
if input_line.rstrip("| \r\n") == "---":
2553-
state = "document"
2554-
output_lines.append(input_line)
2555-
elif state == "document":
2556-
m = MIR_FUNC_NAME_RE.match(input_line)
2557-
if m:
2558-
state = "mir function metadata"
2559-
func_name = m.group("func")
2560-
if input_line.strip() == "...":
2561-
state = "toplevel"
2562-
func_name = None
2563-
if should_add_mir_line_to_output(input_line, prefix_set):
2564-
output_lines.append(input_line)
2565-
elif state == "mir function metadata":
2566-
if should_add_mir_line_to_output(input_line, prefix_set):
2567-
output_lines.append(input_line)
2568-
m = MIR_BODY_BEGIN_RE.match(input_line)
2569-
if m:
2570-
if func_name in simple_functions:
2571-
# If there's only one block, put the checks inside it
2572-
state = "mir function prefix"
2573-
continue
2574-
state = "mir function body"
2575-
add_mir_checks_for_function(
2576-
test,
2577-
output_lines,
2578-
run_list,
2579-
func_dict,
2580-
func_name,
2581-
single_bb=False,
2582-
print_fixed_stack=print_fixed_stack,
2583-
first_check_is_next=first_check_is_next,
2584-
at_the_function_name=at_the_function_name,
2585-
)
2586-
elif state == "mir function prefix":
2587-
m = MIR_PREFIX_DATA_RE.match(input_line)
2588-
if not m:
2589-
state = "mir function body"
2590-
add_mir_checks_for_function(
2591-
test,
2592-
output_lines,
2593-
run_list,
2594-
func_dict,
2595-
func_name,
2596-
single_bb=True,
2597-
print_fixed_stack=print_fixed_stack,
2598-
first_check_is_next=first_check_is_next,
2599-
at_the_function_name=at_the_function_name,
2600-
)
2601-
2602-
if should_add_mir_line_to_output(input_line, prefix_set):
2603-
output_lines.append(input_line)
2604-
elif state == "mir function body":
2605-
if input_line.strip() == "...":
2606-
state = "toplevel"
2607-
func_name = None
2608-
if should_add_mir_line_to_output(input_line, prefix_set):
2609-
output_lines.append(input_line)
2610-
elif state == "ir function prefix":
2611-
m = IR_PREFIX_DATA_RE.match(input_line)
2612-
if not m:
2613-
state = "ir function body"
2614-
add_mir_checks_for_function(
2615-
test,
2616-
output_lines,
2617-
run_list,
2618-
func_dict,
2619-
func_name,
2620-
single_bb=False,
2621-
print_fixed_stack=print_fixed_stack,
2622-
first_check_is_next=first_check_is_next,
2623-
at_the_function_name=at_the_function_name,
2624-
)
2625-
2626-
if should_add_mir_line_to_output(input_line, prefix_set):
2627-
output_lines.append(input_line)
2628-
elif state == "ir function body":
2629-
if input_line.strip() == "}":
2630-
state = "toplevel"
2631-
func_name = None
2632-
if should_add_mir_line_to_output(input_line, prefix_set):
2633-
output_lines.append(input_line)
2634-
return output_lines
2635-
2636-
26372399
def build_global_values_dictionary(glob_val_dict, raw_tool_output, prefixes, ginfo):
26382400
for nameless_value in ginfo.get_nameless_values():
26392401
if nameless_value.global_ir_rhs_regexp is None:

0 commit comments

Comments
 (0)