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

    <tr>
        <th>Summary</th>
        <td>
            [Clang] OpenMP Target offloading yields linking errors with -finstrument-functions
        </td>
    </tr>

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

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

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

<pre>
    Description
---
When trying to compile a program with OpenMP Target offloading to AMD or NVIDIA GPUs and the flag `-finstrument-functions`, the compiler fails to link both `__cyg_profile_func_enter` and `__cyg_profile_func_exit`. Offloading to `x86_64` works fine. 

Steps to reproduce
---
Source code:
```C
int main(void) {
    int a;
    #pragma omp target 
    {
        a = 0;
    }
    return a;
}
```

Run the following compilation command:
`clang -finstrument-functions -fopenmp -fopenmp-targets=nvptx64 minimal.c`

You should see the error message down below:
```
clang-15: warning: CUDA version is newer than the latest supported version 11.5 [-Wunknown-cuda-version]
nvlink error   : Undefined reference to '__cyg_profile_func_enter' in '/tmp/minimal-bd015f-nvptx64-nvidia-cuda-sm_75-6aca7c.cubin'
nvlink error : Undefined reference to '__cyg_profile_func_exit' in '/tmp/minimal-bd015f-nvptx64-nvidia-cuda-sm_75-6aca7c.cubin'
/home/[...]/opt/clang/bin/clang-linker-wrapper: error: 'nvlink' failed
clang-15: error: linker command failed with exit code 1 (use -v to see invocation)
```

Workaround
---
Add the following code:
```C
#pragma omp declare target
void __cyg_profile_func_enter(void *, void *) __attribute__((no_instrument_function));
void __cyg_profile_func_exit(void *, void *) __attribute__((no_instrument_function));
void __cyg_profile_func_enter(void* fn, void* caller){}
void __cyg_profile_func_exit(void* fn, void* caller){}
#pragma omp end declare target
```

Environment
---
- Fedora Linux 37
- Intel Core i7-1260P, NVIDIA MX550
- clang version 15.0.6 (https://github.com/llvm/llvm-project.git 088f33605d8a61ff519c580a71b1dd57d16a03f8)
- CUDA 12.0

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8Vl1v2zYU_TX0y4UEirI-_OAHJ56GAutarO26PQmUeCVzoUiBpOzk3w-UbOejSdECw4AgJs1L8px7zz0md072GnFLshuS7Vd88gdjt58PDxZXjREP2z261srRS6MJ3RO6i6JoGXw9oAZvH6TuwRtozTBKhcBhtKa3fICT9Af4MKJ-_xE-c9ujB9N1ynBx3rJ7vwdj4fc_3-3f7eDXj18ccC3AHxA6xXsgOY06qZ2304DaR92k2wDEkZwSdjsHnq-10HGpXDhVSX0HjfGHsL-u24e-Hq3ppMI6HFCj9mhJTue73gi5l57kNIYPz_CSnN6XeZ2vw-6TsXcOOqkxhiUhy_9PHscZh8XRGjG1-CJvn8xk2wBcIEnPmwKh-e92mUvtYeBSE1YejRSEbYAUN8saAEBY5yR98g1h6Wh5P3Awwwh-SfeT5ctuQjccSLoH-nx7sX-cWPST1U8uuK5ecT5l_Mekl5oZpcwp5GopCg-1CuOBa_GUaqu47uH10kLUmRH1MF4H0ULGkXSvj6O_z9cwSC0HruL2BZK_zQTuYCYlwCHOoNBaY2FA53iPIMxJQ4PKnL5N_TKdsUVJRtIdnLjVUvdhePtlv4MjWhcoSQcaT2jBH_hCXXGPzoObxtFYj-IamiRxBiS7ib5O-k6bk47aSfDovEyyc171cVbtAhYg3PhFCwzqEmCxQ4u6xVmDrHhT06wAqUMEYZUfRsKqc6KiRtAk66Jz_iJ9lELyBYob6iKLct7yoo3bqQmiK15B9fOYQhP915AIqw5mQMIqkt3EcRwyyCozesKquXSEVXP8MosCAbTRyfJxRBs4zGzCgLBiIRhABvdA8a0ErtHLQRc1n-MXiwtE53aGBAgrJ4cQHUNeggalPpp27gTCNt9poq_G3nFrJi1e2MVOiG-66zvO8dwHBLaKWzz7wRIRDAXe1tBsOEDYLjjs43gDdc29t7KZPNY1YSVhpTb1Yw_Xlx4ORNnmah5v3jfr4_-77gk9wnbQ6cuNYdZypcLyJh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