Skip to content

Commit 58a0ea7

Browse files
author
Erich Keane
authored
[NFCI][SYCL] Remove some unnecessary string allocs during int-header emit (#2071)
These two have resulted in some awkwardly written code in order to provide/return std::strings, so replace them with streams, which creates a more natural interface. Additionally, this should reduce the amount of std::string allocations, so we should go a little faster.
1 parent 87b94d5 commit 58a0ea7

File tree

1 file changed

+49
-37
lines changed

1 file changed

+49
-37
lines changed

clang/lib/Sema/SemaSYCL.cpp

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,13 +2115,19 @@ static const char *paramKind2Str(KernelParamKind K) {
21152115
#undef CASE
21162116
}
21172117

2118-
// Removes all "(anonymous namespace)::" substrings from given string
2119-
static std::string eraseAnonNamespace(std::string S) {
2118+
// Removes all "(anonymous namespace)::" substrings from given string, and emits
2119+
// it.
2120+
static void emitWithoutAnonNamespaces(llvm::raw_ostream &OS, StringRef Source) {
21202121
const char S1[] = "(anonymous namespace)::";
21212122

2122-
for (auto Pos = S.find(S1); Pos != StringRef::npos; Pos = S.find(S1, Pos))
2123-
S.erase(Pos, sizeof(S1) - 1);
2124-
return S;
2123+
size_t Pos;
2124+
2125+
while ((Pos = Source.find(S1)) != StringRef::npos) {
2126+
OS << Source.take_front(Pos);
2127+
Source = Source.drop_front(Pos + sizeof(S1) - 1);
2128+
}
2129+
2130+
OS << Source;
21252131
}
21262132

21272133
static bool checkEnumTemplateParameter(const EnumDecl *ED,
@@ -2370,19 +2376,19 @@ void SYCLIntegrationHeader::emitForwardClassDecls(
23702376
}
23712377
}
23722378

2373-
static std::string getCPPTypeString(QualType Ty) {
2379+
static void emitCPPTypeString(raw_ostream &OS, QualType Ty) {
23742380
LangOptions LO;
23752381
PrintingPolicy P(LO);
23762382
P.SuppressTypedefs = true;
2377-
return eraseAnonNamespace(Ty.getAsString(P));
2383+
emitWithoutAnonNamespaces(OS, Ty.getAsString(P));
23782384
}
23792385

23802386
static void printArguments(ASTContext &Ctx, raw_ostream &ArgOS,
23812387
ArrayRef<TemplateArgument> Args,
23822388
const PrintingPolicy &P);
23832389

2384-
static std::string getKernelNameTypeString(QualType T, ASTContext &Ctx,
2385-
const PrintingPolicy &TypePolicy);
2390+
static void emitKernelNameType(QualType T, ASTContext &Ctx, raw_ostream &OS,
2391+
const PrintingPolicy &TypePolicy);
23862392

23872393
static void printArgument(ASTContext &Ctx, raw_ostream &ArgOS,
23882394
TemplateArgument Arg, const PrintingPolicy &P) {
@@ -2413,7 +2419,8 @@ static void printArgument(ASTContext &Ctx, raw_ostream &ArgOS,
24132419
TypePolicy.SuppressTypedefs = true;
24142420
TypePolicy.SuppressTagKeyword = true;
24152421
QualType T = Arg.getAsType();
2416-
ArgOS << getKernelNameTypeString(T, Ctx, TypePolicy);
2422+
2423+
emitKernelNameType(T, Ctx, ArgOS, TypePolicy);
24172424
break;
24182425
}
24192426
case TemplateArgument::ArgKind::Template: {
@@ -2451,42 +2458,46 @@ static void printTemplateArguments(ASTContext &Ctx, raw_ostream &ArgOS,
24512458
ArgOS << ">";
24522459
}
24532460

2454-
static std::string printRecordType(QualType T, const CXXRecordDecl *RD,
2455-
const PrintingPolicy &TypePolicy) {
2461+
static void emitRecordType(raw_ostream &OS, QualType T, const CXXRecordDecl *RD,
2462+
const PrintingPolicy &TypePolicy) {
24562463
SmallString<64> Buf;
2457-
llvm::raw_svector_ostream OS(Buf);
2458-
T.getCanonicalType().getQualifiers().print(OS, TypePolicy,
2464+
llvm::raw_svector_ostream RecOS(Buf);
2465+
T.getCanonicalType().getQualifiers().print(RecOS, TypePolicy,
24592466
/*appendSpaceIfNotEmpty*/ true);
24602467
if (const auto *TSD = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
24612468

24622469
// Print template class name
2463-
TSD->printQualifiedName(OS, TypePolicy, /*WithGlobalNsPrefix*/ true);
2470+
TSD->printQualifiedName(RecOS, TypePolicy, /*WithGlobalNsPrefix*/ true);
24642471

24652472
// Print template arguments substituting enumerators
24662473
ASTContext &Ctx = RD->getASTContext();
24672474
const TemplateArgumentList &Args = TSD->getTemplateArgs();
2468-
printTemplateArguments(Ctx, OS, Args.asArray(), TypePolicy);
2475+
printTemplateArguments(Ctx, RecOS, Args.asArray(), TypePolicy);
24692476

2470-
return eraseAnonNamespace(OS.str().str());
2477+
emitWithoutAnonNamespaces(OS, RecOS.str());
2478+
return;
24712479
}
2472-
if (RD->getDeclContext()->isFunctionOrMethod())
2473-
return eraseAnonNamespace(T.getCanonicalType().getAsString(TypePolicy));
2480+
if (RD->getDeclContext()->isFunctionOrMethod()) {
2481+
emitWithoutAnonNamespaces(OS, T.getCanonicalType().getAsString(TypePolicy));
2482+
return;
2483+
}
2484+
24742485
const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(RD->getDeclContext());
2475-
RD->printQualifiedName(OS, TypePolicy, !(NS && NS->isAnonymousNamespace()));
2476-
return eraseAnonNamespace(OS.str().str());
2486+
RD->printQualifiedName(RecOS, TypePolicy,
2487+
!(NS && NS->isAnonymousNamespace()));
2488+
emitWithoutAnonNamespaces(OS, RecOS.str());
24772489
}
24782490

2479-
static std::string getKernelNameTypeString(QualType T, ASTContext &Ctx,
2480-
const PrintingPolicy &TypePolicy) {
2481-
if (T->isRecordType())
2482-
return printRecordType(T, T->getAsCXXRecordDecl(), TypePolicy);
2483-
if (T->isEnumeralType()) {
2484-
SmallString<64> Buf;
2485-
llvm::raw_svector_ostream OS(Buf);
2486-
OS << "::" << T.getCanonicalType().getAsString(TypePolicy);
2487-
return eraseAnonNamespace(OS.str().str());
2488-
}
2489-
return eraseAnonNamespace(T.getCanonicalType().getAsString(TypePolicy));
2491+
static void emitKernelNameType(QualType T, ASTContext &Ctx, raw_ostream &OS,
2492+
const PrintingPolicy &TypePolicy) {
2493+
if (T->isRecordType()) {
2494+
emitRecordType(OS, T, T->getAsCXXRecordDecl(), TypePolicy);
2495+
return;
2496+
}
2497+
2498+
if (T->isEnumeralType())
2499+
OS << "::";
2500+
emitWithoutAnonNamespaces(OS, T.getCanonicalType().getAsString(TypePolicy));
24902501
}
24912502

24922503
void SYCLIntegrationHeader::emit(raw_ostream &O) {
@@ -2514,9 +2525,9 @@ void SYCLIntegrationHeader::emit(raw_ostream &O) {
25142525
});
25152526
O << "// Specialization constants IDs:\n";
25162527
for (const auto &P : llvm::make_range(SpecConsts.begin(), End)) {
2517-
std::string CPPName = getCPPTypeString(P.first);
2518-
O << "template <> struct sycl::detail::SpecConstantInfo<" << CPPName
2519-
<< "> {\n";
2528+
O << "template <> struct sycl::detail::SpecConstantInfo<";
2529+
emitCPPTypeString(O, P.first);
2530+
O << "> {\n";
25202531
O << " static constexpr const char* getName() {\n";
25212532
O << " return \"" << P.second << "\";\n";
25222533
O << " }\n";
@@ -2608,8 +2619,9 @@ void SYCLIntegrationHeader::emit(raw_ostream &O) {
26082619
LangOptions LO;
26092620
PrintingPolicy P(LO);
26102621
P.SuppressTypedefs = true;
2611-
O << "template <> struct KernelInfo<"
2612-
<< getKernelNameTypeString(K.NameType, S.getASTContext(), P) << "> {\n";
2622+
O << "template <> struct KernelInfo<";
2623+
emitKernelNameType(K.NameType, S.getASTContext(), O, P);
2624+
O << "> {\n";
26132625
}
26142626
O << " DLL_LOCAL\n";
26152627
O << " static constexpr const char* getName() { return \"" << K.Name

0 commit comments

Comments
 (0)