Skip to content

Commit 4a35214

Browse files
[mlir][ODS] Fix TableGen for AttrOrTypeDef::hasStorageCustomConstructor (#147957)
There is a `hasStorageCustomConstructor` flag that allows one to provide custom attribute/type construction implementation. Unfortunately, it seems like the flag does not work properly: the generated C++ produces *empty body* method instead of producing only a declaration.
1 parent fa921d1 commit 4a35214

File tree

6 files changed

+49
-2
lines changed

6 files changed

+49
-2
lines changed

mlir/test/lib/Dialect/Test/TestAttrDefs.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,4 +431,11 @@ def SlashAttr: Test_Attr<"Slash">{
431431
let hasCustomAssemblyFormat = 1;
432432
}
433433

434+
def TestCustomStorageCtorAttr : Test_Attr<"TestCustomStorageCtorAttr"> {
435+
let mnemonic = "custom_storage_ctor_attr";
436+
let parameters = (ins "int":$value);
437+
let assemblyFormat = "`<` $value `>`";
438+
let hasStorageCustomConstructor = 1;
439+
}
440+
434441
#endif // TEST_ATTRDEFS

mlir/test/lib/Dialect/Test/TestAttributes.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,18 @@ void SlashAttr::print(AsmPrinter &printer) const {
515515
printer << "<" << getLhs() << " / " << getRhs() << ">";
516516
}
517517

518+
//===----------------------------------------------------------------------===//
519+
// TestCustomStorageCtorAttr
520+
//===----------------------------------------------------------------------===//
521+
522+
test::detail::TestCustomStorageCtorAttrAttrStorage *
523+
test::detail::TestCustomStorageCtorAttrAttrStorage::construct(
524+
mlir::StorageUniquer::StorageAllocator &, std::tuple<int> &&) {
525+
// Note: this tests linker error ("undefined symbol"), the actual
526+
// implementation is not important.
527+
return nullptr;
528+
}
529+
518530
//===----------------------------------------------------------------------===//
519531
// TestDialect
520532
//===----------------------------------------------------------------------===//

mlir/test/lib/Dialect/Test/TestTypeDefs.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,13 @@ def TestTypeCustomString : Test_Type<"TestTypeCustomString"> {
352352
custom<BarString>(ref($foo)) `>` }];
353353
}
354354

355+
def TestCustomStorageCtor : Test_Type<"TestCustomStorageCtor"> {
356+
let mnemonic = "custom_storage_ctor_type";
357+
let parameters = (ins "int":$value);
358+
let assemblyFormat = "`<` $value `>`";
359+
let hasStorageCustomConstructor = 1;
360+
}
361+
355362
def TestTypeOptionalString : Test_Type<"TestTypeOptionalString"> {
356363
let parameters = (ins StringRefParameter<"description", [{"default"}]>:$str);
357364
let mnemonic = "optional_type_string";

mlir/test/lib/Dialect/Test/TestTypes.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,14 @@ getCustomAssemblyFormatDynamicType(TestDialect *testDialect) {
392392
std::move(parser), std::move(printer));
393393
}
394394

395+
test::detail::TestCustomStorageCtorTypeStorage *
396+
test::detail::TestCustomStorageCtorTypeStorage::construct(
397+
mlir::StorageUniquer::StorageAllocator &, std::tuple<int> &&) {
398+
// Note: this tests linker error ("undefined symbol"), the actual
399+
// implementation is not important.
400+
return nullptr;
401+
}
402+
395403
//===----------------------------------------------------------------------===//
396404
// TestDialect
397405
//===----------------------------------------------------------------------===//

mlir/test/mlir-tblgen/attrdefs.td

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,16 @@ def I_TestGenMnemonicAliasAttr : TestAttr<"TestGenMnemonicAlias"> {
186186
// DEF-NEXT: os << "test_gen_mnemonic_alias";
187187
// DEF-NEXT: return ::mlir::OpAsmAliasResult::OverridableAlias;
188188
// DEF-NEXT: }
189+
190+
def J_CustomStorageCtorAttr : AttrDef<Test_Dialect, "CustomStorageCtorAttr"> {
191+
let attrName = "test_custom_storage_ctor_attr";
192+
let parameters = (ins "bool":$flag);
193+
let hasStorageCustomConstructor = 1;
194+
}
195+
196+
// Note: ';' at the end of construct method declaration is important - otherwise
197+
// one cannot provide custom definition
198+
199+
// DEF-LABEL: struct CustomStorageCtorAttrAttrStorage : public ::mlir::AttributeStorage
200+
// DEF: static CustomStorageCtorAttrAttrStorage *construct
201+
// DEF-SAME: (::mlir::AttributeStorageAllocator &allocator, KeyTy &&tblgenKey);

mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,10 +668,10 @@ void DefGen::emitHashKey() {
668668
}
669669

670670
void DefGen::emitConstruct() {
671-
Method *construct = storageCls->addMethod<Method::Inline>(
671+
Method *construct = storageCls->addMethod(
672672
strfmt("{0} *", def.getStorageClassName()), "construct",
673673
def.hasStorageCustomConstructor() ? Method::StaticDeclaration
674-
: Method::Static,
674+
: Method::StaticInline,
675675
MethodParameter(strfmt("::mlir::{0}StorageAllocator &", valueType),
676676
"allocator"),
677677
MethodParameter("KeyTy &&", "tblgenKey"));

0 commit comments

Comments
 (0)