[llvm] [WIP][InstCombine] Add assume-based optimizations for equality and AMDGPU ballot patterns (PR #160670)
Pankaj Dwivedi via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 15 02:19:01 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))) {
----------------
PankajDwivedi-25 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.
https://github.com/llvm/llvm-project/pull/160670
More information about the llvm-commits
mailing list