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

    <tr>
        <th>Summary</th>
        <td>
            Miscompiling LDS variable in openmp
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    Test case from aomp smoke (teams_nest.c) reproduced inline, 

```
#include <stdio.h>
#include <omp.h>


int main(void) {

  int fail = 0;

  // 
  // Test: num_teams and omp_get_team_num() 
  #pragma omp target
 {
   printf("Num_teams=%d\n", omp_get_num_teams());
 }
  #pragma omp target
 {
    #pragma omp teams
    {
        if (omp_get_team_num() == 0)
        printf("Num_teams=%d\n", omp_get_num_teams());
      #pragma omp distribute
      for (int i=0; i< 10; i++)
        printf("team %d thread %d\n", omp_get_team_num(), omp_get_thread_num());
    }
  }

  return fail;
}
```

Compile with

```
clang -O2 -fopenmp --offload-arch=gfx1010  teams_nest.c -o teams_nest -mllvm -openmp-opt-disable=true
```

Fails with
```
LLVM ERROR: Absolute address LDS variable inconsistent with variable alignment
```

Save temps suggests LDS variables have had addresses allocated before codegen. I guess that's related to the thin-lto work, though I thought that was only in rocm and this is reproducing in trunk.

The LDS lowering makes a very definite "allocating whole code object at a time" assumption which means it's not safe to run repeatedly. The thinlto work was supposed to run it on pieces and then recombine them without running the pass a second time, but it looks like the pass is running a second time and decides to increase the alignment of some already allocated variables, i.e. miscompile them.

This specific error can probably be fixed by not over-aligning any already allocated variables, but it seems the writing is on the wall for the closed world assumption LDS lowering currently relies on.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVk2P2zYQ_TX0ZSBDpvx58GFjZ4EUSQMkQa8LihpJ06VIgaTs-N8XQ8te2222KFBAkEXNm-F7nA9LhUCNRdyKxQch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