Skip to content

Commit 5b5fd84

Browse files
authored
[SYCL][NativeCPU] Limit generic ABI to builtins. (#19564)
In #17408, NativeCPU became a target in order to be able to pick the ABI for its own libclc functions consistently, without having targets affect this. This was, and is, required to be able to use libclc independent of target and target options. However, it breaks some calls into libc. Therefore, this PR allows the calling convention to be explicitly specified, ensures it is specified for any libclc functions, and ensures it is not specified for any libc functions. Fixes the SYCL-E2E acos, cmath, and exp-std-complex tests.
1 parent 834c683 commit 5b5fd84

File tree

18 files changed

+133
-7
lines changed

18 files changed

+133
-7
lines changed

clang/include/clang/Basic/Attr.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ def TargetAnyX86 : TargetArch<["x86", "x86_64"]>;
494494
def TargetSPIRV : TargetArch<["spirv", "spirv32", "spirv64"]>;
495495
def TargetWebAssembly : TargetArch<["wasm32", "wasm64"]>;
496496
def TargetNVPTX : TargetArch<["nvptx", "nvptx64"]>;
497+
def TargetNativeCPU : TargetArch<["native_cpu"]>;
497498
def TargetWindows : TargetSpec {
498499
let OSes = ["Win32"];
499500
}
@@ -4530,6 +4531,11 @@ def RISCVVLSCC: DeclOrTypeAttr, TargetSpecificAttr<TargetRISCV> {
45304531
let Documentation = [RISCVVLSCCDocs];
45314532
}
45324533

4534+
def NativeCPULibclcCall : DeclOrTypeAttr, TargetSpecificAttr<TargetNativeCPU> {
4535+
let Spellings = [Clang<"libclc_call", 0>];
4536+
let Documentation = [Undocumented];
4537+
}
4538+
45334539
def Target : InheritableAttr {
45344540
let Spellings = [GCC<"target">];
45354541
let Args = [StringArgument<"featuresStr">];

clang/include/clang/Basic/TargetInfo.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,6 +1711,11 @@ class TargetInfo : public TransferrableTargetInfo,
17111711
return CC_C;
17121712
}
17131713

1714+
/// Gets the calling convention for libclc built-ins for the given target.
1715+
virtual CallingConv getLibclcCallingConv() const {
1716+
return getDefaultCallingConv();
1717+
}
1718+
17141719
/// Get the default atomic options.
17151720
AtomicOptions getAtomicOpts() const { return AtomicOpts; }
17161721

clang/lib/AST/Type.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4341,6 +4341,7 @@ bool AttributedType::isCallingConv() const {
43414341
case attr::PreserveNone:
43424342
case attr::RISCVVectorCC:
43434343
case attr::RISCVVLSCC:
4344+
case attr::NativeCPULibclcCall:
43444345
return true;
43454346
}
43464347
llvm_unreachable("invalid attr kind");

clang/lib/AST/TypePrinter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2125,6 +2125,9 @@ void TypePrinter::printAttributedAfter(const AttributedType *T,
21252125
case attr::RISCVVLSCC:
21262126
OS << "riscv_vls_cc";
21272127
break;
2128+
case attr::NativeCPULibclcCall:
2129+
OS << "libclc_call";
2130+
break;
21282131
case attr::NoDeref:
21292132
OS << "noderef";
21302133
break;

clang/lib/Basic/Targets/NativeCPU.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ class LLVM_LIBRARY_VISIBILITY NativeCPUTargetInfo final : public TargetInfo {
4949

5050
void setSupportedOpenCLOpts() override { supportAllOpenCLOpts(); }
5151

52+
CallingConv getLibclcCallingConv() const override { return CC_SpirFunction; }
53+
5254
CallingConvCheckResult checkCallingConvention(CallingConv CC) const override {
55+
if (CC == CC_SpirFunction)
56+
return CCCR_OK;
57+
5358
if (HostTarget)
5459
return HostTarget->checkCallingConvention(CC);
5560

clang/lib/CodeGen/CGCall.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,9 @@ static CallingConv getCallingConventionForDecl(const ObjCMethodDecl *D,
334334
}
335335
}
336336

337+
if (D->hasAttr<NativeCPULibclcCallAttr>())
338+
return CC_SpirFunction;
339+
337340
return CC_C;
338341
}
339342

clang/lib/CodeGen/Targets/NativeCPU.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class NativeCPUABIInfo : public DefaultABIInfo {
2020
public:
2121
NativeCPUABIInfo(CodeGen::CodeGenTypes &CGT, const ABIInfo *HostABIInfo)
2222
: DefaultABIInfo(CGT), HostABIInfo(HostABIInfo) {}
23+
24+
void computeInfo(CGFunctionInfo &FI) const override;
2325
};
2426

2527
class NativeCPUTargetCodeGenInfo : public TargetCodeGenInfo {
@@ -37,6 +39,17 @@ class NativeCPUTargetCodeGenInfo : public TargetCodeGenInfo {
3739
};
3840
} // namespace
3941

42+
void NativeCPUABIInfo::computeInfo(CGFunctionInfo &FI) const {
43+
if (HostABIInfo &&
44+
FI.getCallingConvention() != llvm::CallingConv::SPIR_FUNC) {
45+
HostABIInfo->computeInfo(FI);
46+
return;
47+
}
48+
49+
DefaultABIInfo::computeInfo(FI);
50+
FI.setEffectiveCallingConvention(llvm::CallingConv::C);
51+
}
52+
4053
std::unique_ptr<TargetCodeGenInfo> CodeGen::createNativeCPUTargetCodeGenInfo(
4154
CodeGenModule &CGM,
4255
std::unique_ptr<TargetCodeGenInfo> HostTargetCodeGenInfo) {

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5428,6 +5428,9 @@ static void handleCallConvAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
54285428
D->addAttr(::new (S.Context) RISCVVLSCCAttr(S.Context, AL, VectorLength));
54295429
return;
54305430
}
5431+
case ParsedAttr::AT_NativeCPULibclcCall:
5432+
D->addAttr(::new (S.Context) NativeCPULibclcCallAttr(S.Context, AL));
5433+
return;
54315434
default:
54325435
llvm_unreachable("unexpected attribute kind");
54335436
}
@@ -5699,6 +5702,9 @@ bool Sema::CheckCallingConvAttr(const ParsedAttr &Attrs, CallingConv &CC,
56995702
CC = CC_DeviceKernel;
57005703
break;
57015704
}
5705+
case ParsedAttr::AT_NativeCPULibclcCall:
5706+
CC = CC_SpirFunction;
5707+
break;
57025708
default: llvm_unreachable("unexpected attribute kind");
57035709
}
57045710

