Skip to content

Commit 675e3cb

Browse files
committed
[LoopUnroll] unroll small loops with 'pragma unroll' when its trip count is destroyed by preious optimization
1 parent 4936c4e commit 675e3cb

File tree

2 files changed

+793
-47
lines changed

2 files changed

+793
-47
lines changed

llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp

Lines changed: 7 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,11 @@ shouldPragmaUnroll(Loop *L, const PragmaInfo &PInfo,
776776
if (PInfo.PragmaFullUnroll && TripCount != 0)
777777
return TripCount;
778778

779+
// Small MaxTripCount is clearly calculated with "pragma unroll".
780+
if (PInfo.PragmaEnableUnroll && !TripCount && MaxTripCount &&
781+
MaxTripCount <= UnrollMaxUpperBound)
782+
return MaxTripCount;
783+
779784
// if didn't return until here, should continue to other priorties
780785
return std::nullopt;
781786
}
@@ -902,7 +907,7 @@ bool llvm::computeUnrollCount(
902907
// 1st priority is unroll count set by "unroll-count" option.
903908
// 2nd priority is unroll count set by pragma.
904909
if (auto UnrollFactor = shouldPragmaUnroll(L, PInfo, TripMultiple, TripCount,
905-
UCE, UP)) {
910+
MaxTripCount, UCE, UP)) {
906911
UP.Count = *UnrollFactor;
907912

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

0 commit comments

Comments
 (0)