[Mlir-commits] [mlir] [MLIR] Add getter for the action handler of MLIR context (PR #197230)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Wed May 13 03:15:24 PDT 2026
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff origin/main HEAD --extensions cpp,h -- mlir/unittests/Debug/ActionHandlerTest.cpp mlir/include/mlir/IR/MLIRContext.h mlir/lib/IR/MLIRContext.cpp --diff_from_common_commit
``````````
:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/mlir/include/mlir/IR/MLIRContext.h b/mlir/include/mlir/IR/MLIRContext.h
index ac08695b7..ab121777a 100644
--- a/mlir/include/mlir/IR/MLIRContext.h
+++ b/mlir/include/mlir/IR/MLIRContext.h
@@ -267,8 +267,8 @@ public:
/// context. A nullptr handler can be set to disable a previously set handler.
void registerActionHandler(HandlerTy handler);
- /// Return a reference to the currently registered action handler. Its target can
- /// be used to gain access to the handler's state, if any.
+ /// 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();
/// Return true if a valid ActionHandler is set.
diff --git a/mlir/lib/IR/MLIRContext.cpp b/mlir/lib/IR/MLIRContext.cpp
index ba9d22518..624121c27 100644
--- a/mlir/lib/IR/MLIRContext.cpp
+++ b/mlir/lib/IR/MLIRContext.cpp
@@ -380,7 +380,9 @@ void MLIRContext::registerActionHandler(HandlerTy handler) {
getImpl().actionHandler = std::move(handler);
}
-const MLIRContext::HandlerTy &MLIRContext::getActionHandler() { return getImpl().actionHandler; }
+const MLIRContext::HandlerTy &MLIRContext::getActionHandler() {
+ return getImpl().actionHandler;
+}
/// Dispatch the provided action to the handler if any, or just execute it.
void MLIRContext::executeActionInternal(function_ref<void()> actionFn,
diff --git a/mlir/unittests/Debug/ActionHandlerTest.cpp b/mlir/unittests/Debug/ActionHandlerTest.cpp
index 95f31250c..4cc3dde38 100644
--- a/mlir/unittests/Debug/ActionHandlerTest.cpp
+++ b/mlir/unittests/Debug/ActionHandlerTest.cpp
@@ -19,10 +19,10 @@ using namespace mlir::tracing;
namespace {
struct DummyAction final : ActionImpl<DummyAction> {
- MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(DummyAction)
- static constexpr StringLiteral tag = "dummy-action";
+ MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(DummyAction)
+ static constexpr StringLiteral tag = "dummy-action";
- DummyAction(llvm::ArrayRef<IRUnit> irUnits) {}
+ DummyAction(llvm::ArrayRef<IRUnit> irUnits) {}
};
} // namespace
@@ -31,65 +31,66 @@ namespace {
// State class
struct HandlerState {
- bool enabled{true};
+ bool enabled{true};
};
// Owner of a shared_ptr to the state
struct StatefulHandler {
- std::shared_ptr<HandlerState> state;
-
- void operator()(mlir::function_ref<void()> actionFn,
- const mlir::tracing::Action& /*action*/) const {
- if (!state->enabled) {
- // Skip execution entirely when disabled.
- return;
- }
- actionFn();
+ std::shared_ptr<HandlerState> state;
+
+ void operator()(mlir::function_ref<void()> actionFn,
+ const mlir::tracing::Action & /*action*/) const {
+ if (!state->enabled) {
+ // Skip execution entirely when disabled.
+ return;
}
+ actionFn();
+ }
};
TEST(ActionHandlerSharedState, EnabledState) {
- mlir::MLIRContext ctx;
+ mlir::MLIRContext ctx;
- ctx.registerActionHandler(StatefulHandler{std::make_shared<HandlerState>()});
+ ctx.registerActionHandler(StatefulHandler{std::make_shared<HandlerState>()});
- auto handlerRef = ctx.getActionHandler();
- ASSERT_TRUE(static_cast<bool>(handlerRef));
+ auto handlerRef = ctx.getActionHandler();
+ ASSERT_TRUE(static_cast<bool>(handlerRef));
- int executionCount = 0;
- auto workFn = [&]() { ++executionCount; };
+ int executionCount = 0;
+ auto workFn = [&]() { ++executionCount; };
- ctx.executeAction<DummyAction>(workFn, {});
+ ctx.executeAction<DummyAction>(workFn, {});
- // Recover the shared_ptr from the handler via target<StatefulHandler>().
- // target<T>() returns a non-null pointer only when the stored callable type
- // matches T exactly — which is guaranteed here since we registered StatefulHandler.
- auto* recovered = handlerRef.target<StatefulHandler>();
- ASSERT_NE(recovered, nullptr);
+ // Recover the shared_ptr from the handler via target<StatefulHandler>().
+ // target<T>() returns a non-null pointer only when the stored callable type
+ // matches T exactly — which is guaranteed here since we registered
+ // StatefulHandler.
+ auto *recovered = handlerRef.target<StatefulHandler>();
+ ASSERT_NE(recovered, nullptr);
- EXPECT_EQ(executionCount, 1);
- EXPECT_TRUE(recovered->state->enabled == true);
+ EXPECT_EQ(executionCount, 1);
+ EXPECT_TRUE(recovered->state->enabled == true);
}
TEST(ActionHandlerSharedState, DisabledState) {
- mlir::MLIRContext ctx;
+ mlir::MLIRContext ctx;
- ctx.registerActionHandler(StatefulHandler{std::make_shared<HandlerState>()});
+ ctx.registerActionHandler(StatefulHandler{std::make_shared<HandlerState>()});
- // Recover the shared_ptr and disable the state
- auto handlerRef = ctx.getActionHandler();
- auto* recovered = handlerRef.target<StatefulHandler>();
- ASSERT_NE(recovered, nullptr);
- recovered->state->enabled = false;
+ // Recover the shared_ptr and disable the state
+ auto handlerRef = ctx.getActionHandler();
+ auto *recovered = handlerRef.target<StatefulHandler>();
+ ASSERT_NE(recovered, nullptr);
+ recovered->state->enabled = false;
- int executionCount = 0;
- auto workFn = [&]() { ++executionCount; };
+ int executionCount = 0;
+ auto workFn = [&]() { ++executionCount; };
- ctx.executeAction<DummyAction>(workFn, {});
+ ctx.executeAction<DummyAction>(workFn, {});
- // workFn was skipped because the handler saw enabled==false
- EXPECT_EQ(executionCount, 0);
- EXPECT_TRUE(recovered->state->enabled == false);
+ // workFn was skipped because the handler saw enabled==false
+ EXPECT_EQ(executionCount, 0);
+ EXPECT_TRUE(recovered->state->enabled == false);
}
} // namespace
``````````
</details>
https://github.com/llvm/llvm-project/pull/197230
More information about the Mlir-commits
mailing list