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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

AlexandruLorinti
Copy link

Amending #68067 ; adding std::move to getChecked method as well.

Copy link

github-actions bot commented Jul 9, 2025

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@AlexandruLorinti AlexandruLorinti marked this pull request as ready for review July 9, 2025 13:17
@llvmbot llvmbot added mlir:core MLIR Core Infrastructure mlir labels Jul 9, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 9, 2025

@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-core

Author: Alexandru Lorinti (AlexandruLorinti)

Changes

Amending #68067 ; adding std::move to getChecked method as well.


Full diff: https://github.com/llvm/llvm-project/pull/147721.diff

1 Files Affected:

  • (modified) mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp (+1-1)
diff --git a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
index d9aa901ee2b28..5f6e7e7d4ab35 100644
--- a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
+++ b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
@@ -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>

@joker-eph
Copy link
Collaborator

Hi,

Thanks for the contribution! I would think there would be a tablegen test to update? Otherwise we probably miss coverage.

As I remember it: I couldn't add move to the getChecked() sequence because in assert build we do getChecked() as part of the get(), and that would lead to a use-after-move issue.
But maybe you're only touching one level further in the call-stack?

@AlexandruLorinti
Copy link
Author

AlexandruLorinti commented Jul 15, 2025

Thanks @joker-eph for feedback!

As per current implementation of getChecked, we only have a return and wouldn't get in the situation of a use-after-move:

  auto scope = body.scope("return Base::getChecked(emitError, context", ");");
  for (const auto &param : params)
    body << ", " << param.getName();

just an example, without the change we would have this generated code:

ModuleFlagAttr ModuleFlagAttr::getChecked(::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError, ::mlir::MLIRContext *context, ModFlagBehavior behavior, StringAttr key, Attribute value) {
  return Base::getChecked(emitError, context, behavior, key, value);
}

and with the change, the above generated code would be:

ModuleFlagAttr ModuleFlagAttr::getChecked(::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError, ::mlir::MLIRContext *context, ModFlagBehavior behavior, StringAttr key, Attribute value) {
  return Base::getChecked(emitError, context, std::move(behavior), std::move(key), std::move(value));
}

Could you point me to specific test that would need to be updated?

Copy link
Collaborator

@joker-eph joker-eph left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG, thanks!

@joker-eph
Copy link
Collaborator

For testing, could you adopt the same strategy as the original PR you're referencing?

@AlexandruLorinti AlexandruLorinti force-pushed the avoid_copies_in_getChecked branch 2 times, most recently from 1c0face to 640d3ba Compare July 18, 2025 11:24
@joker-eph
Copy link
Collaborator

For testing, could you adopt the same strategy as the original PR you're referencing?

I was thinking about adding a case in mlir/unittests/IR/AttributeTest.cpp for getChecked() actually, did you look into this?

@AlexandruLorinti AlexandruLorinti force-pushed the avoid_copies_in_getChecked branch from ad7fe7f to 498f094 Compare July 22, 2025 11:39
@AlexandruLorinti
Copy link
Author

Hi @joker-eph, could you review latest changes?

Copy link

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff HEAD~1 HEAD --extensions h,cpp -- mlir/include/mlir/IR/StorageUniquerSupport.h mlir/test/lib/Dialect/Test/TestAttributes.cpp mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp mlir/unittests/IR/AttributeTest.cpp
View the diff from clang-format here.
diff --git a/mlir/test/lib/Dialect/Test/TestAttributes.cpp b/mlir/test/lib/Dialect/Test/TestAttributes.cpp
index d728d2c47..58909131e 100644
--- a/mlir/test/lib/Dialect/Test/TestAttributes.cpp
+++ b/mlir/test/lib/Dialect/Test/TestAttributes.cpp
@@ -217,7 +217,9 @@ static void printTrueFalse(AsmPrinter &p, std::optional<int> result) {
 // TestCopyCountAttr Implementation
 //===----------------------------------------------------------------------===//
 
-LogicalResult TestCopyCountAttr::verify(llvm::function_ref<::mlir::InFlightDiagnostic()> /*emitError*/, CopyCount /*copy_count*/) {
+LogicalResult TestCopyCountAttr::verify(
+    llvm::function_ref<::mlir::InFlightDiagnostic()> /*emitError*/,
+    CopyCount /*copy_count*/) {
   return success();
 }
 

@joker-eph
Copy link
Collaborator

You need to fix the formatting (git clang-format <hash>) before I can merge this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mlir:core MLIR Core Infrastructure mlir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants