Skip to content

Commit

Permalink
[NFC][MLIR][TableGen] Eliminate llvm:: for commonly used types
Browse files Browse the repository at this point in the history
Eliminate `llvm::` namespace qualifier for commonly used types in
MLIR TableGen backends to reduce code clutter.
  • Loading branch information
jurahul committed Oct 2, 2024
1 parent 9bf02a8 commit c6da346
Show file tree
Hide file tree
Showing 18 changed files with 540 additions and 550 deletions.
59 changes: 30 additions & 29 deletions mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

using namespace mlir;
using namespace mlir::tblgen;
using llvm::Record;
using llvm::RecordKeeper;
namespace cl = llvm::cl;

//===----------------------------------------------------------------------===//
// Utility Functions
Expand All @@ -30,14 +33,14 @@ using namespace mlir::tblgen;
/// Find all the AttrOrTypeDef for the specified dialect. If no dialect
/// specified and can only find one dialect's defs, use that.
static void collectAllDefs(StringRef selectedDialect,
ArrayRef<const llvm::Record *> records,
ArrayRef<const Record *> records,
SmallVectorImpl<AttrOrTypeDef> &resultDefs) {
// Nothing to do if no defs were found.
if (records.empty())
return;

auto defs = llvm::map_range(
records, [&](const llvm::Record *rec) { return AttrOrTypeDef(rec); });
records, [&](const Record *rec) { return AttrOrTypeDef(rec); });
if (selectedDialect.empty()) {
// If a dialect was not specified, ensure that all found defs belong to the
// same dialect.
Expand Down Expand Up @@ -690,15 +693,14 @@ class DefGenerator {
bool emitDefs(StringRef selectedDialect);

protected:
DefGenerator(ArrayRef<const llvm::Record *> defs, raw_ostream &os,
DefGenerator(ArrayRef<const Record *> defs, raw_ostream &os,
StringRef defType, StringRef valueType, bool isAttrGenerator)
: defRecords(defs), os(os), defType(defType), valueType(valueType),
isAttrGenerator(isAttrGenerator) {
// Sort by occurrence in file.
llvm::sort(defRecords,
[](const llvm::Record *lhs, const llvm::Record *rhs) {
return lhs->getID() < rhs->getID();
});
llvm::sort(defRecords, [](const Record *lhs, const Record *rhs) {
return lhs->getID() < rhs->getID();
});
}

/// Emit the list of def type names.
Expand All @@ -707,7 +709,7 @@ class DefGenerator {
void emitParsePrintDispatch(ArrayRef<AttrOrTypeDef> defs);

/// The set of def records to emit.
std::vector<const llvm::Record *> defRecords;
std::vector<const Record *> defRecords;
/// The attribute or type class to emit.
/// The stream to emit to.
raw_ostream &os;
Expand All @@ -722,13 +724,13 @@ class DefGenerator {

/// A specialized generator for AttrDefs.
struct AttrDefGenerator : public DefGenerator {
AttrDefGenerator(const llvm::RecordKeeper &records, raw_ostream &os)
AttrDefGenerator(const RecordKeeper &records, raw_ostream &os)
: DefGenerator(records.getAllDerivedDefinitionsIfDefined("AttrDef"), os,
"Attr", "Attribute", /*isAttrGenerator=*/true) {}
};
/// A specialized generator for TypeDefs.
struct TypeDefGenerator : public DefGenerator {
TypeDefGenerator(const llvm::RecordKeeper &records, raw_ostream &os)
TypeDefGenerator(const RecordKeeper &records, raw_ostream &os)
: DefGenerator(records.getAllDerivedDefinitionsIfDefined("TypeDef"), os,
"Type", "Type", /*isAttrGenerator=*/false) {}
};
Expand Down Expand Up @@ -1030,9 +1032,9 @@ bool DefGenerator::emitDefs(StringRef selectedDialect) {

/// Find all type constraints for which a C++ function should be generated.
static std::vector<Constraint>
getAllTypeConstraints(const llvm::RecordKeeper &records) {
getAllTypeConstraints(const RecordKeeper &records) {
std::vector<Constraint> result;
for (const llvm::Record *def :
for (const Record *def :
records.getAllDerivedDefinitionsIfDefined("TypeConstraint")) {
// Ignore constraints defined outside of the top-level file.
if (llvm::SrcMgr.FindBufferContainingLoc(def->getLoc()[0]) !=
Expand All @@ -1047,7 +1049,7 @@ getAllTypeConstraints(const llvm::RecordKeeper &records) {
return result;
}

static void emitTypeConstraintDecls(const llvm::RecordKeeper &records,
static void emitTypeConstraintDecls(const RecordKeeper &records,
raw_ostream &os) {
static const char *const typeConstraintDecl = R"(
bool {0}(::mlir::Type type);
Expand All @@ -1057,7 +1059,7 @@ bool {0}(::mlir::Type type);
os << strfmt(typeConstraintDecl, *constr.getCppFunctionName());
}

static void emitTypeConstraintDefs(const llvm::RecordKeeper &records,
static void emitTypeConstraintDefs(const RecordKeeper &records,
raw_ostream &os) {
static const char *const typeConstraintDef = R"(
bool {0}(::mlir::Type type) {
Expand All @@ -1080,58 +1082,57 @@ bool {0}(::mlir::Type type) {
//===----------------------------------------------------------------------===//
// AttrDef

static llvm::cl::OptionCategory attrdefGenCat("Options for -gen-attrdef-*");
static llvm::cl::opt<std::string>
static cl::OptionCategory attrdefGenCat("Options for -gen-attrdef-*");
static cl::opt<std::string>
attrDialect("attrdefs-dialect",
llvm::cl::desc("Generate attributes for this dialect"),
llvm::cl::cat(attrdefGenCat), llvm::cl::CommaSeparated);
cl::desc("Generate attributes for this dialect"),
cl::cat(attrdefGenCat), cl::CommaSeparated);

static mlir::GenRegistration
genAttrDefs("gen-attrdef-defs", "Generate AttrDef definitions",
[](const llvm::RecordKeeper &records, raw_ostream &os) {
[](const RecordKeeper &records, raw_ostream &os) {
AttrDefGenerator generator(records, os);
return generator.emitDefs(attrDialect);
});
static mlir::GenRegistration
genAttrDecls("gen-attrdef-decls", "Generate AttrDef declarations",
[](const llvm::RecordKeeper &records, raw_ostream &os) {
[](const RecordKeeper &records, raw_ostream &os) {
AttrDefGenerator generator(records, os);
return generator.emitDecls(attrDialect);
});

//===----------------------------------------------------------------------===//
// TypeDef

static llvm::cl::OptionCategory typedefGenCat("Options for -gen-typedef-*");
static llvm::cl::opt<std::string>
typeDialect("typedefs-dialect",
llvm::cl::desc("Generate types for this dialect"),
llvm::cl::cat(typedefGenCat), llvm::cl::CommaSeparated);
static cl::OptionCategory typedefGenCat("Options for -gen-typedef-*");
static cl::opt<std::string>
typeDialect("typedefs-dialect", cl::desc("Generate types for this dialect"),
cl::cat(typedefGenCat), cl::CommaSeparated);

static mlir::GenRegistration
genTypeDefs("gen-typedef-defs", "Generate TypeDef definitions",
[](const llvm::RecordKeeper &records, raw_ostream &os) {
[](const RecordKeeper &records, raw_ostream &os) {
TypeDefGenerator generator(records, os);
return generator.emitDefs(typeDialect);
});
static mlir::GenRegistration
genTypeDecls("gen-typedef-decls", "Generate TypeDef declarations",
[](const llvm::RecordKeeper &records, raw_ostream &os) {
[](const RecordKeeper &records, raw_ostream &os) {
TypeDefGenerator generator(records, os);
return generator.emitDecls(typeDialect);
});

static mlir::GenRegistration
genTypeConstrDefs("gen-type-constraint-defs",
"Generate type constraint definitions",
[](const llvm::RecordKeeper &records, raw_ostream &os) {
[](const RecordKeeper &records, raw_ostream &os) {
emitTypeConstraintDefs(records, os);
return false;
});
static mlir::GenRegistration
genTypeConstrDecls("gen-type-constraint-decls",
"Generate type constraint declarations",
[](const llvm::RecordKeeper &records, raw_ostream &os) {
[](const RecordKeeper &records, raw_ostream &os) {
emitTypeConstraintDecls(records, os);
return false;
});
11 changes: 5 additions & 6 deletions mlir/tools/mlir-tblgen/BytecodeDialectGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@

using namespace llvm;

static llvm::cl::OptionCategory dialectGenCat("Options for -gen-bytecode");
static llvm::cl::opt<std::string>
selectedBcDialect("bytecode-dialect",
llvm::cl::desc("The dialect to gen for"),
llvm::cl::cat(dialectGenCat), llvm::cl::CommaSeparated);
static cl::OptionCategory dialectGenCat("Options for -gen-bytecode");
static cl::opt<std::string>
selectedBcDialect("bytecode-dialect", cl::desc("The dialect to gen for"),
cl::cat(dialectGenCat), cl::CommaSeparated);

namespace {

Expand Down Expand Up @@ -306,7 +305,7 @@ void Generator::emitPrint(StringRef kind, StringRef type,
auto funScope = os.scope("{\n", "}\n\n");

// Check that predicates specified if multiple bytecode instances.
for (const llvm::Record *rec : make_second_range(vec)) {
for (const Record *rec : make_second_range(vec)) {
StringRef pred = rec->getValueAsString("printerPredicate");
if (vec.size() > 1 && pred.empty()) {
for (auto [index, rec] : vec) {
Expand Down
32 changes: 17 additions & 15 deletions mlir/tools/mlir-tblgen/DialectGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,21 @@

using namespace mlir;
using namespace mlir::tblgen;
using llvm::Record;
using llvm::RecordKeeper;
namespace cl = llvm::cl;

static llvm::cl::OptionCategory dialectGenCat("Options for -gen-dialect-*");
llvm::cl::opt<std::string>
selectedDialect("dialect", llvm::cl::desc("The dialect to gen for"),
llvm::cl::cat(dialectGenCat), llvm::cl::CommaSeparated);
static cl::OptionCategory dialectGenCat("Options for -gen-dialect-*");
cl::opt<std::string> selectedDialect("dialect",
cl::desc("The dialect to gen for"),
cl::cat(dialectGenCat),
cl::CommaSeparated);

/// Utility iterator used for filtering records for a specific dialect.
namespace {
using DialectFilterIterator =
llvm::filter_iterator<ArrayRef<llvm::Record *>::iterator,
std::function<bool(const llvm::Record *)>>;
llvm::filter_iterator<ArrayRef<Record *>::iterator,
std::function<bool(const Record *)>>;
} // namespace

static void populateDiscardableAttributes(
Expand All @@ -62,8 +66,8 @@ static void populateDiscardableAttributes(
/// the given dialect.
template <typename T>
static iterator_range<DialectFilterIterator>
filterForDialect(ArrayRef<llvm::Record *> records, Dialect &dialect) {
auto filterFn = [&](const llvm::Record *record) {
filterForDialect(ArrayRef<Record *> records, Dialect &dialect) {
auto filterFn = [&](const Record *record) {
return T(record).getDialect() == dialect;
};
return {DialectFilterIterator(records.begin(), records.end(), filterFn),
Expand Down Expand Up @@ -295,7 +299,7 @@ static void emitDialectDecl(Dialect &dialect, raw_ostream &os) {
<< "::" << dialect.getCppClassName() << ")\n";
}

static bool emitDialectDecls(const llvm::RecordKeeper &recordKeeper,
static bool emitDialectDecls(const RecordKeeper &recordKeeper,
raw_ostream &os) {
emitSourceFileHeader("Dialect Declarations", os, recordKeeper);

Expand Down Expand Up @@ -340,8 +344,7 @@ static const char *const dialectDestructorStr = R"(
)";

static void emitDialectDef(Dialect &dialect,
const llvm::RecordKeeper &recordKeeper,
static void emitDialectDef(Dialect &dialect, const RecordKeeper &recordKeeper,
raw_ostream &os) {
std::string cppClassName = dialect.getCppClassName();

Expand Down Expand Up @@ -389,8 +392,7 @@ static void emitDialectDef(Dialect &dialect,
os << llvm::formatv(dialectDestructorStr, cppClassName);
}

static bool emitDialectDefs(const llvm::RecordKeeper &recordKeeper,
raw_ostream &os) {
static bool emitDialectDefs(const RecordKeeper &recordKeeper, raw_ostream &os) {
emitSourceFileHeader("Dialect Definitions", os, recordKeeper);

auto dialectDefs = recordKeeper.getAllDerivedDefinitions("Dialect");
Expand All @@ -411,12 +413,12 @@ static bool emitDialectDefs(const llvm::RecordKeeper &recordKeeper,

static mlir::GenRegistration
genDialectDecls("gen-dialect-decls", "Generate dialect declarations",
[](const llvm::RecordKeeper &records, raw_ostream &os) {
[](const RecordKeeper &records, raw_ostream &os) {
return emitDialectDecls(records, os);
});

static mlir::GenRegistration
genDialectDefs("gen-dialect-defs", "Generate dialect definitions",
[](const llvm::RecordKeeper &records, raw_ostream &os) {
[](const RecordKeeper &records, raw_ostream &os) {
return emitDialectDefs(records, os);
});
10 changes: 5 additions & 5 deletions mlir/tools/mlir-tblgen/DirectiveCommonGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ using llvm::ClauseVal;
using llvm::raw_ostream;
using llvm::Record;
using llvm::RecordKeeper;
namespace cl = llvm::cl;

// LLVM has multiple places (Clang, Flang, MLIR) where information about
// the directives (OpenMP/OpenACC), and clauses are needed. It is good software
Expand Down Expand Up @@ -96,12 +97,11 @@ static bool emitDecls(const RecordKeeper &recordKeeper, llvm::StringRef dialect,
return false;
}

static llvm::cl::OptionCategory
directiveGenCat("Options for gen-directive-decl");
static llvm::cl::opt<std::string>
static cl::OptionCategory directiveGenCat("Options for gen-directive-decl");
static cl::opt<std::string>
dialect("directives-dialect",
llvm::cl::desc("Generate directives for this dialect"),
llvm::cl::cat(directiveGenCat), llvm::cl::CommaSeparated);
cl::desc("Generate directives for this dialect"),
cl::cat(directiveGenCat), cl::CommaSeparated);

// Registers the generator to mlir-tblgen.
static mlir::GenRegistration genDirectiveDecls(
Expand Down
Loading

0 comments on commit c6da346

Please sign in to comment.