[llvm] [WIP][InstCombine] Add assume-based optimizations for equality and AMDGPU ballot patterns (PR #160670)

Sameer Sahasrabuddhe via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 8 21:50:58 PDT 2025


================
@@ -2206,6 +2207,23 @@ bool GVNPass::processAssumeIntrinsic(AssumeInst *IntrinsicI) {
           std::swap(CmpLHS, CmpRHS);
       }
 
+      // Optimize AMDGPU ballot pattern: assume(ballot(cmp) == -1) or
+      // assume(ballot(cmp) == exec_mask). This implies cmp is true on all
+      // active lanes and hence can be replaced with true.
+      if (isa<IntrinsicInst>(CmpLHS) && isa<Constant>(CmpRHS)) {
+        auto *IntrCall = cast<IntrinsicInst>(CmpLHS);
+        // Check if CmpLHS is a ballot intrinsic
+        if (IntrCall->getIntrinsicID() ==
+            Intrinsic::AMDGCNIntrinsics::amdgcn_ballot) {
+          Value *BallotArg = IntrCall->getArgOperand(0);
+          if (BallotArg->getType()->isIntegerTy(1) &&
+              (match(CmpRHS, m_AllOnes()) || !isa<Constant>(CmpRHS))) {
----------------
ssahasra wrote:

The second part here, `!isa<Constant>(CmpRHS)` ... is this meant to be execmask? What if it is simply a value that the programmer knows will match the current state of `cmp` across active threads? For example:

```
unsigned threadmask = foo(); // where a bit is 1 for whatever reason
if (divergent_condition) {
  cmp = bar();
  assume(ballot(cmp) == threadmask);
}
```


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


More information about the llvm-commits mailing list