[llvm] [WIP][InstCombine] Add assume-based optimizations for equality and AMDGPU ballot patterns (PR #160670)
Teja Alaghari via llvm-commits
llvm-commits at lists.llvm.org
Sun Nov 2 22:47:25 PST 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))) {
----------------
TejaX-Alaghari wrote:
> > The second part here, `!isa<Constant>(CmpRHS)` ... is this meant to be execmask?
>
> I think yes, the point is we can not always predict what cmp will be for all threads. It should some trivial patterns like `assume(ballot(cmp) == threadmask);` `assume(ballot(cmp) == -1);` `assume(ballot(cmp) == 0);` etc. and handle them seperately.
>
> In the case of a thread mask we have to make sure ballot is executed by same threads for threadmask is computed.
Improved the code by adding the logic to identify an `execmask` (not comprehensive - only the most common pattern of `ballot(true)`).
Also, added more test cases to make the comparison logic more robust.
https://github.com/llvm/llvm-project/pull/160670
More information about the llvm-commits
mailing list