<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/55970>55970</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Miscompile of collapse(3) with non-rectangular loop nest
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            openmp
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          mikerice1969
      </td>
    </tr>
</table>

<pre>
    Small test shows serial code and parallel for code for comparison.  Works okay if collapse(2) is used.


```
extern "C" int printf(const char *, ...);        
                                                 
#define N 3                                      
static int dat[N][N][N];                         
static int datref[N][N][N];                      
                                                 
inline int get_index(int i, int j, int k) {      
  return k + N * (j + N * i);                    
}                                                
                                                 
void print(const char* msg, int arr[N][N][N]) {  
  printf("%s\n", msg);                           
  for (unsigned int i = 0; i < N; ++i) {         
    for (unsigned int j = 0; j < N; ++j) {       
      for (unsigned int k = 0; k < N; ++k) {     
        printf("%2d ", arr[i][j][k]);            
      }                                          
      printf("\n");                              
    }                                            
    printf("\n");                                
  }                                              
}                                                
                                                 
void foo(unsigned n) {                           
                                                 
  for (unsigned int i = 0; i < n; ++i) {         
    for (unsigned int j = i; j < n; ++j) {       
      for (unsigned int k = j; k < n; ++k) {     
        datref[i][j][k] = get_index(i, j, k) + 1;
      }                                          
    }                                            
  }                                              
  print("expected:", datref);                    
                                                 
#pragma omp parallel for collapse(3)             
  for (unsigned int i = 0; i < n; ++i)           
    for (unsigned int j = i; j < n; ++j)         
      for (unsigned int k = j; k < n; ++k)       
        dat[i][j][k] = get_index(i, j, k) + 1;   
                                                 
  print("seen:", dat);                           
}                                                
                                                 
int main()                                       
{                                                
  foo(N);                                        
  return 0;                                      
}
```

```
expected:
 1  2  3 
 0  5  6 
 0  0  9 
         
 0  0  0 
 0 14 15 
 0  0 18 
         
 0  0  0 
 0  0  0 
 0  0 27 
         
seen:    
 1  0  0 
 0  5  0 
 0  0  9 
         
 0  0  0 
 0 14  0 
 0  0 18 
         
 0  0  0 
 0  0  0 
 0  0 27 

```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzFV0uTokgQ_jV4yRiChygcOHRr7G37soc9bpSQaGlZRVQVMz3_fpOHIshMK92xa6BUdnV-mfXlg2Sn8p_pX2cmBFg0FsxB_TBgUHMmIFM5ApM5lEzTf6CAQun2r-3iTBvcKOkC_K30yYA6sZ_AC9oSgpUGnSAOnCABbqAymLuOt3W8l8HvyuuuRsR3i1qCEwQb-gKXFkpNvwUhZUqSh9mBadp_cYINuK5L6E74Ct2nBYFnP50rQZhjwSXCG4TPKBrLLM8aZ3Nmnej1zYm2o1vv40MgGovncD51ci5Ffeza9h7tP1zm-E6E1zKvea4Xx8viVAfUWb8O7Wq0FQXuRKF5Jf4oQPSNjzciH4bq3gtnvZ3n_sxTf1c8b7NrkFy1r2ezvxyXaT0diQsLFx-ueUqJ6wSRcaKNbJabFu5Xhx8dpa4sAqmk4XuJeeMDByfcglcD1MsNvNVLopYuPgjHgJMpqGMPdRxDHYdQt-ROQZ16qNMYapAkwyiNeApy6FhqmeYtxcf2dmqZHlF3C_hU1twqDvy4xOqDIA0gnszXXnGm5R7i6Ur5HwusUOo2ceQ4Xb_Y5oMFJD9ZQLwvIPnJAjr2BSQfKqDr4-G-VBrAQQuvC6tp3S0gdWOfjHxBAc3M_pmpC9dGTcWC7yVmFnMnfOlaR0fJBw-Y5wxf1WguKDXbnxnQwDOeha5zTljzO2VzVj5OuT4rG6cYmJWLU0S20878NPxsZAZZYRDlICMefOT-112xZvvMuGy8Tj7WulH9fd_8jSJ0bfjtsefMWLkb7ryHVa_MTg75v5j8-6JuzfoAAdAk3okeQASwuhHpSsaxGOx6vegvwY8Gu378qO69GKyndbscvAHz77SjO-xnDjHS_ZJDjOKxyNMwT8KELSy3AtM_ualf9bhAUMVdy_vB7QGkkt80hY_JfSXo_UwoVYKkF8pFpUV6sLY0TWn-QdeeFKqdS5AkCPH9cvtWanUkDBK5MRUaWkRRsvYWhzQKkjAO42KZrXMMWRyRgDufrTJk2S6PF4LtUJiU-g_VvypRnstmuNoueBp4QeCtfM_zlv7Sc71wt4yjXciWMYYFhs7SQ6pH4dZOuErvFzpt_NlVe0Obghtr-k1mmq6JjS3CZ5U9KJ2e-YlemjP0k1WyaPxPG-f_BWycebA">