Files
clang-p2996/mlir/unittests/Support/DebugActionTest.cpp
River Riddle 72d5afa4ac [mlir] Add a new debug action framework.
This revision adds the infrastructure for `Debug Actions`. This is a DEBUG only
API that allows for external entities to control various aspects of compiler
execution. This is conceptually similar to something like DebugCounters in LLVM, but at a lower level. This framework doesn't make any assumptions about how the higher level driver is controlling the execution, it merely provides a framework for connecting the two together. This means that on top of DebugCounter functionality, we could also provide more interesting drivers such as interactive execution. A high level overview of the workflow surrounding debug actions is
shown below:

*   Compiler developer defines an `action` that is taken by the a pass,
    transformation, utility that they are developing.
*   Depending on the needs, the developer dispatches various queries, pertaining
    to this action, to an `action manager` that will provide an answer as to
    what behavior the action should do.
*   An external entity registers an `action handler` with the action manager,
    and provides the logic to resolve queries on actions.

The exact definition of an `external entity` is left opaque, to allow for more
interesting handlers.

This framework was proposed here: https://llvm.discourse.group/t/rfc-debug-actions-in-mlir-debug-counters-for-the-modern-world

Differential Revision: https://reviews.llvm.org/D84986
2021-02-23 00:52:17 -08:00

91 lines
3.0 KiB
C++

//===- DebugActionTest.cpp - Debug Action Tests ---------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "mlir/Support/DebugAction.h"
#include "gmock/gmock.h"
// DebugActionManager is only enabled in DEBUG mode.
#ifndef NDEBUG
using namespace mlir;
namespace {
struct SimpleAction : public DebugAction<> {
static StringRef getTag() { return "simple-action"; }
static StringRef getDescription() { return "simple-action-description"; }
};
struct ParametricAction : public DebugAction<bool> {
static StringRef getTag() { return "param-action"; }
static StringRef getDescription() { return "param-action-description"; }
};
TEST(DebugActionTest, GenericHandler) {
DebugActionManager manager;
// A generic handler that always executes the simple action, but not the
// parametric action.
struct GenericHandler : public DebugActionManager::GenericHandler {
FailureOr<bool> shouldExecute(StringRef tag, StringRef desc) final {
if (tag == SimpleAction::getTag()) {
EXPECT_EQ(desc, SimpleAction::getDescription());
return true;
}
EXPECT_EQ(tag, ParametricAction::getTag());
EXPECT_EQ(desc, ParametricAction::getDescription());
return false;
}
};
manager.registerActionHandler<GenericHandler>();
EXPECT_TRUE(manager.shouldExecute<SimpleAction>());
EXPECT_FALSE(manager.shouldExecute<ParametricAction>(true));
}
TEST(DebugActionTest, ActionSpecificHandler) {
DebugActionManager manager;
// Handler that simply uses the input as the decider.
struct ActionSpecificHandler : public ParametricAction::Handler {
FailureOr<bool> shouldExecute(bool shouldExecuteParam) final {
return shouldExecuteParam;
}
};
manager.registerActionHandler<ActionSpecificHandler>();
EXPECT_TRUE(manager.shouldExecute<ParametricAction>(true));
EXPECT_FALSE(manager.shouldExecute<ParametricAction>(false));
// There is no handler for the simple action, so it is always executed.
EXPECT_TRUE(manager.shouldExecute<SimpleAction>());
}
TEST(DebugActionTest, DebugCounterHandler) {
DebugActionManager manager;
// Handler that uses the number of action executions as the decider.
struct DebugCounterHandler : public SimpleAction::Handler {
FailureOr<bool> shouldExecute() final {
return numExecutions++ < 3;
}
unsigned numExecutions = 0;
};
manager.registerActionHandler<DebugCounterHandler>();
// Check that the action is executed 3 times, but no more after.
EXPECT_TRUE(manager.shouldExecute<SimpleAction>());
EXPECT_TRUE(manager.shouldExecute<SimpleAction>());
EXPECT_TRUE(manager.shouldExecute<SimpleAction>());
EXPECT_FALSE(manager.shouldExecute<SimpleAction>());
EXPECT_FALSE(manager.shouldExecute<SimpleAction>());
}
} // namespace
#endif