Skip to content

Commit b34e885

Browse files
committed
Improve unrolling for user-requested loop unrolling via pragma directive
1 parent 7aed77e commit b34e885

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,8 @@ class TargetTransformInfo {
633633
/// Fall back to the generic logic to determine whether multi-exit unrolling
634634
/// is profitable if set to false.
635635
bool RuntimeUnrollMultiExit;
636+
// Relax conditions for unrolling when user requests unrolling via pragma.
637+
bool RelaxPragmaUnrollThresholds;
636638
};
637639

638640
/// Get target-customized preferences for the generic loop unrolling

llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ void AMDGPUTTIImpl::getUnrollingPreferences(
116116
UP.MaxCount = std::numeric_limits<unsigned>::max();
117117
UP.Partial = true;
118118

119+
// Relax conditions for unrolling when user requests unrolling via pragma.
120+
UP.RelaxPragmaUnrollThresholds = true;
121+
119122
// Conditional branch in a loop back edge needs 3 additional exec
120123
// manipulations in average.
121124
UP.BEInsns += 3;

llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ TargetTransformInfo::UnrollingPreferences llvm::gatherUnrollingPreferences(
221221
UP.MaxIterationsCountToAnalyze = UnrollMaxIterationsCountToAnalyze;
222222
UP.SCEVExpansionBudget = SCEVCheapExpansionBudget;
223223
UP.RuntimeUnrollMultiExit = false;
224+
UP.RelaxPragmaUnrollThresholds = false;
224225

225226
// Override with any target specific settings
226227
TTI.getUnrollingPreferences(L, SE, UP, &ORE);
@@ -939,6 +940,10 @@ bool llvm::computeUnrollCount(
939940

940941
const bool ExplicitUnroll = PragmaCount > 0 || PragmaFullUnroll ||
941942
PragmaEnableUnroll || UserUnrollCount;
943+
// If enabled, relax unrolling thresholds when pragma unroll is used.
944+
const bool RelaxUnrollThrehsholds = UP.RelaxPragmaUnrollThresholds &&
945+
(PragmaEnableUnroll && !UserUnrollCount &&
946+
!PragmaFullUnroll && PragmaCount == 0);
942947

943948
PragmaInfo PInfo(UserUnrollCount, PragmaFullUnroll, PragmaCount,
944949
PragmaEnableUnroll);
@@ -967,7 +972,7 @@ bool llvm::computeUnrollCount(
967972
UP.Runtime |= (PragmaCount > 0);
968973
return ExplicitUnroll;
969974
} else {
970-
if (ExplicitUnroll && TripCount != 0) {
975+
if (RelaxUnrollThrehsholds || (ExplicitUnroll && TripCount != 0)) {
971976
// If the loop has an unrolling pragma, we want to be more aggressive with
972977
// unrolling limits. Set thresholds to at least the PragmaUnrollThreshold
973978
// value which is larger than the default limits.
@@ -1077,7 +1082,8 @@ bool llvm::computeUnrollCount(
10771082
}
10781083

10791084
// Don't unroll a small upper bound loop unless user or TTI asked to do so.
1080-
if (MaxTripCount && !UP.Force && MaxTripCount < UP.MaxUpperBound) {
1085+
if (!RelaxUnrollThrehsholds && MaxTripCount && !UP.Force &&
1086+
MaxTripCount < UP.MaxUpperBound) {
10811087
UP.Count = 0;
10821088
return false;
10831089
}

0 commit comments

Comments
 (0)