Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -12197,7 +12197,7 @@ def err_sycl_entry_point_on_main : Error<
def err_sycl_kernel_name_type : Error<
"'sycl_kernel_entry_point' kernel name argument must be a class type">;
def err_sycl_kernel_name_conflict : Error<
"'sycl_kernel_entry_point' kernel name argument conflicts with a previous"
"'sycl_kernel_entry_point' kernel name %0 conflicts with a previous"
" declaration">;

def warn_cuda_maxclusterrank_sm_90 : Warning<
Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -10348,6 +10348,8 @@ class Sema final : public SemaBase {
PrintPragmaAttributeInstantiationPoint();
}
void PrintInstantiationStack();
void
PrintInstantiationStack(std::function<void(const PartialDiagnosticAt &)>);

/// Determines whether we are currently in a context where
/// template argument substitution failures are not considered
Expand Down
14 changes: 14 additions & 0 deletions clang/include/clang/Sema/SemaSYCL.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ class SemaSYCL : public SemaBase {
public:
SemaSYCL(Sema &S);

using ContextNotes = SmallVector<PartialDiagnosticAt, 1>;
llvm::DenseMap<CanonicalDeclPtr<const FunctionDecl>, ContextNotes>
SYCLKernelEntryContextNotes;
// Similar to SuppressedDiagnostics,
// we don't emit diagnostics straight away in case of template instantiation.
// This is to work around the fact that during overload resolution we end up
// instantiating decls without commiting to use them. During attribute checks
// we can not tell if the decl will actually be selected and therefore needed.
// We emit the diagnostic and raise them iff they are required.
typedef llvm::DenseMap<Decl *, SmallVector<PartialDiagnosticAt, 1>>
SuppressedDiagnosticsMap;
SuppressedDiagnosticsMap EntryPointSuppressedDiagnostics;

/// Creates a SemaDiagnosticBuilder that emits the diagnostic if the current
/// context is "used as device code".
///
Expand Down Expand Up @@ -66,6 +79,7 @@ class SemaSYCL : public SemaBase {

void CheckSYCLEntryPointFunctionDecl(FunctionDecl *FD);
StmtResult BuildSYCLKernelCallStmt(FunctionDecl *FD, Stmt *Body);
bool EmitDelayedKernelEntryPointDiagnostics(Decl *FD);
};

} // namespace clang
Expand Down
53 changes: 27 additions & 26 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16049,38 +16049,39 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body,
CheckCoroutineWrapper(FD);
}

// Create SYCL kernel entry point function outline.
if (FD && !FD->isInvalidDecl() && !FD->isDependentContext() &&
FD->hasAttr<SYCLKernelEntryPointAttr>()) {
if (FD->isDeleted()) {
Diag(FD->getAttr<SYCLKernelEntryPointAttr>()->getLocation(),
diag::err_sycl_entry_point_invalid)
<< /*deleted function*/2;
FD->setInvalidDecl();
} else if (FD->isDefaulted()) {
Diag(FD->getAttr<SYCLKernelEntryPointAttr>()->getLocation(),
diag::err_sycl_entry_point_invalid)
<< /*defaulted function*/3;
FD->setInvalidDecl();
} else if (FSI->isCoroutine()) {
Diag(FD->getAttr<SYCLKernelEntryPointAttr>()->getLocation(),
diag::err_sycl_entry_point_invalid)
<< /*coroutine*/7;
FD->setInvalidDecl();
} else if (Body) {
StmtResult SR = SYCL().BuildSYCLKernelCallStmt(FD, Body);
if (SR.isInvalid())
return nullptr;
Body = SR.get();
}
}

