[llvm] [LoopInterchange] Fix overflow in cost calculation (PR #111807)

Madhur Amilkanthwar via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 10 03:26:09 PDT 2024


================
@@ -712,7 +717,12 @@ CacheCost::computeLoopCacheCost(const Loop &L,
   CacheCostTy LoopCost = 0;
   for (const ReferenceGroupTy &RG : RefGroups) {
     CacheCostTy RefGroupCost = computeRefGroupCacheCost(RG, L);
-    LoopCost += RefGroupCost * TripCountsProduct;
+
+    // Saturate the cost to INT MAX if the value can overflow.
+    if (RefGroupCost > (std::numeric_limits<int64_t>::max() / TripCountsProduct))
+      LoopCost = std::numeric_limits<int64_t>::max();
+    else
+      LoopCost += RefGroupCost * TripCountsProduct;
----------------
madhur13490 wrote:

Could use ternary operator to simplify

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


More information about the llvm-commits mailing list