Skip to content

[MLIR] Add a non-const ActionHandler getter to MLIRContext#199652

Merged
hockyy merged 2 commits into
llvm:mainfrom
akigyosi:mlir_context_getter_nonconst
May 27, 2026
Merged

[MLIR] Add a non-const ActionHandler getter to MLIRContext#199652
hockyy merged 2 commits into
llvm:mainfrom
akigyosi:mlir_context_getter_nonconst

Conversation

@akigyosi
Copy link
Copy Markdown
Contributor

@akigyosi akigyosi commented May 26, 2026

#197230 added a getter for the ActionHandler, but only returns a const ref with a non-const accessor.
Instead provide both variants: a const accessor returning a const ref and non-const one returning a mutable ref.

@github-actions
Copy link
Copy Markdown

Hello @akigyosi 👋

Thank you for submitting a Pull Request (PR) to the LLVM Project. Since this is your first PR, here are a few useful links covering our main contribution policies and review practices.

  • All contributions to LLVM must follow our LLVM AI Tool Use Policy. In particular, if you used AI while working on this PR, remember to add a note to the PR description.
  • The LLVM Code-Review Policy and Practices document contains practical information about the PR process, including how patches are reviewed and accepted, and who can review a PR.
  • Our LLVM Developer Policy describes our expectations for code quality, commit summaries and contains notes on our CI system.

Please reply to this message to confirm that you have read these policies, especially the LLVM AI Tool Use Policy, and that any AI tool usage has been noted in the PR description.


Frequently asked questions

How do I add reviewers?

This PR will be automatically labeled, and the relevant teams will be notified. For some parts of the project, reviewers may also be added automatically.

You can also add reviewers manually using the Reviewers section on this page. If you cannot use that section, it is probably because you do not have write permissions for the repository. In that case, you can request a review by tagging reviewers in a comment using @ followed by their GitHub username.

What if there are no comments?

If you have not received any comments on your PR after a week, you can request a review by pinging the PR with a comment such as “Ping”. The common courtesy ping rate is once a week. Please remember that you are asking for volunteer time from other developers.

Are any special GitHub settings required to contribute to LLVM?

We only require contributors to have a public email address associated with their GitHub commits, see this section of LLVM Developer Policy for details.


If you have questions, feel free to leave a comment on this PR, or ask on LLVM Discord or LLVM Discourse.

Thank you,
The LLVM Community

@llvmorg-github-actions llvmorg-github-actions Bot added mlir:core MLIR Core Infrastructure mlir labels May 26, 2026
@llvmorg-github-actions
Copy link
Copy Markdown

llvmorg-github-actions Bot commented May 26, 2026

@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-core

Author: Kigyosi Alexandru (akigyosi)

Changes

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

2 Files Affected:

  • (modified) mlir/include/mlir/IR/MLIRContext.h (+2-1)
  • (modified) mlir/lib/IR/MLIRContext.cpp (+5-1)
