[llvm] 1c308d6 - [LV] Clean up LoopVectorizationCostModel::calculateRegisterUsage. NFC
Bjorn Pettersson via llvm-commits
llvm-commits at lists.llvm.org
Sun Nov 20 11:52:28 PST 2022
Author: Bjorn Pettersson
Date: 2022-11-20T20:52:13+01:00
New Revision: 1c308d6641735b51d511342bd0353093254579f1
URL: https://github.com/llvm/llvm-project/commit/1c308d6641735b51d511342bd0353093254579f1
DIFF: https://github.com/llvm/llvm-project/commit/1c308d6641735b51d511342bd0353093254579f1.diff
LOG: [LV] Clean up LoopVectorizationCostModel::calculateRegisterUsage. NFC
Minor refactoring in LoopVectorizationCostModel::calculateRegisterUsage.
Also adding some FIXME:s related to what appears to be some short
comings related to how the register usage is calculated.
Differential Revision: https://reviews.llvm.org/D138342
Added:
Modified:
llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index ba10a4702f71b..82cee7d1cd1b1 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -5951,8 +5951,9 @@ LoopVectorizationCostModel::calculateRegisterUsage(ArrayRef<ElementCount> VFs) {
IntervalMap EndPoint;
// Saves the list of instruction indices that are used in the loop.
SmallPtrSet<Instruction *, 8> Ends;
- // Saves the list of values that are used in the loop but are
- // defined outside the loop, such as arguments and constants.
+ // Saves the list of values that are used in the loop but are defined outside
+ // the loop (not including non-instruction values such as arguments and
+ // constants).
SmallPtrSet<Value *, 8> LoopInvariants;
for (BasicBlock *BB : make_range(DFS.beginRPO(), DFS.endRPO())) {
@@ -5964,6 +5965,9 @@ LoopVectorizationCostModel::calculateRegisterUsage(ArrayRef<ElementCount> VFs) {
auto *Instr = dyn_cast<Instruction>(U);
// Ignore non-instruction values such as arguments, constants, etc.
+ // FIXME: Might need some motivation why these values are ignored. If
+ // for example an argument is used inside the loop it will increase the
+ // register pressure (so shouldn't we add it to LoopInvariants).
if (!Instr)
continue;
@@ -6019,14 +6023,19 @@ LoopVectorizationCostModel::calculateRegisterUsage(ArrayRef<ElementCount> VFs) {
// For each VF find the maximum usage of registers.
for (unsigned j = 0, e = VFs.size(); j < e; ++j) {
- // Count the number of live intervals.
+ // Count the number of registers used, per register class, given all open
+ // intervals.
+ // Note that elements in this SmallMapVector will be default constructed
+ // as 0. So we can use "RegUsage[ClassID] += n" in the code below even if
+ // there is no previous entry for ClassID.
SmallMapVector<unsigned, unsigned, 4> RegUsage;
if (VFs[j].isScalar()) {
for (auto *Inst : OpenIntervals) {
- unsigned ClassID = TTI.getRegisterClassForType(false, Inst->getType());
- // If RegUsage[ClassID] doesn't exist, it will be default
- // constructed as 0 before the addition
+ unsigned ClassID =
+ TTI.getRegisterClassForType(false, Inst->getType());
+ // FIXME: The target might use more than one register for the type
+ // even in the scalar case.
RegUsage[ClassID] += 1;
}
} else {
@@ -6036,14 +6045,14 @@ LoopVectorizationCostModel::calculateRegisterUsage(ArrayRef<ElementCount> VFs) {
if (VecValuesToIgnore.count(Inst))
continue;
if (isScalarAfterVectorization(Inst, VFs[j])) {
- unsigned ClassID = TTI.getRegisterClassForType(false, Inst->getType());
- // If RegUsage[ClassID] doesn't exist, it will be default
- // constructed as 0 before the addition
+ unsigned ClassID =
+ TTI.getRegisterClassForType(false, Inst->getType());
+ // FIXME: The target might use more than one register for the type
+ // even in the scalar case.
RegUsage[ClassID] += 1;
} else {
- unsigned ClassID = TTI.getRegisterClassForType(true, Inst->getType());
- // If RegUsage[ClassID] doesn't exist, it will be default
- // constructed as 0 before the addition
+ unsigned ClassID =
+ TTI.getRegisterClassForType(true, Inst->getType());
RegUsage[ClassID] += GetRegUsage(Inst->getType(), VFs[j]);
}
}
@@ -6063,17 +6072,19 @@ LoopVectorizationCostModel::calculateRegisterUsage(ArrayRef<ElementCount> VFs) {
}
for (unsigned i = 0, e = VFs.size(); i < e; ++i) {
+ // Note that elements in this SmallMapVector will be default constructed
+ // as 0. So we can use "Invariant[ClassID] += n" in the code below even if
+ // there is no previous entry for ClassID.
SmallMapVector<unsigned, unsigned, 4> Invariant;
for (auto *Inst : LoopInvariants) {
+ // FIXME: The target might use more than one register for the type
+ // even in the scalar case.
unsigned Usage =
VFs[i].isScalar() ? 1 : GetRegUsage(Inst->getType(), VFs[i]);
unsigned ClassID =
TTI.getRegisterClassForType(VFs[i].isVector(), Inst->getType());
- if (Invariant.find(ClassID) == Invariant.end())
- Invariant[ClassID] = Usage;
- else
- Invariant[ClassID] += Usage;
+ Invariant[ClassID] += Usage;
}
LLVM_DEBUG({
More information about the llvm-commits
mailing list