[llvm] [LoopInterchange] Avoid overflow in the memory-instruction ratio check (PR #214920)

via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 17 12:23:32 PDT 2026


https://github.com/MattPD updated https://github.com/llvm/llvm-project/pull/214920

>From ea8bc613c617760c4b2ae8065bb2b30c4b7e4b5d Mon Sep 17 00:00:00 2001
From: "Matt P. Dziubinski" <matt-p.dziubinski at hpe.com>
Date: Thu, 17 Sep 2026 04:45:53 -0500
Subject: [PATCH] [LoopInterchange] Avoid overflow in the memory-instruction
 ratio check

populateDependencyMatrix bails out if MaxMemInstrRatio * NumInsts is
less than NumMemInstr * NumMemInstr. Both products are computed in
32-bit unsigned arithmetic and can wrap, e.g., with
-loop-interchange-max-mem-instr-ratio=2147483648 and 20 instructions
the left product wraps to 0 and the pass rejects an otherwise eligible
nest.

Compute both products in uint64_t instead, which cannot overflow as all
three operands are 32 bits wide.

Assisted-by: Claude Opus 5, GPT-5.6 Sol, GPT-6 Astra, Claude Fable 5.1.
---
 .../lib/Transforms/Scalar/LoopInterchange.cpp |  4 +-
 .../LoopInterchange/memory-instr-ratio.ll     | 48 +++++++++++++++++++
 2 files changed, 51 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
index 48a5ad97871b6..12708deda5096 100644
--- a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
@@ -48,6 +48,7 @@
 #include "llvm/Transforms/Utils/Local.h"
 #include "llvm/Transforms/Utils/LoopUtils.h"
 #include <cassert>
+#include <cstdint>
 #include <utility>
 #include <vector>
 
@@ -203,7 +204,8 @@ static bool populateDependencyMatrix(CharMatrix &DepMatrix, unsigned Level,
   unsigned NumMemInstr = MemInstr.size();
   LLVM_DEBUG(dbgs() << "Found " << NumMemInstr
                     << " Loads and Stores to analyze\n");
-  if (MaxMemInstrRatio * NumInsts < NumMemInstr * NumMemInstr) {
+  if (static_cast<uint64_t>(MaxMemInstrRatio) * NumInsts <
+      static_cast<uint64_t>(NumMemInstr) * NumMemInstr) {
     ORE->emit([&]() {
       return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedLoop",
                                       L->getStartLoc(), L->getHeader())
diff --git a/llvm/test/Transforms/LoopInterchange/memory-instr-ratio.ll b/llvm/test/Transforms/LoopInterchange/memory-instr-ratio.ll
index 7f76f25612d03..cadcd2372fde3 100644
--- a/llvm/test/Transforms/LoopInterchange/memory-instr-ratio.ll
+++ b/llvm/test/Transforms/LoopInterchange/memory-instr-ratio.ll
@@ -4,6 +4,14 @@
 ; RUN: opt < %s -passes=loop-interchange -S -loop-interchange-profitabilities=ignore \
 ; RUN:          -loop-interchange-max-mem-instr-ratio=100 | FileCheck %s --check-prefixes=CHECK,CHECK-RATIO-100
 
+; The loop nest has 20 instructions and 10 stores. With 32-bit arithmetic,
+; 2147483648 * 20 wraps to zero, so the ratio guard rejects this otherwise
+; eligible nest. The guard bounds analysis cost, not legality. If the body
+; changes, keep the instruction count even, or choose a ratio whose 32-bit
+; product with the instruction count wraps below the squared load/store count.
+; RUN: opt < %s -passes=loop-interchange -S -loop-interchange-profitabilities=ignore \
+; RUN:          -loop-interchange-max-mem-instr-ratio=2147483648 | FileCheck %s --check-prefixes=CHECK,CHECK-RATIO-2147483648
+
 define void @f(ptr noalias %A) {
 ; CHECK-RATIO-1-LABEL: define void @f(
 ; CHECK-RATIO-1-SAME: ptr noalias [[A:%.*]]) {
@@ -75,6 +83,46 @@ define void @f(ptr noalias %A) {
 ; CHECK-RATIO-100:       [[EXIT]]:
 ; CHECK-RATIO-100-NEXT:    ret void
 ;
+; CHECK-RATIO-2147483648-LABEL: define void @f(
+; CHECK-RATIO-2147483648-SAME: ptr noalias [[A:%.*]]) {
+; CHECK-RATIO-2147483648-NEXT:  [[ENTRY:.*:]]
+; CHECK-RATIO-2147483648-NEXT:    br label %[[LOOP_J_PREHEADER:.*]]
+; CHECK-RATIO-2147483648:       [[LOOP_I_HEADER_PREHEADER:.*]]:
+; CHECK-RATIO-2147483648-NEXT:    br label %[[LOOP_I_HEADER:.*]]
+; CHECK-RATIO-2147483648:       [[LOOP_I_HEADER]]:
+; CHECK-RATIO-2147483648-NEXT:    [[I:%.*]] = phi i64 [ [[I_INC:%.*]], %[[LOOP_I_LATCH:.*]] ], [ 0, %[[LOOP_I_HEADER_PREHEADER]] ]
+; CHECK-RATIO-2147483648-NEXT:    br label %[[LOOP_J_SPLIT1:.*]]
+; CHECK-RATIO-2147483648:       [[LOOP_J_PREHEADER]]:
+; CHECK-RATIO-2147483648-NEXT:    br label %[[LOOP_J:.*]]
+; CHECK-RATIO-2147483648:       [[LOOP_J]]:
+; CHECK-RATIO-2147483648-NEXT:    [[J:%.*]] = phi i64 [ [[TMP0:%.*]], %[[LOOP_J_SPLIT:.*]] ], [ 0, %[[LOOP_J_PREHEADER]] ]
+; CHECK-RATIO-2147483648-NEXT:    br label %[[LOOP_I_HEADER_PREHEADER]]
+; CHECK-RATIO-2147483648:       [[LOOP_J_SPLIT1]]:
+; CHECK-RATIO-2147483648-NEXT:    [[GEP:%.*]] = getelementptr [10 x i8], ptr [[A]], i64 [[J]], i64 [[I]]
+; CHECK-RATIO-2147483648-NEXT:    store i8 0, ptr [[GEP]], align 1
+; CHECK-RATIO-2147483648-NEXT:    store i8 0, ptr [[GEP]], align 1
+; CHECK-RATIO-2147483648-NEXT:    store i8 0, ptr [[GEP]], align 1
+; CHECK-RATIO-2147483648-NEXT:    store i8 0, ptr [[GEP]], align 1
+; CHECK-RATIO-2147483648-NEXT:    store i8 0, ptr [[GEP]], align 1
+; CHECK-RATIO-2147483648-NEXT:    store i8 0, ptr [[GEP]], align 1
+; CHECK-RATIO-2147483648-NEXT:    store i8 0, ptr [[GEP]], align 1
+; CHECK-RATIO-2147483648-NEXT:    store i8 0, ptr [[GEP]], align 1
+; CHECK-RATIO-2147483648-NEXT:    store i8 0, ptr [[GEP]], align 1
+; CHECK-RATIO-2147483648-NEXT:    store i8 0, ptr [[GEP]], align 1
+; CHECK-RATIO-2147483648-NEXT:    [[J_INC:%.*]] = add i64 [[J]], 1
+; CHECK-RATIO-2147483648-NEXT:    [[EC_J:%.*]] = icmp eq i64 [[J_INC]], 10
+; CHECK-RATIO-2147483648-NEXT:    br label %[[LOOP_I_LATCH]]
+; CHECK-RATIO-2147483648:       [[LOOP_J_SPLIT]]:
+; CHECK-RATIO-2147483648-NEXT:    [[TMP0]] = add i64 [[J]], 1
+; CHECK-RATIO-2147483648-NEXT:    [[TMP1:%.*]] = icmp eq i64 [[TMP0]], 10
+; CHECK-RATIO-2147483648-NEXT:    br i1 [[TMP1]], label %[[EXIT:.*]], label %[[LOOP_J]]
+; CHECK-RATIO-2147483648:       [[LOOP_I_LATCH]]:
+; CHECK-RATIO-2147483648-NEXT:    [[I_INC]] = add i64 [[I]], 1
+; CHECK-RATIO-2147483648-NEXT:    [[EC_I:%.*]] = icmp eq i64 [[I_INC]], 10
+; CHECK-RATIO-2147483648-NEXT:    br i1 [[EC_I]], label %[[LOOP_J_SPLIT]], label %[[LOOP_I_HEADER]]
+; CHECK-RATIO-2147483648:       [[EXIT]]:
+; CHECK-RATIO-2147483648-NEXT:    ret void
+;
 entry:
   br label %loop.i.header
 



More information about the llvm-commits mailing list