Skip to content

Commit a9cf741

Browse files
committed
[SYCL][NativeCPU] Limit generic ABI to builtins.
In intel#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 72919a0 commit a9cf741

File tree

17 files changed

+117
-7
lines changed

17 files changed

+117
-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
}
@@ -4490,6 +4491,11 @@ def RISCVVLSCC: DeclOrTypeAttr, TargetSpecificAttr<TargetRISCV> {
44904491
let Documentation = [RISCVVLSCCDocs];
44914492
}
44924493

4494+
def NativeCPULibclcCall : DeclOrTypeAttr, TargetSpecificAttr<TargetNativeCPU> {
4495+
let Spellings = [Clang<"libclc_call", 0>];
4496+
let Documentation = [Undocumented];
4497+
}
4498+
44934499
def Target : InheritableAttr {
44944500
let Spellings = [GCC<"target">];
44954501
let Args = [StringArgument<"featuresStr">];

clang/include/clang/Basic/TargetInfo.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,11 @@ class TargetInfo : public TransferrableTargetInfo,
17021702
return CC_C;
17031703
}
17041704

1705+
/// Gets the calling convention for libclc built-ins for the given target.
1706+
virtual CallingConv getLibclcCallingConv() const {
1707+
return getDefaultCallingConv();
1708+
}
1709+
17051710
/// Get the default atomic options.
17061711
AtomicOptions getAtomicOpts() const { return AtomicOpts; }
17071712

clang/lib/AST/Type.cpp

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

clang/lib/AST/TypePrinter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,6 +2112,9 @@ void TypePrinter::printAttributedAfter(const AttributedType *T,
21122112
case attr::RISCVVLSCC:
21132113
OS << "riscv_vls_cc";
21142114
break;
2115+
case attr::NativeCPULibclcCall:
2116+
OS << "libclc_call";
2117+
break;
21152118
case attr::NoDeref:
21162119
OS << "noderef";
21172120
break;

clang/lib/Basic/Targets/NativeCPU.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,5 @@ void NativeCPUTargetInfo::setAuxTarget(const TargetInfo *Aux) {
6868
assert(Aux && "Cannot invoke setAuxTarget without a valid auxiliary target!");
6969
copyAuxTarget(Aux);
7070
getTargetOpts() = Aux->getTargetOpts();
71+
resetDataLayout(Aux->getDataLayoutString());
7172
}

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
@@ -5374,6 +5374,9 @@ static void handleCallConvAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
53745374
D->addAttr(::new (S.Context) RISCVVLSCCAttr(S.Context, AL, VectorLength));
53755375
return;
53765376
}
5377+
case ParsedAttr::AT_NativeCPULibclcCall:
5378+
D->addAttr(::new (S.Context) NativeCPULibclcCallAttr(S.Context, AL));
5379+
return;
53775380
default:
53785381
llvm_unreachable("unexpected attribute kind");
53795382
}
@@ -5645,6 +5648,9 @@ bool Sema::CheckCallingConvAttr(const ParsedAttr &Attrs, CallingConv &CC,
56455648
CC = CC_DeviceKernel;
56465649
break;
56475650
}
5651+
case ParsedAttr::AT_NativeCPULibclcCall:
5652+
CC = CC_SpirFunction;
5653+
break;
56485654
default: llvm_unreachable("unexpected attribute kind");
56495655
}
56505656

@@ -7646,6 +7652,7 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL,
76467652
case ParsedAttr::AT_PreserveNone:
76477653
case ParsedAttr::AT_RISCVVectorCC:
76487654
case ParsedAttr::AT_RISCVVLSCC:
7655+
case ParsedAttr::AT_NativeCPULibclcCall:
76497656
handleCallConvAttr(S, D, AL);
76507657
break;
76517658
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

0 commit comments

Comments
 (0)