Skip to content

[SYCL] Replace hardcoded namespaces with attribute #6674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 6, 2022
Merged
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
17 changes: 17 additions & 0 deletions clang/include/clang/Basic/Attr.td
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,23 @@ def SYCLSpecialClass: InheritableAttr {
let Documentation = [SYCLSpecialClassDocs];
}

def SYCLType: InheritableAttr {
let Spellings = [CXX11<"__sycl_detail__", "sycl_type">];
let Subjects = SubjectList<[CXXRecord, Enum], ErrorDiag>;
let LangOpts = [SYCLIsDevice, SYCLIsHost];
let Args = [EnumArgument<"Type", "SYCLType",
["accessor", "local_accessor", "spec_constant",
"specialization_id", "kernel_handler", "buffer_location",
"no_alias", "accessor_property_list", "group",
"private_memory", "aspect"],
["accessor", "local_accessor", "spec_constant",
"specialization_id", "kernel_handler", "buffer_location",
"no_alias", "accessor_property_list", "group",
"private_memory", "aspect"]>];
// Only used internally by SYCL implementation
let Documentation = [InternalOnly];
}

def SYCLDeviceHas : InheritableAttr {
let Spellings = [CXX11<"sycl", "device_has">];
let Subjects = SubjectList<[Function], ErrorDiag>;
Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -4098,6 +4098,8 @@ def warn_transparent_union_attribute_zero_fields : Warning<
def warn_attribute_type_not_supported : Warning<
"%0 attribute argument not supported: %1">,
InGroup<IgnoredAttributes>;
def err_attribute_argument_not_supported : Error<
"%0 attribute argument %1 is not supported">;
def warn_attribute_unknown_visibility : Warning<"unknown visibility %0">,
InGroup<IgnoredAttributes>;
def warn_attribute_protected_visibility :
Expand Down
9 changes: 8 additions & 1 deletion clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -10858,6 +10858,9 @@ class Sema final {
ReqdWorkGroupSizeAttr *
MergeReqdWorkGroupSizeAttr(Decl *D, const ReqdWorkGroupSizeAttr &A);

SYCLTypeAttr *MergeSYCLTypeAttr(Decl *D, const AttributeCommonInfo &CI,
SYCLTypeAttr::SYCLType TypeName);

/// Only called on function definitions; if there is a MSVC #pragma optimize
/// in scope, consider changing the function's attributes based on the
/// optimization list passed to the pragma.
Expand Down Expand Up @@ -13546,12 +13549,16 @@ class Sema final {
const CXXRecordDecl *RecTy = Ty->getAsCXXRecordDecl();
if (!RecTy)
return false;

if (RecTy->hasAttr<AttrTy>())
return true;

if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RecTy)) {
ClassTemplateDecl *Template = CTSD->getSpecializedTemplate();
if (CXXRecordDecl *RD = Template->getTemplatedDecl())
return RD->hasAttr<AttrTy>();
}
return RecTy->hasAttr<AttrTy>();
return false;
}

