@@ -316,37 +316,73 @@ LLVM_ABI TransformationMode hasDistributeTransformation(const Loop *L);
316
316
LLVM_ABI TransformationMode hasLICMVersioningTransformation (const Loop *L);
317
317
// / @}
318
318
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
326
- // / llvm.loop.estimated_trip_count metadata or, if none, branch weight metadata.
327
- // / In addition if \p EstimatedLoopInvocationWeight is not null it is
328
- // / initialized with weight of loop's latch leading to the exit.
329
- // / Returns a valid positive trip count, saturated at UINT_MAX, or std::nullopt
330
- // / when a meaningful estimate cannot be made.
319
+ // / Set the string \p MDString into the loop metadata of \p TheLoop while
320
+ // / keeping other loop metadata intact. Set \p *V as its value, or set it
321
+ // / without a value if \p V is \c std::nullopt to indicate the value is unknown.
322
+ // / If \p MDString is already in the loop metadata, update it if its value (or
323
+ // / lack of value) is different. Return true if metadata was changed.
324
+ LLVM_ABI bool addStringMetadataToLoop (Loop *TheLoop, const char *MDString,
325
+ std::optional<unsigned > V = 0 );
326
+
327
+ // / Return either:
328
+ // / - The value of \c llvm.loop.estimated_trip_count from the loop metadata of
329
+ // / \p L, if that metadata is present and has a value.
330
+ // / - Else, a new estimate of the trip count from the latch branch weights of
331
+ // / \p L, if the estimation's implementation is able to handle the loop form
332
+ // / of \p L (e.g., \p L must have a latch block that controls the loop exit).
333
+ // / - Else, \c std::nullopt.
334
+ // /
335
+ // / An estimated trip count is always a valid positive trip count, saturated at
336
+ // / \c UINT_MAX.
337
+ // /
338
+ // / Via \c LLVM_DEBUG, emit diagnostics that include "WARNING" when the metadata
339
+ // / is in an unexpected state as that indicates some transformation has
340
+ // / corrupted it. If \p DbgForInit, expect the metadata to be missing.
341
+ // / Otherwise, expect the metadata to be present, and expect it to have no value
342
+ // / only if the trip count is currently inestimable from the latch branch
343
+ // / weights.
344
+ // /
345
+ // / In addition, if \p EstimatedLoopInvocationWeight, then either:
346
+ // / - Set \p *EstimatedLoopInvocationWeight to the weight of the latch's branch
347
+ // / to the loop exit.
348
+ // / - Do not set it and return \c std::nullopt if the current implementation
349
+ // / cannot compute that weight (e.g., if \p L does not have a latch block that
350
+ // / controls the loop exit) or the weight is zero (because zero cannot be
351
+ // / used to compute new branch weights that reflect the estimated trip count).
352
+ // /
353
+ // / TODO: Eventually, once all passes have migrated away from setting branch
354
+ // / weights to indicate estimated trip counts, this function will drop the
355
+ // / \p EstimatedLoopInvocationWeight parameter.
331
356
LLVM_ABI std::optional<unsigned >
332
357
getLoopEstimatedTripCount (Loop *L,
333
- unsigned *EstimatedLoopInvocationWeight = nullptr );
334
-
335
- // / Set a loop's llvm.loop.estimated_trip_count metadata and, if \p
336
- // / EstimatedLoopInvocationWeight, branch weight metadata to reflect that loop
337
- // / has \p EstimatedTripCount iterations and \p EstimatedLoopInvocationWeight
338
- // / exit weight through latch. Returns true if metadata is successfully updated,
339
- // / false otherwise. Note that loop must have a latch block which controls loop
340
- // / exit in order to succeed.
358
+ unsigned *EstimatedLoopInvocationWeight = nullptr ,
359
+ bool DbgForInit = false );
360
+
361
+ // / Set \c llvm.loop.estimated_trip_count with the value \c *EstimatedTripCount
362
+ // / in the loop metadata of \p L, or set it without a value if
363
+ // / \c !EstimatedTripCount to indicate that \c getLoopEstimatedTripCount cannot
364
+ // / estimate the trip count from latch branch weights. If
365
+ // / \c !EstimatedTripCount but \c getLoopEstimatedTripCount can estimate the
366
+ // / trip counts, future calls to \c getLoopEstimatedTripCount will diagnose the
367
+ // / metadata as corrupt.
368
+ // /
369
+ // / In addition, if \p EstimatedLoopInvocationWeight, set the branch weight
370
+ // / metadata of \p L to reflect that \p L has an estimated
371
+ // / \c *EstimatedTripCount iterations and has \c *EstimatedLoopInvocationWeight
372
+ // / exit weight through the loop's latch.
373
+ // /
374
+ // / Return false if \c llvm.loop.estimated_trip_count was already set according
375
+ // / to \p EstimatedTripCount and so was not updated. Return false if
376
+ // / \p EstimatedLoopInvocationWeight and if branch weight metadata could not be
377
+ // / successfully updated (e.g., if \p L does not have a latch block that
378
+ // / controls the loop exit). Otherwise, return true.
341
379
// /
342
- // / The use case for not setting branch weight metadata is when the original
343
- // / branch weight metadata is correct for computing block frequencies but the
344
- // / trip count has changed due to a loop transformation. The branch weight
345
- // / metadata cannot be adjusted to reflect the new trip count, so we store the
346
- // / new trip count separately.
380
+ // / TODO: Eventually, once all passes have migrated away from setting branch
381
+ // / weights to indicate estimated trip counts, this function will drop the
382
+ // / \p EstimatedLoopInvocationWeight parameter.
347
383
LLVM_ABI bool setLoopEstimatedTripCount (
348
- Loop *L, unsigned EstimatedTripCount,
349
- std::optional<unsigned > EstimatedLoopInvocationWeight);
384
+ Loop *L, std::optional< unsigned > EstimatedTripCount,
385
+ std::optional<unsigned > EstimatedLoopInvocationWeight = std:: nullopt );
350
386
351
387
// / Check inner loop (L) backedge count is known to be invariant on all
352
388
// / iterations of its outer loop. If the loop has no parent, this is trivially
0 commit comments