Skip to content

Commit 592e875

Browse files
committed
Do not serialize the input Twine more than once
1 parent 5e067b2 commit 592e875

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

llvm/include/llvm/TableGen/CodeGenHelpers.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef LLVM_TABLEGEN_CODEGENHELPERS_H
1414
#define LLVM_TABLEGEN_CODEGENHELPERS_H
1515

16+
#include "llvm/ADT/SmallString.h"
1617
#include "llvm/ADT/StringRef.h"
1718
#include "llvm/ADT/Twine.h"
1819
#include "llvm/Support/raw_ostream.h"
@@ -23,8 +24,8 @@ namespace llvm {
2324
class IfDefEmitter {
2425
public:
2526
IfDefEmitter(raw_ostream &OS, const Twine &Name) : Name(Name.str()), OS(OS) {
26-
OS << "#ifdef " << Name << "\n"
27-
<< "#undef " << Name << "\n\n";
27+
OS << "#ifdef " << this->Name << "\n"
28+
<< "#undef " << this->Name << "\n\n";
2829
}
2930
~IfDefEmitter() { OS << "\n#endif // " << Name << "\n\n"; }
3031

@@ -38,8 +39,8 @@ class IncludeGuardEmitter {
3839
public:
3940
IncludeGuardEmitter(raw_ostream &OS, const Twine &Name)
4041
: Name(Name.str()), OS(OS) {
41-
OS << "#ifndef " << Name << "\n"
42-
<< "#define " << Name << "\n\n";
42+
OS << "#ifndef " << this->Name << "\n"
43+
<< "#define " << this->Name << "\n\n";
4344
}
4445
~IncludeGuardEmitter() { OS << "\n#endif // " << Name << "\n"; }
4546

@@ -53,10 +54,10 @@ class IncludeGuardEmitter {
5354
// namespace scope.
5455
class NamespaceEmitter {
5556
public:
56-
NamespaceEmitter(raw_ostream &OS, const Twine &NameUntrimmed)
57-
: Name(trim(NameUntrimmed)), OS(OS) {
58-
if (!Name.empty())
59-
OS << "namespace " << Name << " {\n";
57+
NamespaceEmitter(raw_ostream &OS, const Twine &Name)
58+
: Name(trim(Name)), OS(OS) {
59+
if (!this->Name.empty())
60+
OS << "namespace " << this->Name << " {\n";
6061
}
6162

6263
~NamespaceEmitter() { close(); }
@@ -76,9 +77,9 @@ class NamespaceEmitter {
7677
// }
7778
//
7879
// and cannot use "namespace ::mlir::toy".
79-
static std::string trim(const Twine &NameUntrimmed) {
80-
std::string Name = NameUntrimmed.str();
81-
StringRef StrRef = Name;
80+
static std::string trim(const Twine &Name) {
81+
SmallString<64> Storage;
82+
StringRef StrRef = Name.toStringRef(Storage);
8283
StrRef.consume_front("::");
8384
return StrRef.str();
8485
}

mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4938,7 +4938,7 @@ static void emitOpDefShard(const RecordKeeper &records,
49384938
ArrayRef<const Record *> defs,
49394939
const Dialect &dialect, unsigned shardIndex,
49404940
unsigned shardCount, raw_ostream &os) {
4941-
IfDefEmitter scope(os, Twine("GET_OP_DEFS_") + Twine(shardIndex));
4941+
IfDefEmitter scope(os, "GET_OP_DEFS_" + Twine(shardIndex));
49424942

49434943
// Emit the op registration hook in the first shard.
49444944
const char *const opRegistrationHook =

0 commit comments

Comments
 (0)