|
10 | 10 |
|
11 | 11 | #include "clang/Sema/SemaSYCL.h" |
12 | 12 | #include "clang/AST/Mangle.h" |
| 13 | +#include "clang/AST/SYCLKernelInfo.h" |
13 | 14 | #include "clang/AST/TypeOrdering.h" |
| 15 | +#include "clang/Basic/Diagnostic.h" |
14 | 16 | #include "clang/Sema/Attr.h" |
15 | 17 | #include "clang/Sema/ParsedAttr.h" |
16 | 18 | #include "clang/Sema/Sema.h" |
@@ -206,3 +208,127 @@ void SemaSYCL::handleKernelEntryPointAttr(Decl *D, const ParsedAttr &AL) { |
206 | 208 | D->addAttr(::new (SemaRef.Context) |
207 | 209 | SYCLKernelEntryPointAttr(SemaRef.Context, AL, TSI)); |
208 | 210 | } |
| 211 | + |
| 212 | +static SourceLocation SourceLocationForType(QualType QT) { |
| 213 | + SourceLocation Loc; |
| 214 | + const Type *T = QT->getUnqualifiedDesugaredType(); |
| 215 | + if (const TagType *TT = dyn_cast<TagType>(T)) |
| 216 | + Loc = TT->getDecl()->getLocation(); |
| 217 | + else if (const ObjCInterfaceType *ObjCIT = dyn_cast<ObjCInterfaceType>(T)) |
| 218 | + Loc = ObjCIT->getDecl()->getLocation(); |
| 219 | + return Loc; |
| 220 | +} |
| 221 | + |
| 222 | +static bool CheckSYCLKernelName(Sema &S, SourceLocation Loc, |
| 223 | + QualType KernelName) { |
| 224 | + assert(!KernelName->isDependentType()); |
| 225 | + |
| 226 | + if (!KernelName->isStructureOrClassType()) { |
| 227 | + // SYCL 2020 section 5.2, "Naming of kernels", only requires that the |
| 228 | + // kernel name be a C++ typename. However, the definition of "kernel name" |
| 229 | + // in the glossary states that a kernel name is a class type. Neither |
| 230 | + // section explicitly states whether the kernel name type can be |
| 231 | + // cv-qualified. For now, kernel name types are required to be class types |
| 232 | + // and that they may be cv-qualified. The following issue requests |
| 233 | + // clarification from the SYCL WG. |
| 234 | + // https://github.com/KhronosGroup/SYCL-Docs/issues/568 |
| 235 | + S.Diag(Loc, diag::warn_sycl_kernel_name_not_a_class_type) << KernelName; |
| 236 | + SourceLocation DeclTypeLoc = SourceLocationForType(KernelName); |
| 237 | + if (DeclTypeLoc.isValid()) |
| 238 | + S.Diag(DeclTypeLoc, diag::note_entity_declared_at) |
| 239 | + << KernelName; |
| 240 | + return true; |
| 241 | + } |
| 242 | + |
| 243 | + return false; |
| 244 | +} |
| 245 | + |
| 246 | +void SemaSYCL::CheckSYCLEntryPointFunctionDecl(FunctionDecl *FD) { |
| 247 | + // Ensure that all attributes present on the declaration are consistent |
| 248 | + // and warn about any redundant ones. |
| 249 | + const SYCLKernelEntryPointAttr *SKEPAttr = nullptr; |
| 250 | + for (auto SAI = FD->specific_attr_begin<SYCLKernelEntryPointAttr>(); |
| 251 | + SAI != FD->specific_attr_end<SYCLKernelEntryPointAttr>(); |
| 252 | + ++SAI) { |
| 253 | + if (!SKEPAttr) { |
| 254 | + SKEPAttr = *SAI; |
| 255 | + continue; |
| 256 | + } |
| 257 | + if (!getASTContext().hasSameType(SAI->getKernelName(), |
| 258 | + SKEPAttr->getKernelName())) { |
| 259 | + Diag(SAI->getLocation(), diag::err_sycl_entry_point_invalid_redeclaration) |
| 260 | + << SAI->getKernelName() << SKEPAttr->getKernelName(); |
| 261 | + Diag(SKEPAttr->getLocation(), diag::note_previous_attribute); |
| 262 | + } else { |
| 263 | + Diag(SAI->getLocation(), |
| 264 | + diag::warn_sycl_entry_point_redundant_declaration); |
| 265 | + Diag(SKEPAttr->getLocation(), diag::note_previous_attribute); |
| 266 | + } |
| 267 | + } |
| 268 | + assert(SKEPAttr && "Missing sycl_kernel_entry_point attribute"); |
| 269 | + |
| 270 | + // Ensure the kernel name type is valid. |
| 271 | + if (!SKEPAttr->getKernelName()->isDependentType()) { |
| 272 | + CheckSYCLKernelName(SemaRef, SKEPAttr->getLocation(), |
| 273 | + SKEPAttr->getKernelName()); |
| 274 | + } |
| 275 | + |
| 276 | + // Ensure that an attribute present on the previous declaration |
| 277 | + // matches the one on this declaration. |
| 278 | + FunctionDecl *PrevFD = FD->getPreviousDecl(); |
| 279 | + if (PrevFD && !PrevFD->isInvalidDecl()) { |
| 280 | + const auto *PrevSKEPAttr = PrevFD->getAttr<SYCLKernelEntryPointAttr>(); |
| 281 | + if (PrevSKEPAttr) { |
| 282 | + if (!getASTContext().hasSameType(SKEPAttr->getKernelName(), |
| 283 | + PrevSKEPAttr->getKernelName())) { |
| 284 | + Diag(SKEPAttr->getLocation(), |
| 285 | + diag::err_sycl_entry_point_invalid_redeclaration) |
| 286 | + << SKEPAttr->getKernelName() << PrevSKEPAttr->getKernelName(); |
| 287 | + Diag(PrevSKEPAttr->getLocation(), diag::note_previous_decl) |
| 288 | + << PrevFD;; |
| 289 | + } |
| 290 | + } |
| 291 | + } |
| 292 | + |
| 293 | + if (auto *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 294 | + if (!MD->isStatic()) { |
| 295 | + Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_invalid) |
| 296 | + << /*non-static member function*/0; |
| 297 | + } |
| 298 | + } |
| 299 | + if (FD->isVariadic()) { |
| 300 | + Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_invalid) |
| 301 | + << /*variadic function*/1; |
| 302 | + } |
| 303 | + if (FD->isConsteval()) { |
| 304 | + Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_invalid) |
| 305 | + << /*consteval function*/5; |
| 306 | + } else if (FD->isConstexpr()) { |
| 307 | + Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_invalid) |
| 308 | + << /*constexpr function*/4; |
| 309 | + } |
| 310 | + if (FD->isNoReturn()) { |
| 311 | + Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_invalid) |
| 312 | + << /*noreturn function*/6; |
| 313 | + } |
| 314 | + |
| 315 | + if (!FD->getReturnType()->isVoidType()) { |
| 316 | + Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_return_type); |
| 317 | + } |
| 318 | + |
| 319 | + if (!FD->isInvalidDecl() && !FD->isTemplated()) { |
| 320 | + const SYCLKernelInfo *SKI = |
| 321 | + getASTContext().findSYCLKernelInfo(SKEPAttr->getKernelName()); |
| 322 | + if (SKI) { |
| 323 | + if (!declaresSameEntity(FD, SKI->getKernelEntryPointDecl())) { |
| 324 | + // FIXME: This diagnostic should include the origin of the kernel |
| 325 | + // FIXME: names; not just the locations of the conflicting declarations. |
| 326 | + Diag(FD->getLocation(), diag::err_sycl_kernel_name_conflict); |
| 327 | + Diag(SKI->getKernelEntryPointDecl()->getLocation(), |
| 328 | + diag::note_previous_declaration); |
| 329 | + } |
| 330 | + } else { |
| 331 | + getASTContext().registerSYCLEntryPointFunction(FD); |
| 332 | + } |
| 333 | + } |
| 334 | +} |
0 commit comments