[llvm] [LoopVectorizer] Prune VFs based on plan register pressure (PR #132190)
Sander de Smalen via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 11 00:55:53 PDT 2025
================
@@ -7792,6 +7582,29 @@ VectorizationFactor LoopVectorizationPlanner::computeBestVF() {
VectorizationFactor LegacyVF = selectVectorizationFactor();
VPlan &BestPlan = getPlanFor(BestFactor.Width);
+ // VPlan calculates register pressure from the plan, so it can come to
+ // different conclusions than the legacy cost model.
+ bool RegUsageDeterminedVF = false;
+ if (BestFactor.Width != LegacyVF.Width) {
+ SmallVector<ElementCount, 1> LegacyVFs = {LegacyVF.Width};
+ SmallVector<ElementCount, 1> VFs = {BestFactor.Width};
+
+ auto LegacyRUs =
+ calculateRegisterUsage(getPlanFor(LegacyVF.Width), LegacyVFs, TTI);
+ auto RUs = calculateRegisterUsage(BestPlan, VFs, TTI);
+
+ auto GetMaxUsage = [](SmallMapVector<unsigned, unsigned, 4> MaxLocalUsers) {
+ unsigned Max = 0;
+ for (auto Pair : MaxLocalUsers)
+ if (Pair.second > Max)
+ Max = Pair.second;
+ return Max;
+ };
+ unsigned MaxLegacyRegUsage = GetMaxUsage(LegacyRUs[0].MaxLocalUsers);
+ unsigned MaxRegUsage = GetMaxUsage(RUs[0].MaxLocalUsers);
+ RegUsageDeterminedVF = MaxRegUsage <= MaxLegacyRegUsage;
----------------
sdesmalen-arm wrote:
I had a closer look into this, and ignore my suggestion above. Instead, I think it is sufficient to check that the LegacyVF that would otherwise have been chosen has a register usage that exceeds the maximum number of registers. Otherwise, the cost would have chosen the same VF.
That means, this can be simplified to:
```suggestion
auto &MLU = LegacyRUs[0].MaxLocalUsers;
RegUsageDeterminedVF = any_of(MLU, [&](decltype(MLU.front()) &LU) {
return LU.second > TTI.getNumberOfRegisters(LU.first);
});
```
https://github.com/llvm/llvm-project/pull/132190
More information about the llvm-commits
mailing list