Skip to content

Commit 0a34492

Browse files
Federico Lebrónjoker-eph
authored andcommitted
Give attributes C++ namespaces.
Reviewed By: mehdi_amini, jpienaar Differential Revision: https://reviews.llvm.org/D89014
1 parent d94f70f commit 0a34492

File tree

4 files changed

+21
-32
lines changed

4 files changed

+21
-32
lines changed

mlir/include/mlir/IR/OpBase.td

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -791,12 +791,16 @@ class Attr<Pred condition, string descr = ""> :
791791
// instantiation.
792792
// TOOD(b/132458159): deduplicate the fields in attribute wrapper classes.
793793
Attr baseAttr = ?;
794+
795+
// The fully-qualified C++ namespace where the generated class lives.
796+
string cppNamespace = "";
794797
}
795798

796799
// An attribute of a specific dialect.
797800
class DialectAttr<Dialect d, Pred condition, string descr = ""> :
798801
Attr<condition, descr> {
799802
Dialect dialect = d;
803+
let cppNamespace = d.cppNamespace;
800804
}
801805

802806
//===----------------------------------------------------------------------===//
@@ -1115,16 +1119,6 @@ class EnumAttrInfo<string name, list<EnumAttrCaseInfo> cases> {
11151119
// underlying type is not explicitly specified.
11161120
string underlyingType = "";
11171121

1118-
// The C++ namespaces that the enum class definition and utility functions
1119-
// should be placed into.
1120-
//
1121-
// Normally you want to place the full namespace path here. If it is nested,
1122-
// use "::" as the delimiter, e.g., given "A::B", generated code will be
1123-
// placed in `namespace A { namespace B { ... } }`. To avoid placing in any
1124-
// namespace, use "".
1125-
// TODO: use dialect to provide the namespace.
1126-
string cppNamespace = "";
1127-
11281122
// The name of the utility function that converts a value of the underlying
11291123
// type to the corresponding symbol. It will have the following signature:
11301124
//
@@ -1463,22 +1457,25 @@ class StructFieldAttr<string thisName, Attr thisType> {
14631457
// useful when representing data that would normally be in a structure.
14641458
class StructAttr<string name, Dialect d,
14651459
list<StructFieldAttr> attributes> :
1466-
DictionaryAttrBase<CPred<"$_self.isa<" # name # ">()">,
1460+
DictionaryAttrBase<CPred<"$_self.isa<" # d.cppNamespace
1461+
# "::" # name # ">()">,
14671462
"DictionaryAttr with field(s): " #
14681463
StrJoin<!foreach(a, attributes, "'" # a.name # "'"), ", ">.result #
14691464
" (each field having its own constraints)"> {
14701465
// Name for this StructAttr.
14711466
string className = name;
14721467

14731468
// Return type should match the name of the structure.
1474-
let returnType = name;
1469+
let returnType = d.cppNamespace # "::" # name;
14751470

14761471
// Storage type should match the name of the structure.
1477-
let storageType = name;
1472+
let storageType = d.cppNamespace # "::" # name;
14781473

14791474
// The dialect this StructAttr belongs to.
14801475
Dialect dialect = d;
14811476

1477+
let cppNamespace = d.cppNamespace;
1478+
14821479
// List of fields that the StructAttr contains.
14831480
list<StructFieldAttr> fields = attributes;
14841481
}

mlir/lib/TableGen/Pattern.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,11 @@ std::string SymbolInfoMap::SymbolInfo::getVarDecl(StringRef name) const {
219219
case Kind::Operand: {
220220
// Use operand range for captured operands (to support potential variadic
221221
// operands).
222-
return std::string(
223-
formatv("Operation::operand_range {0}(op0->getOperands());\n", name));
222+
return std::string(formatv(
223+
"::mlir::Operation::operand_range {0}(op0->getOperands());\n", name));
224224
}
225225
case Kind::Value: {
226-
return std::string(formatv("ArrayRef<Value> {0};\n", name));
226+
return std::string(formatv("::llvm::ArrayRef<::mlir::Value> {0};\n", name));
227227
}
228228
case Kind::Result: {
229229
// Use the op itself for captured results.

mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -487,16 +487,7 @@ void OpEmitter::genAttrGetters() {
487487

488488
// Emit with return type specified.
489489
auto emitAttrWithReturnType = [&](StringRef name, Attribute attr) {
490-
Dialect attrDialect = attr.getDialect();
491-
// Does the current operation have a different namespace than the attribute?
492-
bool differentNamespace =
493-
attrDialect && opDialect && attrDialect != opDialect;
494-
std::string returnType = differentNamespace
495-
? (llvm::Twine(attrDialect.getCppNamespace()) +
496-
"::" + attr.getReturnType())
497-
.str()
498-
: attr.getReturnType().str();
499-
auto *method = opClass.addMethodAndPrune(returnType, name);
490+
auto *method = opClass.addMethodAndPrune(attr.getReturnType(), name);
500491
auto &body = method->body();
501492
body << " auto attr = " << name << "Attr();\n";
502493
if (attr.hasDefaultValue()) {
@@ -2000,8 +1991,8 @@ void OpEmitter::genOpAsmInterface() {
20001991
opClass.addTrait("::mlir::OpAsmOpInterface::Trait");
20011992

20021993
// Generate the right accessor for the number of results.
2003-
auto *method = opClass.addMethodAndPrune("void", "getAsmResultNames",
2004-
"OpAsmSetValueNameFn", "setNameFn");
1994+
auto *method = opClass.addMethodAndPrune(
1995+
"void", "getAsmResultNames", "::mlir::OpAsmSetValueNameFn", "setNameFn");
20051996
auto &body = method->body();
20061997
for (int i = 0; i != numResults; ++i) {
20071998
body << " auto resultGroup" << i << " = getODSResults(" << i << ");\n"

mlir/tools/mlir-tblgen/RewriterGen.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,8 @@ void PatternEmitter::emitOpMatch(DagNode tree, int depth) {
221221

222222
int indent = 4 + 2 * depth;
223223
os.indent(indent) << formatv(
224-
"auto castedOp{0} = dyn_cast_or_null<{1}>(op{0}); (void)castedOp{0};\n",
224+
"auto castedOp{0} = ::llvm::dyn_cast_or_null<{1}>(op{0}); "
225+
"(void)castedOp{0};\n",
225226
depth, op.getQualCppClassName());
226227
// Skip the operand matching at depth 0 as the pattern rewriter already does.
227228
if (depth != 0) {
@@ -535,7 +536,7 @@ void PatternEmitter::emit(StringRef rewriteName) {
535536
os << "\n// Rewrite\n";
536537
emitRewriteLogic();
537538

538-
os << "return success();\n";
539+
os << "return ::mlir::success();\n";
539540
}
540541
os << "};\n";
541542
}
@@ -1145,8 +1146,8 @@ static void emitRewriters(const RecordKeeper &recordKeeper, raw_ostream &os) {
11451146
}
11461147

11471148
// Emit function to add the generated matchers to the pattern list.
1148-
os << "void LLVM_ATTRIBUTE_UNUSED populateWithGenerated(MLIRContext "
1149-
"*context, OwningRewritePatternList *patterns) {\n";
1149+
os << "void LLVM_ATTRIBUTE_UNUSED populateWithGenerated(::mlir::MLIRContext "
1150+
"*context, ::mlir::OwningRewritePatternList *patterns) {\n";
11501151
for (const auto &name : rewriterNames) {
11511152
os << " patterns->insert<" << name << ">(context);\n";
11521153
}

0 commit comments

Comments
 (0)