Skip to content

Commit 20d6375

Browse files
authored
[clang] Handle CC attrs for UEFI (#138935)
UEFI's default ABI is MS ABI. Handle the calling convention attributes accordingly.
1 parent 7348d7e commit 20d6375

File tree

8 files changed

+24
-14
lines changed

8 files changed

+24
-14
lines changed

clang/lib/Basic/Targets/X86.h

+1
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,7 @@ class LLVM_LIBRARY_VISIBILITY UEFIX86_64TargetInfo
866866
switch (CC) {
867867
case CC_C:
868868
case CC_Win64:
869+
case CC_X86_64SysV:
869870
return CCCR_OK;
870871
default:
871872
return CCCR_Warning;

clang/lib/CodeGen/CGCall.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ CodeGenTypes::arrangeFreeFunctionType(CanQual<FunctionProtoType> FTP) {
254254
}
255255

256256
static CallingConv getCallingConventionForDecl(const ObjCMethodDecl *D,
257-
bool IsWindows) {
257+
bool IsTargetDefaultMSABI) {
258258
// Set the appropriate calling convention for the Function.
259259
if (D->hasAttr<StdCallAttr>())
260260
return CC_X86StdCall;
@@ -290,10 +290,10 @@ static CallingConv getCallingConventionForDecl(const ObjCMethodDecl *D,
290290
return CC_IntelOclBicc;
291291

292292
if (D->hasAttr<MSABIAttr>())
293-
return IsWindows ? CC_C : CC_Win64;
293+
return IsTargetDefaultMSABI ? CC_C : CC_Win64;
294294

295295
if (D->hasAttr<SysVABIAttr>())
296-
return IsWindows ? CC_X86_64SysV : CC_C;
296+
return IsTargetDefaultMSABI ? CC_X86_64SysV : CC_C;
297297

298298
if (D->hasAttr<PreserveMostAttr>())
299299
return CC_PreserveMost;
@@ -581,8 +581,11 @@ CodeGenTypes::arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
581581
}
582582

583583
FunctionType::ExtInfo einfo;
584-
bool IsWindows = getContext().getTargetInfo().getTriple().isOSWindows();
585-
einfo = einfo.withCallingConv(getCallingConventionForDecl(MD, IsWindows));
584+
bool IsTargetDefaultMSABI =
585+
getContext().getTargetInfo().getTriple().isOSWindows() ||
586+
getContext().getTargetInfo().getTriple().isUEFI();
587+
einfo = einfo.withCallingConv(
588+
getCallingConventionForDecl(MD, IsTargetDefaultMSABI));
586589

587590
if (getContext().getLangOpts().ObjCAutoRefCount &&
588591
MD->hasAttr<NSReturnsRetainedAttr>())

clang/lib/Sema/SemaChecking.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -4868,27 +4868,27 @@ static bool checkVAStartABI(Sema &S, unsigned BuiltinID, Expr *Fn) {
48684868
bool IsX64 = TT.getArch() == llvm::Triple::x86_64;
48694869
bool IsAArch64 = (TT.getArch() == llvm::Triple::aarch64 ||
48704870
TT.getArch() == llvm::Triple::aarch64_32);
4871-
bool IsWindows = TT.isOSWindows();
4871+
bool IsWindowsOrUEFI = TT.isOSWindows() || TT.isUEFI();
48724872
bool IsMSVAStart = BuiltinID == Builtin::BI__builtin_ms_va_start;
48734873
if (IsX64 || IsAArch64) {
48744874
CallingConv CC = CC_C;
48754875
if (const FunctionDecl *FD = S.getCurFunctionDecl())
48764876
CC = FD->getType()->castAs<FunctionType>()->getCallConv();
48774877
if (IsMSVAStart) {
48784878
// Don't allow this in System V ABI functions.
4879-
if (CC == CC_X86_64SysV || (!IsWindows && CC != CC_Win64))
4879+
if (CC == CC_X86_64SysV || (!IsWindowsOrUEFI && CC != CC_Win64))
48804880
return S.Diag(Fn->getBeginLoc(),
48814881
diag::err_ms_va_start_used_in_sysv_function);
48824882
} else {
48834883
// On x86-64/AArch64 Unix, don't allow this in Win64 ABI functions.
48844884
// On x64 Windows, don't allow this in System V ABI functions.
48854885
// (Yes, that means there's no corresponding way to support variadic
48864886
// System V ABI functions on Windows.)
4887-
if ((IsWindows && CC == CC_X86_64SysV) ||
4888-
(!IsWindows && CC == CC_Win64))
4887+
if ((IsWindowsOrUEFI && CC == CC_X86_64SysV) ||
4888+
(!IsWindowsOrUEFI && CC == CC_Win64))
48894889
return S.Diag(Fn->getBeginLoc(),
48904890
diag::err_va_start_used_in_wrong_abi_function)
4891-
<< !IsWindows;
4891+
<< !IsWindowsOrUEFI;
48924892
}
48934893
return false;
48944894
}

clang/lib/Sema/SemaDeclAttr.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -5397,6 +5397,9 @@ bool Sema::CheckCallingConvAttr(const ParsedAttr &Attrs, CallingConv &CC,
53975397
}
53985398
}
53995399

5400+
bool IsTargetDefaultMSABI =
5401+
Context.getTargetInfo().getTriple().isOSWindows() ||
5402+
Context.getTargetInfo().getTriple().isUEFI();
54005403
// TODO: diagnose uses of these conventions on the wrong target.
54015404
switch (Attrs.getKind()) {
54025405
case ParsedAttr::AT_CDecl:
@@ -5436,12 +5439,10 @@ bool Sema::CheckCallingConvAttr(const ParsedAttr &Attrs, CallingConv &CC,
54365439
CC = CC_X86RegCall;
54375440
break;
54385441
case ParsedAttr::AT_MSABI:
5439-
CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C :
5440-
CC_Win64;
5442+
CC = IsTargetDefaultMSABI ? CC_C : CC_Win64;
54415443
break;
54425444
case ParsedAttr::AT_SysVABI:
5443-
CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV :
5444-
CC_C;
5445+
CC = IsTargetDefaultMSABI ? CC_X86_64SysV : CC_C;
54455446
break;
54465447
case ParsedAttr::AT_Pcs: {
54475448
StringRef StrRef;

clang/test/CodeGen/ms_abi.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// RUN: %clang_cc1 -triple x86_64-unknown-freebsd10.0 -emit-llvm < %s | FileCheck -check-prefix=FREEBSD %s
22
// RUN: %clang_cc1 -triple x86_64-pc-win32 -emit-llvm < %s | FileCheck -check-prefix=WIN64 %s
3+
// RUN: %clang_cc1 -triple x86_64-uefi -emit-llvm < %s | FileCheck -check-prefix=WIN64 %s
34

45
struct foo {
56
int x;

clang/test/CodeGen/sysv_abi.c

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// RUN: %clang_cc1 -triple x86_64-pc-win32 -emit-llvm -target-cpu skylake-avx512 < %s | FileCheck %s --check-prefixes=CHECK,AVX
22
// RUN: %clang_cc1 -triple x86_64-linux -emit-llvm -target-cpu skylake-avx512 < %s | FileCheck %s --check-prefixes=CHECK,AVX
3+
// RUN: %clang_cc1 -triple x86_64-uefi -emit-llvm -target-cpu skylake-avx512 < %s | FileCheck %s --check-prefixes=CHECK,AVX
34
// RUN: %clang_cc1 -triple x86_64-pc-win32 -emit-llvm < %s | FileCheck %s --check-prefixes=CHECK,NOAVX
45
// RUN: %clang_cc1 -triple x86_64-linux -emit-llvm < %s | FileCheck %s --check-prefixes=CHECK,NOAVX
6+
// RUN: %clang_cc1 -triple x86_64-uefi -emit-llvm < %s | FileCheck %s --check-prefixes=CHECK,NOAVX
57

68
#define SYSV_CC __attribute__((sysv_abi))
79

clang/test/Sema/callingconv-ms_abi.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %clang_cc1 -fsyntax-only -verify -triple x86_64-pc-win32 %s
2+
// RUN: %clang_cc1 -fsyntax-only -verify -triple x86_64-uefi %s
23

34
void __attribute__((ms_abi)) foo(void);
45
void (*pfoo)(void) = foo;

clang/test/Sema/varargs-win64.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %clang_cc1 -fsyntax-only -verify %s -triple x86_64-pc-win32
2+
// RUN: %clang_cc1 -fsyntax-only -verify %s -triple x86_64-uefi
23

34
void __attribute__((sysv_abi)) foo(int a, ...) {
45
__builtin_va_list ap;

0 commit comments

Comments
 (0)