[llvm] [AMDGPU][NewInsertWaitcnt] Add Event Tracker (PR #226970)
Sameer Sahasrabuddhe via llvm-commits
llvm-commits at lists.llvm.org
Sun Oct 11 00:29:34 PDT 2026
================
@@ -0,0 +1,277 @@
+//===- AMDGPUEventTracking.h ------------------------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+/// \file Tracks a per-InstCounterType event timeline which preserves
+/// information about previously encountered MachineInstr and HWEvents.
+///
+/// The goal of this file is to implement a minimal API to preserve information
+/// about events that increment an instruction counter. Such events are
+/// contained in records called \ref EventTrackerRecord, which include:
+/// - The event kind, as a `HWEvent`
+/// - The instruction that triggered the event
+/// - The height of the record in the overall timeline of the counter, see
+/// \ref EventTrackerRecord for more details.
+///
+/// The "timeline" represents the state of an InstCounterType at a
+/// given point in time for a given MBB during a reverse postorder dataflow
+/// analysis of a function. Such a dataflow analysis is expected to pass over
+/// blocks as many times as necessary until a "fix point" is reached, which is a
+/// state in which the client (the pass that uses the APIs) decides it has
+/// enough information to safely perform its duties (generally, inserting
+/// `s_waitcnt` instructions).
+///
+/// See \ref EventTracker for more information as well.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUEVENTTRACKING_H
+#define LLVM_LIB_TARGET_AMDGPU_AMDGPUEVENTTRACKING_H
+
+#include "AMDGPUHWEvents.h"
+#include "AMDGPUWaitcntUtils.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/SmallVector.h"
+
+namespace llvm {
+class raw_ostream;
+class MachineBasicBlock;
+
+namespace AMDGPU {
+namespace eventtracking {
+
+class EventTracker;
+
+/// FIXME: Move this to a common file and make it a generic utility.
+///
+/// Represents target-specific information about available \ref InstCounterType
+/// and their specifities. This is handled by reference and is expected to
+/// outlive \ref EventTracker
+struct CounterInfo {
+ CounterInfo() = default;
+
+ constexpr CounterInfo(InstCounterType T, HWEvents Events, unsigned Limit)
+ : CounterT(T), Events(Events), Limit(Limit) {}
+
+ /// Type of counter this is.
+ /// This must match the index in the container, e.g. CounterT=2 must be at
+ /// index 2.
+ InstCounterType CounterT;
+ /// HWEvents for this counter.
+ HWEvents Events;
+ /// Hardware limit for this counter.
+ unsigned Limit;
+};
+
+/// Information about an event recorded within the \ref EventTracker's timeline
+/// for one \ref InstCounterType. An event (see \ref HWEvents) has an
+/// accompanying \ref MachineInstr, and exists at a fixed height in the
+/// timeline.
+///
+/// An event's height is a conservative minimum estimate of *when* the event
+/// occured. There are two examples that help illustrate this concept:
+///
+/// If we are iterating a single block where there are no records inherited from
+/// incoming basic blocks, then the height of an event is trivial: if there are
+/// N events, the oldest event is at N-1, while the youngest one is at 0.
+/// The height of each existing event increases whenever a new one is recorded.
+///
+/// However, in cases where one record is carried over into multiple successor
+/// blocks, and then merged later (in a diamond pattern for example), then the
+/// divergence (the same record could be at a different height depending on the
+/// CFG path taken) is reconciled by merging the records with the same
+/// \ref MachineInstr and \ref SingleHWEvent into a single record, and the
+/// height of the record is the minimum across the predecessors.
----------------
ssahasra wrote:
This part talks about how the height is maintained, but it doesn't explain much about what it is, or how it is used.
https://github.com/llvm/llvm-project/pull/226970
More information about the llvm-commits
mailing list