Skip to content

Avoid copies in getChecked #147721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -495,7 +495,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
31 changes: 26 additions & 5 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 @@ -489,15 +490,35 @@ TEST(CopyCountAttr, CopyCount) {
test::CopyCount::counter = 0;
test::TestCopyCountAttr::get(&context, std::move(copyCount));
#ifndef NDEBUG
// One verification enabled only in assert-mode requires a copy.
EXPECT_EQ(counter1, 1);
EXPECT_EQ(test::CopyCount::counter, 1);
// One verification enabled only in assert-mode requires two copies: one for
// calling 'verifyInvariants' and one for calling 'verify' inside
// 'verifyInvariants'.
EXPECT_EQ(counter1, 2);
EXPECT_EQ(test::CopyCount::counter, 2);
#else
EXPECT_EQ(counter1, 0);
EXPECT_EQ(test::CopyCount::counter, 0);
#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));
// The verifiers require two copies: one for calling 'verifyInvariants' and
// one for calling 'verify' inside 'verifyInvariants'.
EXPECT_EQ(counter1, 2);
EXPECT_EQ(test::CopyCount::counter, 2);
}

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