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

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 10 03:18:34 PDT 2024


================
@@ -338,8 +340,11 @@ CacheCostTy IndexedReference::computeRefCost(const Loop &L,
   assert(RefCost && "Expecting a valid RefCost");
 
   // Attempt to fold RefCost into a constant.
+  // CacheCostTy is a signed integer, but the tripcount value can be large
+  // and may not fit, so saturate/limit the value to the maximum signed
+  // integer value.
   if (auto ConstantCost = dyn_cast<SCEVConstant>(RefCost))
-    return ConstantCost->getValue()->getZExtValue();
+    return (CacheCostTy)ConstantCost->getValue()->getLimitedValue(~0ULL >> 1);
----------------
fhahn wrote:

If `CacheCostTy` is a regular integer type, using numeric_limits may be clearer?

```suggestion
    return (CacheCostTy)ConstantCost->getValue()->getLimitedValue(std::numeric_limits<CacheCostTy>::max());
```

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


More information about the llvm-commits mailing list