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

Ryotaro Kasuga via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 17 06:52:38 PDT 2026


================
@@ -1,8 +1,15 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
 ; RUN: opt < %s -passes=loop-interchange -S -loop-interchange-profitabilities=ignore \
-; RUN:          -loop-interchange-max-mem-instr-ratio=1 | FileCheck %s --check-prefixes=CHECK,CHECK-RATIO-1
+; RUN:          -loop-interchange-max-mem-instr-ratio=1 | FileCheck %s --check-prefixes=CHECK-RATIO-1
 ; 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
+; RUN:          -loop-interchange-max-mem-instr-ratio=100 | FileCheck %s --check-prefixes=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-RATIO-100
----------------
kasuga-fj wrote:

nit: Maybe it's better to retain the existing tests as they are, and use new prefix for the added case. So it will be something like below:

```llvm
; RUN: opt < %s -passes=loop-interchange -S -loop-interchange-profitabilities=ignore \
; RUN:          -loop-interchange-max-mem-instr-ratio=1 | FileCheck %s --check-prefixes=CHECK,CHECK-RATIO-1

; 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

; 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
```

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


More information about the llvm-commits mailing list