Files
clang-p2996/mlir/unittests/Support/DebugCounterTest.cpp
River Riddle dc6a84fce6 [mlir] Add support for DebugCounters using the new DebugAction infrastructure
DebugCounters allow for selectively enabling the execution of a debug action based upon a "counter". This counter is comprised of two components that are used in the control of execution of an action, a "skip" value and a "count" value. The "skip" value is used to skip a certain number of initial executions of a debug action. The "count" value is used to prevent a debug action from executing after it has executed for a set number of times (not including any executions that have been skipped). For example, a counter for a debug action with `skip=47` and `count=2`, would skip the first 47 executions, then execute twice, and finally prevent any further executions.

This is effectively the same as the DebugCounter infrastructure in LLVM, but using the DebugAction infrastructure in MLIR. We can't simply reuse the DebugCounter support already present in LLVM due to its heavy reliance on global constructors (which are not allowed in MLIR). The DebugAction infrastructure already nicely supports the debug counter use case, and promotes the separation of policy and mechanism design philosophy.

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

45 lines
1.4 KiB
C++

//===- DebugCounterTest.cpp - Debug Counter 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/DebugCounter.h"
#include "gmock/gmock.h"
using namespace mlir;
// DebugActionManager is only enabled in DEBUG mode.
#ifndef NDEBUG
namespace {
struct CounterAction : public DebugAction<> {
static StringRef getTag() { return "counter-action"; }
static StringRef getDescription() { return "Test action for debug counters"; }
};
TEST(DebugCounterTest, CounterTest) {
std::unique_ptr<DebugCounter> counter = std::make_unique<DebugCounter>();
counter->addCounter(CounterAction::getTag(), /*countToSkip=*/1,
/*countToStopAfter=*/3);
DebugActionManager manager;
manager.registerActionHandler(std::move(counter));
// The first execution is skipped.
EXPECT_FALSE(manager.shouldExecute<CounterAction>());
// The counter stops after 3 successful executions.
EXPECT_TRUE(manager.shouldExecute<CounterAction>());
EXPECT_TRUE(manager.shouldExecute<CounterAction>());
EXPECT_TRUE(manager.shouldExecute<CounterAction>());
EXPECT_FALSE(manager.shouldExecute<CounterAction>());
}
} // namespace
#endif