-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[LV][EVL] Introduce the EVLIndVarSimplify Pass for EVL-vectorized loops #131005
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
e96162f
9b41ae3
f21428d
8616f22
896009b
eeddd6d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| //===------ EVLIndVarSimplify.h - Optimize vectorized loops w/ EVL IV------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This pass optimizes a vectorized loop with canonical IV to using EVL-based | ||
| // IV if it was tail-folded by predicated EVL. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLVM_TRANSFORMS_VECTORIZE_EVLINDVARSIMPLIFY_H | ||
| #define LLVM_TRANSFORMS_VECTORIZE_EVLINDVARSIMPLIFY_H | ||
|
|
||
| #include "llvm/Analysis/LoopAnalysisManager.h" | ||
| #include "llvm/IR/PassManager.h" | ||
|
|
||
| namespace llvm { | ||
| class Loop; | ||
| class LPMUpdater; | ||
|
|
||
| /// Turn vectorized loops with canonical induction variables into loops that | ||
| /// only use a single EVL-based induction variable. | ||
| struct EVLIndVarSimplifyPass : public PassInfoMixin<EVLIndVarSimplifyPass> { | ||
| PreservedAnalyses run(Loop &L, LoopAnalysisManager &LAM, | ||
| LoopStandardAnalysisResults &AR, LPMUpdater &U); | ||
| }; | ||
| } // namespace llvm | ||
| #endif |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,277 @@ | ||||||
| //===---- EVLIndVarSimplify.cpp - Optimize vectorized loops w/ EVL IV------===// | ||||||
| // | ||||||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||||
| // See https://llvm.org/LICENSE.txt for license information. | ||||||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||||
| // | ||||||
| //===----------------------------------------------------------------------===// | ||||||
| // | ||||||
| // This pass optimizes a vectorized loop with canonical IV to using EVL-based | ||||||
| // IV if it was tail-folded by predicated EVL. | ||||||
| // | ||||||
| //===----------------------------------------------------------------------===// | ||||||
|
|
||||||
| #include "llvm/Transforms/Vectorize/EVLIndVarSimplify.h" | ||||||
| #include "llvm/ADT/Statistic.h" | ||||||
| #include "llvm/Analysis/IVDescriptors.h" | ||||||
| #include "llvm/Analysis/LoopInfo.h" | ||||||
| #include "llvm/Analysis/LoopPass.h" | ||||||
| #include "llvm/Analysis/OptimizationRemarkEmitter.h" | ||||||
| #include "llvm/Analysis/ScalarEvolution.h" | ||||||
| #include "llvm/Analysis/ScalarEvolutionExpressions.h" | ||||||
| #include "llvm/Analysis/ValueTracking.h" | ||||||
| #include "llvm/IR/IRBuilder.h" | ||||||
| #include "llvm/IR/PatternMatch.h" | ||||||
| #include "llvm/Support/CommandLine.h" | ||||||
| #include "llvm/Support/Debug.h" | ||||||
| #include "llvm/Support/MathExtras.h" | ||||||
| #include "llvm/Support/raw_ostream.h" | ||||||
| #include "llvm/Transforms/Scalar/LoopPassManager.h" | ||||||
| #include "llvm/Transforms/Utils/Local.h" | ||||||
| #include "llvm/Transforms/Utils/ScalarEvolutionExpander.h" | ||||||
|
|
||||||
| #define DEBUG_TYPE "evl-iv-simplify" | ||||||
|
|
||||||
| using namespace llvm; | ||||||
|
|
||||||
| STATISTIC(NumEliminatedCanonicalIV, "Number of canonical IVs we eliminated"); | ||||||
|
|
||||||
| static cl::opt<bool> EnableEVLIndVarSimplify( | ||||||
| "enable-evl-indvar-simplify", | ||||||
| cl::desc("Enable EVL-based induction variable simplify Pass"), cl::Hidden, | ||||||
| cl::init(true)); | ||||||
|
|
||||||
| namespace { | ||||||
| struct EVLIndVarSimplifyImpl { | ||||||
| ScalarEvolution &SE; | ||||||
| OptimizationRemarkEmitter *ORE = nullptr; | ||||||
|
|
||||||
| EVLIndVarSimplifyImpl(LoopStandardAnalysisResults &LAR, | ||||||
| OptimizationRemarkEmitter *ORE) | ||||||
| : SE(LAR.SE), ORE(ORE) {} | ||||||
|
|
||||||
| // Returns true if modify the loop. | ||||||
| bool run(Loop &L); | ||||||
| }; | ||||||
| } // anonymous namespace | ||||||
|
|
||||||
| /// Returns the constant part of vectorization factor from the induction | ||||||
| /// variable's step value SCEV expression. | ||||||
| static uint32_t getVFFromIndVar(const SCEV *Step, const Function &F) { | ||||||
| if (!Step) | ||||||
| return 0U; | ||||||
|
|
||||||
| // Looking for loops with IV step value in the form of `(<constant VF> x | ||||||
| // vscale)`. | ||||||
| if (auto *Mul = dyn_cast<SCEVMulExpr>(Step)) { | ||||||
|
||||||
| if (auto *Mul = dyn_cast<SCEVMulExpr>(Step)) { | |
| if (const auto *Mul = dyn_cast<SCEVMulExpr>(Step)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can it have number of operands != 2 at all?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is one of the first few checks against the candidate loops, I think it is possible to have a loop with step value consisting of consecutive multiplications, in which case SCEV would merge them into a single SCEVMulExpr with more than two operands.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| if (auto *Const = dyn_cast<SCEVConstant>(LHS)) { | |
| if (const auto *Const = dyn_cast<SCEVConstant>(LHS); Const && isa<SCEVVScale>(RHS)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| if (isa<SCEVVScale>(RHS) && llvm::isUInt<32>(V)) | |
| if (llvm::isUInt<32>(V)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| if (auto *ConstStep = dyn_cast<SCEVConstant>(Step)) { | |
| if (const auto *ConstStep = dyn_cast<SCEVConstant>(Step)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add remarks to other places, where the analysis fails too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's done now.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Expand auto here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add assertion message
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add assertion message
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed