Skip to content

Commit 680bdc2

Browse files
committed
Remove PGOEstimateTripCountsPass and no-value form of metadata
1 parent 0973ab3 commit 680bdc2

33 files changed

+169
-609
lines changed

llvm/docs/LangRef.rst

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7971,13 +7971,12 @@ loop distribution pass. See
79717971

79727972
This metadata records an estimated trip count for the loop. The first operand
79737973
is the string ``llvm.loop.estimated_trip_count``. The second operand is an
7974-
integer constant of type ``i32`` or smaller specifying the count, which might be
7975-
omitted for the reasons described below. For example:
7974+
integer constant of type ``i32`` or smaller specifying the estimate. For
7975+
example:
79767976

79777977
.. code-block:: llvm
79787978

79797979
!0 = !{!"llvm.loop.estimated_trip_count", i32 8}
7980-
!1 = !{!"llvm.loop.estimated_trip_count"}
79817980

79827981
Purpose
79837982
"""""""
@@ -7994,38 +7993,26 @@ determine how many iterations to peel or how aggressively to unroll.
79947993
Initialization and Maintenance
79957994
""""""""""""""""""""""""""""""
79967995

7997-
The ``pgo-estimate-trip-counts`` pass typically runs immediately after profile
7998-
ingestion to add this metadata to all loops. It estimates each loop's trip
7999-
count from the loop's ``branch_weights`` metadata. This way of initially
8000-
estimating trip counts appears to be useful for the passes that consume them.
8001-
8002-
As passes transform existing loops and create new loops, they must be free to
8003-
update and create ``branch_weights`` metadata to maintain accurate block
8004-
frequencies. Trip counts estimated from this new ``branch_weights`` metadata
8005-
are not necessarily useful to the passes that consume them. In general, when
8006-
passes transform and create loops, they should separately estimate new trip
8007-
counts from previously estimated trip counts, and they should record them by
8008-
creating or updating this metadata. For this or any other work involving
8009-
estimated trip counts, passes should always call
7996+
Passes should interact with estimated trip counts always via
80107997
``llvm::getLoopEstimatedTripCount`` and ``llvm::setLoopEstimatedTripCount``.
80117998

