Skip to content

[SYCL] Reland https://github.com/llvm/llvm-project/pull/114899 #16495

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 1 commit into from
Jan 3, 2025
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
14 changes: 14 additions & 0 deletions clang/include/clang/Basic/AttributeCommonInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ class AttributeCommonInfo {
IgnoredAttribute,
UnknownAttribute,
};
enum class Scope {
NONE,
CLANG,
GNU,
MSVC,
OMP,
HLSL,
GSL,
RISCV,
INTEL,
SYCL,
CL,
SYCL_DETAIL
};

private:
const IdentifierInfo *AttrName = nullptr;
Expand Down
26 changes: 24 additions & 2 deletions clang/lib/Basic/Attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#include "clang/Basic/ParsedAttrInfo.h"
#include "clang/Basic/TargetInfo.h"

#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringSwitch.h"

using namespace clang;

static int hasAttributeImpl(AttributeCommonInfo::Syntax Syntax, StringRef Name,
Expand Down Expand Up @@ -156,13 +159,32 @@ std::string AttributeCommonInfo::getNormalizedFullName() const {
return static_cast<std::string>(
normalizeName(getAttrName(), getScopeName(), getSyntax()));
}
static AttributeCommonInfo::Scope
getScopeFromNormalizedScopeName(StringRef ScopeName) {
return llvm::StringSwitch<AttributeCommonInfo::Scope>(ScopeName)
.Case("", AttributeCommonInfo::Scope::NONE)
.Case("clang", AttributeCommonInfo::Scope::CLANG)
.Case("gnu", AttributeCommonInfo::Scope::GNU)
.Case("gsl", AttributeCommonInfo::Scope::GSL)
.Case("hlsl", AttributeCommonInfo::Scope::HLSL)
.Case("msvc", AttributeCommonInfo::Scope::MSVC)
.Case("omp", AttributeCommonInfo::Scope::OMP)
.Case("riscv", AttributeCommonInfo::Scope::RISCV)
.Case("intel", AttributeCommonInfo::Scope::INTEL)
.Case("sycl", AttributeCommonInfo::Scope::SYCL)
.Case("cl", AttributeCommonInfo::Scope::CL)
.Case("__sycl_detail__", AttributeCommonInfo::Scope::SYCL_DETAIL);
}

unsigned AttributeCommonInfo::calculateAttributeSpellingListIndex() const {
// Both variables will be used in tablegen generated
// attribute spell list index matching code.
auto Syntax = static_cast<AttributeCommonInfo::Syntax>(getSyntax());
StringRef Scope = normalizeAttrScopeName(getScopeName(), Syntax);
StringRef Name = normalizeAttrName(getAttrName(), Scope, Syntax);
StringRef ScopeName = normalizeAttrScopeName(getScopeName(), Syntax);
StringRef Name = normalizeAttrName(getAttrName(), ScopeName, Syntax);

AttributeCommonInfo::Scope ComputedScope =
getScopeFromNormalizedScopeName(ScopeName);

#include "clang/Sema/AttrSpellingListIndex.inc"
}
58 changes: 49 additions & 9 deletions clang/utils/TableGen/ClangAttrEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3851,19 +3851,59 @@ void EmitClangAttrSpellingListIndex(const RecordKeeper &Records,
const Record &R = *I.second;
std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
OS << " case AT_" << I.first << ": {\n";
for (unsigned I = 0; I < Spellings.size(); ++ I) {
OS << " if (Name == \"" << Spellings[I].name() << "\" && "
<< "getSyntax() == AttributeCommonInfo::AS_" << Spellings[I].variety()
<< " && Scope == \"" << Spellings[I].nameSpace() << "\")\n"
<< " return " << I << ";\n";

// If there are none or one spelling to check, resort to the default
// behavior of returning index as 0.
if (Spellings.size() <= 1) {
OS << " return 0;\n"
<< " break;\n"
<< " }\n";
continue;
}

OS << " break;\n";
OS << " }\n";
std::vector<StringRef> Names;
llvm::transform(Spellings, std::back_inserter(Names),
[](const FlattenedSpelling &FS) { return FS.name(); });
llvm::sort(Names);
Names.erase(llvm::unique(Names), Names.end());

for (const auto &[Idx, FS] : enumerate(Spellings)) {
OS << " if (";
if (Names.size() > 1) {
SmallVector<StringRef, 6> SameLenNames;
StringRef FSName = FS.name();
llvm::copy_if(Names, std::back_inserter(SameLenNames),
[&](StringRef N) { return N.size() == FSName.size(); });

if (SameLenNames.size() == 1) {
OS << "Name.size() == " << FS.name().size() << " && ";
} else {
// FIXME: We currently fall back to comparing entire strings if there
// are 2 or more spelling names with the same length. This can be
// optimized to check only for the the first differing character
// between them instead.
OS << "Name == \"" << FS.name() << "\""
<< " && ";
}
}

OS << "getSyntax() == AttributeCommonInfo::AS_" << FS.variety()
<< " && ComputedScope == ";
if (FS.nameSpace() == "")
OS << "AttributeCommonInfo::Scope::NONE";
else
OS << "AttributeCommonInfo::Scope::" + FS.nameSpace().upper();

OS << ")\n"
<< " return " << Idx << ";\n";
}

OS << " break;\n"
<< " }\n";
}

OS << " }\n";
OS << " return 0;\n";
OS << " }\n"
<< " return 0;\n";
}

// Emits code used by RecursiveASTVisitor to visit attributes
Expand Down
Loading