{
// Do not call PopExpressionEvaluationContext() if it is a lambda because
// one is already popped when finishing the lambda in BuildLambdaExpr().
// This is meant to pop the context added in ActOnStartOfFunctionDef().
ExitFunctionBodyRAII ExitRAII(*this, isLambdaCallOperator(FD));
if (FD) {
// Create SYCL kernel entry point function outline.
if (!FD->isInvalidDecl() && !FD->isDependentContext() &&
FD->hasAttr<SYCLKernelEntryPointAttr>()) {
if (FD->isDeleted()) {
Diag(FD->getAttr<SYCLKernelEntryPointAttr>()->getLocation(),
diag::err_sycl_entry_point_invalid)
<< /*deleted function*/2;
FD->setInvalidDecl();
} else if (FD->isDefaulted()) {
Diag(FD->getAttr<SYCLKernelEntryPointAttr>()->getLocation(),
diag::err_sycl_entry_point_invalid)
<< /*defaulted function*/3;
FD->setInvalidDecl();
} else if (FSI->isCoroutine()) {
Diag(FD->getAttr<SYCLKernelEntryPointAttr>()->getLocation(),
diag::err_sycl_entry_point_invalid)
<< /*coroutine*/7;
FD->setInvalidDecl();
} else if (Body) {
StmtResult SR = SYCL().BuildSYCLKernelCallStmt(FD, Body);
if (SR.isInvalid()) {
FD->setInvalidDecl();
return nullptr;
}
Body = SR.get();
}
}
// If this is called by Parser::ParseFunctionDefinition() after marking
// the declaration as deleted, and if the deleted-function-body contains
// a message (C++26), then a DefaultedOrDeletedInfo will have already been
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ bool Sema::DiagnoseUseOfDecl(NamedDecl *D, ArrayRef<SourceLocation> Locs,

diagnoseUnavailableAlignedAllocation(*cast<FunctionDecl>(D), Loc);
}
SYCL().EmitDelayedKernelEntryPointDiagnostics(D->getCanonicalDecl());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I experimented a little. I wonder if we can simply call CheckSYCLEntryPointFunctionDecl() here without having to collect partial diagnostics. Take a look at https://godbolt.org/z/T1Pz5T6qf (and enjoy the implementation divergence while you're there!) The diagnostic issued for use of a deleted function isn't issued until the use occurs, but still includes the instantiation stack; see the code starting at line 271 below.


// See if this is an auto-typed variable whose initializer we are parsing.
if (ParsingInitForAutoVars.count(D)) {
Expand Down
174 changes: 121 additions & 53 deletions clang/lib/Sema/SemaSYCL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,41 @@ void SemaSYCL::handleKernelEntryPointAttr(Decl *D, const ParsedAttr &AL) {
TypeSourceInfo *TSI = nullptr;
(void)SemaRef.GetTypeFromParser(PT, &TSI);
assert(TSI && "no type source info for attribute argument");
D->addAttr(::new (SemaRef.Context) SYCLKernelEntryPointAttr(SemaRef.Context,
AL, TSI));

FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
assert(FD && "Not a function decl");

bool hasError = false;
if (auto *MD = dyn_cast<CXXMethodDecl>(D)) {
if (!MD->isStatic()) {
Diag(AL.getLoc(), diag::err_sycl_entry_point_invalid)
<< /*non-static member function*/ 0;
hasError = true;
}
}
if (FD->isVariadic()) {
Diag(AL.getLoc(), diag::err_sycl_entry_point_invalid)
<< /*variadic function*/ 1;
hasError = true;
}
if (FD->isConsteval()) {
Diag(AL.getLoc(), diag::err_sycl_entry_point_invalid)
<< /*consteval function*/ 5;
hasError = true;
} else if (FD->isConstexpr()) {
Diag(AL.getLoc(), diag::err_sycl_entry_point_invalid)
<< /*constexpr function*/ 4;
hasError = true;
}
QualType Ret = FD->getReturnType();
if (!Ret->isDependentType() && !Ret->isVoidType()) {
Diag(AL.getLoc(), diag::err_sycl_entry_point_return_type);
hasError = true;
}

if (!hasError)
D->addAttr(::new (SemaRef.Context)
SYCLKernelEntryPointAttr(SemaRef.Context, AL, TSI));
}

namespace {
Expand Down Expand Up @@ -259,38 +292,63 @@ class OutlinedFunctionDeclBodyInstantiator
ParmDeclMap &MapRef;
};

void DiagnoseSYCLEntryPoint(Sema &S, FunctionDecl *FD,
const PartialDiagnosticAt &Diag) {
if (S.inTemplateInstantiation()) {
// We are synthesizing decls but we may not commit to use at all.
// Delay diagnostics until we know it is needed.
auto &SuppressedDiags = S.SYCL().EntryPointSuppressedDiagnostics[FD];
SuppressedDiags.push_back(Diag);
if (S.getDiagnostics().getDiagnosticLevel(
Diag.second.getDiagID(), Diag.first) >= DiagnosticsEngine::Warning)
S.PrintInstantiationStack([&](const PartialDiagnosticAt &PD) {
SuppressedDiags.push_back(PD);
});
} else {
S.Diag(Diag.first, Diag.second);
}
}

} // unnamed namespace

void SemaSYCL::CheckSYCLEntryPointFunctionDecl(FunctionDecl *FD) {
// Ensure that all attributes present on the declaration are consistent
// and warn about any redundant ones.
const SYCLKernelEntryPointAttr *SKEPAttr = nullptr;
for (auto SAI = FD->specific_attr_begin<SYCLKernelEntryPointAttr>();
SAI != FD->specific_attr_end<SYCLKernelEntryPointAttr>();
++SAI) {
SAI != FD->specific_attr_end<SYCLKernelEntryPointAttr>(); ++SAI) {
if (!SKEPAttr) {
SKEPAttr = *SAI;
continue;
}
if (!getASTContext().hasSameType(SAI->getKernelName(),
SKEPAttr->getKernelName())) {
Diag(SAI->getLocation(), diag::err_sycl_entry_point_invalid_redeclaration)
<< SAI->getKernelName() << SKEPAttr->getKernelName();
Diag(SKEPAttr->getLocation(), diag::note_previous_attribute);
FD->setInvalidDecl();
DiagnoseSYCLEntryPoint(
SemaRef, FD,
{SAI->getLocation(),
PDiag(diag::err_sycl_entry_point_invalid_redeclaration)
<< SAI->getKernelName() << SKEPAttr->getKernelName()});
DiagnoseSYCLEntryPoint(
SemaRef, FD,
{SKEPAttr->getLocation(), PDiag(diag::note_previous_attribute)});
} else {
Diag(SAI->getLocation(),
diag::warn_sycl_entry_point_redundant_declaration);
Diag(SKEPAttr->getLocation(), diag::note_previous_attribute);
DiagnoseSYCLEntryPoint(
SemaRef, FD,
{SAI->getLocation(),
PDiag(diag::warn_sycl_entry_point_redundant_declaration)});
DiagnoseSYCLEntryPoint(
SemaRef, FD,
{SKEPAttr->getLocation(), PDiag(diag::note_previous_attribute)});
}
}
assert(SKEPAttr && "Missing sycl_kernel_entry_point attribute");

// Ensure the kernel name type is a class type.
if (!SKEPAttr->getKernelName()->isDependentType() &&
!SKEPAttr->getKernelName()->isStructureOrClassType()) {
Diag(SKEPAttr->getLocation(), diag::err_sycl_kernel_name_type);
FD->setInvalidDecl();
DiagnoseSYCLEntryPoint(
SemaRef, FD,
{SKEPAttr->getLocation(), PDiag(diag::err_sycl_kernel_name_type)});
}

// Ensure that an attribute present on the previous declaration
Expand All @@ -301,61 +359,55 @@ void SemaSYCL::CheckSYCLEntryPointFunctionDecl(FunctionDecl *FD) {
if (PrevSKEPAttr) {
if (!getASTContext().hasSameType(SKEPAttr->getKernelName(),
PrevSKEPAttr->getKernelName())) {
Diag(SKEPAttr->getLocation(),
diag::err_sycl_entry_point_invalid_redeclaration)
<< SKEPAttr->getKernelName() << PrevSKEPAttr->getKernelName();
Diag(PrevSKEPAttr->getLocation(), diag::note_previous_decl)
<< PrevFD;;
FD->setInvalidDecl();
DiagnoseSYCLEntryPoint(
SemaRef, FD,
{SKEPAttr->getLocation(),
PDiag(diag::err_sycl_entry_point_invalid_redeclaration)
<< SKEPAttr->getKernelName()
<< PrevSKEPAttr->getKernelName()});
DiagnoseSYCLEntryPoint(SemaRef, FD,
{PrevSKEPAttr->getLocation(),
PDiag(diag::note_previous_decl) << PrevFD});
}
}
}

if (auto *MD = dyn_cast<CXXMethodDecl>(FD)) {
if (!MD->isStatic()) {
Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_invalid)
<< /*non-static member function*/0;
FD->setInvalidDecl();
}
}
if (FD->isVariadic()) {
Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_invalid)
<< /*variadic function*/1;
FD->setInvalidDecl();
}
if (FD->isConsteval()) {
Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_invalid)
<< /*consteval function*/5;
FD->setInvalidDecl();
} else if (FD->isConstexpr()) {
Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_invalid)
<< /*constexpr function*/4;
FD->setInvalidDecl();
}
if (FD->isNoReturn()) {
Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_invalid)
<< /*noreturn function*/6;
FD->setInvalidDecl();
DiagnoseSYCLEntryPoint(
SemaRef, FD,
{SKEPAttr->getLocation(), PDiag(diag::err_sycl_entry_point_invalid)
<< /*noreturn function*/ 6});
}

