[Mlir-commits] [mlir] 0a34492 - Give attributes C++ namespaces.
Mehdi Amini
llvmlistbot at llvm.org
Thu Oct 8 22:36:43 PDT 2020
Author: Federico Lebrón
Date: 2020-10-09T05:36:27Z
New Revision: 0a34492f36d77f043d371cc91f359b2d65e86475
URL: https://github.com/llvm/llvm-project/commit/0a34492f36d77f043d371cc91f359b2d65e86475
DIFF: https://github.com/llvm/llvm-project/commit/0a34492f36d77f043d371cc91f359b2d65e86475.diff
LOG: Give attributes C++ namespaces.
Reviewed By: mehdi_amini, jpienaar
Differential Revision: https://reviews.llvm.org/D89014
Added:
Modified:
mlir/include/mlir/IR/OpBase.td
mlir/lib/TableGen/Pattern.cpp
mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
mlir/tools/mlir-tblgen/RewriterGen.cpp
Removed:
################################################################################
diff --git a/mlir/include/mlir/IR/OpBase.td b/mlir/include/mlir/IR/OpBase.td
index 5845371161bf..b38189f64605 100644
--- a/mlir/include/mlir/IR/OpBase.td
+++ b/mlir/include/mlir/IR/OpBase.td
@@ -791,12 +791,16 @@ class Attr<Pred condition, string descr = ""> :
// instantiation.
// TOOD(b/132458159): deduplicate the fields in attribute wrapper classes.
Attr baseAttr = ?;
+
+ // The fully-qualified C++ namespace where the generated class lives.
+ string cppNamespace = "";
}
// An attribute of a specific dialect.
class DialectAttr<Dialect d, Pred condition, string descr = ""> :
Attr<condition, descr> {
Dialect dialect = d;
+ let cppNamespace = d.cppNamespace;
}
//===----------------------------------------------------------------------===//
@@ -1115,16 +1119,6 @@ class EnumAttrInfo<string name, list<EnumAttrCaseInfo> cases> {
// underlying type is not explicitly specified.
string underlyingType = "";
- // The C++ namespaces that the enum class definition and utility functions
- // should be placed into.
- //
- // Normally you want to place the full namespace path here. If it is nested,
- // use "::" as the delimiter, e.g., given "A::B", generated code will be
- // placed in `namespace A { namespace B { ... } }`. To avoid placing in any
- // namespace, use "".
- // TODO: use dialect to provide the namespace.
- string cppNamespace = "";
-
// The name of the utility function that converts a value of the underlying
// type to the corresponding symbol. It will have the following signature:
//
@@ -1463,7 +1457,8 @@ class StructFieldAttr<string thisName, Attr thisType> {
// useful when representing data that would normally be in a structure.
class StructAttr<string name, Dialect d,
list<StructFieldAttr> attributes> :
- DictionaryAttrBase<CPred<"$_self.isa<" # name # ">()">,
+ DictionaryAttrBase<CPred<"$_self.isa<" # d.cppNamespace
+ # "::" # name # ">()">,
"DictionaryAttr with field(s): " #
StrJoin<!foreach(a, attributes, "'" # a.name # "'"), ", ">.result #
" (each field having its own constraints)"> {
@@ -1471,14 +1466,16 @@ class StructAttr<string name, Dialect d,
string className = name;
// Return type should match the name of the structure.
- let returnType = name;
+ let returnType = d.cppNamespace # "::" # name;
// Storage type should match the name of the structure.
- let storageType = name;
+ let storageType = d.cppNamespace # "::" # name;
// The dialect this StructAttr belongs to.
Dialect dialect = d;
+ let cppNamespace = d.cppNamespace;
+
// List of fields that the StructAttr contains.
list<StructFieldAttr> fields = attributes;
}
diff --git a/mlir/lib/TableGen/Pattern.cpp b/mlir/lib/TableGen/Pattern.cpp
index b32775318df8..cfa3da2c417a 100644
--- a/mlir/lib/TableGen/Pattern.cpp
+++ b/mlir/lib/TableGen/Pattern.cpp
@@ -219,11 +219,11 @@ std::string SymbolInfoMap::SymbolInfo::getVarDecl(StringRef name) const {
case Kind::Operand: {
// Use operand range for captured operands (to support potential variadic
// operands).
- return std::string(
- formatv("Operation::operand_range {0}(op0->getOperands());\n", name));
+ return std::string(formatv(
+ "::mlir::Operation::operand_range {0}(op0->getOperands());\n", name));
}
case Kind::Value: {
- return std::string(formatv("ArrayRef<Value> {0};\n", name));
+ return std::string(formatv("::llvm::ArrayRef<::mlir::Value> {0};\n", name));
}
case Kind::Result: {
// Use the op itself for captured results.
diff --git a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
index 5345bc598da9..3bcf02114555 100644
--- a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
@@ -487,16 +487,7 @@ void OpEmitter::genAttrGetters() {
// Emit with return type specified.
auto emitAttrWithReturnType = [&](StringRef name, Attribute attr) {
- Dialect attrDialect = attr.getDialect();
- // Does the current operation have a
diff erent namespace than the attribute?
- bool
diff erentNamespace =
- attrDialect && opDialect && attrDialect != opDialect;
- std::string returnType =
diff erentNamespace
- ? (llvm::Twine(attrDialect.getCppNamespace()) +
- "::" + attr.getReturnType())
- .str()
- : attr.getReturnType().str();
- auto *method = opClass.addMethodAndPrune(returnType, name);
+ auto *method = opClass.addMethodAndPrune(attr.getReturnType(), name);
auto &body = method->body();
body << " auto attr = " << name << "Attr();\n";
if (attr.hasDefaultValue()) {
@@ -2000,8 +1991,8 @@ void OpEmitter::genOpAsmInterface() {
opClass.addTrait("::mlir::OpAsmOpInterface::Trait");
// Generate the right accessor for the number of results.
- auto *method = opClass.addMethodAndPrune("void", "getAsmResultNames",
- "OpAsmSetValueNameFn", "setNameFn");
+ auto *method = opClass.addMethodAndPrune(
+ "void", "getAsmResultNames", "::mlir::OpAsmSetValueNameFn", "setNameFn");
auto &body = method->body();
for (int i = 0; i != numResults; ++i) {
body << " auto resultGroup" << i << " = getODSResults(" << i << ");\n"
diff --git a/mlir/tools/mlir-tblgen/RewriterGen.cpp b/mlir/tools/mlir-tblgen/RewriterGen.cpp
index e16900227759..495fbe1715e0 100644
--- a/mlir/tools/mlir-tblgen/RewriterGen.cpp
+++ b/mlir/tools/mlir-tblgen/RewriterGen.cpp
@@ -221,7 +221,8 @@ void PatternEmitter::emitOpMatch(DagNode tree, int depth) {
int indent = 4 + 2 * depth;
os.indent(indent) << formatv(
- "auto castedOp{0} = dyn_cast_or_null<{1}>(op{0}); (void)castedOp{0};\n",
+ "auto castedOp{0} = ::llvm::dyn_cast_or_null<{1}>(op{0}); "
+ "(void)castedOp{0};\n",
depth, op.getQualCppClassName());
// Skip the operand matching at depth 0 as the pattern rewriter already does.
if (depth != 0) {
@@ -535,7 +536,7 @@ void PatternEmitter::emit(StringRef rewriteName) {
os << "\n// Rewrite\n";
emitRewriteLogic();
- os << "return success();\n";
+ os << "return ::mlir::success();\n";
}
os << "};\n";
}
@@ -1145,8 +1146,8 @@ static void emitRewriters(const RecordKeeper &recordKeeper, raw_ostream &os) {
}
// Emit function to add the generated matchers to the pattern list.
- os << "void LLVM_ATTRIBUTE_UNUSED populateWithGenerated(MLIRContext "
- "*context, OwningRewritePatternList *patterns) {\n";
+ os << "void LLVM_ATTRIBUTE_UNUSED populateWithGenerated(::mlir::MLIRContext "
+ "*context, ::mlir::OwningRewritePatternList *patterns) {\n";
for (const auto &name : rewriterNames) {
os << " patterns->insert<" << name << ">(context);\n";
}
More information about the Mlir-commits
mailing list