@@ -7700,6 +7706,7 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL,
77007706
case ParsedAttr::AT_PreserveNone:
77017707
case ParsedAttr::AT_RISCVVectorCC:
77027708
case ParsedAttr::AT_RISCVVLSCC:
7709+
case ParsedAttr::AT_NativeCPULibclcCall:
77037710
handleCallConvAttr(S, D, AL);
77047711
break;
77057712
case ParsedAttr::AT_DeviceKernel:

clang/lib/Sema/SemaLookup.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "clang/AST/ExprCXX.h"
2323
#include "clang/Basic/Builtins.h"
2424
#include "clang/Basic/LangOptions.h"
25+
#include "clang/Basic/TargetInfo.h"
2526
#include "clang/Lex/HeaderSearch.h"
2627
#include "clang/Lex/ModuleLoader.h"
2728
#include "clang/Lex/Preprocessor.h"
@@ -788,7 +789,7 @@ static void GetProgModelBuiltinFctOverloads(
788789
std::vector<QualType> &FunctionList, SmallVector<QualType, 1> &RetTypes,
789790
SmallVector<SmallVector<QualType, 1>, 5> &ArgTypes, bool IsVariadic) {
790791
FunctionProtoType::ExtProtoInfo PI(
791-
Context.getDefaultCallingConvention(false, false, true));
792+
Context.getTargetInfo().getLibclcCallingConv());
792793
PI.Variadic = IsVariadic;
793794
PI.ExceptionSpec = FunctionProtoType::ExceptionSpecInfo{EST_BasicNoexcept};
794795

clang/lib/Sema/SemaType.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ static void diagnoseBadTypeAttribute(Sema &S, const ParsedAttr &attr,
144144
case ParsedAttr::AT_M68kRTD: \
145145
case ParsedAttr::AT_PreserveNone: \
146146
case ParsedAttr::AT_RISCVVectorCC: \
147-
case ParsedAttr::AT_RISCVVLSCC
147+
case ParsedAttr::AT_RISCVVLSCC: \
148+
case ParsedAttr::AT_NativeCPULibclcCall
148149

149150
// Function type attributes.
150151
#define FUNCTION_TYPE_ATTRS_CASELIST \
@@ -7661,6 +7662,8 @@ static Attr *getCCTypeAttr(ASTContext &Ctx, ParsedAttr &Attr) {
76617662

76627663
return ::new (Ctx) RISCVVLSCCAttr(Ctx, Attr, ABIVLen);
76637664
}
7665+
case ParsedAttr::AT_NativeCPULibclcCall:
7666+
return createSimpleAttr<NativeCPULibclcCallAttr>(Ctx, Attr);
76647667
}
76657668
llvm_unreachable("unexpected attribute kind!");
76667669
}

0 commit comments

Comments
 (0)