if (!FD->getReturnType()->isVoidType()) {
Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_return_type);
FD->setInvalidDecl();
DiagnoseSYCLEntryPoint(SemaRef, FD,
{SKEPAttr->getLocation(),
PDiag(diag::err_sycl_entry_point_return_type)});
}

if (!FD->isInvalidDecl() && !FD->isDependentContext()) {
const SYCLKernelInfo *SKI =
getASTContext().findSYCLKernelInfo(SKEPAttr->getKernelName());
if (SKI) {
if (!declaresSameEntity(FD, SKI->GetKernelEntryPointDecl())) {
// FIXME: This diagnostic should include the origin of the kernel
// FIXME: names; not just the locations of the conflicting declarations.
Diag(FD->getLocation(), diag::err_sycl_kernel_name_conflict);
Diag(SKI->GetKernelEntryPointDecl()->getLocation(),
diag::note_previous_declaration);
FD->setInvalidDecl();
DiagnoseSYCLEntryPoint(
SemaRef, FD,
{FD->getLocation(), PDiag(diag::err_sycl_kernel_name_conflict)
<< SKEPAttr->getKernelName()});
DiagnoseSYCLEntryPoint(SemaRef, FD,
{SKI->GetKernelEntryPointDecl()->getLocation(),
PDiag(diag::note_previous_declaration)});
for (const PartialDiagnosticAt &PD :
SYCLKernelEntryContextNotes.at(SKI->GetKernelEntryPointDecl())) {
DiagnoseSYCLEntryPoint(SemaRef, FD, PD);
}
}
} else {
// Note: In order to not interfere with SFINAE, we delay the diagnostic of
// conflicting names to when we act on the attribute.
ContextNotes &Notes = SYCLKernelEntryContextNotes[FD];
SemaRef.PrintInstantiationStack(
[&](const PartialDiagnosticAt &PD) { Notes.push_back(PD); });
getASTContext().registerSYCLEntryPointFunction(FD);
}
}
Expand All @@ -373,8 +425,10 @@ StmtResult SemaSYCL::BuildSYCLKernelCallStmt(FunctionDecl *FD, Stmt *Body) {
// stored declaration matches.
const SYCLKernelInfo &SKI =
getASTContext().getSYCLKernelInfo(SKEPAttr->getKernelName());
if (!declaresSameEntity(SKI.GetKernelEntryPointDecl(), FD))
llvm::report_fatal_error("SYCL kernel name conflict");
if (!declaresSameEntity(SKI.GetKernelEntryPointDecl(), FD)) {
FD->setInvalidDecl();
return {/*Invalid=*/true};
}

using ParmDeclMap = OutlinedFunctionDeclBodyInstantiator::ParmDeclMap;
ParmDeclMap ParmMap;
Expand All @@ -401,3 +455,17 @@ StmtResult SemaSYCL::BuildSYCLKernelCallStmt(FunctionDecl *FD, Stmt *Body) {

return NewBody;
}

bool SemaSYCL::EmitDelayedKernelEntryPointDiagnostics(Decl *FD) {
auto Pos = EntryPointSuppressedDiagnostics.find(FD);
if (Pos != EntryPointSuppressedDiagnostics.end()) {
for (const PartialDiagnosticAt &Suppressed : Pos->second) {
DiagnosticBuilder Builder(SemaRef.Diags.Report(
Suppressed.first, Suppressed.second.getDiagID()));
Suppressed.second.Emit(Builder);
}
Pos->second.clear();
return true;
}
return false;
}
2 changes: 2 additions & 0 deletions clang/lib/Sema/SemaTemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11684,6 +11684,8 @@ DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
diag::ext_explicit_instantiation_without_qualified_id)
<< Specialization << D.getCXXScopeSpec().getRange();

SYCL().EmitDelayedKernelEntryPointDiagnostics(Specialization);

CheckExplicitInstantiation(
*this,
FunTmpl ? (NamedDecl *)FunTmpl
Expand Down
Loading