[llvm] [DA] Add tests for nsw doesn't hold on entier iteration (PR #162281)

Ehsan Amiri via llvm-commits llvm-commits at lists.llvm.org
Sat Nov 8 06:18:19 PST 2025


amehsan wrote:


> It looks to me that you are conflating "what dependence testing functions assume" with "when the result should be correct". It would be true that it’s sufficient for the result to be correct only when it is actually executed, but I’m talking about the preconditions which the testing functions rely on. For example, as for an addrec `{c,+,a}` (`a*i + c`), many testing functions (perhaps implicitly) assume that it takes its maximum value at the last iteration (`i = BTC`) if `a` is non-negative. This assumption holds when the addrec is monotonic over the entire iteration space. However, as the example I gave shows, this is not necessarily the case when some loop guards exist.

It is not clear to me where we make this assumption and what are the consequences of that. But before discussing that I have another question: I believe you are talking about this example from your testcase:
```
; for (i = 0; i < INT64_MAX - 1; i++)
;   if (i < 1000)
;     for (j = 0; j < 2000; j++)
;       a[i + j] = 0;
```

and you are saying for DA to be correct we need to check that a loop guard exists (or may be check what the loop guard is) and bail out. Now I can change your example to the following:

```
for (i = 0; i < INT64_MAX - 1; i++)
   if (i < 1000) {
     for (k = 0;  k < 5000; k++) {
           // do something in the loop
      }
     // some complex code possibly involving control flow 
     for (j = 0; j < 2000; j++)   
       a[i + j] = 0;
     }
  } // if (i < 1000)
```

Your comment is applicable to this example as well. Here `j` loop has no guard. How do you want to handle this case? Checking the guard doesn't seem to work here. Also please answer the same question about the following example


```
for (i = 0; i < INT64_MAX - 1; i++)
   
     for (k = 0;  k < 5000; k++) {
           if (i > 1000) goto X; // X is a label outside the loopnest.
      }
     // some complex code possibly involving control flow 
     for (j = 0; j < 2000; j++)   
       a[i + j] = 0;
     }
  } 
```


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


More information about the llvm-commits mailing list