| 1 |
//===- DebugActionTest.cpp - Debug Action Tests ---------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "mlir/Support/DebugAction.h" |
| 10 |
#include "mlir/Support/TypeID.h" |
| 11 |
#include "gmock/gmock.h" |
| 12 |
|
| 13 |
// DebugActionManager is only enabled in DEBUG mode. |
| 14 |
#if LLVM_ENABLE_ABI_BREAKING_CHECKS |
| 15 |
|
| 16 |
using namespace mlir; |
| 17 |
|
| 18 |
namespace { |
| 19 |
struct SimpleAction : DebugAction<SimpleAction> { |
| 20 |
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(SimpleAction) |
| 21 |
static StringRef getTag() { return "simple-action"; } |
| 22 |
static StringRef getDescription() { return "simple-action-description"; } |
| 23 |
}; |
| 24 |
struct OtherSimpleAction : DebugAction<OtherSimpleAction> { |
| 25 |
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(OtherSimpleAction) |
| 26 |
static StringRef getTag() { return "other-simple-action"; } |
| 27 |
static StringRef getDescription() { |
| 28 |
return "other-simple-action-description"; |
| 29 |
} |
| 30 |
}; |
| 31 |
struct ParametricAction : DebugAction<ParametricAction, bool> { |
| 32 |
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(ParametricAction) |
| 33 |
static StringRef getTag() { return "param-action"; } |
| 34 |
static StringRef getDescription() { return "param-action-description"; } |
| 35 |
}; |
| 36 |
|
| 37 |
TEST(DebugActionTest, GenericHandler) { |
| 38 |
DebugActionManager manager; |
| 39 |
|
| 40 |
// A generic handler that always executes the simple action, but not the |
| 41 |
// parametric action. |
| 42 |
struct GenericHandler : DebugActionManager::GenericHandler { |
| 43 |
FailureOr<bool> shouldExecute(StringRef tag, StringRef desc) final { |
| 44 |
if (tag == SimpleAction::getTag()) { |
| 45 |
EXPECT_EQ(desc, SimpleAction::getDescription()); |
| 46 |
return true; |
| 47 |
} |
| 48 |
|
| 49 |
EXPECT_EQ(tag, ParametricAction::getTag()); |
| 50 |
EXPECT_EQ(desc, ParametricAction::getDescription()); |
| 51 |
return false; |
| 52 |
} |
| 53 |
}; |
| 54 |
manager.registerActionHandler<GenericHandler>(); |
| 55 |
|
| 56 |
EXPECT_TRUE(manager.shouldExecute<SimpleAction>()); |
| 57 |
EXPECT_FALSE(manager.shouldExecute<ParametricAction>(true)); |
| 58 |
} |
| 59 |
|
| 60 |
TEST(DebugActionTest, ActionSpecificHandler) { |
| 61 |
DebugActionManager manager; |
| 62 |
|
| 63 |
// Handler that simply uses the input as the decider. |
| 64 |
struct ActionSpecificHandler : ParametricAction::Handler { |
| 65 |
FailureOr<bool> shouldExecute(bool shouldExecuteParam) final { |
| 66 |
return shouldExecuteParam; |
| 67 |
} |
| 68 |
}; |
| 69 |
manager.registerActionHandler<ActionSpecificHandler>(); |
| 70 |
|
| 71 |
EXPECT_TRUE(manager.shouldExecute<ParametricAction>(true)); |
| 72 |
EXPECT_FALSE(manager.shouldExecute<ParametricAction>(false)); |
| 73 |
|
| 74 |
// There is no handler for the simple action, so it is always executed. |
| 75 |
EXPECT_TRUE(manager.shouldExecute<SimpleAction>()); |
| 76 |
} |
| 77 |
|
| 78 |
TEST(DebugActionTest, DebugCounterHandler) { |
| 79 |
DebugActionManager manager; |
| 80 |
|
| 81 |
// Handler that uses the number of action executions as the decider. |
| 82 |
struct DebugCounterHandler : SimpleAction::Handler { |
| 83 |
FailureOr<bool> shouldExecute() final { return numExecutions++ < 3; } |
| 84 |
unsigned numExecutions = 0; |
| 85 |
}; |
| 86 |
manager.registerActionHandler<DebugCounterHandler>(); |
| 87 |
|
| 88 |
// Check that the action is executed 3 times, but no more after. |
| 89 |
EXPECT_TRUE(manager.shouldExecute<SimpleAction>()); |
| 90 |
EXPECT_TRUE(manager.shouldExecute<SimpleAction>()); |
| 91 |
EXPECT_TRUE(manager.shouldExecute<SimpleAction>()); |
| 92 |
EXPECT_FALSE(manager.shouldExecute<SimpleAction>()); |
| 93 |
EXPECT_FALSE(manager.shouldExecute<SimpleAction>()); |
| 94 |
} |
| 95 |
|
| 96 |
TEST(DebugActionTest, NonOverlappingActionSpecificHandlers) { |
| 97 |
DebugActionManager manager; |
| 98 |
|
| 99 |
// One handler returns true and another returns false |
| 100 |
struct SimpleActionHandler : SimpleAction::Handler { |
| 101 |
FailureOr<bool> shouldExecute() final { return true; } |
| 102 |
}; |
| 103 |
struct OtherSimpleActionHandler : OtherSimpleAction::Handler { |
| 104 |
FailureOr<bool> shouldExecute() final { return false; } |
| 105 |
}; |
| 106 |
manager.registerActionHandler<SimpleActionHandler>(); |
| 107 |
manager.registerActionHandler<OtherSimpleActionHandler>(); |
| 108 |
EXPECT_TRUE(manager.shouldExecute<SimpleAction>()); |
| 109 |
EXPECT_FALSE(manager.shouldExecute<OtherSimpleAction>()); |
| 110 |
} |
| 111 |
|
| 112 |
} // namespace |
| 113 |
|
| 114 |
#endif |