[llvm] [PGO] Add `llvm.loop.estimated_trip_count` metadata (PR #148758)

Joel E. Denny via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 24 08:00:05 PDT 2025


================
@@ -316,28 +318,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
----------------
jdenny-ornl wrote:

> should getLoopEstimatedTripCount just return what's in the metadata, and this recalculation be handled separately, with an update of the metadata?

Possibly, but not yet.  See the new LangRef.rst entry's "Missing Metadata and Values" section.

Let's converge on this issue before I address your other comments, many of which seem to stem from it.

https://github.com/llvm/llvm-project/pull/148758


More information about the llvm-commits mailing list