[llvm] [AMDGPU][SIInsertWaitcnts][NFC] Introduce WaitEventSet container for events (PR #178511)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 29 09:22:40 PST 2026


================
@@ -321,10 +340,88 @@ unsigned getWait(AMDGPU::Waitcnt &Wait, InstCounterType T) {
   return getCounterRef(Wait, T);
 }
 
+/// A small set of events.
+class WaitEventSet {
+  unsigned Mask = 0;
+
+public:
+  WaitEventSet() = default;
+  explicit constexpr WaitEventSet(WaitEventType Event) {
+    assert((size_t)Event < sizeof(Mask) * 8 && "Not enough bits in mask!");
+    Mask |= 1 << Event;
+  }
+  constexpr WaitEventSet(std::initializer_list<WaitEventType> Events) {
+    for (auto &E : Events) {
+      assert((size_t)E < sizeof(Mask) * 8 && "Not enough bits in mask!");
+      Mask |= 1 << E;
+    }
+  }
+  void insert(const WaitEventType &Event) {
+    assert((size_t)Event < sizeof(Mask) * 8 && "Not enough bits in mask!");
+    Mask |= 1 << Event;
+  }
+  void remove(const WaitEventType &Event) {
+    assert((size_t)Event < sizeof(Mask) * 8 && "Not enough bits in mask!");
+    Mask &= ~(1 << Event);
+  }
+  bool contains(const WaitEventType &Event) const {
+    assert((size_t)Event < sizeof(Mask) * 8 && "Not enough bits in mask!");
+    return Mask & (1 << Event);
+  }
+  /// \Returns the intersection of this and \p Other.
+  WaitEventSet operator&(const WaitEventSet &Other) const {
+    auto Copy = *this;
+    Copy.Mask &= Other.Mask;
+    return Copy;
+  }
+  /// \Returns the union of this and \p Other.
+  WaitEventSet operator|(const WaitEventSet &Other) const {
+    auto Copy = *this;
+    Copy.Mask |= Other.Mask;
+    return Copy;
+  }
+  /// \Returns the inverse of this set.
+  WaitEventSet operator~() const {
+    auto Copy = *this;
+    Copy.Mask = ~Copy.Mask;
+    return Copy;
+  }
+  /// This set becomes the union of this and \p Other.
----------------
vporpo wrote:

I was worried about that too, though the inverse of a set is an expected operator for a set class.

Anyway, I added a `remove(const WaitEventSet &)` just as you suggested.

There was one more place where operator~ was used: `if (OtherEvents & ~OldEvents)`, which should be equivalent to checking if OtherEvents contains OldEvents, so I added a `bool contains(const WaitEventSet &)`,

https://github.com/llvm/llvm-project/pull/178511


More information about the llvm-commits mailing list