private:
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2918,6 +2918,8 @@ static bool mergeDeclAttribute(Sema &S, NamedDecl *D,
NewAttr = S.MergeSYCLDeviceHasAttr(D, *A);
else if (const auto *A = dyn_cast<SYCLUsesAspectsAttr>(Attr))
NewAttr = S.MergeSYCLUsesAspectsAttr(D, *A);
else if (const auto *A = dyn_cast<SYCLTypeAttr>(Attr))
NewAttr = S.MergeSYCLTypeAttr(D, *A, A->getType());
else if (const auto *A = dyn_cast<SYCLIntelPipeIOAttr>(Attr))
NewAttr = S.MergeSYCLIntelPipeIOAttr(D, *A);
else if (const auto *A = dyn_cast<SYCLIntelMaxWorkGroupSizeAttr>(Attr))
Expand Down
73 changes: 39 additions & 34 deletions clang/lib/Sema/SemaDeclAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10426,46 +10426,15 @@ static void handleFunctionReturnThunksAttr(Sema &S, Decl *D,
D->addAttr(FunctionReturnThunksAttr::Create(S.Context, Kind, AL));
}

static constexpr std::pair<Decl::Kind, StringRef>
MakeDeclContextDesc(Decl::Kind K, StringRef SR) {
return std::pair<Decl::Kind, StringRef>{K, SR};
}

// FIXME: Refactor Util class in SemaSYCL.cpp to avoid following
// code duplication.
bool isDeviceAspectType(const QualType Ty) {
const EnumType *ET = Ty->getAs<EnumType>();
if (!ET)
return false;

std::array<std::pair<Decl::Kind, StringRef>, 3> Scopes = {
MakeDeclContextDesc(Decl::Kind::Namespace, "sycl"),
MakeDeclContextDesc(Decl::Kind::Namespace, "_V1"),
MakeDeclContextDesc(Decl::Kind::Enum, "aspect")};

const auto *Ctx = cast<DeclContext>(ET->getDecl());
StringRef Name = "";

for (const auto &Scope : llvm::reverse(Scopes)) {
Decl::Kind DK = Ctx->getDeclKind();
if (DK != Scope.first)
return false;
if (const auto *Attr = ET->getDecl()->getAttr<SYCLTypeAttr>())
return Attr->getType() == SYCLTypeAttr::aspect;

switch (DK) {
case Decl::Kind::Enum:
Name = cast<EnumDecl>(Ctx)->getName();
break;
case Decl::Kind::Namespace:
Name = cast<NamespaceDecl>(Ctx)->getName();
break;
default:
llvm_unreachable("isDeviceAspectType: decl kind not supported");
}
if (Name != Scope.second)
return false;
Ctx = Ctx->getParent();
}
return Ctx->isTranslationUnit();
return false;
}

SYCLDeviceHasAttr *Sema::MergeSYCLDeviceHasAttr(Decl *D,
Expand Down Expand Up @@ -10590,6 +10559,39 @@ static void handleSYCLKernelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
handleSimpleAttribute<SYCLKernelAttr>(S, D, AL);
}

SYCLTypeAttr *Sema::MergeSYCLTypeAttr(Decl *D, const AttributeCommonInfo &CI,
SYCLTypeAttr::SYCLType TypeName) {
if (const auto *ExistingAttr = D->getAttr<SYCLTypeAttr>()) {
if (ExistingAttr->getType() != TypeName) {
Diag(ExistingAttr->getLoc(), diag::err_duplicate_attribute)
<< ExistingAttr;
Diag(CI.getLoc(), diag::note_previous_attribute);
}
// Do not add duplicate attribute
return nullptr;
}
return ::new (Context) SYCLTypeAttr(Context, CI, TypeName);
}

static void handleSYCLTypeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
if (!AL.isArgIdent(0)) {
S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
<< AL << AANT_ArgumentIdentifier;
return;
}

IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
SYCLTypeAttr::SYCLType Type;

if (!SYCLTypeAttr::ConvertStrToSYCLType(II->getName(), Type)) {
S.Diag(AL.getLoc(), diag::err_attribute_argument_not_supported) << AL << II;
return;
}

if (SYCLTypeAttr *NewAttr = S.MergeSYCLTypeAttr(D, AL, Type))
D->addAttr(NewAttr);
}

static void handleDestroyAttr(Sema &S, Decl *D, const ParsedAttr &A) {
if (!cast<VarDecl>(D)->hasGlobalStorage()) {
S.Diag(D->getLocation(), diag::err_destroy_attr_on_non_static_var)
Expand Down Expand Up @@ -11142,6 +11144,9 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL,
case ParsedAttr::AT_SYCLSpecialClass:
handleSimpleAttribute<SYCLSpecialClassAttr>(S, D, AL);
break;
case ParsedAttr::AT_SYCLType:
handleSYCLTypeAttr(S, D, AL);
break;
case ParsedAttr::AT_SYCLDevice:
handleSYCLDeviceAttr(S, D, AL);
break;
Expand Down
Loading