8012-
Missing Metadata and Values
8013-
"""""""""""""""""""""""""""
8014-
8015-
If the current implementation of ``pgo-estimate-trip-counts`` cannot estimate a
8016-
trip count from the loop's ``branch_weights`` metadata due to the loop's form or
8017-
due to missing profile data, it creates this metadata for the loop but omits the
8018-
value. This situation is currently common (e.g., the LLVM IR loop that Clang
8019-
emits for a simple C ``for`` loop). A later pass (e.g., ``loop-rotate``) might
8020-
modify the loop's form in a way that enables estimating its trip count even if
8021-
those modifications provably never impact the actual number of loop iterations.
8022-
That later pass should then add an appropriate value to the metadata.
8023-
8024-
However, not all such passes currently do so. Thus, if this metadata has no
8025-
value, ``llvm::getLoopEstimatedTripCount`` will disregard it and estimate the
8026-
trip count from the loop's ``branch_weights`` metadata. It does the same when
8027-
the metadata is missing altogether, perhaps because ``pgo-estimate-trip-counts``
8028-
was not specified in a minimal pass list to a tool like ``opt``.
7999+
When the ``llvm.loop.estimated_trip_count`` metadata is not present on a loop,
8000+
``llvm::getLoopEstimatedTripCount`` estimates the loop's trip count from the
8001+
loop's ``branch_weights`` metadata under the assumption that the latter still
8002+
accurately encodes the program's original profile data. However, as passes
8003+
transform existing loops and create new loops, they must be free to update and
8004+
create ``branch_weights`` metadata in a way that maintains accurate block
8005+
frequencies. Trip counts estimated from this new ``branch_weights`` metadata
8006+
are not necessarily useful to the passes that consume estimated trip counts.
8007+
8008+
For this reason, when a pass transforms or creates loops, the pass should
8009+
separately estimate new trip counts based on the estimated trip counts that
8010+
``llvm::getLoopEstimatedTripCount`` returns at the start of the pass, and the
8011+
pass should record the new estimates by calling
8012+
``llvm::setLoopEstimatedTripCount``, which creates or updates
8013+
``llvm.loop.estimated_trip_count`` metadata. Once this metadata is present on a
8014+
loop, ``llvm::getLoopEstimatedTripCount`` returns its value instead of
8015+
estimating the trip count from the loop's ``branch_weights`` metadata.
80298016

80308017
'``llvm.licm.disable``' Metadata
80318018
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

llvm/include/llvm/Analysis/LoopInfo.h

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

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

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

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: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -317,17 +317,15 @@ LLVM_ABI TransformationMode hasDistributeTransformation(const Loop *L);
317317
LLVM_ABI TransformationMode hasLICMVersioningTransformation(const Loop *L);
318318
/// @}
319319

320-
/// Set the string \p MDString into the loop metadata of \p TheLoop while
321-
/// keeping other loop metadata intact. Set \p *V as its value, or set it
322-
/// without a value if \p V is \c std::nullopt to indicate the value is unknown.
323-
/// If \p MDString is already in the loop metadata, update it if its value (or
324-
/// lack of value) is different. Return true if metadata was changed.
325-
LLVM_ABI bool addStringMetadataToLoop(Loop *TheLoop, const char *MDString,
326-
std::optional<unsigned> V = 0);
320+
/// Set input string into loop metadata by keeping other values intact.
321+
/// If the string is already in loop metadata update value if it is
322+
/// different.
323+
LLVM_ABI void addStringMetadataToLoop(Loop *TheLoop, const char *MDString,
324+
unsigned V = 0);
327325

328326
/// Return either:
329327
/// - The value of \c llvm.loop.estimated_trip_count from the loop metadata of
330-
/// \p L, if that metadata is present and has a value.
328+
/// \p L, if that metadata is present.
331329
/// - Else, a new estimate of the trip count from the latch branch weights of
332330
/// \p L, if the estimation's implementation is able to handle the loop form
333331
/// of \p L (e.g., \p L must have a latch block that controls the loop exit).
@@ -336,61 +334,38 @@ LLVM_ABI bool addStringMetadataToLoop(Loop *TheLoop, const char *MDString,
336334
/// An estimated trip count is always a valid positive trip count, saturated at
337335
/// \c UINT_MAX.
338336
///
339-
/// Via \c LLVM_DEBUG, emit diagnostics that include "WARNING" when the metadata
340-
/// is in an unexpected state as that indicates some transformation has
341-
/// corrupted it. If \p DbgForInit, expect the metadata to be missing.
342-
/// Otherwise, expect the metadata to be present, and expect it to have no value
343-
/// only if the trip count is currently inestimable from the latch branch
344-
/// weights.
345-
///
346337
/// In addition, if \p EstimatedLoopInvocationWeight, then either:
347-
/// - Set \p *EstimatedLoopInvocationWeight to the weight of the latch's branch
338+
/// - Set \c *EstimatedLoopInvocationWeight to the weight of the latch's branch
348339
/// to the loop exit.
349-
/// - Do not set it and return \c std::nullopt if the current implementation
340+
/// - Do not set it, and return \c std::nullopt, if the current implementation
350341
/// cannot compute that weight (e.g., if \p L does not have a latch block that
351342
/// controls the loop exit) or the weight is zero (because zero cannot be
352343
/// used to compute new branch weights that reflect the estimated trip count).
353344
///
354345
/// TODO: Eventually, once all passes have migrated away from setting branch
355346
/// weights to indicate estimated trip counts, this function will drop the
356347
/// \p EstimatedLoopInvocationWeight parameter.
357-
///
358-
/// TODO: There are also passes that currently do not consider estimated trip
359-
/// counts at all but that, for example, affect whether trip counts can be
360-
/// estimated from branch weights. Once all such passes have been adjusted to
361-
/// update this metadata, this function might stop estimating trip counts from
362-
/// branch weights and instead simply get the \c llvm.loop_estimated_trip_count
363-
/// metadata. See also the \c llvm.loop.estimated_trip_count entry in
364-
/// \c LangRef.rst.
365348
LLVM_ABI std::optional<unsigned>
366349
getLoopEstimatedTripCount(Loop *L,
367-
unsigned *EstimatedLoopInvocationWeight = nullptr,
368-
bool DbgForInit = false);
369-
370-
/// Set \c llvm.loop.estimated_trip_count with the value \c *EstimatedTripCount
371-
/// in the loop metadata of \p L, or set it without a value if
372-
/// \c !EstimatedTripCount to indicate that \c getLoopEstimatedTripCount cannot
373-
/// estimate the trip count from latch branch weights. If
374-
/// \c !EstimatedTripCount but \c getLoopEstimatedTripCount can estimate the
375-
/// trip counts, future calls to \c getLoopEstimatedTripCount will diagnose the
376-
/// metadata as corrupt.
350+
unsigned *EstimatedLoopInvocationWeight = nullptr);
351+
352+
/// Set \c llvm.loop.estimated_trip_count with the value \p EstimatedTripCount
353+
/// in the loop metadata of \p L.
377354
///
378355
/// In addition, if \p EstimatedLoopInvocationWeight, set the branch weight
379356
/// metadata of \p L to reflect that \p L has an estimated
380-
/// \c *EstimatedTripCount iterations and has \c *EstimatedLoopInvocationWeight
357+
/// \p EstimatedTripCount iterations and has \c *EstimatedLoopInvocationWeight
381358
/// exit weight through the loop's latch.
382359
///
383-
/// Return false if \c llvm.loop.estimated_trip_count was already set according
384-
/// to \p EstimatedTripCount and so was not updated. Return false if
385-
/// \p EstimatedLoopInvocationWeight and if branch weight metadata could not be
386-
/// successfully updated (e.g., if \p L does not have a latch block that
387-
/// controls the loop exit). Otherwise, return true.
360+
/// Return false if \p EstimatedLoopInvocationWeight and if branch weight
361+
/// metadata could not be successfully updated (e.g., if \p L does not have a
362+
/// latch block that controls the loop exit). Otherwise, return true.
388363
///
389364
/// TODO: Eventually, once all passes have migrated away from setting branch
390365
/// weights to indicate estimated trip counts, this function will drop the
391366
/// \p EstimatedLoopInvocationWeight parameter.
392367
LLVM_ABI bool setLoopEstimatedTripCount(
393-
Loop *L, std::optional<unsigned> EstimatedTripCount,
368+
Loop *L, unsigned EstimatedTripCount,
394369
std::optional<unsigned> EstimatedLoopInvocationWeight = std::nullopt);
395370

396371
/// Check inner loop (L) backedge count is known to be invariant on all

llvm/lib/Analysis/LoopInfo.cpp

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

11251125
std::optional<int> llvm::getOptionalIntLoopAttribute(const Loop *TheLoop,
1126-
StringRef Name,
1127-
bool *Missing) {
1128-
std::optional<const MDOperand *> AttrMDOpt =
1129-
findStringMetadataForLoop(TheLoop, Name);
1130-
if (Missing)
1131-
*Missing = !AttrMDOpt;
1132-
const MDOperand *AttrMD = AttrMDOpt.value_or(nullptr);
1126+
StringRef Name) {
1127+
const MDOperand *AttrMD =
1128+
findStringMetadataForLoop(TheLoop, Name).value_or(nullptr);
11331129
if (!AttrMD)
11341130
return std::nullopt;
11351131

llvm/lib/IR/Verifier.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,16 +1077,13 @@ void Verifier::visitMDNode(const MDNode &MD, AreDebugLocsAllowed AllowLocs) {
10771077
// Check llvm.loop.estimated_trip_count.
10781078
if (MD.getNumOperands() > 0 &&
10791079
MD.getOperand(0).equalsStr(LLVMLoopEstimatedTripCount)) {
1080-
Check(MD.getNumOperands() == 1 || MD.getNumOperands() == 2,
1081-
"Expected one or two operands", &MD);
1082-
if (MD.getNumOperands() == 2) {
1083-
auto *Count = dyn_cast_or_null<ConstantAsMetadata>(MD.getOperand(1));
1084-
Check(Count && Count->getType()->isIntegerTy() &&
1085-
cast<IntegerType>(Count->getType())->getBitWidth() <= 32,
1086-
"Expected optional second operand to be an integer constant of "
1087-
"type i32 or smaller",
1088-
&MD);
1089-
}
1080+
Check(MD.getNumOperands() == 2, "Expected two operands", &MD);
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 second operand to be an integer constant of type i32 or "
1085+
"smaller",
1086+
&MD);
10901087
}
10911088

10921089
// Check these last, so we diagnose problems in operands first.

llvm/lib/Passes/PassBuilder.cpp

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

llvm/lib/Passes/PassBuilderPipelines.cpp

Lines changed: 2 additions & 7 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,8 +1239,6 @@ PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
12401239
MPM.addPass(AssignGUIDPass());
12411240
if (IsCtxProfUse) {
12421241
MPM.addPass(PGOCtxProfFlatteningPass(/*IsPreThinlink=*/true));
1243-
MPM.addPass(
1244-
createModuleToFunctionPassAdaptor(PGOEstimateTripCountsPass()));
12451242
return MPM;
12461243
}
12471244
// Block further inlining in the instrumented ctxprof case. This avoids
@@ -1271,10 +1268,8 @@ PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
12711268
MPM.addPass(MemProfUsePass(PGOOpt->MemoryProfile, PGOOpt->FS));
12721269

12731270
if (PGOOpt && (PGOOpt->Action == PGOOptions::IRUse ||
1274-
PGOOpt->Action == PGOOptions::SampleUse)) {
1271+
PGOOpt->Action == PGOOptions::SampleUse))
12751272
MPM.addPass(PGOForceFunctionAttrsPass(PGOOpt->ColdOptType));
1276-
}
1277-
MPM.addPass(createModuleToFunctionPassAdaptor(PGOEstimateTripCountsPass()));
12781273

12791274
MPM.addPass(AlwaysInlinerPass(/*InsertLifetimeIntrinsics=*/true));
12801275

@@ -2360,4 +2355,4 @@ AAManager PassBuilder::buildDefaultAAPipeline() {
23602355
bool PassBuilder::isInstrumentedPGOUse() const {
23612356
return (PGOOpt && PGOOpt->Action == PGOOptions::IRUse) ||
23622357
!UseCtxProfile.empty();
2363-
}
2358+
}

llvm/lib/Passes/PassRegistry.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,6 @@ FUNCTION_PASS("objc-arc-contract", ObjCARCContractPass())
487487
FUNCTION_PASS("objc-arc-expand", ObjCARCExpandPass())
488488
FUNCTION_PASS("pa-eval", PAEvalPass())
489489
FUNCTION_PASS("partially-inline-libcalls", PartiallyInlineLibCallsPass())
490-
FUNCTION_PASS("pgo-estimate-trip-counts", PGOEstimateTripCountsPass())
491490
FUNCTION_PASS("pgo-memop-opt", PGOMemOPSizeOpt())
492491
FUNCTION_PASS("place-safepoints", PlaceSafepointsPass())
493492
FUNCTION_PASS("print", PrintFunctionPass(errs()))

llvm/lib/Transforms/Instrumentation/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ add_llvm_component_library(LLVMInstrumentation
1616
LowerAllowCheckPass.cpp
1717
PGOCtxProfFlattening.cpp
1818
PGOCtxProfLowering.cpp
19-
PGOEstimateTripCounts.cpp
2019
PGOForceFunctionAttrs.cpp
2120
PGOInstrumentation.cpp
2221
PGOMemOPSizeOpt.cpp

0 commit comments

Comments
 (0)