Skip to content

Commit 37e03b5

Browse files
authored
Revert "[PGO] Add llvm.loop.estimated_trip_count metadata" (#151585)
Reverts #148758 [As requested.](#148758 (review))
1 parent a85c725 commit 37e03b5

36 files changed

+198
-938
lines changed

llvm/docs/LangRef.rst

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -7960,67 +7960,6 @@ The attributes in this metadata is added to all followup loops of the
79607960
loop distribution pass. See
79617961
:ref:`Transformation Metadata <transformation-metadata>` for details.
79627962

7963-
'``llvm.loop.estimated_trip_count``' Metadata
7964-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7965-
7966-
This metadata records an estimated trip count for the loop. The first operand
7967-
is the string ``llvm.loop.estimated_trip_count``. The second operand is an
7968-
integer constant of type ``i32`` or smaller specifying the count, which might be
7969-
omitted for the reasons described below. For example:
7970-
7971-
.. code-block:: llvm
7972-
7973-
!0 = !{!"llvm.loop.estimated_trip_count", i32 8}
7974-
!1 = !{!"llvm.loop.estimated_trip_count"}
7975-
7976-
Purpose
7977-
"""""""
7978-
7979-
A loop's estimated trip count is an estimate of the average number of loop
7980-
iterations (specifically, the number of times the loop's header executes) each
7981-
time execution reaches the loop. It is usually only an estimate based on, for
7982-
example, profile data. The actual number of iterations might vary widely.
7983-
7984-
The estimated trip count serves as a parameter for various loop transformations
7985-
and typically helps estimate transformation cost. For example, it can help
7986-
determine how many iterations to peel or how aggressively to unroll.
7987-
7988-
Initialization and Maintenance
7989-
""""""""""""""""""""""""""""""
7990-
7991-
The ``pgo-estimate-trip-counts`` pass typically runs immediately after profile
7992-
ingestion to add this metadata to all loops. It estimates each loop's trip
7993-
count from the loop's ``branch_weights`` metadata. This way of initially
7994-
estimating trip counts appears to be useful for the passes that consume them.
7995-
7996-
As passes transform existing loops and create new loops, they must be free to
7997-
update and create ``branch_weights`` metadata to maintain accurate block
7998-
frequencies. Trip counts estimated from this new ``branch_weights`` metadata
7999-
are not necessarily useful to the passes that consume them. In general, when
8000-
passes transform and create loops, they should separately estimate new trip
8001-
counts from previously estimated trip counts, and they should record them by
8002-
creating or updating this metadata. For this or any other work involving
8003-
estimated trip counts, passes should always call
8004-
``llvm::getLoopEstimatedTripCount`` and ``llvm::setLoopEstimatedTripCount``.
8005-
8006-
Missing Metadata and Values
8007-
"""""""""""""""""""""""""""
8008-
8009-
If the current implementation of ``pgo-estimate-trip-counts`` cannot estimate a
8010-
trip count from the loop's ``branch_weights`` metadata due to the loop's form or
8011-
due to missing profile data, it creates this metadata for the loop but omits the
8012-
value. This situation is currently common (e.g., the LLVM IR loop that Clang
8013-
emits for a simple C ``for`` loop). A later pass (e.g., ``loop-rotate``) might
8014-
modify the loop's form in a way that enables estimating its trip count even if
8015-
those modifications provably never impact the actual number of loop iterations.
8016-
That later pass should then add an appropriate value to the metadata.
8017-
8018-
However, not all such passes currently do so. Thus, if this metadata has no
8019-
value, ``llvm::getLoopEstimatedTripCount`` will disregard it and estimate the
8020-
trip count from the loop's ``branch_weights`` metadata. It does the same when
8021-
the metadata is missing altogether, perhaps because ``pgo-estimate-trip-counts``
8022-
was not specified in a minimal pass list to a tool like ``opt``.
8023-
80247963
'``llvm.licm.disable``' Metadata
80257964
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
80267965

llvm/include/llvm/Analysis/LoopInfo.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -637,13 +637,9 @@ LLVM_ABI std::optional<bool> getOptionalBoolLoopAttribute(const Loop *TheLoop,
637637
/// Returns true if Name is applied to TheLoop and enabled.
638638
LLVM_ABI bool getBooleanLoopAttribute(const Loop *TheLoop, StringRef Name);
639639

640-
/// Find named metadata for a loop with an integer value. Return
641-
/// \c std::nullopt if the metadata has no value or is missing altogether. If
642-
/// \p Missing, set \c *Missing to indicate whether the metadata is missing
643-
/// altogether.
644-
LLVM_ABI std::optional<int>
645-
getOptionalIntLoopAttribute(const Loop *TheLoop, StringRef Name,
646-
bool *Missing = nullptr);
640+
/// Find named metadata for a loop with an integer value.
641+
LLVM_ABI std::optional<int> getOptionalIntLoopAttribute(const Loop *TheLoop,
642+
StringRef Name);
647643

648644
/// Find named metadata for a loop with an integer value. Return \p Default if
649645
/// not set.

llvm/include/llvm/IR/Metadata.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -919,8 +919,8 @@ class MDOperand {
919919

920920
// Check if MDOperand is of type MDString and equals `Str`.
921921
bool equalsStr(StringRef Str) const {
922-
return isa_and_nonnull<MDString>(get()) &&
923-
cast<MDString>(get())->getString() == Str;
922+
return isa<MDString>(this->get()) &&
923+
cast<MDString>(this->get())->getString() == Str;
924924
}
925925

926926
~MDOperand() { untrack(); }

llvm/include/llvm/Transforms/Instrumentation/PGOEstimateTripCounts.h

Lines changed: 0 additions & 24 deletions
This file was deleted.

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

Lines changed: 20 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ typedef std::pair<const RuntimeCheckingPtrGroup *,
5252
template <typename T, unsigned N> class SmallSetVector;
5353
template <typename T, unsigned N> class SmallPriorityWorklist;
5454

55-
const char *const LLVMLoopEstimatedTripCount = "llvm.loop.estimated_trip_count";
56-
5755
LLVM_ABI BasicBlock *InsertPreheaderForLoop(Loop *L, DominatorTree *DT,
5856
LoopInfo *LI,
5957
MemorySSAUpdater *MSSAU,
@@ -318,81 +316,28 @@ LLVM_ABI TransformationMode hasDistributeTransformation(const Loop *L);
318316
LLVM_ABI TransformationMode hasLICMVersioningTransformation(const Loop *L);
319317
/// @}
320318

321-
/// Set the string \p MDString into the loop metadata of \p TheLoop while
322-
/// keeping other loop metadata intact. Set \p *V as its value, or set it
323-
/// without a value if \p V is \c std::nullopt to indicate the value is unknown.
324-
/// If \p MDString is already in the loop metadata, update it if its value (or
325-
/// lack of value) is different. Return true if metadata was changed.
326-
LLVM_ABI bool addStringMetadataToLoop(Loop *TheLoop, const char *MDString,
327-
std::optional<unsigned> V = 0);
328-
329-
/// Return either:
330-
/// - The value of \c llvm.loop.estimated_trip_count from the loop metadata of
331-
/// \p L, if that metadata is present and has a value.
332-
/// - Else, a new estimate of the trip count from the latch branch weights of
333-
/// \p L, if the estimation's implementation is able to handle the loop form
334-
/// of \p L (e.g., \p L must have a latch block that controls the loop exit).
335-
/// - Else, \c std::nullopt.
336-
///
337-
/// An estimated trip count is always a valid positive trip count, saturated at
338-
/// \c UINT_MAX.
339-
///
340-
/// Via \c LLVM_DEBUG, emit diagnostics that include "WARNING" when the metadata
341-
/// is in an unexpected state as that indicates some transformation has
342-
/// corrupted it. If \p DbgForInit, expect the metadata to be missing.
343-
/// Otherwise, expect the metadata to be present, and expect it to have no value
344-
/// only if the trip count is currently inestimable from the latch branch
345-
/// weights.
346-
///
347-
/// In addition, if \p EstimatedLoopInvocationWeight, then either:
348-
/// - Set \p *EstimatedLoopInvocationWeight to the weight of the latch's branch
349-
/// to the loop exit.
350-
/// - Do not set it and return \c std::nullopt if the current implementation
351-
/// cannot compute that weight (e.g., if \p L does not have a latch block that
352-
/// controls the loop exit) or the weight is zero (because zero cannot be
353-
/// used to compute new branch weights that reflect the estimated trip count).
354-
///
355-
/// TODO: Eventually, once all passes have migrated away from setting branch
356-
/// weights to indicate estimated trip counts, this function will drop the
357-
/// \p EstimatedLoopInvocationWeight parameter.
358-
///
359-
/// TODO: There are also passes that currently do not consider estimated trip
360-
/// counts at all but that, for example, affect whether trip counts can be
361-
/// estimated from branch weights. Once all such passes have been adjusted to
362-
/// update this metadata, this function might stop estimating trip counts from
363-
/// branch weights and instead simply get the \c llvm.loop_estimated_trip_count
364-
/// metadata. See also the \c llvm.loop.estimated_trip_count entry in
365-
/// \c LangRef.rst.
319+
/// Set input string into loop metadata by keeping other values intact.
320+
/// If the string is already in loop metadata update value if it is
321+
/// different.
322+
LLVM_ABI void addStringMetadataToLoop(Loop *TheLoop, const char *MDString,
323+
unsigned V = 0);
324+
325+
/// Returns a loop's estimated trip count based on branch weight metadata.
326+
/// In addition if \p EstimatedLoopInvocationWeight is not null it is
327+
/// initialized with weight of loop's latch leading to the exit.
328+
/// Returns a valid positive trip count, saturated at UINT_MAX, or std::nullopt
329+
/// when a meaningful estimate cannot be made.
366330
LLVM_ABI std::optional<unsigned>
367331
getLoopEstimatedTripCount(Loop *L,
368-
unsigned *EstimatedLoopInvocationWeight = nullptr,
369-
bool DbgForInit = false);
370-
371-
/// Set \c llvm.loop.estimated_trip_count with the value \c *EstimatedTripCount
372-
/// in the loop metadata of \p L, or set it without a value if
373-
/// \c !EstimatedTripCount to indicate that \c getLoopEstimatedTripCount cannot
374-
/// estimate the trip count from latch branch weights. If
375-
/// \c !EstimatedTripCount but \c getLoopEstimatedTripCount can estimate the
376-
/// trip counts, future calls to \c getLoopEstimatedTripCount will diagnose the
377-
/// metadata as corrupt.
378-
///
379-
/// In addition, if \p EstimatedLoopInvocationWeight, set the branch weight
380-
/// metadata of \p L to reflect that \p L has an estimated
381-
/// \c *EstimatedTripCount iterations and has \c *EstimatedLoopInvocationWeight
382-
/// exit weight through the loop's latch.
383-
///
384-
/// Return false if \c llvm.loop.estimated_trip_count was already set according
385-
/// to \p EstimatedTripCount and so was not updated. Return false if
386-
/// \p EstimatedLoopInvocationWeight and if branch weight metadata could not be
387-
/// successfully updated (e.g., if \p L does not have a latch block that
388-
/// controls the loop exit). Otherwise, return true.
389-
///
390-
/// TODO: Eventually, once all passes have migrated away from setting branch
391-
/// weights to indicate estimated trip counts, this function will drop the
392-
/// \p EstimatedLoopInvocationWeight parameter.
393-
LLVM_ABI bool setLoopEstimatedTripCount(
394-
Loop *L, std::optional<unsigned> EstimatedTripCount,
395-
std::optional<unsigned> EstimatedLoopInvocationWeight = std::nullopt);
332+
unsigned *EstimatedLoopInvocationWeight = nullptr);
333+
334+
/// Set a loop's branch weight metadata to reflect that loop has \p
335+
/// EstimatedTripCount iterations and \p EstimatedLoopInvocationWeight exits
336+
/// through latch. Returns true if metadata is successfully updated, false
337+
/// otherwise. Note that loop must have a latch block which controls loop exit
338+
/// in order to succeed.
339+
LLVM_ABI bool setLoopEstimatedTripCount(Loop *L, unsigned EstimatedTripCount,
340+
unsigned EstimatedLoopInvocationWeight);
396341

397342
/// Check inner loop (L) backedge count is known to be invariant on all
398343
/// iterations of its outer loop. If the loop has no parent, this is trivially

llvm/lib/Analysis/LoopInfo.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,13 +1111,9 @@ bool llvm::getBooleanLoopAttribute(const Loop *TheLoop, StringRef Name) {
11111111
}
11121112

11131113
std::optional<int> llvm::getOptionalIntLoopAttribute(const Loop *TheLoop,
1114-
StringRef Name,
1115-
bool *Missing) {
1116-
std::optional<const MDOperand *> AttrMDOpt =
1117-
findStringMetadataForLoop(TheLoop, Name);
1118-
if (Missing)
1119-
*Missing = !AttrMDOpt;
1120-
const MDOperand *AttrMD = AttrMDOpt.value_or(nullptr);
1114+
StringRef Name) {
1115+
const MDOperand *AttrMD =
1116+
findStringMetadataForLoop(TheLoop, Name).value_or(nullptr);
11211117
if (!AttrMD)
11221118
return std::nullopt;
11231119

llvm/lib/IR/Verifier.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@
121121
#include "llvm/Support/MathExtras.h"
122122
#include "llvm/Support/ModRef.h"
123123
#include "llvm/Support/raw_ostream.h"
124-
#include "llvm/Transforms/Utils/LoopUtils.h"
125124
#include <algorithm>
126125
#include <cassert>
127126
#include <cstdint>
@@ -1072,21 +1071,6 @@ void Verifier::visitMDNode(const MDNode &MD, AreDebugLocsAllowed AllowLocs) {
10721071
}
10731072
}
10741073

1075-
// Check llvm.loop.estimated_trip_count.
1076-
if (MD.getNumOperands() > 0 &&
1077-
MD.getOperand(0).equalsStr(LLVMLoopEstimatedTripCount)) {
1078-
Check(MD.getNumOperands() == 1 || MD.getNumOperands() == 2,
1079-
"Expected one or two operands", &MD);
1080-
if (MD.getNumOperands() == 2) {
1081-
auto *Count = dyn_cast_or_null<ConstantAsMetadata>(MD.getOperand(1));
1082-
Check(Count && Count->getType()->isIntegerTy() &&
1083-
cast<IntegerType>(Count->getType())->getBitWidth() <= 32,
1084-
"Expected optional second operand to be an integer constant of "
1085-
"type i32 or smaller",
1086-
&MD);
1087-
}
1088-
}
1089-
10901074
// Check these last, so we diagnose problems in operands first.
10911075
Check(!MD.isTemporary(), "Expected no forward declarations!", &MD);
10921076
Check(MD.isResolved(), "All nodes should be resolved!", &MD);

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,6 @@
252252
#include "llvm/Transforms/Instrumentation/NumericalStabilitySanitizer.h"
253253
#include "llvm/Transforms/Instrumentation/PGOCtxProfFlattening.h"
254254
#include "llvm/Transforms/Instrumentation/PGOCtxProfLowering.h"
255-
#include "llvm/Transforms/Instrumentation/PGOEstimateTripCounts.h"
256255
#include "llvm/Transforms/Instrumentation/PGOForceFunctionAttrs.h"
257256
#include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"
258257
#include "llvm/Transforms/Instrumentation/RealtimeSanitizer.h"

llvm/lib/Passes/PassBuilderPipelines.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@
8080
#include "llvm/Transforms/Instrumentation/MemProfUse.h"
8181
#include "llvm/Transforms/Instrumentation/PGOCtxProfFlattening.h"
8282
#include "llvm/Transforms/Instrumentation/PGOCtxProfLowering.h"
83-
#include "llvm/Transforms/Instrumentation/PGOEstimateTripCounts.h"
8483
#include "llvm/Transforms/Instrumentation/PGOForceFunctionAttrs.h"
8584
#include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"
8685
#include "llvm/Transforms/Scalar/ADCE.h"
@@ -1240,7 +1239,6 @@ PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
12401239
MPM.addPass(AssignGUIDPass());
12411240
if (IsCtxProfUse) {
12421241
MPM.addPass(PGOCtxProfFlatteningPass(/*IsPreThinlink=*/true));
1243-
MPM.addPass(PGOEstimateTripCountsPass());
12441242
return MPM;
12451243
}
12461244
// Block further inlining in the instrumented ctxprof case. This avoids
@@ -1270,10 +1268,8 @@ PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
12701268
MPM.addPass(MemProfUsePass(PGOOpt->MemoryProfile, PGOOpt->FS));
12711269

12721270
if (PGOOpt && (PGOOpt->Action == PGOOptions::IRUse ||
1273-
PGOOpt->Action == PGOOptions::SampleUse)) {
1271+
PGOOpt->Action == PGOOptions::SampleUse))
12741272
MPM.addPass(PGOForceFunctionAttrsPass(PGOOpt->ColdOptType));
1275-
}
1276-
MPM.addPass(PGOEstimateTripCountsPass());
12771273

12781274
MPM.addPass(AlwaysInlinerPass(/*InsertLifetimeIntrinsics=*/true));
12791275

@@ -2359,4 +2355,4 @@ AAManager PassBuilder::buildDefaultAAPipeline() {
23592355
bool PassBuilder::isInstrumentedPGOUse() const {
23602356
return (PGOOpt && PGOOpt->Action == PGOOptions::IRUse) ||
23612357
!UseCtxProfile.empty();
2362-
}
2358+
}

llvm/lib/Passes/PassRegistry.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ MODULE_PASS("openmp-opt", OpenMPOptPass())
124124
MODULE_PASS("openmp-opt-postlink",
125125
OpenMPOptPass(ThinOrFullLTOPhase::FullLTOPostLink))
126126
MODULE_PASS("partial-inliner", PartialInlinerPass())
127-
MODULE_PASS("pgo-estimate-trip-counts", PGOEstimateTripCountsPass())
128127
MODULE_PASS("pgo-icall-prom", PGOIndirectCallPromotion())
129128
MODULE_PASS("pgo-instr-gen", PGOInstrumentationGen())
130129
MODULE_PASS("pgo-instr-use", PGOInstrumentationUse())

0 commit comments

Comments
 (0)