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

    <tr>
        <th>Summary</th>
        <td>
            [flang][openmp] programs with offloading to amdgpu segfault
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            flang
      </td>
    </tr>

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

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

<pre>
    Any program with OpenMP offloading compiled with flang for amdgpu seems to segfault currently. clang works fine, only flang is affected.

A program with just a target region is enough to reproduce:
```
program crash
!$omp target
!$omp end target
end program
```

```
flang -fopenmp --offload-arch=gfx1100 crash.f90 
flang-21: warning: OpenMP support in flang is still experimental [-Wexperimental-option]
flang-21: warning: OpenMP support in flang is still experimental [-Wexperimental-option]
flang-21: warning: OpenMP support in flang is still experimental [-Wexperimental-option]
$ ./a.out 
Segmentation fault
```
Stacktrace from gdb shows:
```
#0  0x00007ffff3d4fef5 in llvm::omp::target::plugin::AMDGPUKernelTy::launchImpl(llvm::omp::target::plugin::GenericDeviceTy&, unsigned int*, unsigned int*, KernelArgsTy&, KernelLaunchParamsTy, llvm::omp::target::plugin::AsyncInfoWrapperTy&) const
    () from /home/acc/install_llvm2/lib/x86_64-unknown-linux-gnu/libomptarget.so.21.0git
#1 0x00007ffff3d143a5 in llvm::omp::target::plugin::GenericKernelTy::launch(llvm::omp::target::plugin::GenericDeviceTy&, void**, long*, KernelArgsTy&, llvm::omp::target::plugin::AsyncInfoWrapperTy&) const ()
   from /home/acc/install_llvm2/lib/x86_64-unknown-linux-gnu/libomptarget.so.21.0git
#2 0x00007ffff3d1ae04 in llvm::omp::target::plugin::GenericDeviceTy::launchKernel(void*, void**, long*, KernelArgsTy&, __tgt_async_info*) () from /home/acc/install_llvm2/lib/x86_64-unknown-linux-gnu/libomptarget.so.21.0git
#3 0x00007ffff3d1e092 in llvm::omp::target::plugin::GenericPluginTy::launch_kernel(int, void*, void**, long*, KernelArgsTy*, __tgt_async_info*) () from /home/acc/install_llvm2/lib/x86_64-unknown-linux-gnu/libomptarget.so.21.0git
#4 0x00007ffff3becd17 in DeviceTy::launchKernel(void*, void**, long*, KernelArgsTy&, AsyncInfoTy&) () from /home/acc/install_llvm2/lib/x86_64-unknown-linux-gnu/libomptarget.so.21.0git
#5 0x00007ffff3bf847c in target(ident_t*, DeviceTy&, void*, KernelArgsTy&, AsyncInfoTy&) () from /home/acc/install_llvm2/lib/x86_64-unknown-linux-gnu/libomptarget.so.21.0git
#6 0x00007ffff3beff59 in __tgt_target_kernel () from /home/acc/install_llvm2/lib/x86_64-unknown-linux-gnu/libomptarget.so.21.0git
#7 0x000055555555675c in _QQmain ()
#8  0x0000555555556782 in main ()
```

Tested at f7f5aa217a81f2ec036fee765124bd2057531d86.

Machine specs: Ubuntu 24.04.1, rocm 6.3.0, Ryzen 9 9900X, RX 7900 GRE.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzcV91v2zgM_2uUFyGGLH8_5CFblmG4K677uu0tkGXK1ipLhiS3zf31B9nuR7IrcB12xXBFgcg0SZO_HymQzDnZaoANyl6hbLdio--M3fwJ8OZiVZvmuNnqIx6saS3r8Y30Hf5jAH1xiY0QyrBG6hZz0w9SQTO_F4rpFgtjMeubdhixA-gd9gY7aAUblcd8tBa0V8cI80n7xtgrh4XUgOhrbLQ6Lm6kw0wI4B6aCJEtItvtaTTfRucxw57ZFjy20EqjgxVoM7Zd-KqFwZpm5ICSYI9ysvyT7Z0nbpnrwjsaI5qaflj8nYhANw_i8LBYn_k8e5zTWAszgO4HvF4vuK2Z5R1Kdq24jWNC5hAiURF8Z7SmMUq2-IZZLXUbjgvybhwGYz2W-gEk56VSGG4HsLIH7ZnCKHu1_vJYsjaDl0ajbPe_-QSiKY4Q3bPIjD4g9xHaSS2o4KnYzvj46Bm_8pZxwMKaHrdNjV1nbtz31YFoQjAmt4QQUgghR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