Skip to content
This repository was archived by the owner on Nov 27, 2025. It is now read-only.

Commit d113304

Browse files
Avoid copies in getChecked (cherry-pick from upstream) (#151)
Cherry-pick from upstream llvm: llvm/llvm-project#147721; adding std::move to getChecked method.
1 parent 7fec41b commit d113304

File tree

6 files changed

+38
-4
lines changed

6 files changed

+38
-4
lines changed

mlir/include/mlir/IR/StorageUniquerSupport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ class StorageUserBase : public BaseT, public Traits<ConcreteT>... {
200200
// If the construction invariants fail then we return a null attribute.
201201
if (failed(ConcreteT::verifyInvariants(emitErrorFn, args...)))
202202
return ConcreteT();
203-
return UniquerT::template get<ConcreteT>(ctx, args...);
203+
return UniquerT::template get<ConcreteT>(ctx, std::forward<Args>(args)...);
204204
}
205205

206206
/// Get an instance of the concrete type from a void pointer.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ def TestCopyCount : Test_Attr<"TestCopyCount"> {
347347
let mnemonic = "copy_count";
348348
let parameters = (ins TestParamCopyCount:$copy_count);
349349
let assemblyFormat = "`<` $copy_count `>`";
350+
let genVerifyDecl = 1;
350351
}
351352

352353
def TestConditionalAliasAttr : Test_Attr<"TestConditionalAlias"> {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,16 @@ static void printTrueFalse(AsmPrinter &p, std::optional<int> result) {
213213
p << (*result ? "true" : "false");
214214
}
215215

216+
//===----------------------------------------------------------------------===//
217+
// TestCopyCountAttr Implementation
218+
//===----------------------------------------------------------------------===//
219+
220+
LogicalResult TestCopyCountAttr::verify(
221+
llvm::function_ref<::mlir::InFlightDiagnostic()> /*emitError*/,
222+
CopyCount /*copy_count*/) {
223+
return success();
224+
}
225+
216226
//===----------------------------------------------------------------------===//
217227
// CopyCountAttr Implementation
218228
//===----------------------------------------------------------------------===//

mlir/test/mlir-tblgen/attrdefs.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ def B_CompoundAttrA : TestAttr<"CompoundA"> {
115115
// DEF: return new (allocator.allocate<CompoundAAttrStorage>())
116116
// DEF-SAME: CompoundAAttrStorage(std::move(widthOfSomething), std::move(exampleTdType), std::move(apFloat), std::move(dims), std::move(inner));
117117

118+
// DEF: CompoundAAttr CompoundAAttr::getChecked(
119+
// DEF-SAME: int widthOfSomething, ::test::SimpleTypeA exampleTdType, ::llvm::APFloat apFloat, ::llvm::ArrayRef<int> dims, ::mlir::Type inner
120+
// DEF-SAME: )
121+
// DEF-NEXT: return Base::getChecked(emitError, context, std::move(widthOfSomething), std::move(exampleTdType), std::move(apFloat), std::move(dims), std::move(inner));
122+
118123
// DEF: ::mlir::Type CompoundAAttr::getInner() const {
119124
// DEF-NEXT: return getImpl()->inner;
120125
}

mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ void DefGen::emitCheckedBuilder() {
473473
MethodBody &body = m->body().indent();
474474
auto scope = body.scope("return Base::getChecked(emitError, context", ");");
475475
for (const auto &param : params)
476-
body << ", " << param.getName();
476+
body << ", std::move(" << param.getName() << ")";
477477
}
478478

479479
static SmallVector<MethodParameter>

mlir/unittests/IR/AttributeTest.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,9 @@ TEST(SubElementTest, Nested) {
477477
{strAttr, trueAttr, falseAttr, boolArrayAttr, dictAttr}));
478478
}
479479

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

@@ -498,6 +499,23 @@ TEST(CopyCountAttr, CopyCount) {
498499
#endif
499500
}
500501

502+
// Test how many times we call copy-ctor when building an attribute with the
503+
// 'getChecked' method.
504+
TEST(CopyCountAttr, CopyCountGetChecked) {
505+
MLIRContext context;
506+
context.loadDialect<test::TestDialect>();
507+
test::CopyCount::counter = 0;
508+
test::CopyCount copyCount("hello");
509+
auto loc = UnknownLoc::get(&context);
510+
test::TestCopyCountAttr::getChecked(loc, &context, std::move(copyCount));
511+
int counter1 = test::CopyCount::counter;
512+
test::CopyCount::counter = 0;
513+
test::TestCopyCountAttr::getChecked(loc, &context, std::move(copyCount));
514+
// One verification requires a copy.
515+
EXPECT_EQ(counter1, 1);
516+
EXPECT_EQ(test::CopyCount::counter, 1);
517+
}
518+
501519
// Test stripped printing using test dialect attribute.
502520
TEST(CopyCountAttr, PrintStripped) {
503521
MLIRContext context;

0 commit comments

Comments
 (0)