Skip to content

Commit 9d908ec

Browse files
author
Florian Hahn
committed
Revert "[AArch64] Runtime-unroll small load/store loops for Apple Silicon CPUs. (llvm#118317)"
This reverts commit 0bb7bd4.
1 parent c1d69a9 commit 9d908ec

File tree

2 files changed

+7
-179
lines changed

2 files changed

+7
-179
lines changed

llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp

Lines changed: 3 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -4029,92 +4029,6 @@ getFalkorUnrollingPreferences(Loop *L, ScalarEvolution &SE,
40294029
}
40304030
}
40314031

4032-
/// For Apple CPUs, we want to runtime-unroll loops to make better use if the
4033-
/// OOO engine's wide instruction window and various predictors.
4034-
static void
4035-
getAppleRuntimeUnrollPreferences(Loop *L, ScalarEvolution &SE,
4036-
TargetTransformInfo::UnrollingPreferences &UP,
4037-
AArch64TTIImpl &TTI) {
4038-
// Limit loops with structure that is highly likely to benefit from runtime
4039-
// unrolling; that is we exclude outer loops, loops with multiple exits and
4040-
// many blocks (i.e. likely with complex control flow). Note that the
4041-
// heuristics here may be overly conservative and we err on the side of
4042-
// avoiding runtime unrolling rather than unroll excessively. They are all
4043-
// subject to further refinement.
4044-
if (!L->isInnermost() || !L->getExitBlock() || L->getNumBlocks() > 8)
4045-
return;
4046-
4047-
const SCEV *BTC = SE.getBackedgeTakenCount(L);
4048-
if (isa<SCEVConstant>(BTC) || isa<SCEVCouldNotCompute>(BTC) ||
4049-
(SE.getSmallConstantMaxTripCount(L) > 0 &&
4050-
SE.getSmallConstantMaxTripCount(L) <= 32))
4051-
return;
4052-
if (findStringMetadataForLoop(L, "llvm.loop.isvectorized"))
4053-
return;
4054-
4055-
int64_t Size = 0;
4056-
for (auto *BB : L->getBlocks()) {
4057-
for (auto &I : *BB) {
4058-
if (!isa<IntrinsicInst>(&I) && isa<CallBase>(&I))
4059-
return;
4060-
SmallVector<const Value *, 4> Operands(I.operand_values());
4061-
Size +=
4062-
*TTI.getInstructionCost(&I, Operands, TTI::TCK_CodeSize).getValue();
4063-
}
4064-
}
4065-
4066-
// Limit to loops with trip counts that are cheap to expand.
4067-
UP.SCEVExpansionBudget = 1;
4068-
4069-
// Try to unroll small, single block loops, if they have load/store
4070-
// dependencies, to expose more parallel memory access streams.
4071-
if (L->getHeader() != L->getLoopLatch() || Size > 8)
4072-
return;
4073-
4074-
SmallPtrSet<Value *, 8> LoadedValues;
4075-
SmallVector<StoreInst *> Stores;
4076-
for (auto *BB : L->blocks()) {
4077-
for (auto &I : *BB) {
4078-
Value *Ptr = getLoadStorePointerOperand(&I);
4079-
if (!Ptr)
4080-
continue;
4081-
const SCEV *PtrSCEV = SE.getSCEV(Ptr);
4082-
if (SE.isLoopInvariant(PtrSCEV, L))
4083-
continue;
4084-
if (isa<LoadInst>(&I))
4085-
LoadedValues.insert(&I);
4086-
else
4087-
Stores.push_back(cast<StoreInst>(&I));
4088-
}
4089-
}
4090-
4091-
// Try to find an unroll count that maximizes the use of the instruction
4092-
// window, i.e. trying to fetch as many instructions per cycle as possible.
4093-
unsigned MaxInstsPerLine = 16;
4094-
unsigned UC = 1;
4095-
unsigned BestUC = 1;
4096-
unsigned SizeWithBestUC = BestUC * Size;
4097-
while (UC <= 8) {
4098-
unsigned SizeWithUC = UC * Size;
4099-
if (SizeWithUC > 48)
4100-
break;
4101-
if ((SizeWithUC % MaxInstsPerLine) == 0 ||
4102-
(SizeWithBestUC % MaxInstsPerLine) < (SizeWithUC % MaxInstsPerLine)) {
4103-
BestUC = UC;
4104-
SizeWithBestUC = BestUC * Size;
4105-
}
4106-
UC++;
4107-
}
4108-
4109-
if (BestUC == 1 || none_of(Stores, [&LoadedValues](StoreInst *SI) {
4110-
return LoadedValues.contains(SI->getOperand(0));
4111-
}))
4112-
return;
4113-
4114-
UP.Runtime = true;
4115-
UP.DefaultUnrollRuntimeCount = BestUC;
4116-
}
4117-
41184032
void AArch64TTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE,
41194033
TTI::UnrollingPreferences &UP,
41204034
OptimizationRemarkEmitter *ORE) {
@@ -4132,21 +4046,9 @@ void AArch64TTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE,
41324046
// Disable partial & runtime unrolling on -Os.
41334047
UP.PartialOptSizeThreshold = 0;
41344048

4135-
// Apply subtarget-specific unrolling preferences.
4136-
switch (ST->getProcFamily()) {
4137-
case AArch64Subtarget::AppleA14:
4138-
case AArch64Subtarget::AppleA15:
4139-
case AArch64Subtarget::AppleA16:
4140-
case AArch64Subtarget::AppleM4:
4141-
getAppleRuntimeUnrollPreferences(L, SE, UP, *this);
4142-
break;
4143-
case AArch64Subtarget::Falkor:
4144-
if (EnableFalkorHWPFUnrollFix)
4145-
getFalkorUnrollingPreferences(L, SE, UP);
4146-
break;
4147-
default:
4148-
break;
4149-
}
4049+
if (ST->getProcFamily() == AArch64Subtarget::Falkor &&
4050+
EnableFalkorHWPFUnrollFix)
4051+
getFalkorUnrollingPreferences(L, SE, UP);
41504052

41514053
// Scan the loop: don't unroll loops with calls as this could prevent
41524054
// inlining. Don't unroll vector loops either, as they don't benefit much from

llvm/test/Transforms/LoopUnroll/AArch64/apple-unrolling.ll

Lines changed: 4 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -12,91 +12,17 @@ define void @small_load_store_loop(ptr %src, ptr %dst, i64 %N, i64 %scale) {
1212
; APPLE-LABEL: define void @small_load_store_loop(
1313
; APPLE-SAME: ptr [[SRC:%.*]], ptr [[DST:%.*]], i64 [[N:%.*]], i64 [[SCALE:%.*]]) #[[ATTR0:[0-9]+]] {
1414
; APPLE-NEXT: [[ENTRY:.*]]:
15-
; APPLE-NEXT: [[TMP0:%.*]] = add i64 [[N]], -1
16-
; APPLE-NEXT: [[XTRAITER:%.*]] = and i64 [[N]], 7
17-
; APPLE-NEXT: [[TMP1:%.*]] = icmp ult i64 [[TMP0]], 7
18-
; APPLE-NEXT: br i1 [[TMP1]], label %[[EXIT_UNR_LCSSA:.*]], label %[[ENTRY_NEW:.*]]
19-
; APPLE: [[ENTRY_NEW]]:
20-
; APPLE-NEXT: [[UNROLL_ITER:%.*]] = sub i64 [[N]], [[XTRAITER]]
2115
; APPLE-NEXT: br label %[[LOOP:.*]]
2216
; APPLE: [[LOOP]]:
23-
; APPLE-NEXT: [[IV_EPIL:%.*]] = phi i64 [ 0, %[[ENTRY_NEW]] ], [ [[IV_NEXT_7:%.*]], %[[LOOP]] ]
24-
; APPLE-NEXT: [[NITER:%.*]] = phi i64 [ 0, %[[ENTRY_NEW]] ], [ [[NITER_NEXT_7:%.*]], %[[LOOP]] ]
17+
; APPLE-NEXT: [[IV_EPIL:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[IV_NEXT_EPIL:%.*]], %[[LOOP]] ]
2518
; APPLE-NEXT: [[SCALED_IV_EPIL:%.*]] = mul nuw nsw i64 [[IV_EPIL]], [[SCALE]]
2619
; APPLE-NEXT: [[GEP_SRC_EPIL:%.*]] = getelementptr inbounds float, ptr [[SRC]], i64 [[SCALED_IV_EPIL]]
2720
; APPLE-NEXT: [[L_EPIL:%.*]] = load float, ptr [[GEP_SRC_EPIL]], align 4
2821
; APPLE-NEXT: [[GEP_DST_EPIL:%.*]] = getelementptr inbounds float, ptr [[DST]], i64 [[IV_EPIL]]
2922
; APPLE-NEXT: store float [[L_EPIL]], ptr [[GEP_DST_EPIL]], align 4
30-
; APPLE-NEXT: [[IV_NEXT_EPIL:%.*]] = add nuw nsw i64 [[IV_EPIL]], 1
31-
; APPLE-NEXT: [[SCALED_IV_1:%.*]] = mul nuw nsw i64 [[IV_NEXT_EPIL]], [[SCALE]]
32-
; APPLE-NEXT: [[GEP_SRC_1:%.*]] = getelementptr inbounds float, ptr [[SRC]], i64 [[SCALED_IV_1]]
33-
; APPLE-NEXT: [[L_1:%.*]] = load float, ptr [[GEP_SRC_1]], align 4
34-
; APPLE-NEXT: [[GEP_DST_1:%.*]] = getelementptr inbounds float, ptr [[DST]], i64 [[IV_NEXT_EPIL]]
35-
; APPLE-NEXT: store float [[L_1]], ptr [[GEP_DST_1]], align 4
36-
; APPLE-NEXT: [[IV_NEXT_1:%.*]] = add nuw nsw i64 [[IV_EPIL]], 2
37-
; APPLE-NEXT: [[SCALED_IV_2:%.*]] = mul nuw nsw i64 [[IV_NEXT_1]], [[SCALE]]
38-
; APPLE-NEXT: [[GEP_SRC_2:%.*]] = getelementptr inbounds float, ptr [[SRC]], i64 [[SCALED_IV_2]]
39-
; APPLE-NEXT: [[L_2:%.*]] = load float, ptr [[GEP_SRC_2]], align 4
40-
; APPLE-NEXT: [[GEP_DST_2:%.*]] = getelementptr inbounds float, ptr [[DST]], i64 [[IV_NEXT_1]]
41-
; APPLE-NEXT: store float [[L_2]], ptr [[GEP_DST_2]], align 4
42-
; APPLE-NEXT: [[IV_NEXT_2:%.*]] = add nuw nsw i64 [[IV_EPIL]], 3
43-
; APPLE-NEXT: [[SCALED_IV_3:%.*]] = mul nuw nsw i64 [[IV_NEXT_2]], [[SCALE]]
44-
; APPLE-NEXT: [[GEP_SRC_3:%.*]] = getelementptr inbounds float, ptr [[SRC]], i64 [[SCALED_IV_3]]
45-
; APPLE-NEXT: [[L_3:%.*]] = load float, ptr [[GEP_SRC_3]], align 4
46-
; APPLE-NEXT: [[GEP_DST_3:%.*]] = getelementptr inbounds float, ptr [[DST]], i64 [[IV_NEXT_2]]
47-
; APPLE-NEXT: store float [[L_3]], ptr [[GEP_DST_3]], align 4
48-
; APPLE-NEXT: [[IV_NEXT_3:%.*]] = add nuw nsw i64 [[IV_EPIL]], 4
49-
; APPLE-NEXT: [[SCALED_IV_4:%.*]] = mul nuw nsw i64 [[IV_NEXT_3]], [[SCALE]]
50-
; APPLE-NEXT: [[GEP_SRC_4:%.*]] = getelementptr inbounds float, ptr [[SRC]], i64 [[SCALED_IV_4]]
51-
; APPLE-NEXT: [[L_4:%.*]] = load float, ptr [[GEP_SRC_4]], align 4
52-
; APPLE-NEXT: [[GEP_DST_4:%.*]] = getelementptr inbounds float, ptr [[DST]], i64 [[IV_NEXT_3]]
53-
; APPLE-NEXT: store float [[L_4]], ptr [[GEP_DST_4]], align 4
54-
; APPLE-NEXT: [[IV_NEXT_4:%.*]] = add nuw nsw i64 [[IV_EPIL]], 5
55-
; APPLE-NEXT: [[SCALED_IV_5:%.*]] = mul nuw nsw i64 [[IV_NEXT_4]], [[SCALE]]
56-
; APPLE-NEXT: [[GEP_SRC_5:%.*]] = getelementptr inbounds float, ptr [[SRC]], i64 [[SCALED_IV_5]]
57-
; APPLE-NEXT: [[L_5:%.*]] = load float, ptr [[GEP_SRC_5]], align 4
58-
; APPLE-NEXT: [[GEP_DST_5:%.*]] = getelementptr inbounds float, ptr [[DST]], i64 [[IV_NEXT_4]]
59-
; APPLE-NEXT: store float [[L_5]], ptr [[GEP_DST_5]], align 4
60-
; APPLE-NEXT: [[IV_NEXT_5:%.*]] = add nuw nsw i64 [[IV_EPIL]], 6
61-
; APPLE-NEXT: [[SCALED_IV_6:%.*]] = mul nuw nsw i64 [[IV_NEXT_5]], [[SCALE]]
62-
; APPLE-NEXT: [[GEP_SRC_6:%.*]] = getelementptr inbounds float, ptr [[SRC]], i64 [[SCALED_IV_6]]
63-
; APPLE-NEXT: [[L_6:%.*]] = load float, ptr [[GEP_SRC_6]], align 4
64-
; APPLE-NEXT: [[GEP_DST_6:%.*]] = getelementptr inbounds float, ptr [[DST]], i64 [[IV_NEXT_5]]
65-
; APPLE-NEXT: store float [[L_6]], ptr [[GEP_DST_6]], align 4
66-
; APPLE-NEXT: [[IV_NEXT_6:%.*]] = add nuw nsw i64 [[IV_EPIL]], 7
67-
; APPLE-NEXT: [[SCALED_IV_7:%.*]] = mul nuw nsw i64 [[IV_NEXT_6]], [[SCALE]]
68-
; APPLE-NEXT: [[GEP_SRC_7:%.*]] = getelementptr inbounds float, ptr [[SRC]], i64 [[SCALED_IV_7]]
69-
; APPLE-NEXT: [[L_7:%.*]] = load float, ptr [[GEP_SRC_7]], align 4
70-
; APPLE-NEXT: [[GEP_DST_7:%.*]] = getelementptr inbounds float, ptr [[DST]], i64 [[IV_NEXT_6]]
71-
; APPLE-NEXT: store float [[L_7]], ptr [[GEP_DST_7]], align 4
72-
; APPLE-NEXT: [[IV_NEXT_7]] = add nuw nsw i64 [[IV_EPIL]], 8
73-
; APPLE-NEXT: [[NITER_NEXT_7]] = add i64 [[NITER]], 8
74-
; APPLE-NEXT: [[NITER_NCMP_7:%.*]] = icmp eq i64 [[NITER_NEXT_7]], [[UNROLL_ITER]]
75-
; APPLE-NEXT: br i1 [[NITER_NCMP_7]], label %[[EXIT_UNR_LCSSA_LOOPEXIT:.*]], label %[[LOOP]]
76-
; APPLE: [[EXIT_UNR_LCSSA_LOOPEXIT]]:
77-
; APPLE-NEXT: [[IV_UNR_PH:%.*]] = phi i64 [ [[IV_NEXT_7]], %[[LOOP]] ]
78-
; APPLE-NEXT: br label %[[EXIT_UNR_LCSSA]]
79-
; APPLE: [[EXIT_UNR_LCSSA]]:
80-
; APPLE-NEXT: [[IV_UNR:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[IV_UNR_PH]], %[[EXIT_UNR_LCSSA_LOOPEXIT]] ]
81-
; APPLE-NEXT: [[LCMP_MOD:%.*]] = icmp ne i64 [[XTRAITER]], 0
82-
; APPLE-NEXT: br i1 [[LCMP_MOD]], label %[[LOOP_EPIL_PREHEADER:.*]], label %[[EXIT:.*]]
83-
; APPLE: [[LOOP_EPIL_PREHEADER]]:
84-
; APPLE-NEXT: br label %[[LOOP_EPIL:.*]]
85-
; APPLE: [[LOOP_EPIL]]:
86-
; APPLE-NEXT: [[IV_EPIL1:%.*]] = phi i64 [ [[IV_UNR]], %[[LOOP_EPIL_PREHEADER]] ], [ [[IV_NEXT_EPIL1:%.*]], %[[LOOP_EPIL]] ]
87-
; APPLE-NEXT: [[EPIL_ITER:%.*]] = phi i64 [ 0, %[[LOOP_EPIL_PREHEADER]] ], [ [[EPIL_ITER_NEXT:%.*]], %[[LOOP_EPIL]] ]
88-
; APPLE-NEXT: [[SCALED_IV_EPIL1:%.*]] = mul nuw nsw i64 [[IV_EPIL1]], [[SCALE]]
89-
; APPLE-NEXT: [[GEP_SRC_EPIL1:%.*]] = getelementptr inbounds float, ptr [[SRC]], i64 [[SCALED_IV_EPIL1]]
90-
; APPLE-NEXT: [[L_EPIL1:%.*]] = load float, ptr [[GEP_SRC_EPIL1]], align 4
91-
; APPLE-NEXT: [[GEP_DST_EPIL1:%.*]] = getelementptr inbounds float, ptr [[DST]], i64 [[IV_EPIL1]]
92-
; APPLE-NEXT: store float [[L_EPIL1]], ptr [[GEP_DST_EPIL1]], align 4
93-
; APPLE-NEXT: [[IV_NEXT_EPIL1]] = add nuw nsw i64 [[IV_EPIL1]], 1
94-
; APPLE-NEXT: [[EC_EPIL:%.*]] = icmp eq i64 [[IV_NEXT_EPIL1]], [[N]]
95-
; APPLE-NEXT: [[EPIL_ITER_NEXT]] = add i64 [[EPIL_ITER]], 1
96-
; APPLE-NEXT: [[EPIL_ITER_CMP:%.*]] = icmp ne i64 [[EPIL_ITER_NEXT]], [[XTRAITER]]
97-
; APPLE-NEXT: br i1 [[EPIL_ITER_CMP]], label %[[LOOP_EPIL]], label %[[EXIT_EPILOG_LCSSA:.*]], !llvm.loop [[LOOP0:![0-9]+]]
98-
; APPLE: [[EXIT_EPILOG_LCSSA]]:
99-
; APPLE-NEXT: br label %[[EXIT]]
23+
; APPLE-NEXT: [[IV_NEXT_EPIL]] = add nuw nsw i64 [[IV_EPIL]], 1
24+
; APPLE-NEXT: [[EC_EPIL:%.*]] = icmp eq i64 [[IV_NEXT_EPIL]], [[N]]
25+
; APPLE-NEXT: br i1 [[EC_EPIL]], label %[[EXIT:.*]], label %[[LOOP]]
10026
; APPLE: [[EXIT]]:
10127
; APPLE-NEXT: ret void
10228
;

0 commit comments

Comments
 (0)