[llvm] [LoopInterchange] Motivating example for interchange. NFC. (PR #171631)
Ryotaro Kasuga via llvm-commits
llvm-commits at lists.llvm.org
Thu Dec 11 07:51:11 PST 2025
kasuga-fj wrote:
> Please explain why it is not enough "emitting array size information from frontend" as metadata with "the semantics of the original language, e.g., out-of-bounds array access is UB in C/C++/Fortran".
Consider cases where the result of delinearization is not uniquely determined. For example, in the following case:
```c
int A[16][8];
e = ...; // Maybe negative
for (int i = 0; i < N; i++) {
// We can infer `N < 8` as `16 <= 2*i` is UB
use A[2*i][i + e]
}
```
The SCEV for the access `A[2*i][i + e]` will be something like `{(%e + %A),+,17}`. In this case, we cannot determine which of `%A[2 * %i][%i + %e]` or `%A[%i][11 * %i + %e]` is "correct" even though we know the size of the array `A`. So we cannot infer `%N < 8`.
Another example is:
```c
int A[16][4];
for (int i = 0; i < N; i++)
if (i % 4 == 0)
use A[i / 4][0];
```
This can be lowered to the following LLVM:
```llvm
loop.header:
%i = ...
%rem = and i32 %i, 3
%cond = icmp eq i32 %rem, 0
br i1 %cond, label %if.then, ...
if.then:
%gep = getelementptr i32, ptr %A, i32 %i
...
```
In this case, the SCEV for `%gep` will be something like `{%A,+,1}`. So delinearization will yield `%A[0][%i]`, but `%i < 4` doesn't hold.
Even if we somehow know the size of the array, I don't think we can unconditionally justify the delinearization result.
https://github.com/llvm/llvm-project/pull/171631
More information about the llvm-commits
mailing list