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_SUPPORT_DEBUGCOUNTER_H 10 #define MLIR_SUPPORT_DEBUGCOUNTER_H 11 12 #include "mlir/Support/DebugAction.h" 13 #include "llvm/ADT/StringMap.h" 14 #include <string> 15 16 namespace mlir { 17 18 /// This class implements a debug action handler that attaches a counter value 19 /// to debug actions and enables/disables execution of these action based on the 20 /// value of the counter. The counter controls the execution of the action with 21 /// a "skip" and "count" value. The "skip" value is used to skip a certain 22 /// number of initial executions of a debug action. The "count" value is used to 23 /// prevent a debug action from executing after it has executed for a set number 24 /// of times (not including any executions that have been skipped). For example, 25 /// a counter for a debug action with `skip=47` and `count=2`, would skip the 26 /// first 47 executions, then execute twice, and finally prevent any further 27 /// executions. 28 class DebugCounter : public DebugActionManager::GenericHandler { 29 public: 30 DebugCounter(); 31 ~DebugCounter() override; 32 33 /// Add a counter for the given debug action tag. `countToSkip` is the number 34 /// of counter executions to skip before enabling execution of the action. 35 /// `countToStopAfter` is the number of executions of the counter to allow 36 /// before preventing the action from executing any more. 37 void addCounter(StringRef actionTag, int64_t countToSkip, 38 int64_t countToStopAfter); 39 40 /// Register a counter with the specified name. 41 FailureOr<bool> shouldExecute(StringRef tag, StringRef description) final; 42 43 /// Print the counters that have been registered with this instance to the 44 /// provided output stream. 45 void print(raw_ostream &os) const; 46 47 /// Register the command line options for debug counters. 48 static void registerCLOptions(); 49 50 private: 51 /// Apply the registered CL options to this debug counter instance. 52 void applyCLOptions(); 53 54 /// This struct represents a specific counter being tracked. 55 struct Counter { 56 Counter(int64_t countToSkip = 0, int64_t countToStopAfter = -1) countToSkipCounter57 : countToSkip(countToSkip), countToStopAfter(countToStopAfter) {} 58 59 /// The current count of this counter. 60 int64_t count{0}; 61 /// The number of initial executions of this counter to skip. 62 int64_t countToSkip; 63 /// The number of times to execute this counter before stopping. 64 int64_t countToStopAfter; 65 }; 66 67 /// A mapping between a given action tag and its counter information. 68 llvm::StringMap<Counter> counters; 69 }; 70 71 } // namespace mlir 72 73 #endif 74