diff options
author | Mel Chen <mel.chen@sifive.com> | 2024-12-12 16:48:31 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-12-12 16:48:31 +0800 |
commit | b3cba9be41bfa89bc0ec212706c6028a901e127a (patch) | |
tree | 0f07695ed68dc3ba03afee45b9fd7e7723f80632 /llvm/lib/Transforms/Utils/LoopUtils.cpp | |
parent | 0876c11ceeb093904decc4d89bef213d483a5656 (diff) | |
download | llvm-b3cba9be41bfa89bc0ec212706c6028a901e127a.zip llvm-b3cba9be41bfa89bc0ec212706c6028a901e127a.tar.gz llvm-b3cba9be41bfa89bc0ec212706c6028a901e127a.tar.bz2 |
[LoopVectorize] Vectorize select-cmp reduction pattern for increasing integer induction variable (#67812)
Consider the following loop:
```
int rdx = init;
for (int i = 0; i < n; ++i)
rdx = (a[i] > b[i]) ? i : rdx;
```
We can vectorize this loop if `i` is an increasing induction variable.
The final reduced value will be the maximum of `i` that the condition
`a[i] > b[i]` is satisfied, or the start value `init`.
This patch added new RecurKind enums - IFindLastIV and FFindLastIV.
---------
Co-authored-by: Alexey Bataev <5361294+alexey-bataev@users.noreply.github.com>
Diffstat (limited to 'llvm/lib/Transforms/Utils/LoopUtils.cpp')
-rw-r--r-- | llvm/lib/Transforms/Utils/LoopUtils.cpp | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/llvm/lib/Transforms/Utils/LoopUtils.cpp b/llvm/lib/Transforms/Utils/LoopUtils.cpp index 7004727..45915c10 100644 --- a/llvm/lib/Transforms/Utils/LoopUtils.cpp +++ b/llvm/lib/Transforms/Utils/LoopUtils.cpp @@ -1208,6 +1208,23 @@ Value *llvm::createAnyOfReduction(IRBuilderBase &Builder, Value *Src, return Builder.CreateSelect(AnyOf, NewVal, InitVal, "rdx.select"); } +Value *llvm::createFindLastIVReduction(IRBuilderBase &Builder, Value *Src, + const RecurrenceDescriptor &Desc) { + assert(RecurrenceDescriptor::isFindLastIVRecurrenceKind( + Desc.getRecurrenceKind()) && + "Unexpected reduction kind"); + Value *StartVal = Desc.getRecurrenceStartValue(); + Value *Sentinel = Desc.getSentinelValue(); + Value *MaxRdx = Src->getType()->isVectorTy() + ? Builder.CreateIntMaxReduce(Src, true) + : Src; + // Correct the final reduction result back to the start value if the maximum + // reduction is sentinel value. + Value *Cmp = + Builder.CreateCmp(CmpInst::ICMP_NE, MaxRdx, Sentinel, "rdx.select.cmp"); + return Builder.CreateSelect(Cmp, MaxRdx, StartVal, "rdx.select"); +} + Value *llvm::getReductionIdentity(Intrinsic::ID RdxID, Type *Ty, FastMathFlags Flags) { bool Negative = false; @@ -1315,6 +1332,8 @@ Value *llvm::createReduction(IRBuilderBase &B, RecurKind RK = Desc.getRecurrenceKind(); if (RecurrenceDescriptor::isAnyOfRecurrenceKind(RK)) return createAnyOfReduction(B, Src, Desc, OrigPhi); + if (RecurrenceDescriptor::isFindLastIVRecurrenceKind(RK)) + return createFindLastIVReduction(B, Src, Desc); return createSimpleReduction(B, Src, RK); } |