[llvm-branch-commits] [llvm] [AMDGPU][SIInsertWaitcnts][NFC] Introduce Counter::merge() (PR #193369)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Apr 21 17:50:28 PDT 2026


https://github.com/vporpo created https://github.com/llvm/llvm-project/pull/193369

When moving to a new BB, the counters of the predecessors BBs get merged into a new counter.

This patch moves the counter merge logic into a new Counter::merge() member function.\n---\nPrevious PRs:\n\n0. https://github.com/llvm/llvm-project/pull/193368\n

>From a254c647f57144f573acff3356099d7806b02b96 Mon Sep 17 00:00:00 2001
From: Vasileios Porpodas <vasileios.porpodas at amd.com>
Date: Fri, 10 Apr 2026 21:10:28 +0000
Subject: [PATCH] [AMDGPU][SIInsertWaitcnts][NFC] Introduce Counter::merge()

When moving to a new BB, the counters of the predecessors BBs get merged into a new counter.

This patch moves the counter merge logic into a new Counter::merge() member function.
---
 llvm/lib/Target/AMDGPU/SIInsertWaitcnts.cpp | 26 ++++++++++-----------
 1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/llvm/lib/Target/AMDGPU/SIInsertWaitcnts.cpp b/llvm/lib/Target/AMDGPU/SIInsertWaitcnts.cpp
index 1ad4263c23608..e43181a76fc5c 100644
--- a/llvm/lib/Target/AMDGPU/SIInsertWaitcnts.cpp
+++ b/llvm/lib/Target/AMDGPU/SIInsertWaitcnts.cpp
@@ -786,8 +786,6 @@ class WaitcntBrackets {
       // TODO: Make private: we should not provide raw access to the internals.
       void setLB(unsigned NewLB) { LB = NewLB; }
       // TODO: Make private: we should not provide raw access to the internals.
-      void setUBNoLBClamp(unsigned NewUB) { UB = NewUB; }
-      // TODO: Make private: we should not provide raw access to the internals.
       void setUB(unsigned NewUB) {
         UB = NewUB;
         if (CntT == AMDGPU::EXP_CNT) {
@@ -799,7 +797,18 @@ class WaitcntBrackets {
       unsigned getUB() const { return UB; }
       // TODO: Make private: we should not provide raw access to the internals.
       unsigned getLB() const { return LB; }
-
+      /// Merge \p Other into this counter. This sets this counter to the
+      /// maximum counter value of this and \p Other.
+      /// \returns the pair of score shifts for this and \p Other.
+      std::pair<unsigned, unsigned> merge(const Counter &Other) {
+        unsigned NewUB = LB + std::max(getCount(), Other.getCount());
+        if (NewUB < LB)
+          report_fatal_error("waitcnt score overflow");
+        unsigned MyShift = NewUB - UB;
+        unsigned OtherShift = NewUB - Other.UB;
+        UB = NewUB;
+        return {MyShift, OtherShift};
+      }
       /// \returns true if the counter includes \p Score, i.e., it has
       /// contributed to its current value, or in other words it is pending.
       bool contains(unsigned Score) const { return LB < Score && Score <= UB; }
@@ -3156,20 +3165,11 @@ bool WaitcntBrackets::merge(const WaitcntBrackets &Other) {
     PendingEvents |= OtherEvents;
 
     // Merge scores for this counter
-    const unsigned MyPending = Counters[T].getCount();
-    const unsigned OtherPending = Other.Counters[T].getCount();
-    const unsigned NewUB =
-        Counters[T].getLB() + std::max(MyPending, OtherPending);
-    if (NewUB < Counters[T].getLB())
-      report_fatal_error("waitcnt score overflow");
-
     MergeInfo &M = MergeInfos[T];
     M.OldLB = Counters[T].getLB();
     M.OtherLB = Other.Counters[T].getLB();
-    M.MyShift = NewUB - Counters[T].getUB();
-    M.OtherShift = NewUB - Other.Counters[T].getUB();
 
-    Counters[T].setUBNoLBClamp(NewUB);
+    std::tie(M.MyShift, M.OtherShift) = Counters[T].merge(Other.Counters[T]);
 
     if (T == AMDGPU::LOAD_CNT)
       StrictDom |= mergeScore(M, LastFlatLoadCnt, Other.LastFlatLoadCnt);



More information about the llvm-branch-commits mailing list