Skip to content

Commit ae7490c

Browse files
committed
[MLIR][TableGen] Minor code cleanup in DirectiveCommonGen
Directly Use Clause/ClauseVal as loop iterator. Use llvm::transform instead of std::transform. Use ListSeparator to generate , separated list.
1 parent e2cc63d commit ae7490c

File tree

4 files changed

+12
-16
lines changed

4 files changed

+12
-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: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "mlir/TableGen/GenInfo.h"
1515

16+
#include "llvm/ADT/StringExtras.h"
1617
#include "llvm/ADT/Twine.h"
1718
#include "llvm/Support/CommandLine.h"
1819
#include "llvm/Support/raw_ostream.h"
@@ -25,6 +26,7 @@ using llvm::ClauseVal;
2526
using llvm::raw_ostream;
2627
using llvm::Record;
2728
using llvm::RecordKeeper;
29+
using llvm::Twine;
2830

2931
// LLVM has multiple places (Clang, Flang, MLIR) where information about
3032
// the directives (OpenMP/OpenACC), and clauses are needed. It is good software
@@ -54,8 +56,7 @@ static bool emitDecls(const RecordKeeper &recordKeeper, llvm::StringRef dialect,
5456
recordKeeper.getAllDerivedDefinitions("DirectiveLanguage");
5557
assert(!directiveLanguages.empty() && "DirectiveLanguage missing.");
5658

57-
for (const Record *r : recordKeeper.getAllDerivedDefinitions("Clause")) {
58-
Clause c{r};
59+
for (const Clause c : recordKeeper.getAllDerivedDefinitions("Clause")) {
5960
const auto &clauseVals = c.getClauseVals();
6061
if (clauseVals.empty())
6162
continue;
@@ -65,14 +66,13 @@ static bool emitDecls(const RecordKeeper &recordKeeper, llvm::StringRef dialect,
6566

6667
std::vector<std::string> cvDefs;
6768
for (const auto &it : llvm::enumerate(clauseVals)) {
68-
ClauseVal cval{it.value()};
69+
const ClauseVal cval{it.value()};
6970
if (!cval.isUserVisible())
7071
continue;
7172

7273
std::string name = cval.getFormattedName();
7374
std::string enumValName(name.length(), ' ');
74-
std::transform(name.begin(), name.end(), enumValName.begin(),
75-
llvm::toLower);
75+
llvm::transform(name, enumValName.begin(), llvm::toLower);
7676
enumValName[0] = llvm::toUpper(enumValName[0]);
7777
std::string cvDef{(enumName + llvm::Twine(name)).str()};
7878
os << "def " << cvDef << " : I32EnumAttrCase<\"" << enumValName << "\", "
@@ -84,11 +84,9 @@ static bool emitDecls(const RecordKeeper &recordKeeper, llvm::StringRef dialect,
8484
os << " \"Clause" << enumName << "\",\n";
8585
os << " \"" << enumName << " Clause\",\n";
8686
os << " [";
87-
for (unsigned int i = 0; i < cvDefs.size(); i++) {
88-
os << cvDefs[i];
89-
if (i != cvDefs.size() - 1)
90-
os << ",";
91-
}
87+
llvm::ListSeparator LS;
88+
for (const auto &cvDef : cvDefs)
89+
os << LS << cvDef;
9290
os << "]> {\n";
9391
os << " let cppNamespace = \"::mlir::"
9492
<< directiveLanguages[0]->getValueAsString("cppNamespace") << "\";\n";

0 commit comments

Comments
 (0)