Skip to content

Commit 3efdcfc

Browse files
author
git apple-llvm automerger
committed
Merge commit 'f6ba21389fb0' from llvm.org/main into next
2 parents 7d11e12 + f6ba213 commit 3efdcfc

File tree

12 files changed

+86
-35
lines changed

12 files changed

+86
-35
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ Bug Fixes to Attribute Support
447447
- Using ``[[gnu::cleanup(some_func)]]`` where some_func is annotated with
448448
``[[gnu::error("some error")]]`` now correctly triggers an error. (#GH146520)
449449
- Fix a crash when the function name is empty in the `swift_name` attribute. (#GH157075)
450+
- Fixes crashes or missing diagnostics with the `device_kernel` attribute. (#GH161905)
450451

451452
Bug Fixes to C++ Support
452453
^^^^^^^^^^^^^^^^^^^^^^^^

clang/include/clang/Basic/Attr.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1667,7 +1667,7 @@ def SYCLKernel : InheritableAttr {
16671667
let Documentation = [SYCLKernelDocs];
16681668
}
16691669

1670-
def DeviceKernel : DeclOrTypeAttr {
1670+
def DeviceKernel : InheritableAttr {
16711671
let Spellings = [Clang<"device_kernel">,
16721672
Clang<"nvptx_kernel">, Clang<"amdgpu_kernel">,
16731673
CustomKeyword<"__kernel">, CustomKeyword<"kernel">];

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4185,6 +4185,9 @@ def warn_missing_sdksettings_for_availability_checking : Warning<
41854185
"%0 availability is ignored without a valid 'SDKSettings.json' in the SDK">,
41864186
InGroup<DiagGroup<"ignored-availability-without-sdk-settings">>;
41874187

4188+
def err_hidden_device_kernel
4189+
: Error<"%0 is specified as a device kernel but it is not externally visible">;
4190+
41884191
// Thread Safety Attributes
41894192
def warn_thread_attribute_ignored : Warning<
41904193
"ignoring %0 attribute because its argument is invalid">,

clang/lib/AST/TypePrinter.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2315,9 +2315,6 @@ void TypePrinter::printAttributedAfter(const AttributedType *T,
23152315
}
23162316
case attr::AArch64VectorPcs: OS << "aarch64_vector_pcs"; break;
23172317
case attr::AArch64SVEPcs: OS << "aarch64_sve_pcs"; break;
2318-
case attr::DeviceKernel:
2319-
OS << T->getAttr()->getSpelling();
2320-
break;
23212318
case attr::IntelOclBicc:
23222319
OS << "inteloclbicc";
23232320
break;

clang/lib/Basic/Targets/NVPTX.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ class LLVM_LIBRARY_VISIBILITY NVPTXTargetInfo : public TargetInfo {
200200
// a host function.
201201
if (HostTarget)
202202
return HostTarget->checkCallingConvention(CC);
203-
return CCCR_Warning;
203+
return CC == CC_DeviceKernel ? CCCR_OK : CCCR_Warning;
204204
}
205205

206206
bool hasBitIntType() const override { return true; }

clang/lib/CodeGen/Targets/AMDGPU.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,11 @@ void AMDGPUTargetCodeGenInfo::setTargetAttributes(
439439
return;
440440

441441
const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
442-
if (FD)
442+
if (FD) {
443443
setFunctionDeclAttributes(FD, F, M);
444-
444+
if (FD->hasAttr<DeviceKernelAttr>() && !M.getLangOpts().OpenCL)
445+
F->setCallingConv(getDeviceKernelCallingConv());
446+
}
445447
if (!getABIInfo().getCodeGenOpts().EmitIEEENaNCompliantInsts)
446448
F->addFnAttr("amdgpu-ieee", "false");
447449
}
@@ -658,7 +660,7 @@ llvm::Value *AMDGPUTargetCodeGenInfo::createEnqueuedBlockKernel(
658660
// kernel address (only the kernel descriptor).
659661
auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name,
660662
&Mod);
661-
F->setCallingConv(llvm::CallingConv::AMDGPU_KERNEL);
663+
F->setCallingConv(getDeviceKernelCallingConv());
662664

663665
llvm::AttrBuilder KernelAttrs(C);
664666
// FIXME: The invoke isn't applying the right attributes either

clang/lib/CodeGen/Targets/NVPTX.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ void NVPTXTargetCodeGenInfo::setTargetAttributes(
264264
// And kernel functions are not subject to inlining
265265
F->addFnAttr(llvm::Attribute::NoInline);
266266
if (FD->hasAttr<CUDAGlobalAttr>()) {
267-
F->setCallingConv(llvm::CallingConv::PTX_Kernel);
267+
F->setCallingConv(getDeviceKernelCallingConv());
268268

269269
for (auto IV : llvm::enumerate(FD->parameters()))
270270
if (IV.value()->hasAttr<CUDAGridConstantAttr>())
@@ -278,7 +278,7 @@ void NVPTXTargetCodeGenInfo::setTargetAttributes(
278278
}
279279
// Attach kernel metadata directly if compiling for NVPTX.
280280
if (FD->hasAttr<DeviceKernelAttr>())
281-
F->setCallingConv(llvm::CallingConv::PTX_Kernel);
281+
F->setCallingConv(getDeviceKernelCallingConv());
282282
}
283283

284284
void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::GlobalValue *GV,

clang/lib/CodeGen/Targets/SPIR.cpp

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class CommonSPIRTargetCodeGenInfo : public TargetCodeGenInfo {
6464
llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM,
6565
llvm::PointerType *T,
6666
QualType QT) const override;
67+
void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
68+
CodeGen::CodeGenModule &M) const override;
6769
};
6870
class SPIRVTargetCodeGenInfo : public CommonSPIRTargetCodeGenInfo {
6971
public:
@@ -268,6 +270,22 @@ CommonSPIRTargetCodeGenInfo::getNullPointer(const CodeGen::CodeGenModule &CGM,
268270
llvm::ConstantPointerNull::get(NPT), PT);
269271
}
270272

273+
void CommonSPIRTargetCodeGenInfo::setTargetAttributes(
274+
const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
275+
if (M.getLangOpts().OpenCL || GV->isDeclaration())
276+
return;
277+
278+
const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
279+
if (!FD)
280+
return;
281+
282+
llvm::Function *F = dyn_cast<llvm::Function>(GV);
283+
assert(F && "Expected GlobalValue to be a Function");
284+
285+
if (FD->hasAttr<DeviceKernelAttr>())
286+
F->setCallingConv(getDeviceKernelCallingConv());
287+
}
288+
271289
LangAS
272290
SPIRVTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
273291
const VarDecl *D) const {
@@ -292,19 +310,23 @@ SPIRVTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
292310

293311
void SPIRVTargetCodeGenInfo::setTargetAttributes(
294312
const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
295-
if (!M.getLangOpts().HIP ||
296-
M.getTarget().getTriple().getVendor() != llvm::Triple::AMD)
297-
return;
298313
if (GV->isDeclaration())
299314
return;
300315

301-
auto F = dyn_cast<llvm::Function>(GV);
302-
if (!F)
316+
const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
317+
if (!FD)
303318
return;
304319

305-
auto FD = dyn_cast_or_null<FunctionDecl>(D);
306-
if (!FD)
320+
llvm::Function *F = dyn_cast<llvm::Function>(GV);
321+
assert(F && "Expected GlobalValue to be a Function");
322+
323+
if (FD->hasAttr<DeviceKernelAttr>())
324+
F->setCallingConv(getDeviceKernelCallingConv());
325+
326+
if (!M.getLangOpts().HIP ||
327+
M.getTarget().getTriple().getVendor() != llvm::Triple::AMD)
307328
return;
329+
308330
if (!FD->hasAttr<CUDAGlobalAttr>())
309331
return;
310332

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5352,16 +5352,36 @@ static void handleCallConvAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
53525352
static void handleDeviceKernelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
53535353
const auto *FD = dyn_cast_or_null<FunctionDecl>(D);
53545354
bool IsFunctionTemplate = FD && FD->getDescribedFunctionTemplate();
5355-
if (S.getASTContext().getTargetInfo().getTriple().isNVPTX()) {
5355+
llvm::Triple Triple = S.getASTContext().getTargetInfo().getTriple();
5356+
const LangOptions &LangOpts = S.getLangOpts();
5357+
// OpenCL has its own error messages.
5358+
if (!LangOpts.OpenCL && FD && !FD->isExternallyVisible()) {
5359+
S.Diag(AL.getLoc(), diag::err_hidden_device_kernel) << FD;
5360+
AL.setInvalid();
5361+
return;
5362+
}
5363+
if (Triple.isNVPTX()) {
53565364
handleGlobalAttr(S, D, AL);
53575365
} else {
53585366
// OpenCL C++ will throw a more specific error.
5359-
if (!S.getLangOpts().OpenCLCPlusPlus && (!FD || IsFunctionTemplate)) {
5367+
if (!LangOpts.OpenCLCPlusPlus && (!FD || IsFunctionTemplate)) {
53605368
S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type_str)
53615369
<< AL << AL.isRegularKeywordAttribute() << "functions";
5370+
AL.setInvalid();
5371+
return;
53625372
}
53635373
handleSimpleAttribute<DeviceKernelAttr>(S, D, AL);
53645374
}
5375+
// TODO: isGPU() should probably return true for SPIR.
5376+
bool TargetDeviceEnvironment = Triple.isGPU() || Triple.isSPIR() ||
5377+
LangOpts.isTargetDevice() || LangOpts.OpenCL;
5378+
if (!TargetDeviceEnvironment) {
5379+
S.Diag(AL.getLoc(), diag::warn_cconv_unsupported)
5380+
<< AL << (int)Sema::CallingConventionIgnoredReason::ForThisTarget;
5381+
AL.setInvalid();
5382+
return;
5383+
}
5384+
53655385
// Make sure we validate the CC with the target
53665386
// and warn/error if necessary.
53675387
handleCallConvAttr(S, D, AL);

clang/lib/Sema/SemaType.cpp

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ static void diagnoseBadTypeAttribute(Sema &S, const ParsedAttr &attr,
137137
case ParsedAttr::AT_VectorCall: \
138138
case ParsedAttr::AT_AArch64VectorPcs: \
139139
case ParsedAttr::AT_AArch64SVEPcs: \
140-
case ParsedAttr::AT_DeviceKernel: \
141140
case ParsedAttr::AT_MSABI: \
142141
case ParsedAttr::AT_SysVABI: \
143142
case ParsedAttr::AT_Pcs: \
@@ -3848,7 +3847,8 @@ static CallingConv getCCForDeclaratorChunk(
38483847
}
38493848
}
38503849
}
3851-
for (const ParsedAttr &AL : D.getDeclSpec().getAttributes()) {
3850+
for (const ParsedAttr &AL : llvm::concat<ParsedAttr>(
3851+
D.getDeclSpec().getAttributes(), D.getAttributes())) {
38523852
if (AL.getKind() == ParsedAttr::AT_DeviceKernel) {
38533853
CC = CC_DeviceKernel;
38543854
break;
@@ -7672,8 +7672,6 @@ static Attr *getCCTypeAttr(ASTContext &Ctx, ParsedAttr &Attr) {
76727672
return createSimpleAttr<AArch64SVEPcsAttr>(Ctx, Attr);
76737673
case ParsedAttr::AT_ArmStreaming:
76747674
return createSimpleAttr<ArmStreamingAttr>(Ctx, Attr);
7675-
case ParsedAttr::AT_DeviceKernel:
7676-
return createSimpleAttr<DeviceKernelAttr>(Ctx, Attr);
76777675
case ParsedAttr::AT_Pcs: {
76787676
// The attribute may have had a fixit applied where we treated an
76797677
// identifier as a string literal. The contents of the string are valid,
@@ -9750,16 +9748,6 @@ static void HandleHLSLParamModifierAttr(TypeProcessingState &State,
97509748
}
97519749
}
97529750

9753-
static bool isMultiSubjectAttrAllowedOnType(const ParsedAttr &Attr) {
9754-
// The DeviceKernel attribute is shared for many targets, and
9755-
// it is only allowed to be a type attribute with the AMDGPU
9756-
// spelling, so skip processing the attr as a type attr
9757-
// unless it has that spelling.
9758-
if (Attr.getKind() != ParsedAttr::AT_DeviceKernel)
9759-
return true;
9760-
return DeviceKernelAttr::isAMDGPUSpelling(Attr);
9761-
}
9762-
97639751
static void processTypeAttrs(TypeProcessingState &state, QualType &type,
97649752
TypeAttrLocation TAL,
97659753
const ParsedAttributesView &attrs,
@@ -10029,8 +10017,6 @@ static void processTypeAttrs(TypeProcessingState &state, QualType &type,
1002910017
break;
1003010018
[[fallthrough]];
1003110019
FUNCTION_TYPE_ATTRS_CASELIST:
10032-
if (!isMultiSubjectAttrAllowedOnType(attr))
10033-
break;
1003410020

1003510021
attr.setUsedAsTypeAttr();
1003610022

0 commit comments

Comments
 (0)