diff --git a/mlir/include/mlir/IR/MLIRContext.h b/mlir/include/mlir/IR/MLIRContext.h
index ab121777a1296..d2e0c4a9a1b7f 100644
--- a/mlir/include/mlir/IR/MLIRContext.h
+++ b/mlir/include/mlir/IR/MLIRContext.h
@@ -269,7 +269,8 @@ class MLIRContext {
 
   /// Return a reference to the currently registered action handler. Its target
   /// can be used to gain access to the handler's state, if any.
-  const HandlerTy &getActionHandler();
+  const HandlerTy &getActionHandler() const;
+  HandlerTy &getActionHandler();
 
   /// Return true if a valid ActionHandler is set.
   bool hasActionHandler();
diff --git a/mlir/lib/IR/MLIRContext.cpp b/mlir/lib/IR/MLIRContext.cpp
index 624121c2733ba..da891a7e6e014 100644
--- a/mlir/lib/IR/MLIRContext.cpp
+++ b/mlir/lib/IR/MLIRContext.cpp
@@ -380,7 +380,11 @@ void MLIRContext::registerActionHandler(HandlerTy handler) {
   getImpl().actionHandler = std::move(handler);
 }
 
-const MLIRContext::HandlerTy &MLIRContext::getActionHandler() {
+const MLIRContext::HandlerTy &MLIRContext::getActionHandler() const {
+  return getImpl().actionHandler;
+}
+
+MLIRContext::HandlerTy &MLIRContext::getActionHandler() {
   return getImpl().actionHandler;
 }
 

@joker-eph joker-eph changed the title ActionHandler Non-const getter [MLIR] Add a non-const ActionHandler getter to MLIRContext May 26, 2026
@joker-eph
Copy link
Copy Markdown
Contributor

You have some CI failures to look at here. Here is copilot report:

Root cause

The failing compile is in mlir/lib/IR/MLIRContext.cpp at line 384:

const MLIRContext::HandlerTy &MLIRContext::getActionHandler() const {
  return getImpl().actionHandler;
}

But in mlir/include/mlir/IR/MLIRContext.h, getImpl() is only declared as non-const:

MLIRContextImpl &getImpl() { return *impl; }

So the const overload of getActionHandler() tries to call a non-const member on const MLIRContext, which Clang correctly rejects:

error in MLIRContext.cpp:384
note pointing to MLIRContext.h:213
Fix

Add a const overload of getImpl() in MLIRContext.h.

Suggested patch:

  // This is effectively private given that only MLIRContext.cpp can see the
  // MLIRContextImpl type.
  MLIRContextImpl &getImpl() { return *impl; }
  const MLIRContextImpl &getImpl() const { return *impl; }

Why this is the right fix

  • It preserves the existing API shape.
  • It matches the intended use of getActionHandler() const.
  • It avoids unsafe casts or making getActionHandler() const non-const.

Other const member functions can then safely access implementation state through getImpl().
Minimal code change

In mlir/include/mlir/IR/MLIRContext.h, change:

MLIRContextImpl &getImpl() { return *impl; }

to:

MLIRContextImpl &getImpl() { return *impl; }
const MLIRContextImpl &getImpl() const { return *impl; }

@joker-eph joker-eph enabled auto-merge (squash) May 26, 2026 10:34
auto-merge was automatically disabled May 27, 2026 04:05

Head branch was pushed to by a user without write access

@akigyosi akigyosi force-pushed the mlir_context_getter_nonconst branch from 3e3ec69 to 9255e23 Compare May 27, 2026 04:05
@hockyy hockyy merged commit 2b28679 into llvm:main May 27, 2026
10 checks passed
@github-actions
Copy link
Copy Markdown

@akigyosi Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@hockyy hockyy requested review from hockyy and removed request for hockyy May 27, 2026 06:43
@llvm-ci
Copy link
Copy Markdown

llvm-ci commented May 27, 2026

LLVM Buildbot has detected a new failure on builder ppc64le-mlir-rhel-clang running on ppc64le-mlir-rhel-test while building mlir at step 6 "test-build-check-mlir-build-only-check-mlir".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/129/builds/44980

Here is the relevant piece of the build log for the reference
Step 6 (test-build-check-mlir-build-only-check-mlir) failure: 1200 seconds without output running [b'ninja', b'check-mlir'], attempting to kill
...
PASS: MLIR :: Dialect/Affine/loop-fusion-4.mlir (4188 of 4198)
PASS: MLIR :: mlir-tblgen/op-result.td (4189 of 4198)
PASS: MLIR :: Conversion/IndexToLLVM/index-to-llvm.mlir (4190 of 4198)
PASS: MLIR :: mlir-rewrite/simple.mlir (4191 of 4198)
PASS: MLIR :: Pass/ir-printing.mlir (4192 of 4198)
PASS: MLIR :: Transforms/canonicalize-filter-dialects.mlir (4193 of 4198)
PASS: MLIR :: Pass/action-logging-filter.mlir (4194 of 4198)
PASS: MLIR :: mlir-runner/utils.mlir (4195 of 4198)
PASS: MLIR :: mlir-tblgen/rewriter-errors.td (4196 of 4198)
PASS: MLIR :: mlir-tblgen/llvm-intrinsics.td (4197 of 4198)
command timed out: 1200 seconds without output running [b'ninja', b'check-mlir'], attempting to kill
process killed by signal 9
program finished with exit code -1
elapsedTime=2535.343931

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.

4 participants