From d409f95eecdb482c40dd51f9d104bba14f9f3e97 Mon Sep 17 00:00:00 2001 From: Erich Keane Date: Wed, 7 Apr 2021 07:23:45 -0700 Subject: [PATCH 1/3] [NFC][SYCL] Fix some uses of stringref& and qualtype& Identified elsewhere, these are a few cases of 'simple' types that are intended to be copied being passed around as const references. This patch just replaces them. Also, StringRefs for can be just llvm::StringLiteral for literals, since that does constexpr construction. --- clang/lib/Sema/SemaSYCL.cpp | 58 +++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/clang/lib/Sema/SemaSYCL.cpp b/clang/lib/Sema/SemaSYCL.cpp index 30040a2d9866..215e6135e0e3 100644 --- a/clang/lib/Sema/SemaSYCL.cpp +++ b/clang/lib/Sema/SemaSYCL.cpp @@ -72,34 +72,34 @@ class Util { /// Checks whether given clang type is a full specialization of the SYCL /// accessor class. - static bool isSyclAccessorType(const QualType &Ty); + static bool isSyclAccessorType(QualType Ty); /// Checks whether given clang type is a full specialization of the SYCL /// sampler class. - static bool isSyclSamplerType(const QualType &Ty); + static bool isSyclSamplerType(QualType Ty); /// Checks whether given clang type is a full specialization of the SYCL /// stream class. - static bool isSyclStreamType(const QualType &Ty); + static bool isSyclStreamType(QualType Ty); /// Checks whether given clang type is a full specialization of the SYCL /// half class. - static bool isSyclHalfType(const QualType &Ty); + static bool isSyclHalfType(QualType Ty); /// Checks whether given clang type is a full specialization of the SYCL /// accessor_property_list class. - static bool isAccessorPropertyListType(const QualType &Ty); + static bool isAccessorPropertyListType(QualType Ty); /// Checks whether given clang type is a full specialization of the SYCL /// buffer_location class. - static bool isSyclBufferLocationType(const QualType &Ty); + static bool isSyclBufferLocationType(QualType Ty); /// Checks whether given clang type is a standard SYCL API class with given /// name. /// \param Ty the clang type being checked /// \param Name the class name checked against /// \param Tmpl whether the class is template instantiation or simple record - static bool isSyclType(const QualType &Ty, StringRef Name, bool Tmpl = false); + static bool isSyclType(QualType Ty, StringRef Name, bool Tmpl = false); /// Checks whether given function is a standard SYCL API function with given /// name. @@ -109,11 +109,11 @@ class Util { /// Checks whether given clang type is a full specialization of the SYCL /// specialization constant class. - static bool isSyclSpecConstantType(const QualType &Ty); + static bool isSyclSpecConstantType(QualType Ty); /// Checks whether given clang type is a full specialization of the SYCL /// kernel_handler class. - static bool isSyclKernelHandlerType(const QualType &Ty); + static bool isSyclKernelHandlerType(QualType Ty); // Checks declaration context hierarchy. /// \param DC the context of the item to be checked. @@ -127,7 +127,7 @@ class Util { /// \param Ty the clang type being checked /// \param Scopes the declaration scopes leading from the type to the /// translation unit (excluding the latter) - static bool matchQualifiedTypeName(const QualType &Ty, + static bool matchQualifiedTypeName(QualType Ty, ArrayRef Scopes); }; @@ -1321,7 +1321,7 @@ class SyclKernelFieldChecker : public SyclKernelFieldHandler { DiagnosticsEngine &Diag; // Check whether the object should be disallowed from being copied to kernel. // Return true if not copyable, false if copyable. - bool checkNotCopyableToKernel(const FieldDecl *FD, const QualType &FieldTy) { + bool checkNotCopyableToKernel(const FieldDecl *FD, QualType FieldTy) { if (FieldTy->isArrayType()) { if (const auto *CAT = SemaRef.getASTContext().getAsConstantArrayType(FieldTy)) { @@ -4305,20 +4305,16 @@ bool SYCLIntegrationFooter::emit(raw_ostream &O) { // Utility class methods // ----------------------------------------------------------------------------- -bool Util::isSyclAccessorType(const QualType &Ty) { +bool Util::isSyclAccessorType(QualType Ty) { return isSyclType(Ty, "accessor", true /*Tmpl*/); } -bool Util::isSyclSamplerType(const QualType &Ty) { - return isSyclType(Ty, "sampler"); -} +bool Util::isSyclSamplerType(QualType Ty) { return isSyclType(Ty, "sampler"); } -bool Util::isSyclStreamType(const QualType &Ty) { - return isSyclType(Ty, "stream"); -} +bool Util::isSyclStreamType(QualType Ty) { return isSyclType(Ty, "stream"); } -bool Util::isSyclHalfType(const QualType &Ty) { - const StringRef &Name = "half"; +bool Util::isSyclHalfType(QualType Ty) { + llvm::StringLiteral Name = "half"; std::array Scopes = { Util::DeclContextDesc{clang::Decl::Kind::Namespace, "cl"}, Util::DeclContextDesc{clang::Decl::Kind::Namespace, "sycl"}, @@ -4328,8 +4324,8 @@ bool Util::isSyclHalfType(const QualType &Ty) { return matchQualifiedTypeName(Ty, Scopes); } -bool Util::isSyclSpecConstantType(const QualType &Ty) { - const StringRef &Name = "spec_constant"; +bool Util::isSyclSpecConstantType(QualType Ty) { + llvm::StringLiteral Name = "spec_constant"; std::array Scopes = { Util::DeclContextDesc{clang::Decl::Kind::Namespace, "cl"}, Util::DeclContextDesc{clang::Decl::Kind::Namespace, "sycl"}, @@ -4339,8 +4335,8 @@ bool Util::isSyclSpecConstantType(const QualType &Ty) { return matchQualifiedTypeName(Ty, Scopes); } -bool Util::isSyclKernelHandlerType(const QualType &Ty) { - const StringRef &Name = "kernel_handler"; +bool Util::isSyclKernelHandlerType(QualType Ty) { + llvm::StringLiteral Name = "kernel_handler"; std::array Scopes = { Util::DeclContextDesc{clang::Decl::Kind::Namespace, "cl"}, Util::DeclContextDesc{clang::Decl::Kind::Namespace, "sycl"}, @@ -4348,9 +4344,9 @@ bool Util::isSyclKernelHandlerType(const QualType &Ty) { return matchQualifiedTypeName(Ty, Scopes); } -bool Util::isSyclBufferLocationType(const QualType &Ty) { - const StringRef &PropertyName = "buffer_location"; - const StringRef &InstanceName = "instance"; +bool Util::isSyclBufferLocationType(QualType Ty) { + llvm::StringLiteral PropertyName = "buffer_location"; + llvm::StringLiteral InstanceName = "instance"; std::array Scopes = { Util::DeclContextDesc{Decl::Kind::Namespace, "cl"}, Util::DeclContextDesc{Decl::Kind::Namespace, "sycl"}, @@ -4362,7 +4358,7 @@ bool Util::isSyclBufferLocationType(const QualType &Ty) { return matchQualifiedTypeName(Ty, Scopes); } -bool Util::isSyclType(const QualType &Ty, StringRef Name, bool Tmpl) { +bool Util::isSyclType(QualType Ty, StringRef Name, bool Tmpl) { Decl::Kind ClassDeclKind = Tmpl ? Decl::Kind::ClassTemplateSpecialization : Decl::Kind::CXXRecord; std::array Scopes = { @@ -4387,8 +4383,8 @@ bool Util::isSyclFunction(const FunctionDecl *FD, StringRef Name) { return matchContext(DC, Scopes); } -bool Util::isAccessorPropertyListType(const QualType &Ty) { - const StringRef &Name = "accessor_property_list"; +bool Util::isAccessorPropertyListType(QualType Ty) { + llvm::StringLiteral Name = "accessor_property_list"; std::array Scopes = { Util::DeclContextDesc{clang::Decl::Kind::Namespace, "cl"}, Util::DeclContextDesc{clang::Decl::Kind::Namespace, "sycl"}, @@ -4428,7 +4424,7 @@ bool Util::matchContext(const DeclContext *Ctx, return Ctx->isTranslationUnit(); } -bool Util::matchQualifiedTypeName(const QualType &Ty, +bool Util::matchQualifiedTypeName(QualType Ty, ArrayRef Scopes) { const CXXRecordDecl *RecTy = Ty->getAsCXXRecordDecl(); From e190e7343dc51a102794be19f9624a78d991e871 Mon Sep 17 00:00:00 2001 From: Erich Keane Date: Wed, 7 Apr 2021 08:28:00 -0700 Subject: [PATCH 2/3] Better clean up DeclContextDesc uses, add a 'make' function to ensure constexpr construction --- clang/lib/Sema/SemaSYCL.cpp | 87 ++++++++++++++++++++----------------- 1 file changed, 47 insertions(+), 40 deletions(-) diff --git a/clang/lib/Sema/SemaSYCL.cpp b/clang/lib/Sema/SemaSYCL.cpp index 215e6135e0e3..88a3afe6f1b8 100644 --- a/clang/lib/Sema/SemaSYCL.cpp +++ b/clang/lib/Sema/SemaSYCL.cpp @@ -68,7 +68,18 @@ namespace { /// Various utilities. class Util { public: - using DeclContextDesc = std::pair; + using DeclContextDesc = std::pair; + + template + static constexpr DeclContextDesc MakeDeclContextDesc(Decl::Kind K, + const char (&Str)[N]) { + return DeclContextDesc{K, llvm::StringLiteral{Str}}; + } + + static constexpr DeclContextDesc MakeDeclContextDesc(Decl::Kind K, + llvm::StringRef SR) { + return DeclContextDesc{K, SR}; + } /// Checks whether given clang type is a full specialization of the SYCL /// accessor class. @@ -4314,47 +4325,43 @@ bool Util::isSyclSamplerType(QualType Ty) { return isSyclType(Ty, "sampler"); } bool Util::isSyclStreamType(QualType Ty) { return isSyclType(Ty, "stream"); } bool Util::isSyclHalfType(QualType Ty) { - llvm::StringLiteral Name = "half"; std::array Scopes = { - Util::DeclContextDesc{clang::Decl::Kind::Namespace, "cl"}, - Util::DeclContextDesc{clang::Decl::Kind::Namespace, "sycl"}, - Util::DeclContextDesc{clang::Decl::Kind::Namespace, "detail"}, - Util::DeclContextDesc{clang::Decl::Kind::Namespace, "half_impl"}, - Util::DeclContextDesc{Decl::Kind::CXXRecord, Name}}; + Util::MakeDeclContextDesc(Decl::Kind::Namespace, "cl"), + Util::MakeDeclContextDesc(Decl::Kind::Namespace, "sycl"), + Util::MakeDeclContextDesc(Decl::Kind::Namespace, "detail"), + Util::MakeDeclContextDesc(Decl::Kind::Namespace, "half_impl"), + Util::MakeDeclContextDesc(Decl::Kind::CXXRecord, "half")}; return matchQualifiedTypeName(Ty, Scopes); } bool Util::isSyclSpecConstantType(QualType Ty) { - llvm::StringLiteral Name = "spec_constant"; std::array Scopes = { - Util::DeclContextDesc{clang::Decl::Kind::Namespace, "cl"}, - Util::DeclContextDesc{clang::Decl::Kind::Namespace, "sycl"}, - Util::DeclContextDesc{clang::Decl::Kind::Namespace, "ONEAPI"}, - Util::DeclContextDesc{clang::Decl::Kind::Namespace, "experimental"}, - Util::DeclContextDesc{Decl::Kind::ClassTemplateSpecialization, Name}}; + Util::MakeDeclContextDesc(Decl::Kind::Namespace, "cl"), + Util::MakeDeclContextDesc(Decl::Kind::Namespace, "sycl"), + Util::MakeDeclContextDesc(Decl::Kind::Namespace, "ONEAPI"), + Util::MakeDeclContextDesc(Decl::Kind::Namespace, "experimental"), + Util::MakeDeclContextDesc(Decl::Kind::ClassTemplateSpecialization, + "spec_constant")}; return matchQualifiedTypeName(Ty, Scopes); } bool Util::isSyclKernelHandlerType(QualType Ty) { - llvm::StringLiteral Name = "kernel_handler"; std::array Scopes = { - Util::DeclContextDesc{clang::Decl::Kind::Namespace, "cl"}, - Util::DeclContextDesc{clang::Decl::Kind::Namespace, "sycl"}, - Util::DeclContextDesc{Decl::Kind::CXXRecord, Name}}; + Util::MakeDeclContextDesc(Decl::Kind::Namespace, "cl"), + Util::MakeDeclContextDesc(Decl::Kind::Namespace, "sycl"), + Util::MakeDeclContextDesc(Decl::Kind::CXXRecord, "kernel_handler")}; return matchQualifiedTypeName(Ty, Scopes); } bool Util::isSyclBufferLocationType(QualType Ty) { - llvm::StringLiteral PropertyName = "buffer_location"; - llvm::StringLiteral InstanceName = "instance"; std::array Scopes = { - Util::DeclContextDesc{Decl::Kind::Namespace, "cl"}, - Util::DeclContextDesc{Decl::Kind::Namespace, "sycl"}, - Util::DeclContextDesc{Decl::Kind::Namespace, "INTEL"}, - Util::DeclContextDesc{Decl::Kind::Namespace, "property"}, - Util::DeclContextDesc{Decl::Kind::CXXRecord, PropertyName}, - Util::DeclContextDesc{Decl::Kind::ClassTemplateSpecialization, - InstanceName}}; + Util::MakeDeclContextDesc(Decl::Kind::Namespace, "cl"), + Util::MakeDeclContextDesc(Decl::Kind::Namespace, "sycl"), + Util::MakeDeclContextDesc(Decl::Kind::Namespace, "INTEL"), + Util::MakeDeclContextDesc(Decl::Kind::Namespace, "property"), + Util::MakeDeclContextDesc(Decl::Kind::CXXRecord, "buffer_location"), + Util::MakeDeclContextDesc(Decl::Kind::ClassTemplateSpecialization, + "instance")}; return matchQualifiedTypeName(Ty, Scopes); } @@ -4362,9 +4369,9 @@ bool Util::isSyclType(QualType Ty, StringRef Name, bool Tmpl) { Decl::Kind ClassDeclKind = Tmpl ? Decl::Kind::ClassTemplateSpecialization : Decl::Kind::CXXRecord; std::array Scopes = { - Util::DeclContextDesc{clang::Decl::Kind::Namespace, "cl"}, - Util::DeclContextDesc{clang::Decl::Kind::Namespace, "sycl"}, - Util::DeclContextDesc{ClassDeclKind, Name}}; + Util::MakeDeclContextDesc(Decl::Kind::Namespace, "cl"), + Util::MakeDeclContextDesc(Decl::Kind::Namespace, "sycl"), + Util::MakeDeclContextDesc(ClassDeclKind, Name)}; return matchQualifiedTypeName(Ty, Scopes); } @@ -4378,18 +4385,18 @@ bool Util::isSyclFunction(const FunctionDecl *FD, StringRef Name) { return false; std::array Scopes = { - Util::DeclContextDesc{clang::Decl::Kind::Namespace, "cl"}, - Util::DeclContextDesc{clang::Decl::Kind::Namespace, "sycl"}}; + Util::MakeDeclContextDesc(Decl::Kind::Namespace, "cl"), + Util::MakeDeclContextDesc(Decl::Kind::Namespace, "sycl")}; return matchContext(DC, Scopes); } bool Util::isAccessorPropertyListType(QualType Ty) { - llvm::StringLiteral Name = "accessor_property_list"; std::array Scopes = { - Util::DeclContextDesc{clang::Decl::Kind::Namespace, "cl"}, - Util::DeclContextDesc{clang::Decl::Kind::Namespace, "sycl"}, - Util::DeclContextDesc{clang::Decl::Kind::Namespace, "ONEAPI"}, - Util::DeclContextDesc{Decl::Kind::ClassTemplateSpecialization, Name}}; + Util::MakeDeclContextDesc(Decl::Kind::Namespace, "cl"), + Util::MakeDeclContextDesc(Decl::Kind::Namespace, "sycl"), + Util::MakeDeclContextDesc(Decl::Kind::Namespace, "ONEAPI"), + Util::MakeDeclContextDesc(Decl::Kind::ClassTemplateSpecialization, + "accessor_property_list")}; return matchQualifiedTypeName(Ty, Scopes); } @@ -4401,17 +4408,17 @@ bool Util::matchContext(const DeclContext *Ctx, StringRef Name = ""; for (const auto &Scope : llvm::reverse(Scopes)) { - clang::Decl::Kind DK = Ctx->getDeclKind(); + Decl::Kind DK = Ctx->getDeclKind(); if (DK != Scope.first) return false; switch (DK) { - case clang::Decl::Kind::ClassTemplateSpecialization: + case Decl::Kind::ClassTemplateSpecialization: // ClassTemplateSpecializationDecl inherits from CXXRecordDecl - case clang::Decl::Kind::CXXRecord: + case Decl::Kind::CXXRecord: Name = cast(Ctx)->getName(); break; - case clang::Decl::Kind::Namespace: + case Decl::Kind::Namespace: Name = cast(Ctx)->getName(); break; default: From bdcb819455c5787c00cf08b9093f72e3e30584e9 Mon Sep 17 00:00:00 2001 From: Erich Keane Date: Wed, 7 Apr 2021 09:06:42 -0700 Subject: [PATCH 3/3] Aaron's suggestion to remove llvm:: Co-authored-by: Aaron Ballman --- clang/lib/Sema/SemaSYCL.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/lib/Sema/SemaSYCL.cpp b/clang/lib/Sema/SemaSYCL.cpp index 88a3afe6f1b8..774045145f69 100644 --- a/clang/lib/Sema/SemaSYCL.cpp +++ b/clang/lib/Sema/SemaSYCL.cpp @@ -77,7 +77,7 @@ class Util { } static constexpr DeclContextDesc MakeDeclContextDesc(Decl::Kind K, - llvm::StringRef SR) { + StringRef SR) { return DeclContextDesc{K, SR}; }