Skip to content
This repository was archived by the owner on Nov 27, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mlir/include/mlir/IR/StorageUniquerSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ class StorageUserBase : public BaseT, public Traits<ConcreteT>... {
// If the construction invariants fail then we return a null attribute.
if (failed(ConcreteT::verifyInvariants(emitErrorFn, args...)))
return ConcreteT();
return UniquerT::template get<ConcreteT>(ctx, args...);
return UniquerT::template get<ConcreteT>(ctx, std::forward<Args>(args)...);
}

/// Get an instance of the concrete type from a void pointer.
Expand Down
1 change: 1 addition & 0 deletions mlir/test/lib/Dialect/Test/TestAttrDefs.td
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ def TestCopyCount : Test_Attr<"TestCopyCount"> {
let mnemonic = "copy_count";
let parameters = (ins TestParamCopyCount:$copy_count);
let assemblyFormat = "`<` $copy_count `>`";
let genVerifyDecl = 1;
}

def TestConditionalAliasAttr : Test_Attr<"TestConditionalAlias"> {
Expand Down
10 changes: 10 additions & 0 deletions mlir/test/lib/Dialect/Test/TestAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,16 @@ static void printTrueFalse(AsmPrinter &p, std::optional<int> result) {
p << (*result ? "true" : "false");
}

//===----------------------------------------------------------------------===//
// TestCopyCountAttr Implementation
//===----------------------------------------------------------------------===//

LogicalResult TestCopyCountAttr::verify(
llvm::function_ref<::mlir::InFlightDiagnostic()> /*emitError*/,
CopyCount /*copy_count*/) {
return success();
}

//===----------------------------------------------------------------------===//
// CopyCountAttr Implementation
//===----------------------------------------------------------------------===//
Expand Down
5 changes: 5 additions & 0 deletions mlir/test/mlir-tblgen/attrdefs.td
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ def B_CompoundAttrA : TestAttr<"CompoundA"> {
// DEF: return new (allocator.allocate<CompoundAAttrStorage>())
// DEF-SAME: CompoundAAttrStorage(std::move(widthOfSomething), std::move(exampleTdType), std::move(apFloat), std::move(dims), std::move(inner));

// DEF: CompoundAAttr CompoundAAttr::getChecked(
// DEF-SAME: int widthOfSomething, ::test::SimpleTypeA exampleTdType, ::llvm::APFloat apFloat, ::llvm::ArrayRef<int> dims, ::mlir::Type inner
// DEF-SAME: )
// DEF-NEXT: return Base::getChecked(emitError, context, std::move(widthOfSomething), std::move(exampleTdType), std::move(apFloat), std::move(dims), std::move(inner));

// DEF: ::mlir::Type CompoundAAttr::getInner() const {
// DEF-NEXT: return getImpl()->inner;
}
Expand Down
2 changes: 1 addition & 1 deletion mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ void DefGen::emitCheckedBuilder() {
MethodBody &body = m->body().indent();
auto scope = body.scope("return Base::getChecked(emitError, context", ");");
for (const auto &param : params)
body << ", " << param.getName();
body << ", std::move(" << param.getName() << ")";
}

static SmallVector<MethodParameter>
Expand Down
22 changes: 20 additions & 2 deletions mlir/unittests/IR/AttributeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,9 @@ TEST(SubElementTest, Nested) {
{strAttr, trueAttr, falseAttr, boolArrayAttr, dictAttr}));
}

// Test how many times we call copy-ctor when building an attribute.
TEST(CopyCountAttr, CopyCount) {
// Test how many times we call copy-ctor when building an attribute with the
// 'get' method.
TEST(CopyCountAttr, CopyCountGet) {
MLIRContext context;
context.loadDialect<test::TestDialect>();

Expand All @@ -498,6 +499,23 @@ TEST(CopyCountAttr, CopyCount) {
#endif
}

// Test how many times we call copy-ctor when building an attribute with the
// 'getChecked' method.
TEST(CopyCountAttr, CopyCountGetChecked) {
MLIRContext context;
context.loadDialect<test::TestDialect>();
test::CopyCount::counter = 0;
test::CopyCount copyCount("hello");
auto loc = UnknownLoc::get(&context);
test::TestCopyCountAttr::getChecked(loc, &context, std::move(copyCount));
int counter1 = test::CopyCount::counter;
test::CopyCount::counter = 0;
test::TestCopyCountAttr::getChecked(loc, &context, std::move(copyCount));
// One verification requires a copy.
EXPECT_EQ(counter1, 1);
EXPECT_EQ(test::CopyCount::counter, 1);
}

// Test stripped printing using test dialect attribute.
TEST(CopyCountAttr, PrintStripped) {
MLIRContext context;
Expand Down
Loading