Skip to content

Commit fb2bc05

Browse files
fix: do not necessitate the internal attribute's mnemonic
1 parent 39f57c0 commit fb2bc05

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

mlir/include/mlir/IR/OpImplementation.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,29 @@ class AsmPrinter {
212212
std::enable_if_t<std::is_convertible_v<AttrOrType, Attribute>> *
213213
sfinae = nullptr>
214214
void printQualifiedAttrOrType(AttrOrType attrOrType) {
215-
printAttribute(attrOrType);
215+
Attribute attr = attrOrType;
216+
Dialect &dialect = attr.getDialect();
217+
StringRef dialectNamespace = dialect.getNamespace();
218+
if (dialectNamespace.empty() || dialectNamespace == "builtin") {
219+
printStrippedAttrOrType(attrOrType);
220+
return;
221+
}
222+
printAttribute(attr);
216223
}
217224

218225
template <typename AttrOrType,
219226
std::enable_if_t<std::is_convertible_v<AttrOrType, Type> &&
220227
!std::is_convertible_v<AttrOrType, Attribute>> *
221228
sfinae = nullptr>
222229
void printQualifiedAttrOrType(AttrOrType attrOrType) {
223-
printType(attrOrType);
230+
Type type = attrOrType;
231+
Dialect &dialect = type.getDialect();
232+
StringRef dialectNamespace = dialect.getNamespace();
233+
if (dialectNamespace.empty() || dialectNamespace == "builtin") {
234+
printStrippedAttrOrType(attrOrType);
235+
return;
236+
}
237+
printType(type);
224238
}
225239

226240
template <typename ElementT,

mlir/test/mlir-tblgen/op-format.mlir

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,16 +352,16 @@ module attributes {test.internal = #test.internal<key = 8, value = 9>} {
352352

353353
//-----
354354

355-
// CHECK: module attributes {test.external = #test.external<internal = #test.internal<key = 1, value = 2>>} {
355+
// CHECK: module attributes {test.external = #test.external<internal = <key = 1, value = 2>>} {
356356
// CHECK-NEXT: }
357-
module attributes {test.external = #test.external<internal = #test.internal<key = 1, value = 2>>} {
357+
module attributes {test.external = #test.external<internal = <key = 1, value = 2>>} {
358358
}
359359

360360
//-----
361361

362-
// CHECK: module attributes {test.external_array = #test.external_array<[internals = #test.internal<key = 1, value = 2>, #test.internal<key = 8, value = 9>]>} {
362+
// CHECK: module attributes {test.external_array = #test.external_array<[internals = <key = 1, value = 2>, <key = 8, value = 9>]>} {
363363
// CHECK-NEXT: }
364-
module attributes {test.external_array = #test.external_array<[internals = #test.internal<key = 1, value = 2>, #test.internal<key = 8, value = 9>]>} {
364+
module attributes {test.external_array = #test.external_array<[internals = <key = 1, value = 2>, <key = 8, value = 9>]>} {
365365
}
366366

367367
//-----

mlir/tools/mlir-tblgen/AttrOrTypeFormatGen.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,23 @@ class StructDirective
146146
/// into this category to avoid ambiguity when nested within structured
147147
/// properties.
148148
static bool shouldPrintQualified(ParameterElement *param) {
149-
StringRef cppType = param->getParam().getCppType();
150-
return cppType.contains("Attr") || cppType.contains("Type");
149+
const AttrOrTypeParameter &parameter = param->getParam();
150+
StringRef cppType = parameter.getCppType();
151+
if (!cppType.contains("Attr") && !cppType.contains("Type"))
152+
return false;
153+
154+
if (parameter.getPrinter())
155+
return false;
156+
157+
if (const llvm::Init *init = parameter.getDef())
158+
if (const auto *defInit = dyn_cast<llvm::DefInit>(init))
159+
if (defInit->getDef()->isSubClassOf("EnumAttrInfo"))
160+
return false;
161+
162+
if (cppType.contains("mlir::Attribute") || cppType.contains("mlir::Type"))
163+
return true;
164+
165+
return false;
151166
}
152167

153168
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)