-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[NFC][MLIR][TableGen] Change emitSummaryAndDescComments
to write to os directly
#162014
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
+29
−35
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
… os directly Change `emitSummaryAndDescComments` to directly w rite to the output stream, avoiding creating large intermediate strings.
@llvm/pr-subscribers-mlir-core @llvm/pr-subscribers-mlir Author: Rahul Joshi (jurahul) ChangesChange Full diff: https://github.com/llvm/llvm-project/pull/162014.diff 6 Files Affected:
diff --git a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
index 06ef396b9b21d..c1130a3632757 100644
--- a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
+++ b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
@@ -864,11 +864,8 @@ bool DefGenerator::emitDecls(StringRef selectedDialect) {
// Declare all the def classes first (in case they reference each other).
for (const AttrOrTypeDef &def : defs) {
- std::string comments = tblgen::emitSummaryAndDescComments(
- def.getSummary(), def.getDescription());
- if (!comments.empty()) {
- os << comments << "\n";
- }
+ tblgen::emitSummaryAndDescComments(os, def.getSummary(),
+ def.getDescription());
os << "class " << def.getCppClassName() << ";\n";
}
diff --git a/mlir/tools/mlir-tblgen/CppGenUtilities.cpp b/mlir/tools/mlir-tblgen/CppGenUtilities.cpp
index ebca20cc685f4..fddd7790a4375 100644
--- a/mlir/tools/mlir-tblgen/CppGenUtilities.cpp
+++ b/mlir/tools/mlir-tblgen/CppGenUtilities.cpp
@@ -14,26 +14,31 @@
#include "CppGenUtilities.h"
#include "mlir/Support/IndentedOstream.h"
-std::string
-mlir::tblgen::emitSummaryAndDescComments(llvm::StringRef summary,
- llvm::StringRef description) {
+void mlir::tblgen::emitSummaryAndDescComments(llvm::raw_ostream &os,
+ llvm::StringRef summary,
+ llvm::StringRef description,
+ bool terminateComment) {
std::string comments = "";
StringRef trimmedSummary = summary.trim();
StringRef trimmedDesc = description.trim();
- llvm::raw_string_ostream os(comments);
raw_indented_ostream ros(os);
+ bool empty = true;
if (!trimmedSummary.empty()) {
ros.printReindented(trimmedSummary, "/// ");
+ empty = false;
}
if (!trimmedDesc.empty()) {
- if (!trimmedSummary.empty()) {
+ if (!empty) {
// If there is a summary, add a newline after it.
ros << "\n";
}
ros.printReindented(trimmedDesc, "/// ");
+ empty = false;
}
- return comments;
+
+ if (!empty && terminateComment)
+ ros << "\n";
}
diff --git a/mlir/tools/mlir-tblgen/CppGenUtilities.h b/mlir/tools/mlir-tblgen/CppGenUtilities.h
index 231c59a9e148f..69d8cd85ecf70 100644
--- a/mlir/tools/mlir-tblgen/CppGenUtilities.h
+++ b/mlir/tools/mlir-tblgen/CppGenUtilities.h
@@ -15,14 +15,16 @@
#define MLIR_TOOLS_MLIRTBLGEN_CPPGENUTILITIES_H_
#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/raw_ostream.h"
namespace mlir {
namespace tblgen {
-// Emit the summary and description as a C++ comment, perperly aligned placed
-// adjacent to the class declaration of generated classes.
-std::string emitSummaryAndDescComments(llvm::StringRef summary,
- llvm::StringRef description);
+// Emit the summary and description as a C++ comment. If `terminateComment` is
+// true, terminates the comment with a `\n`.
+void emitSummaryAndDescComments(llvm::raw_ostream &os, llvm::StringRef summary,
+ llvm::StringRef description,
+ bool terminateComment = true);
} // namespace tblgen
} // namespace mlir
diff --git a/mlir/tools/mlir-tblgen/DialectGen.cpp b/mlir/tools/mlir-tblgen/DialectGen.cpp
index 2e8810d5d37f4..c2c0c1f415254 100644
--- a/mlir/tools/mlir-tblgen/DialectGen.cpp
+++ b/mlir/tools/mlir-tblgen/DialectGen.cpp
@@ -109,9 +109,7 @@ tblgen::findDialectToGenerate(ArrayRef<Dialect> dialects) {
/// {0}: The name of the dialect class.
/// {1}: The dialect namespace.
/// {2}: The dialect parent class.
-/// {3}: The summary and description comments.
static const char *const dialectDeclBeginStr = R"(
-{3}
class {0} : public ::mlir::{2} {
explicit {0}(::mlir::MLIRContext *context);
@@ -249,10 +247,11 @@ static void emitDialectDecl(Dialect &dialect, raw_ostream &os) {
StringRef superClassName =
dialect.isExtensible() ? "ExtensibleDialect" : "Dialect";
- std::string comments = tblgen::emitSummaryAndDescComments(
- dialect.getSummary(), dialect.getDescription());
+ tblgen::emitSummaryAndDescComments(os, dialect.getSummary(),
+ dialect.getDescription(),
+ /*terminateCmment=*/false);
os << llvm::formatv(dialectDeclBeginStr, cppName, dialect.getName(),
- superClassName, comments);
+ superClassName);
// If the dialect requested the default attribute printer and parser, emit
// the declarations for the hooks.
diff --git a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
index c3420d433523a..9a7f95a94ceaf 100644
--- a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
@@ -4847,11 +4847,8 @@ static void emitOpClassDecls(const RecordKeeper &records,
for (auto *def : defs) {
Operator op(*def);
NamespaceEmitter emitter(os, op.getCppNamespace());
- std::string comments = tblgen::emitSummaryAndDescComments(
- op.getSummary(), op.getDescription());
- if (!comments.empty()) {
- os << comments << "\n";
- }
+ tblgen::emitSummaryAndDescComments(os, op.getSummary(),
+ op.getDescription());
os << "class " << op.getCppClassName() << ";\n";
}
diff --git a/mlir/tools/mlir-tblgen/OpInterfacesGen.cpp b/mlir/tools/mlir-tblgen/OpInterfacesGen.cpp
index 3cc1636ac3317..25f160d079f9d 100644
--- a/mlir/tools/mlir-tblgen/OpInterfacesGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpInterfacesGen.cpp
@@ -536,11 +536,8 @@ void InterfaceGenerator::forwardDeclareInterface(const Interface &interface) {
// Emit a forward declaration of the interface class so that it becomes usable
// in the signature of its methods.
- std::string comments = tblgen::emitSummaryAndDescComments(
- "", interface.getDescription().value_or(""));
- if (!comments.empty()) {
- os << comments << "\n";
- }
+ tblgen::emitSummaryAndDescComments(os, "",
+ interface.getDescription().value_or(""));
StringRef interfaceName = interface.getName();
os << "class " << interfaceName << ";\n";
@@ -560,11 +557,8 @@ void InterfaceGenerator::emitInterfaceDecl(const Interface &interface) {
// Emit a forward declaration of the interface class so that it becomes usable
// in the signature of its methods.
- std::string comments = tblgen::emitSummaryAndDescComments(
- "", interface.getDescription().value_or(""));
- if (!comments.empty()) {
- os << comments << "\n";
- }
+ tblgen::emitSummaryAndDescComments(os, "",
+ interface.getDescription().value_or(""));
// Emit the traits struct containing the concept and model declarations.
os << "namespace detail {\n"
|
joker-eph
approved these changes
Oct 6, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Change
emitSummaryAndDescComments
to directly write to the output stream, avoiding creating large intermediate strings.