Skip to content

Commit 1d6a678

Browse files
authored
[LoopUnroll] Make use of MaxTripCount for loops with "#pragma unroll" (#74703)
Fix loop unroll fail caused by branches folding. For example: SimplifyCFG foldloop branches then cause loop unroll failed for "#program unroll" loop. ``` #program unroll for (int I = 0; I < ConstNum; ++I) { // folding "I < ConstNum" and "Cond2" if (Cond2) { break; } xxx loop body; } ``` The pragma unroll metadata only takes effect if there is an exact trip count, but not if there is an upper bound trip count. This patch make it work with an upper bound trip count as well in shouldPragmaUnroll(). Loop unroll is important in stack nervous devices (e.g. GPU, and that is why a lot of GPU code mark loop with "#program unroll"). It usually much simplify the address (offset) calculations in old iterations, then we can do a lot of others optimizations, e.g, SROA, for these simplifed address (escape alloca the whole aggregates).
1 parent 5f91335 commit 1d6a678

File tree

2 files changed

+942
-2
lines changed

2 files changed

+942
-2
lines changed

llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ static unsigned getFullUnrollBoostingFactor(const EstimatedUnrollCost &Cost,
755755
static std::optional<unsigned>
756756
shouldPragmaUnroll(Loop *L, const PragmaInfo &PInfo,
757757
const unsigned TripMultiple, const unsigned TripCount,
758-
const UnrollCostEstimator UCE,
758+
unsigned MaxTripCount, const UnrollCostEstimator UCE,
759759
const TargetTransformInfo::UnrollingPreferences &UP) {
760760

761761
// Using unroll pragma
@@ -776,6 +776,10 @@ shouldPragmaUnroll(Loop *L, const PragmaInfo &PInfo,
776776
if (PInfo.PragmaFullUnroll && TripCount != 0)
777777
return TripCount;
778778

779+
if (PInfo.PragmaEnableUnroll && !TripCount && MaxTripCount &&
780+
MaxTripCount <= UnrollMaxUpperBound)
781+
return MaxTripCount;
782+
779783
// if didn't return until here, should continue to other priorties
780784
return std::nullopt;
781785
}
@@ -902,7 +906,7 @@ bool llvm::computeUnrollCount(
902906
// 1st priority is unroll count set by "unroll-count" option.
903907
// 2nd priority is unroll count set by pragma.
904908
if (auto UnrollFactor = shouldPragmaUnroll(L, PInfo, TripMultiple, TripCount,
905-
UCE, UP)) {
909+
MaxTripCount, UCE, UP)) {
906910
UP.Count = *UnrollFactor;
907911

908912
if (UserUnrollCount || (PragmaCount > 0)) {

0 commit comments

Comments
 (0)