MLIR  19.0.0git
Counter.h
Go to the documentation of this file.
1 //===- DebugCounter.h - Debug Counter support -------------------*- C++ -*-===//
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 #ifndef MLIR_TRACING_DEBUGCOUNTER_H
10 #define MLIR_TRACING_DEBUGCOUNTER_H
11 
12 #include "mlir/IR/Action.h"
13 #include "llvm/ADT/StringMap.h"
14 #include <string>
15 
16 namespace mlir {
17 namespace tracing {
18 
19 /// This class implements an action handler that attaches a counter value
20 /// to debug actions and enables/disables execution of these action based on the
21 /// value of the counter. The counter controls the execution of the action with
22 /// a "skip" and "count" value. The "skip" value is used to skip a certain
23 /// number of initial executions of an action. The "count" value is used to
24 /// prevent an action from executing after it has executed for a set number
25 /// of times (not including any executions that have been skipped). For example,
26 /// a counter for an action with `skip=47` and `count=2`, would skip the
27 /// first 47 executions, then execute twice, and finally prevent any further
28 /// executions.
29 class DebugCounter {
30 public:
31  DebugCounter();
32  ~DebugCounter();
33 
34  /// Add a counter for the given action tag. `countToSkip` is the number
35  /// of counter executions to skip before enabling execution of the action.
36  /// `countToStopAfter` is the number of executions of the counter to allow
37  /// before preventing the action from executing any more.
38  void addCounter(StringRef actionTag, int64_t countToSkip,
39  int64_t countToStopAfter);
40 
41  /// Entry point for handling actions.
42  void operator()(llvm::function_ref<void()> transform, const Action &action);
43 
44  /// Print the counters that have been registered with this instance to the
45  /// provided output stream.
46  void print(raw_ostream &os) const;
47 
48  /// Register the command line options for debug counters.
49  static void registerCLOptions();
50  /// Returns true if any of the CL options are activated.
51  static bool isActivated();
52 
53 private:
54  // Returns true if the next action matching this tag should be executed.
55  bool shouldExecute(StringRef tag);
56 
57  /// Apply the registered CL options to this debug counter instance.
58  void applyCLOptions();
59 
60  /// This struct represents a specific counter being tracked.
61  struct Counter {
62  Counter(int64_t countToSkip = 0, int64_t countToStopAfter = -1)
63  : countToSkip(countToSkip), countToStopAfter(countToStopAfter) {}
64 
65  /// The current count of this counter.
66  int64_t count{0};
67  /// The number of initial executions of this counter to skip.
68  int64_t countToSkip;
69  /// The number of times to execute this counter before stopping.
70  int64_t countToStopAfter;
71  };
72 
73  /// A mapping between a given action tag and its counter information.
74  llvm::StringMap<Counter> counters;
75 };
76 
77 } // namespace tracing
78 } // namespace mlir
79 
80 #endif // MLIR_TRACING_DEBUGCOUNTER_H
An action is a specific action that is to be taken by the compiler, that can be toggled and controlle...
Definition: Action.h:39
This class implements an action handler that attaches a counter value to debug actions and enables/di...
Definition: Counter.h:29
static void registerCLOptions()
Register the command line options for debug counters.
void print(raw_ostream &os) const
Print the counters that have been registered with this instance to the provided output stream.
void addCounter(StringRef actionTag, int64_t countToSkip, int64_t countToStopAfter)
Add a counter for the given action tag.
static bool isActivated()
Returns true if any of the CL options are activated.
void operator()(llvm::function_ref< void()> transform, const Action &action)
Entry point for handling actions.
Include the generated interface declarations.