Skip to content

[PGO] Add llvm.loop.estimated_trip_count metadata #148758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions llvm/docs/LangRef.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7945,6 +7945,67 @@ The attributes in this metadata is added to all followup loops of the
loop distribution pass. See
:ref:`Transformation Metadata <transformation-metadata>` for details.

'``llvm.loop.estimated_trip_count``' Metadata
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This metadata records an estimated trip count for the loop. The first operand
is the string ``llvm.loop.estimated_trip_count``. The second operand is an
integer specifying the count, which might be omitted for the reasons described
below. For example:

.. code-block:: llvm

!0 = !{!"llvm.loop.estimated_trip_count", i32 8}
!1 = !{!"llvm.loop.estimated_trip_count"}

Purpose
"""""""

A loop's estimated trip count is an estimate of the average number of loop
iterations (specifically, the number of times the loop's header executes) each
time execution reaches the loop. It is usually only an estimate based on, for
example, profile data. The actual number of iterations might vary widely.

The estimated trip count serves as a parameter for various loop transformations
and typically helps estimate transformation cost. For example, it can help
determine how many iterations to peel or how aggressively to unroll.

Initialization and Maintenance
""""""""""""""""""""""""""""""

The ``pgo-estimate-trip-counts`` pass typically runs immediately after profile
ingestion to add this metadata to all loops. It estimates each loop's trip
count from the loop's ``branch_weights`` metadata. This way of initially
estimating trip counts appears to be useful for the passes that consume them.

As passes transform existing loops and create new loops, they must be free to
update and create ``branch_weights`` metadata to maintain accurate block
frequencies. Trip counts estimated from this new ``branch_weights`` metadata
are not necessarily useful to the passes that consume them. In general, when
passes transform and create loops, they should separately estimate new trip
counts from previously estimated trip counts, and they should record them by
creating or updating this metadata. For this or any other work involving
estimated trip counts, passes should always call
``llvm::getLoopEstimatedTripCount`` and ``llvm::setLoopEstimatedTripCount``.

Missing Metadata and Values
"""""""""""""""""""""""""""

If the current implementation of ``pgo-estimate-trip-counts`` cannot estimate a
trip count from the loop's ``branch_weights`` metadata due to the loop's form or
due to missing profile data, it creates this metadata for the loop but omits the
value. This situation is currently common (e.g., the LLVM IR loop that Clang
emits for a simple C ``for`` loop). A later pass (e.g., ``loop-rotate``) might
modify the loop's form in a way that enables estimating its trip count even if
those modifications provably never impact the actual number of loop iterations.
That later pass should then add an appropriate value to the metadata.

However, not all such passes currently do so. Thus, if this metadata has no
value, ``llvm::getLoopEstimatedTripCount`` will disregard it and estimate the
trip count from the loop's ``branch_weights`` metadata. It does the same when
the metadata is missing altogether, perhaps because ``pgo-estimate-trip-counts``
was not specified in a minimal pass list to a tool like ``opt``.

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

Expand Down
10 changes: 7 additions & 3 deletions llvm/include/llvm/Analysis/LoopInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -637,9 +637,13 @@ LLVM_ABI std::optional<bool> getOptionalBoolLoopAttribute(const Loop *TheLoop,
/// Returns true if Name is applied to TheLoop and enabled.
LLVM_ABI bool getBooleanLoopAttribute(const Loop *TheLoop, StringRef Name);

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

/// Find named metadata for a loop with an integer value. Return \p Default if
/// not set.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===- PGOEstimateTripCounts.h ----------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_TRANSFORMS_INSTRUMENTATION_PGOESTIMATETRIPCOUNTS_H
#define LLVM_TRANSFORMS_INSTRUMENTATION_PGOESTIMATETRIPCOUNTS_H

#include "llvm/IR/PassManager.h"

namespace llvm {

struct PGOEstimateTripCountsPass
: public PassInfoMixin<PGOEstimateTripCountsPass> {
PGOEstimateTripCountsPass() {}
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
};

} // namespace llvm

#endif // LLVM_TRANSFORMS_INSTRUMENTATION_PGOESTIMATETRIPCOUNTS_H
85 changes: 65 additions & 20 deletions llvm/include/llvm/Transforms/Utils/LoopUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,28 +316,73 @@ LLVM_ABI TransformationMode hasDistributeTransformation(const Loop *L);
LLVM_ABI TransformationMode hasLICMVersioningTransformation(const Loop *L);
/// @}

/// Set input string into loop metadata by keeping other values intact.
/// If the string is already in loop metadata update value if it is
/// different.
LLVM_ABI void addStringMetadataToLoop(Loop *TheLoop, const char *MDString,
unsigned V = 0);

/// Returns a loop's estimated trip count based on branch weight metadata.
/// In addition if \p EstimatedLoopInvocationWeight is not null it is
/// initialized with weight of loop's latch leading to the exit.
/// Returns a valid positive trip count, saturated at UINT_MAX, or std::nullopt
/// when a meaningful estimate cannot be made.
/// Set the string \p MDString into the loop metadata of \p TheLoop while
/// keeping other loop metadata intact. Set \p *V as its value, or set it
/// without a value if \p V is \c std::nullopt to indicate the value is unknown.
/// If \p MDString is already in the loop metadata, update it if its value (or
/// lack of value) is different. Return true if metadata was changed.
LLVM_ABI bool addStringMetadataToLoop(Loop *TheLoop, const char *MDString,
std::optional<unsigned> V = 0);

/// Return either:
/// - The value of \c llvm.loop.estimated_trip_count from the loop metadata of
/// \p L, if that metadata is present and has a value.
/// - Else, a new estimate of the trip count from the latch branch weights of
/// \p L, if the estimation's implementation is able to handle the loop form
/// of \p L (e.g., \p L must have a latch block that controls the loop exit).
/// - Else, \c std::nullopt.
///
/// An estimated trip count is always a valid positive trip count, saturated at
/// \c UINT_MAX.
///
/// Via \c LLVM_DEBUG, emit diagnostics that include "WARNING" when the metadata
/// is in an unexpected state as that indicates some transformation has
/// corrupted it. If \p DbgForInit, expect the metadata to be missing.
/// Otherwise, expect the metadata to be present, and expect it to have no value
/// only if the trip count is currently inestimable from the latch branch
/// weights.
///
/// In addition, if \p EstimatedLoopInvocationWeight, then either:
/// - Set \p *EstimatedLoopInvocationWeight to the weight of the latch's branch
/// to the loop exit.
/// - Do not set it and return \c std::nullopt if the current implementation
/// cannot compute that weight (e.g., if \p L does not have a latch block that
/// controls the loop exit) or the weight is zero (because zero cannot be
/// used to compute new branch weights that reflect the estimated trip count).
///
/// TODO: Eventually, once all passes have migrated away from setting branch
/// weights to indicate estimated trip counts, this function will drop the
/// \p EstimatedLoopInvocationWeight parameter.
LLVM_ABI std::optional<unsigned>
getLoopEstimatedTripCount(Loop *L,
unsigned *EstimatedLoopInvocationWeight = nullptr);

/// Set a loop's branch weight metadata to reflect that loop has \p
/// EstimatedTripCount iterations and \p EstimatedLoopInvocationWeight exits
/// through latch. Returns true if metadata is successfully updated, false
/// otherwise. Note that loop must have a latch block which controls loop exit
/// in order to succeed.
LLVM_ABI bool setLoopEstimatedTripCount(Loop *L, unsigned EstimatedTripCount,
unsigned EstimatedLoopInvocationWeight);
unsigned *EstimatedLoopInvocationWeight = nullptr,
bool DbgForInit = false);

/// Set \c llvm.loop.estimated_trip_count with the value \c *EstimatedTripCount
/// in the loop metadata of \p L, or set it without a value if
/// \c !EstimatedTripCount to indicate that \c getLoopEstimatedTripCount cannot
/// estimate the trip count from latch branch weights. If
/// \c !EstimatedTripCount but \c getLoopEstimatedTripCount can estimate the
/// trip counts, future calls to \c getLoopEstimatedTripCount will diagnose the
/// metadata as corrupt.
///
/// In addition, if \p EstimatedLoopInvocationWeight, set the branch weight
/// metadata of \p L to reflect that \p L has an estimated
/// \c *EstimatedTripCount iterations and has \c *EstimatedLoopInvocationWeight
/// exit weight through the loop's latch.
///
/// Return false if \c llvm.loop.estimated_trip_count was already set according
/// to \p EstimatedTripCount and so was not updated. Return false if
/// \p EstimatedLoopInvocationWeight and if branch weight metadata could not be
/// successfully updated (e.g., if \p L does not have a latch block that
/// controls the loop exit). Otherwise, return true.
///
/// TODO: Eventually, once all passes have migrated away from setting branch
/// weights to indicate estimated trip counts, this function will drop the
/// \p EstimatedLoopInvocationWeight parameter.
LLVM_ABI bool setLoopEstimatedTripCount(
Loop *L, std::optional<unsigned> EstimatedTripCount,
std::optional<unsigned> EstimatedLoopInvocationWeight = std::nullopt);

/// Check inner loop (L) backedge count is known to be invariant on all
/// iterations of its outer loop. If the loop has no parent, this is trivially
Expand Down
10 changes: 7 additions & 3 deletions llvm/lib/Analysis/LoopInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1111,9 +1111,13 @@ bool llvm::getBooleanLoopAttribute(const Loop *TheLoop, StringRef Name) {
}

std::optional<int> llvm::getOptionalIntLoopAttribute(const Loop *TheLoop,
StringRef Name) {
const MDOperand *AttrMD =
findStringMetadataForLoop(TheLoop, Name).value_or(nullptr);
StringRef Name,
bool *Missing) {
std::optional<const MDOperand *> AttrMDOpt =
findStringMetadataForLoop(TheLoop, Name);
if (Missing)
*Missing = !AttrMDOpt;
const MDOperand *AttrMD = AttrMDOpt.value_or(nullptr);
if (!AttrMD)
return std::nullopt;

Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Passes/PassBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@
#include "llvm/Transforms/Instrumentation/NumericalStabilitySanitizer.h"
#include "llvm/Transforms/Instrumentation/PGOCtxProfFlattening.h"
#include "llvm/Transforms/Instrumentation/PGOCtxProfLowering.h"
#include "llvm/Transforms/Instrumentation/PGOEstimateTripCounts.h"
#include "llvm/Transforms/Instrumentation/PGOForceFunctionAttrs.h"
#include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"
#include "llvm/Transforms/Instrumentation/RealtimeSanitizer.h"
Expand Down
8 changes: 6 additions & 2 deletions llvm/lib/Passes/PassBuilderPipelines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
#include "llvm/Transforms/Instrumentation/MemProfUse.h"
#include "llvm/Transforms/Instrumentation/PGOCtxProfFlattening.h"
#include "llvm/Transforms/Instrumentation/PGOCtxProfLowering.h"
#include "llvm/Transforms/Instrumentation/PGOEstimateTripCounts.h"
#include "llvm/Transforms/Instrumentation/PGOForceFunctionAttrs.h"
#include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"
#include "llvm/Transforms/Scalar/ADCE.h"
Expand Down Expand Up @@ -1239,6 +1240,7 @@ PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
MPM.addPass(AssignGUIDPass());
if (IsCtxProfUse) {
MPM.addPass(PGOCtxProfFlatteningPass(/*IsPreThinlink=*/true));
MPM.addPass(PGOEstimateTripCountsPass());
return MPM;
}
// Block further inlining in the instrumented ctxprof case. This avoids
Expand Down Expand Up @@ -1268,8 +1270,10 @@ PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
MPM.addPass(MemProfUsePass(PGOOpt->MemoryProfile, PGOOpt->FS));

if (PGOOpt && (PGOOpt->Action == PGOOptions::IRUse ||
PGOOpt->Action == PGOOptions::SampleUse))
PGOOpt->Action == PGOOptions::SampleUse)) {
MPM.addPass(PGOForceFunctionAttrsPass(PGOOpt->ColdOptType));
}
MPM.addPass(PGOEstimateTripCountsPass());

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

Expand Down Expand Up @@ -2355,4 +2359,4 @@ AAManager PassBuilder::buildDefaultAAPipeline() {
bool PassBuilder::isInstrumentedPGOUse() const {
return (PGOOpt && PGOOpt->Action == PGOOptions::IRUse) ||
!UseCtxProfile.empty();
}
}
1 change: 1 addition & 0 deletions llvm/lib/Passes/PassRegistry.def
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ MODULE_PASS("openmp-opt", OpenMPOptPass())
MODULE_PASS("openmp-opt-postlink",
OpenMPOptPass(ThinOrFullLTOPhase::FullLTOPostLink))
MODULE_PASS("partial-inliner", PartialInlinerPass())
MODULE_PASS("pgo-estimate-trip-counts", PGOEstimateTripCountsPass())
MODULE_PASS("pgo-icall-prom", PGOIndirectCallPromotion())
MODULE_PASS("pgo-instr-gen", PGOInstrumentationGen())
MODULE_PASS("pgo-instr-use", PGOInstrumentationUse())
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Transforms/Instrumentation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ add_llvm_component_library(LLVMInstrumentation
LowerAllowCheckPass.cpp
PGOCtxProfFlattening.cpp
PGOCtxProfLowering.cpp
PGOEstimateTripCounts.cpp
PGOForceFunctionAttrs.cpp
PGOInstrumentation.cpp
PGOMemOPSizeOpt.cpp
Expand Down
45 changes: 45 additions & 0 deletions llvm/lib/Transforms/Instrumentation/PGOEstimateTripCounts.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/Transforms/Instrumentation/PGOEstimateTripCounts.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/IR/Module.h"
#include "llvm/Transforms/Utils/LoopUtils.h"

using namespace llvm;

#define DEBUG_TYPE "pgo-estimate-trip-counts"

static bool runOnLoop(Loop *L) {
bool MadeChange = false;
std::optional<unsigned> TC = getLoopEstimatedTripCount(
L, /*EstimatedLoopInvocationWeight=*/nullptr, /*DbgForInit=*/true);
MadeChange |= setLoopEstimatedTripCount(L, TC);
for (Loop *SL : *L)
MadeChange |= runOnLoop(SL);
return MadeChange;
}

PreservedAnalyses PGOEstimateTripCountsPass::run(Module &M,
ModuleAnalysisManager &AM) {
FunctionAnalysisManager &FAM =
AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
bool MadeChange = false;
LLVM_DEBUG(dbgs() << DEBUG_TYPE << ": start\n");
for (Function &F : M) {
if (F.isDeclaration())
continue;
LoopInfo *LI = &FAM.getResult<LoopAnalysis>(F);
if (!LI)
continue;
for (Loop *L : *LI)
MadeChange |= runOnLoop(L);
}
LLVM_DEBUG(dbgs() << DEBUG_TYPE << ": end\n");
return MadeChange ? PreservedAnalyses::none() : PreservedAnalyses::all();
}
Loading
Loading