Skip to content

Commit 95215a3

Browse files
authored
[NFC][MLIR][TableGen] Change emitSummaryAndDescComments to write to os directly (llvm#162014)
Change `emitSummaryAndDescComments` to directly write to the output stream, avoiding creating large intermediate strings.
1 parent 2b153a4 commit 95215a3

File tree

6 files changed

+29
-35
lines changed

6 files changed

+29
-35
lines changed

mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -864,11 +864,8 @@ bool DefGenerator::emitDecls(StringRef selectedDialect) {
864864

865865
// Declare all the def classes first (in case they reference each other).
866866
for (const AttrOrTypeDef &def : defs) {
867-
std::string comments = tblgen::emitSummaryAndDescComments(
868-
def.getSummary(), def.getDescription());
869-
if (!comments.empty()) {
870-
os << comments << "\n";
871-
}
867+
tblgen::emitSummaryAndDescComments(os, def.getSummary(),
868+
def.getDescription());
872869
os << "class " << def.getCppClassName() << ";\n";
873870
}
874871

mlir/tools/mlir-tblgen/CppGenUtilities.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,31 @@
1414
#include "CppGenUtilities.h"
1515
#include "mlir/Support/IndentedOstream.h"
1616

17-
std::string
18-
mlir::tblgen::emitSummaryAndDescComments(llvm::StringRef summary,
19-
llvm::StringRef description) {
17+
void mlir::tblgen::emitSummaryAndDescComments(llvm::raw_ostream &os,
18+
llvm::StringRef summary,
19+
llvm::StringRef description,
20+
bool terminateComment) {
2021

2122
std::string comments = "";
2223
StringRef trimmedSummary = summary.trim();
2324
StringRef trimmedDesc = description.trim();
24-
llvm::raw_string_ostream os(comments);
2525
raw_indented_ostream ros(os);
2626

27+
bool empty = true;
2728
if (!trimmedSummary.empty()) {
2829
ros.printReindented(trimmedSummary, "/// ");
30+
empty = false;
2931
}
3032

3133
if (!trimmedDesc.empty()) {
32-
if (!trimmedSummary.empty()) {
34+
if (!empty) {
3335
// If there is a summary, add a newline after it.
3436
ros << "\n";
3537
}
3638
ros.printReindented(trimmedDesc, "/// ");
39+
empty = false;
3740
}
38-
return comments;
41+
42+
if (!empty && terminateComment)
43+
ros << "\n";
3944
}

mlir/tools/mlir-tblgen/CppGenUtilities.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@
1515
#define MLIR_TOOLS_MLIRTBLGEN_CPPGENUTILITIES_H_
1616

1717
#include "llvm/ADT/StringRef.h"
18+
#include "llvm/Support/raw_ostream.h"
1819

1920
namespace mlir {
2021
namespace tblgen {
2122

22-
// Emit the summary and description as a C++ comment, perperly aligned placed
23-
// adjacent to the class declaration of generated classes.
24-
std::string emitSummaryAndDescComments(llvm::StringRef summary,
25-
llvm::StringRef description);
23+
// Emit the summary and description as a C++ comment. If `terminateComment` is
24+
// true, terminates the comment with a `\n`.
25+
void emitSummaryAndDescComments(llvm::raw_ostream &os, llvm::StringRef summary,
26+
llvm::StringRef description,
27+
bool terminateComment = true);
2628
} // namespace tblgen
2729
} // namespace mlir
2830

mlir/tools/mlir-tblgen/DialectGen.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,7 @@ tblgen::findDialectToGenerate(ArrayRef<Dialect> dialects) {
109109
/// {0}: The name of the dialect class.
110110
/// {1}: The dialect namespace.
111111
/// {2}: The dialect parent class.
112-
/// {3}: The summary and description comments.
113112
static const char *const dialectDeclBeginStr = R"(
114-
{3}
115113
class {0} : public ::mlir::{2} {
116114
explicit {0}(::mlir::MLIRContext *context);
117115
@@ -249,10 +247,11 @@ static void emitDialectDecl(Dialect &dialect, raw_ostream &os) {
249247
StringRef superClassName =
250248
dialect.isExtensible() ? "ExtensibleDialect" : "Dialect";
251249

252-
std::string comments = tblgen::emitSummaryAndDescComments(
253-
dialect.getSummary(), dialect.getDescription());
250+
tblgen::emitSummaryAndDescComments(os, dialect.getSummary(),
251+
dialect.getDescription(),
252+
/*terminateCmment=*/false);
254253
os << llvm::formatv(dialectDeclBeginStr, cppName, dialect.getName(),
255-
superClassName, comments);
254+
superClassName);
256255

257256
// If the dialect requested the default attribute printer and parser, emit
258257
// the declarations for the hooks.

mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4844,11 +4844,8 @@ static void emitOpClassDecls(const RecordKeeper &records,
48444844
for (const Record *def : defs) {
48454845
Operator op(*def);
48464846
NamespaceEmitter emitter(os, op.getCppNamespace());
4847-
std::string comments = tblgen::emitSummaryAndDescComments(
4848-
op.getSummary(), op.getDescription());
4849-
if (!comments.empty()) {
4850-
os << comments << "\n";
4851-
}
4847+
tblgen::emitSummaryAndDescComments(os, op.getSummary(),
4848+
op.getDescription());
48524849
os << "class " << op.getCppClassName() << ";\n";
48534850
}
48544851

mlir/tools/mlir-tblgen/OpInterfacesGen.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -536,11 +536,8 @@ void InterfaceGenerator::forwardDeclareInterface(const Interface &interface) {
536536

537537
// Emit a forward declaration of the interface class so that it becomes usable
538538
// in the signature of its methods.
539-
std::string comments = tblgen::emitSummaryAndDescComments(
540-
"", interface.getDescription().value_or(""));
541-
if (!comments.empty()) {
542-
os << comments << "\n";
543-
}
539+
tblgen::emitSummaryAndDescComments(os, "",
540+
interface.getDescription().value_or(""));
544541

545542
StringRef interfaceName = interface.getName();
546543
os << "class " << interfaceName << ";\n";
@@ -560,11 +557,8 @@ void InterfaceGenerator::emitInterfaceDecl(const Interface &interface) {
560557

561558
// Emit a forward declaration of the interface class so that it becomes usable
562559
// in the signature of its methods.
563-
std::string comments = tblgen::emitSummaryAndDescComments(
564-
"", interface.getDescription().value_or(""));
565-
if (!comments.empty()) {
566-
os << comments << "\n";
567-
}
560+
tblgen::emitSummaryAndDescComments(os, "",
561+
interface.getDescription().value_or(""));
568562

569563
// Emit the traits struct containing the concept and model declarations.
570564
os << "namespace detail {\n"

0 commit comments

Comments
 (0)