[llvm] [unroll-and-jam] Document dependencies_multidims.ll and fix loop bounds (NFC) (PR #156578)

Michael Kruse via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 6 08:04:14 PDT 2025


================
@@ -47,6 +53,14 @@ cleanup:
 ; CHECK: %j.1 = phi
 ; CHECK: %j.2 = phi
 ; CHECK: %j.3 = phi
+;
+; sub_sub_eq SHOULD be unroll-and-jammed (count=4) as it's safe.
+; Memory accesses:
+;   - A[i][j] = 1      (write to current iteration)
+;   - A[i+1][j] = add  (write to next i iteration, same j iteration)
+; No dependency conflict: A[i+1][j] from iteration (i,j) doesn't conflict with
+; any A[i'][j'] from unrolled j iterations since j' values are different and
+; i+1 from current doesn't overlap with i' from unrolled iterations.
----------------
Meinersbur wrote:

```c
sum = 0;
for (int i = 0; i < N; ++i) // N > 0
  for (int j = 0; j < N; ++j) 
    sum += i * B[j];
    A[i][j] = 1;
    A[i+1][j] = sum;
```
> unrolled j iterations

The i (outer loop) iterations are unrolled with unroll-and-jam, not the j iterations.

> since j' values are different and i+1 from current doesn't overlap with i' from unrolled iteration

`A[i+1][j]` and `A[i'][j]` with `i'==i+1` (next iteration that will be in the same body ofter unroll-and-jam) do point to the same memory, so there seems to be a hazard between the iterations. It is just there is no iteration from [i,j+1] to [i,N-1], and [i+1,0] to [i+1,j-1]  (iterations that execute between [i,j] and [i',j] in the original loop) do access `A[i+1][j]` or `A[i+2][j]`. Maybe this is saying this, but I have difficulty understanding this explanation.

Factor-2 unroll:
```c
sum0 = 0;
sum1 = 0;
for (int i = 0; i < N; i+=2)
  for (int j = 0; j < N; ++j) 
    sum0 += i * B[j];
    A[i][j] = 1;
    A[i + 1][j] = sum0;
    
    sum1 += (i+1) * B[j];
    A[i+1][j] = 1;
    A[i+2][j] = sum1;
```

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


More information about the llvm-commits mailing list