Skip to content

Commit fcb5905

Browse files
authored
[MLIR][TableGen] Minor code cleanup in DirectiveCommonGen (#110290)
Directly Use Clause/ClauseVal as loop iterator. Use llvm::transform instead of std::transform. Use interleaveComma() to generate comma separated list.
1 parent 7ac474b commit fcb5905

File tree

4 files changed

+8
-16
lines changed

4 files changed

+8
-16
lines changed

llvm/include/llvm/TableGen/DirectiveEmitter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ class VersionedClause {
247247

248248
class ClauseVal : public BaseRecord {
249249
public:
250-
explicit ClauseVal(const Record *Def) : BaseRecord(Def) {}
250+
ClauseVal(const Record *Def) : BaseRecord(Def) {}
251251

252252
int getValue() const { return Def->getValueAsInt("value"); }
253253

llvm/utils/TableGen/DirectiveEmitter.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,8 @@ static void GenerateEnumClauseVal(ArrayRef<const Record *> Records,
9898

9999
OS << "\n";
100100
OS << "enum class " << EnumName << " {\n";
101-
for (const auto &CV : ClauseVals) {
102-
ClauseVal CVal(CV);
103-
OS << " " << CV->getName() << "=" << CVal.getValue() << ",\n";
104-
}
101+
for (const ClauseVal CVal : ClauseVals)
102+
OS << " " << CVal.getRecordName() << "=" << CVal.getValue() << ",\n";
105103
OS << "};\n";
106104

107105
if (DirLang.hasMakeEnumAvailableInNamespace()) {

mlir/test/mlir-tblgen/directive-common.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def TDLC_ClauseA : Clause<"clausea"> {
2626
// CHECK: def AKind: I32EnumAttr<
2727
// CHECK: "ClauseAKind",
2828
// CHECK: "AKind Clause",
29-
// CHECK: [AKindvala,AKindvalb]> {
29+
// CHECK: [AKindvala, AKindvalb]> {
3030
// CHECK: let cppNamespace = "::mlir::tdl";
3131
// CHECK: }
3232
// CHECK: def AKindAttr : EnumAttr<TDL_Dialect, AKind, "akind">;

mlir/tools/mlir-tblgen/DirectiveCommonGen.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ static bool emitDecls(const RecordKeeper &recordKeeper, llvm::StringRef dialect,
5454
recordKeeper.getAllDerivedDefinitions("DirectiveLanguage");
5555
assert(!directiveLanguages.empty() && "DirectiveLanguage missing.");
5656

57-
for (const Record *r : recordKeeper.getAllDerivedDefinitions("Clause")) {
58-
Clause c{r};
57+
for (const Clause c : recordKeeper.getAllDerivedDefinitions("Clause")) {
5958
const auto &clauseVals = c.getClauseVals();
6059
if (clauseVals.empty())
6160
continue;
@@ -65,14 +64,13 @@ static bool emitDecls(const RecordKeeper &recordKeeper, llvm::StringRef dialect,
6564

6665
std::vector<std::string> cvDefs;
6766
for (const auto &it : llvm::enumerate(clauseVals)) {
68-
ClauseVal cval{it.value()};
67+
const ClauseVal cval{it.value()};
6968
if (!cval.isUserVisible())
7069
continue;
7170

7271
std::string name = cval.getFormattedName();
7372
std::string enumValName(name.length(), ' ');
74-
std::transform(name.begin(), name.end(), enumValName.begin(),
75-
llvm::toLower);
73+
llvm::transform(name, enumValName.begin(), llvm::toLower);
7674
enumValName[0] = llvm::toUpper(enumValName[0]);
7775
std::string cvDef{(enumName + llvm::Twine(name)).str()};
7876
os << "def " << cvDef << " : I32EnumAttrCase<\"" << enumValName << "\", "
@@ -84,11 +82,7 @@ static bool emitDecls(const RecordKeeper &recordKeeper, llvm::StringRef dialect,
8482
os << " \"Clause" << enumName << "\",\n";
8583
os << " \"" << enumName << " Clause\",\n";
8684
os << " [";
87-
for (unsigned int i = 0; i < cvDefs.size(); i++) {
88-
os << cvDefs[i];
89-
if (i != cvDefs.size() - 1)
90-
os << ",";
91-
}
85+
llvm::interleaveComma(cvDefs, os);
9286
os << "]> {\n";
9387
os << " let cppNamespace = \"::mlir::"
9488
<< directiveLanguages[0]->getValueAsString("cppNamespace") << "\";\n";

0 commit comments

Comments
 (0)