Skip to content

Commit 5315f3f

Browse files
Handle leading underscores in update_cc_test_checks.py (#121800)
For some ABIs `update_cc_test_checks.py` is unable to generate tests because of the mismatch between the mangled function names reported by clang's `-asd-dump` and the function names in LLVM IR. This patch fixes it by striping the leading underscore from the mangled name for global functions if the data layout string says they have one.
1 parent 795e35a commit 5315f3f

File tree

4 files changed

+55
-8
lines changed

4 files changed

+55
-8
lines changed

clang/test/utils/update_cc_test_checks/Inputs/c-symbol-mangling.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
// UTC_ARGS: --enable
1919

2020
#ifdef __arm__
21-
/// FIXME: UTC does not find this function, but can find all others.
2221
typedef __attribute__((neon_vector_type(8))) __INT8_TYPE__ int8x8_t;
2322
int8x8_t test_vaba_s8(int8x8_t a, int8x8_t b, int8x8_t c) {
2423
return a + b + c;

clang/test/utils/update_cc_test_checks/Inputs/c-symbol-mangling.c.expected

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,22 @@
1818
// UTC_ARGS: --enable
1919

2020
#ifdef __arm__
21-
/// FIXME: UTC does not find this function, but can find all others.
2221
typedef __attribute__((neon_vector_type(8))) __INT8_TYPE__ int8x8_t;
22+
// THUMB-DARWIN-LABEL: @test_vaba_s8(
23+
// THUMB-DARWIN-NEXT: entry:
24+
// THUMB-DARWIN-NEXT: [[A_ADDR:%.*]] = alloca <8 x i8>, align 8
25+
// THUMB-DARWIN-NEXT: [[B_ADDR:%.*]] = alloca <8 x i8>, align 8
26+
// THUMB-DARWIN-NEXT: [[C_ADDR:%.*]] = alloca <8 x i8>, align 8
27+
// THUMB-DARWIN-NEXT: store <8 x i8> [[A:%.*]], ptr [[A_ADDR]], align 8
28+
// THUMB-DARWIN-NEXT: store <8 x i8> [[B:%.*]], ptr [[B_ADDR]], align 8
29+
// THUMB-DARWIN-NEXT: store <8 x i8> [[C:%.*]], ptr [[C_ADDR]], align 8
30+
// THUMB-DARWIN-NEXT: [[TMP0:%.*]] = load <8 x i8>, ptr [[A_ADDR]], align 8
31+
// THUMB-DARWIN-NEXT: [[TMP1:%.*]] = load <8 x i8>, ptr [[B_ADDR]], align 8
32+
// THUMB-DARWIN-NEXT: [[ADD:%.*]] = add <8 x i8> [[TMP0]], [[TMP1]]
33+
// THUMB-DARWIN-NEXT: [[TMP2:%.*]] = load <8 x i8>, ptr [[C_ADDR]], align 8
34+
// THUMB-DARWIN-NEXT: [[ADD1:%.*]] = add <8 x i8> [[ADD]], [[TMP2]]
35+
// THUMB-DARWIN-NEXT: ret <8 x i8> [[ADD1]]
36+
//
2337
int8x8_t test_vaba_s8(int8x8_t a, int8x8_t b, int8x8_t c) {
2438
return a + b + c;
2539
}

llvm/utils/UpdateTestChecks/common.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,10 @@ def invoke_tool(exe, cmd_args, ir, preprocess_cmd=None, verbose=False):
557557
UTC_AVOID = "NOTE: Do not autogenerate"
558558
UNUSED_NOTE = "NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:"
559559

560+
DATA_LAYOUT_RE = re.compile(
561+
r"target\s+datalayout\s+=\s+\"(?P<layout>.+)\"$", flags=(re.M | re.S)
562+
)
563+
560564
OPT_FUNCTION_RE = re.compile(
561565
r"^(\s*;\s*Function\sAttrs:\s(?P<attrs>[\w\s():,]+?))?\s*define\s+(?P<funcdef_attrs_and_ret>[^@]*)@(?P<func>[\w.$-]+?)\s*"
562566
r"(?P<args_and_sig>\((\)|(.*?[\w.-]+?)\))[^{]*\{)\n(?P<body>.*?)^\}$",
@@ -651,6 +655,18 @@ def get_triple_from_march(march):
651655
return "x86"
652656

653657

658+
def get_globals_name_prefix(raw_tool_output):
659+
m = DATA_LAYOUT_RE.search(raw_tool_output)
660+
if not m:
661+
return None
662+
data_layout = m.group("layout")
663+
idx = data_layout.find("m:")
664+
if idx < 0:
665+
return None
666+
ch = data_layout[idx + 2]
667+
return "_" if ch == "o" or ch == "x" else None
668+
669+
654670
def apply_filters(line, filters):
655671
has_filter = False
656672
for f in filters:

llvm/utils/update_cc_test_checks.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
}
3535

3636

37-
def get_line2func_list(args, clang_args):
37+
def get_line2func_list(args, clang_args, globals_name_prefix):
3838
ret = collections.defaultdict(list)
3939
# Use clang's JSON AST dump to get the mangled name
4040
json_dump_args = [args.clang] + clang_args + ["-fsyntax-only", "-o", "-"]
@@ -122,6 +122,14 @@ def parse_clang_ast_json(node, loc, search):
122122
if search is None:
123123
search = spell
124124
mangled = node.get("mangledName", spell)
125+
# Clang's AST dump includes the globals prefix, but when Clang emits
126+
# LLVM IR this is not included and instead added as part of the asm
127+
# output. Strip it from the mangled name of globals when needed
128+
# (see DataLayout::getGlobalPrefix()).
129+
if globals_name_prefix:
130+
storage = node.get("storageClass", None)
131+
if storage != "static" and mangled[0] == globals_name_prefix:
132+
mangled = mangled[1:]
125133
ret[int(line) - 1].append((spell, mangled, search))
126134

127135
ast = json.loads(stdout)
@@ -249,10 +257,10 @@ def config():
249257
return args, parser
250258

251259

252-
def get_function_body(builder, args, filename, clang_args, extra_commands, prefixes):
260+
def get_function_body(
261+
builder, args, filename, clang_args, extra_commands, prefixes, raw_tool_output
262+
):
253263
# TODO Clean up duplication of asm/common build_function_body_dictionary
254-
# Invoke external tool and extract function bodies.
255-
raw_tool_output = common.invoke_tool(args.clang, clang_args, filename)
256264
for extra_command in extra_commands:
257265
extra_args = shlex.split(extra_command)
258266
with tempfile.NamedTemporaryFile() as f:
@@ -383,13 +391,23 @@ def main():
383391
common.debug("Extracted clang cmd: clang {}".format(clang_args))
384392
common.debug("Extracted FileCheck prefixes: {}".format(prefixes))
385393

394+
# Invoke external tool and extract function bodies.
395+
raw_tool_output = common.invoke_tool(ti.args.clang, clang_args, ti.path)
386396
get_function_body(
387-
builder, ti.args, ti.path, clang_args, extra_commands, prefixes
397+
builder,
398+
ti.args,
399+
ti.path,
400+
clang_args,
401+
extra_commands,
402+
prefixes,
403+
raw_tool_output,
388404
)
389405

390406
# Invoke clang -Xclang -ast-dump=json to get mapping from start lines to
391407
# mangled names. Forward all clang args for now.
392-
for k, v in get_line2func_list(ti.args, clang_args).items():
408+
for k, v in get_line2func_list(
409+
ti.args, clang_args, common.get_globals_name_prefix(raw_tool_output)
410+
).items():
393411
line2func_list[k].extend(v)
394412

395413
func_dict = builder.finish_and_get_func_dict()

0 commit comments

Comments
 (0)