[llvm] [InstCombine] Optimize AMDGPU ballot + assume uniformity patterns (PR #160670)

Pankaj Dwivedi via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 26 04:51:01 PDT 2025


================
@@ -3519,6 +3519,39 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
       }
     }
 
+    // Optimize AMDGPU ballot uniformity assumptions:
+    // assume(icmp eq (ballot(cmp), -1)) implies that cmp is uniform and true
+    // This allows us to optimize away the ballot and replace cmp with true
+    Value *BallotInst;
+    if (match(IIOperand, m_SpecificICmp(ICmpInst::ICMP_EQ, m_Value(BallotInst),
+                                        m_AllOnes()))) {
+      // Check if this is an AMDGPU ballot intrinsic
+      if (auto *BallotCall = dyn_cast<IntrinsicInst>(BallotInst)) {
+        if (BallotCall->getIntrinsicID() == Intrinsic::amdgcn_ballot) {
+          Value *BallotCondition = BallotCall->getArgOperand(0);
+
+          // If ballot(cmp) == -1, then cmp is uniform across all lanes and
+          // evaluates to true We can safely replace BallotCondition with true
+          // since ballot == -1 implies all lanes are true
+          if (BallotCondition->getType()->isIntOrIntVectorTy(1) &&
+              !isa<Constant>(BallotCondition)) {
----------------
PankajDwivedi-25 wrote:

The ballot here can not be assumed to be uniform; the right way would be to query the UI, and similar work is already been done in patch https://github.com/llvm/llvm-project/pull/116953.

The point here is that even UI reports it as divergent because it doesn't consider the presence of `@llvm.assume` later.

better we apply after considering the presence of `@llvm.assume`.

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


More information about the llvm-commits mailing list