LLVM 24.0.0git
AMDGPUHWEvents.h
Go to the documentation of this file.
1//===- AMDGPUHWEvents.h -----------------------------------------*- 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 LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPUHWEVENTS_H
10#define LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPUHWEVENTS_H
11
12#include "llvm/ADT/bit.h"
13#include "llvm/ADT/iterator.h"
16#include <cassert>
17#include <cstdint>
18#include <iterator>
19
20namespace llvm {
21class GCNSubtarget;
22class MachineInstr;
23class raw_ostream;
24class SIInstrInfo;
25
26namespace AMDGPU {
27
28/// Bit mask of hardware events.
29///
30/// This is useful to manipulate events as hardware events rarely come alone.
31/// This class implements all the usual operators one would need to manipulate a
32/// bit mask, and also supports printing to a \ref raw_ostream and iterating
33/// over all the set bits of the event mask.
34///
35/// This class behaves like a constexpr set of flags. None of the methods should
36/// be able to mutate the data unless they are assignment operators. Examples:
37/// \verbatim
38/// A |= B; // Add flags (union).
39/// A -= B; // Remove flags (substraction).
40/// A &= B; // Intersection.
41/// A ^= B; // Bitwise XOR.
42/// (bool)A; // Check whether any bits are set; A.any() also works.
43/// !A; // Check if no bits are set; A.none() also works.
44/// A.size(); // Check how many bits are set.
45/// \endverbatim
46///
47/// This type also provides certain stronger guarantees than a simple integer:
48/// - Default constructor initializes the mask to zero.
49/// - Constructor ensures undefined bits cannot be set.
50class HWEvents {
51public:
53
54 enum : value_type {
55 NONE = 0,
56#define AMDGPU_HW_EVENT(X, V) X = (static_cast<value_type>(1) << V),
57#define AMDGPU_LAST_HW_EVENT(X) HWEVENT_LAST_EVENT = X,
58#include "AMDGPUHWEvents.def"
59
60 ALL = ((HWEVENT_LAST_EVENT << 1) - 1)
61 };
62
63 /// Iterates over the set bits of an HWEvent.
64 /// NOLINTNEXTLINE
66 : public iterator_facade_base<const_iterator, std::forward_iterator_tag,
67 HWEvents> {
68 // The "end" iterator is also the default-constructed iterator.
69 // We naturally move towards the "end" by clearing the set bits from least
70 // to most significant.
71 HWEvents::value_type Cur = 0;
72
73 public:
74 const_iterator() = default;
76
77 bool operator==(const const_iterator &Other) const {
78 return Cur == Other.Cur;
79 }
80
82 // Return only rightmost (least significant) bit set.
83 return Cur ? (Cur &
84 (static_cast<HWEvents::value_type>(1) << countr_zero(Cur)))
85 : 0;
86 }
87
89 // Keep all bits except the least significant bit set.
91 return *this;
92 }
93 };
94
95 constexpr HWEvents() = default;
96 constexpr HWEvents(value_type V) : Data(V) {
97 assert((V & ALL) == V && "Bits set out of bounds!");
98 }
99
100 constexpr unsigned size() const { return popcount(Data); }
101 constexpr bool any() const { return Data != 0; }
102 constexpr bool none() const { return Data == 0; }
103 constexpr value_type value() const { return Data; }
104
105 explicit constexpr operator bool() const { return any(); }
106
107 constexpr bool contains(HWEvents Other) const {
108 return (~Data & Other.Data) == 0;
109 }
110
111 const_iterator begin() const { return *this; }
112 const_iterator end() const { return {}; }
113
114 constexpr HWEvents operator|(HWEvents Other) const {
115 return Data | Other.Data;
116 }
117 constexpr HWEvents operator&(HWEvents Other) const {
118 return Data & Other.Data;
119 }
120 constexpr HWEvents operator^(HWEvents Other) const {
121 return Data ^ Other.Data;
122 }
124 constexpr HWEvents operator-(HWEvents Other) const {
125 return Data & ~Other.Data;
126 }
127
128 constexpr HWEvents operator~() const { return Data ^ ALL; }
129
130 constexpr bool operator==(HWEvents Other) const { return Data == Other.Data; }
131 constexpr bool operator!=(HWEvents Other) const { return Data != Other.Data; }
132
134 Data |= Other.Data;
135 return *this;
136 }
138 Data &= Other.Data;
139 return *this;
140 }
142 Data ^= Other.Data;
143 return *this;
144 }
145
147 Data &= ~Other.Data;
148 return *this;
149 }
150
151 /// Overload both bitwise AND operators w/ the value_type to avoid an implicit
152 /// conversion to HWEvent in this common pattern used to clear an event bit:
153 /// `Events & ~HWEvent::EVENT_TO_CLEAR`.
154 /// If we had the implicit conversion to HWEvent, we'd assert because
155 /// `~HWEvent::EVENT_TO_CLEAR` has bits set outside of `HWEvent::ALL`.
156 constexpr HWEvents operator&(value_type Other) const { return Data & Other; }
158 Data &= Other;
159 return *this;
160 }
161
162#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
163 LLVM_DUMP_METHOD void dump() const;
164#endif
165
166private:
168};
169
170/// \param Inst A VMEM instruction (as per `SIInstrInfo::isVMEM`).
171/// \returns the simplified set of events triggered by the VMEM instruction \p
172/// Inst. The returned mask is not exhaustive, but is guaranteed to be a subset
173/// of the mask that'd be returned by \ref getEventsFor.
174///
175/// Useful to quickly categorize VMEM instructions without having to fetch all
176/// events.
178 const SIInstrInfo &TII);
179
180/// \returns A bitmask of HWEvent triggered by \p Inst
181HWEvents getEventsFor(const MachineInstr &Inst, const GCNSubtarget &ST,
182 bool IsExpertMode, bool TgSplit);
183
184} // namespace AMDGPU
185
186raw_ostream &operator<<(raw_ostream &OS, AMDGPU::HWEvents E);
187} // namespace llvm
188
189#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:702
const HexagonInstrInfo * TII
#define H(x, y, z)
Definition MD5.cpp:56
AMDGPU::HWEvents HWEvents
This file implements the C++20 <bit> header.
Iterates over the set bits of an HWEvent.
bool operator==(const const_iterator &Other) const
constexpr bool operator==(HWEvents Other) const
constexpr HWEvents operator-=(HWEvents Other)
constexpr HWEvents()=default
constexpr HWEvents & operator&=(value_type Other)
constexpr unsigned size() const
const_iterator begin() const
constexpr HWEvents & operator&=(HWEvents Other)
LLVM_DUMP_METHOD void dump() const
constexpr value_type value() const
constexpr HWEvents operator-(HWEvents Other) const
constexpr HWEvents & operator^=(HWEvents Other)
constexpr bool operator!=(HWEvents Other) const
constexpr HWEvents & operator|=(HWEvents Other)
constexpr HWEvents operator|(HWEvents Other) const
const_iterator end() const
constexpr bool contains(HWEvents Other) const
constexpr bool any() const
constexpr bool none() const
constexpr HWEvents(value_type V)
constexpr HWEvents operator^(HWEvents Other) const
constexpr HWEvents operator&(value_type Other) const
Overload both bitwise AND operators w/ the value_type to avoid an implicit conversion to HWEvent in t...
constexpr HWEvents operator&(HWEvents Other) const
constexpr HWEvents operator~() const
Representation of each machine instruction.
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition iterator.h:80
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
HWEvents getSimplifiedVMEMEventsFor(const MachineInstr &Inst, const SIInstrInfo &TII)
HWEvents getEventsFor(const MachineInstr &Inst, const GCNSubtarget &ST, bool IsExpertMode, bool TgSplit)
This is an optimization pass for GlobalISel generic memory operations.
constexpr int popcount(T Value) noexcept
Count the number of set bits in a value.
Definition bit.h:156
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:204
constexpr T maskTrailingZeros(unsigned N)
Create a bitmask with the N right-most bits set to 0, and all other bits set to 1.
Definition MathExtras.h:95
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)