Skip to content

Commit f42fcfa

Browse files
committed
Rethink fix: Don't convert 0 to 1.
1 parent e2cc9dc commit f42fcfa

File tree

13 files changed

+197
-187
lines changed

13 files changed

+197
-187
lines changed

llvm/docs/LangRef.rst

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7989,9 +7989,9 @@ this metadata is added (i.e., has been distributed). See
79897989
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
79907990

79917991
This metadata records an estimated trip count for the loop. The first operand
7992-
is the string ``llvm.loop.estimated_trip_count``. The second operand is a
7993-
positive integer constant of type ``i32`` or smaller specifying the estimate.
7994-
For example:
7992+
is the string ``llvm.loop.estimated_trip_count``. The second operand is an
7993+
integer constant of type ``i32`` or smaller specifying the estimate. For
7994+
example:
79957995

79967996
.. code-block:: llvm
79977997

@@ -8033,12 +8033,20 @@ pass should record the new estimates by calling
80338033
loop, ``llvm::getLoopEstimatedTripCount`` returns its value instead of
80348034
estimating the trip count from the loop's ``branch_weights`` metadata.
80358035

8036-
Especially after a transformation like loop peeling, the probability of reaching
8037-
a loop's header might be very low. Regardless, in the case that it is reached,
8038-
at least one iteration will execute, so an estimated trip count of zero is
8039-
invalid. Some passes thus rely on non-zero estimated trip counts.
8040-
Nevertheless, some passes naively compute it as zero. To avoid misbehavior,
8041-
``llvm::setLoopEstimatedTripCount`` converts zero to one.
8036+
Zero
8037+
""""
8038+
8039+
Some passes set ``llvm.loop.estimated_trip_count`` to 0. For example, after
8040+
peeling 10 or more iterations from a loop with an estimated trip count of 10,
8041+
``llvm.loop.estimated_trip_count`` becomes 0 on the remaining loop. It
8042+
indicates that, each time execution reaches the peeled iterations, execution is
8043+
estimated to exit them without reaching the remaining loop's header.
8044+
8045+
Even if the probability of reaching a loop's header is low, if it is reached, it
8046+
is the start of an iteration. Consequently, some passes historically assume
8047+
that ``llvm::getLoopEstimatedTripCount`` always returns a positive count or
8048+
``std::nullopt``. Thus, it returns ``std::nullopt`` when
8049+
``llvm.loop.estimated_trip_count`` is 0.
80428050

80438051
'``llvm.licm.disable``' Metadata
80448052
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

llvm/include/llvm/Transforms/Utils/LoopUtils.h

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -322,21 +322,14 @@ LLVM_ABI TransformationMode hasLICMVersioningTransformation(const Loop *L);
322322
LLVM_ABI void addStringMetadataToLoop(Loop *TheLoop, const char *MDString,
323323
unsigned V = 0);
324324

325-
/// Set \p StringMD in the loop metadata of \p TheLoop while keeping other
326-
/// values intact:
327-
/// - If \p V is \c std::nullopt, remove \p StringMD, or do nothing if
328-
/// \p StringMD is not present, perhaps because there is no loop metadata.
329-
/// - Else, set \p StringMD to \p V. Either add \p StringMD if absent, update
330-
/// it if the current value is different, or do nothing if the current value
331-
/// is the same.
332-
LLVM_ABI void setLoopStringMetadata(Loop *TheLoop, const char *StringMD,
333-
std::optional<unsigned> V);
334-
335325
/// Return either:
336326
/// - \c std::nullopt, if the implementation is unable to handle the loop form
337327
/// of \p L (e.g., \p L must have a latch block that controls the loop exit).
338328
/// - The value of \c llvm.loop.estimated_trip_count from the loop metadata of
339-
/// \p L, if that metadata is present.
329+
/// \p L, if that metadata is present. In the special case that the value is
330+
/// zero, return \c std::nullopt instead as that is historically what callers
331+
/// expect when a loop is estimated to execute no iterations (i.e., its header
332+
/// is not reached).
340333
/// - Else, a new estimate of the trip count from the latch branch weights of
341334
/// \p L.
342335
///
@@ -363,23 +356,15 @@ getLoopEstimatedTripCount(Loop *L,
363356
/// to handle the loop form of \p L (e.g., \p L must have a latch block that
364357
/// controls the loop exit). Otherwise, return true.
365358
///
366-
/// Some passes rely on estimated trip counts to be non-zero because, once a
367-
/// loop header is reached, at least one iteration will execute. However, some
368-
/// passes naively compute it as zero. To avoid misbehavior, if
369-
/// \p EstimatedTripCount is zero, interpret it as one.
370-
///
371359
/// In addition, if \p EstimatedLoopInvocationWeight:
372360
/// - Set the branch weight metadata of \p L to reflect that \p L has an
373361
/// estimated \p EstimatedTripCount iterations and has
374362
/// \c *EstimatedLoopInvocationWeight exit weight through the loop's latch.
375-
/// - If \p EstimatedTripCount is zero, zero the branch weights, and drop
376-
/// \c llvm.loop.estimated_trip_count entirely. \c getLoopEstimatedTripCount
377-
/// will then return \c std::nullopt.
363+
/// - If \p EstimatedTripCount is zero, zero the branch weights.
378364
///
379365
/// TODO: Eventually, once all passes have migrated away from setting branch
380366
/// weights to indicate estimated trip counts, this function will drop the
381-
/// \p EstimatedLoopInvocationWeight parameter, so its historical handling of
382-
/// \p EstimatedTripCount == 0 should no longer be needed.
367+
/// \p EstimatedLoopInvocationWeight parameter.
383368
LLVM_ABI bool setLoopEstimatedTripCount(
384369
Loop *L, unsigned EstimatedTripCount,
385370
std::optional<unsigned> EstimatedLoopInvocationWeight = std::nullopt);

llvm/lib/IR/Verifier.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,10 +1094,9 @@ void Verifier::visitMDNode(const MDNode &MD, AreDebugLocsAllowed AllowLocs) {
10941094
Check(MD.getNumOperands() == 2, "Expected two operands", &MD);
10951095
auto *Count = dyn_cast_or_null<ConstantAsMetadata>(MD.getOperand(1));
10961096
Check(Count && Count->getType()->isIntegerTy() &&
1097-
cast<IntegerType>(Count->getType())->getBitWidth() <= 32 &&
1098-
!cast<ConstantInt>(Count->getValue())->isZero(),
1099-
"Expected second operand to be a positive integer constant of type "
1100-
"i32 or smaller",
1097+
cast<IntegerType>(Count->getType())->getBitWidth() <= 32,
1098+
"Expected second operand to be an integer constant of type i32 or "
1099+
"smaller",
11011100
&MD);
11021101
}
11031102

llvm/lib/Transforms/Utils/LoopUtils.cpp

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -214,43 +214,31 @@ static MDNode *createStringMetadata(Loop *TheLoop, StringRef Name, unsigned V) {
214214
/// different.
215215
void llvm::addStringMetadataToLoop(Loop *TheLoop, const char *StringMD,
216216
unsigned V) {
217-
setLoopStringMetadata(TheLoop, StringMD, V);
218-
}
219-
220-
void llvm::setLoopStringMetadata(Loop *TheLoop, const char *StringMD,
221-
std::optional<unsigned> V) {
222217
SmallVector<Metadata *, 4> MDs(1);
223218
// If the loop already has metadata, retain it.
224219
MDNode *LoopID = TheLoop->getLoopID();
225-
bool Found = false;
226220
if (LoopID) {
227221
for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
228222
MDNode *Node = cast<MDNode>(LoopID->getOperand(i));
229223
// If it is of form key = value, try to parse it.
230224
if (Node->getNumOperands() == 2) {
231225
MDString *S = dyn_cast<MDString>(Node->getOperand(0));
232226
if (S && S->getString() == StringMD) {
233-
Found = true;
234-
if (V) {
235-
ConstantInt *IntMD =
236-
mdconst::extract_or_null<ConstantInt>(Node->getOperand(1));
237-
if (IntMD && IntMD->getSExtValue() == *V)
238-
// It is already in place. Do nothing.
239-
return;
240-
}
241-
// We need to update/remove the value, so just skip it here and it
242-
// will be added/removed after copying other existed nodes.
227+
ConstantInt *IntMD =
228+
mdconst::extract_or_null<ConstantInt>(Node->getOperand(1));
229+
if (IntMD && IntMD->getSExtValue() == V)
230+
// It is already in place. Do nothing.
231+
return;
232+
// We need to update the value, so just skip it here and it will
233+
// be added after copying other existed nodes.
243234
continue;
244235
}
245236
}
246237
MDs.push_back(Node);
247238
}
248239
}
249-
if (!V && !Found)
250-
return;
251240
// Add new metadata.
252-
if (V)
253-
MDs.push_back(createStringMetadata(TheLoop, StringMD, *V));
241+
MDs.push_back(createStringMetadata(TheLoop, StringMD, V));
254242
// Replace current metadata node with new one.
255243
LLVMContext &Context = TheLoop->getHeader()->getContext();
256244
MDNode *NewLoopID = MDNode::get(Context, MDs);
@@ -924,13 +912,14 @@ llvm::getLoopEstimatedTripCount(Loop *L,
924912
}
925913

926914
// Return the estimated trip count from metadata unless the metadata is
927-
// missing or has no value.
915+
// missing or has no value. Return std::nullopt if it's zero.
928916
if (auto TC = getOptionalIntLoopAttribute(L, LLVMLoopEstimatedTripCount)) {
929-
assert(TC != 0 && "Reached loop header executes at least one iteration");
930917
LLVM_DEBUG(dbgs() << "getLoopEstimatedTripCount: "
931918
<< LLVMLoopEstimatedTripCount << " metadata has trip "
932-
<< "count of " << *TC << " for " << DbgLoop(L) << "\n");
933-
return TC;
919+
<< "count of " << *TC
920+
<< (*TC == 0 ? " (returning std::nullopt)" : "")
921+
<< " for " << DbgLoop(L) << "\n");
922+
return *TC == 0 ? std::nullopt : std::optional(*TC);
934923
}
935924

936925
// Estimate the trip count from latch branch weights.
@@ -949,13 +938,8 @@ bool llvm::setLoopEstimatedTripCount(
949938
if (!LatchBranch)
950939
return false;
951940

952-
// Set the metadata. Some users of the estimated trip count rely on the value
953-
// to be non-zero.
954-
if (!EstimatedloopInvocationWeight) {
955-
setLoopStringMetadata(L, LLVMLoopEstimatedTripCount,
956-
EstimatedTripCount == 0 ? 1 : EstimatedTripCount);
957-
return true;
958-
}
941+
// Set the metadata.
942+
addStringMetadataToLoop(L, LLVMLoopEstimatedTripCount, EstimatedTripCount);
959943

960944
// At the moment, we currently support changing the estimated trip count in
961945
// the latch branch's branch weights only. We could extend this API to
@@ -964,13 +948,8 @@ bool llvm::setLoopEstimatedTripCount(
964948
// TODO: Eventually, once all passes have migrated away from setting branch
965949
// weights to indicate estimated trip counts, we will not set branch weights
966950
// here at all.
967-
968-
// Set the metadata. Some users of the estimated trip count rely on the value
969-
// to be non-zero.
970-
setLoopStringMetadata(L, LLVMLoopEstimatedTripCount,
971-
EstimatedTripCount == 0
972-
? std::nullopt
973-
: std::optional(EstimatedTripCount));
951+
if (!EstimatedloopInvocationWeight)
952+
return true;
974953

975954
// Calculate taken and exit weights.
976955
unsigned LatchExitWeight = 0;

llvm/test/Transforms/LoopUnroll/branch-weights-freq/unroll-epilog.ll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ do.end:
111111
!0 = !{!"branch_weights", i32 1, i32 10}
112112

113113
; ------------------------------------------------------------------------------
114-
; Check branch weight metadata and estimated trip count metadata. The minimum
115-
; valid estimated trip count is 1 not 0.
114+
; Check branch weight metadata and estimated trip count metadata.
116115
;
117116
; UR2: ![[#PROF_UR_GUARD]] = !{!"branch_weights", i32 195225786, i32 1952257862}
118117
; UR4: ![[#PROF_UR_GUARD]] = !{!"branch_weights", i32 534047398, i32 1613436250}
@@ -136,7 +135,7 @@ do.end:
136135
; UR4: ![[#LOOP_UR_TC]] = !{!"llvm.loop.estimated_trip_count", i32 2}
137136
; UR10: ![[#LOOP_UR_TC]] = !{!"llvm.loop.estimated_trip_count", i32 1}
138137
; UR11: ![[#LOOP_UR_TC]] = !{!"llvm.loop.estimated_trip_count", i32 1}
139-
; UR12: ![[#LOOP_UR_TC]] = !{!"llvm.loop.estimated_trip_count", i32 1}
138+
; UR12: ![[#LOOP_UR_TC]] = !{!"llvm.loop.estimated_trip_count", i32 0}
140139
; UR: ![[#DISABLE]] = !{!"llvm.loop.unroll.disable"}
141140
;
142141
; UR2: ![[#PROF_RM_GUARD]] = !{!"branch_weights", i32 1022611260, i32 1124872388}
@@ -152,9 +151,10 @@ do.end:
152151

153152
; UR4: ![[#LOOP_RM_LATCH]] = distinct !{![[#LOOP_RM_LATCH]], ![[#LOOP_RM_TC:]], ![[#DISABLE:]]}
154153
; UR10: ![[#LOOP_RM_LATCH]] = distinct !{![[#LOOP_RM_LATCH]], ![[#LOOP_UR_TC:]], ![[#DISABLE:]]}
155-
; UR11: ![[#LOOP_RM_LATCH]] = distinct !{![[#LOOP_RM_LATCH]], ![[#LOOP_UR_TC:]], ![[#DISABLE:]]}
154+
; UR11: ![[#LOOP_RM_LATCH]] = distinct !{![[#LOOP_RM_LATCH]], ![[#LOOP_RM_TC:]], ![[#DISABLE:]]}
156155
; UR12: ![[#LOOP_RM_LATCH]] = distinct !{![[#LOOP_RM_LATCH]], ![[#LOOP_RM_TC:]], ![[#DISABLE:]]}
157156
;
158157
; UR4: ![[#LOOP_RM_TC]] = !{!"llvm.loop.estimated_trip_count", i32 3}
159-
; For UR10 and UR11, llvm.loop.estimated_trip_count is the same for both loops.
158+
; For UR10, llvm.loop.estimated_trip_count is the same for both loops.
159+
; UR11: ![[#LOOP_RM_TC]] = !{!"llvm.loop.estimated_trip_count", i32 0}
160160
; UR12: ![[#LOOP_RM_TC]] = !{!"llvm.loop.estimated_trip_count", i32 11}

llvm/test/Transforms/LoopUnroll/peel-loop-pgo-deopt.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,5 @@ attributes #1 = { nounwind optsize }
8686
;CHECK: !16 = !{!"branch_weights", i32 3001, i32 1001}
8787
;CHECK: !17 = distinct !{!17, !18, !19, {{.*}}}
8888
;CHECK: !18 = !{!"llvm.loop.peeled.count", i32 4}
89-
;CHECK: !19 = !{!"llvm.loop.estimated_trip_count", i32 1}
89+
;CHECK: !19 = !{!"llvm.loop.estimated_trip_count", i32 0}
9090

llvm/test/Transforms/LoopUnroll/peel-loop-pgo.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,5 @@ attributes #1 = { nounwind optsize }
106106
;CHECK: !15 = !{!"branch_weights", i32 3001, i32 1001}
107107
;CHECK: !16 = distinct !{!16, !17, !18, {{.*}}}
108108
;CHECK: !17 = !{!"llvm.loop.peeled.count", i32 4}
109-
;CHECK: !18 = !{!"llvm.loop.estimated_trip_count", i32 1}
109+
;CHECK: !18 = !{!"llvm.loop.estimated_trip_count", i32 0}
110110

llvm/test/Transforms/LoopUnroll/runtime-loop-branchweight.ll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ for.end:
7474
; 1073706403 / (1073706403 + 1073777245).
7575
; CHECK: ![[#PROF_RM_LATCH]] = !{!"branch_weights", i32 1073706403, i32 1073777245}
7676

77-
; 10000%4 = 0, so the probability of reaching the remainder loop header is low.
78-
; If it is reached, at least one iteration will execute. The minimum valid
79-
; estimated trip count is 1.
77+
; 10000%4 = 0
8078
; CHECK: ![[#LOOP_RM_LATCH]] = distinct !{![[#LOOP_RM_LATCH]], ![[#LOOP_RM_TC:]], ![[#DISABLE:]]}
81-
; CHECK: ![[#LOOP_RM_TC]] = !{!"llvm.loop.estimated_trip_count", i32 1}
79+
; CHECK: ![[#LOOP_RM_TC]] = !{!"llvm.loop.estimated_trip_count", i32 0}

0 commit comments

Comments
 (0)