Skip to content

Commit 9b3834e

Browse files
committed
[clang] Fix inline builtin functions of an __asm__ renamed function with symbol prefixes
If a function is renamed with `__asm__`, the name provided is the exact symbol name, without any extra implicit symbol prefixes. If the target does use symbol prefixes, the IR level symbol gets an `\01` prefix to indicate that it's a literal symbol name to be taken as is. When a builtin function is specialized by providing an inline version of it, that inline function is named `<funcname>.inline`. When the base function has been renamed due to `__asm__`, the inline function ends up named `<asmname>.inline`. Up to this point, things did work as expected before. However, for targets with symbol prefixes, one codepath that produced the combined name `<asmname>.inline` used the mangled `asmname` with `\01` prefix, while others didn't. This patch fixes this. This fixes the combination of asm renamed builtin function, with inline override of the function, on any target with symbol prefixes (such as i386 windows and any Darwin target). Differential Revision: https://reviews.llvm.org/D137073
1 parent 6254495 commit 9b3834e

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5014,8 +5014,7 @@ static CGCallee EmitDirectCallee(CodeGenFunction &CGF, GlobalDecl GD) {
50145014
std::string NoBuiltinFD = ("no-builtin-" + FD->getName()).str();
50155015
std::string NoBuiltins = "no-builtins";
50165016

5017-
auto *A = FD->getAttr<AsmLabelAttr>();
5018-
StringRef Ident = A ? A->getLabel() : FD->getName();
5017+
StringRef Ident = CGF.CGM.getMangledName(GD);
50195018
std::string FDInlineName = (Ident + ".inline").str();
50205019

50215020
bool IsPredefinedLibFunction =
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// RUN: %clang_cc1 -triple i686-windows-gnu -emit-llvm -o - %s -disable-llvm-optzns | FileCheck %s
2+
3+
// CHECK: call i32 @"\01_asm_func_name.inline"
4+
5+
// CHECK: declare dso_local i32 @"\01_asm_func_name"(ptr noundef, i32 noundef, ptr noundef, ptr noundef)
6+
7+
// CHECK: define internal i32 @"\01_asm_func_name.inline"
8+
9+
// CHECK: call i32 @__mingw_vsnprintf
10+
11+
// CHECK: declare dso_local i32 @__mingw_vsnprintf
12+
13+
typedef unsigned int size_t;
14+
15+
int __mingw_vsnprintf(char *_DstBuf, size_t _MaxCount, const char *_Format, __builtin_va_list _ArgList);
16+
17+
// For the real use case, "_asm_func_name" is actually "___mingw_vsnprintf", but it's renamed in the testcase for disambiguation.
18+
int vsnprintf(char *__stream, size_t __n, const char *__format, __builtin_va_list __local_argv) __asm__("_asm_func_name");
19+
20+
extern __inline__ __attribute__((__always_inline__, __gnu_inline__))
21+
int vsnprintf(char *__stream, size_t __n, const char *__format, __builtin_va_list __local_argv)
22+
{
23+
return __mingw_vsnprintf(__stream, __n, __format, __local_argv);
24+
}
25+
26+
void call(const char* fmt, ...) {
27+
char buf[200];
28+
__builtin_va_list ap;
29+
__builtin_va_start(ap, fmt);
30+
vsnprintf(buf, sizeof(buf), fmt, ap);
31+
__builtin_va_end(ap);
32+
}

0 commit comments

Comments
 (0)