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

    <tr>
        <th>Summary</th>
        <td>
            [OpenMP][libc++] including <iostream> when targeting amdgpu  cases error: use of undeclared identifier '__not_found' 
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            libc++
      </td>
    </tr>

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

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

<pre>
    There seems to be a header inclusion bug in https://github.com/llvm/llvm-project/blob/a7517e12caab0750d3dfd7c0c6faec9acc7e5a2b/clang/lib/Headers/__clang_hip_math.h#L18
That header file doesn't seems to have an actual need for `<algorithm>` but its inclusion cases failures down the line.
For example:
```
#include <iostream>
int main() {
#pragma omp target
  {}
  return 0;
}
```
Compiling the program causes a header related error only when targeting amdgpu:
```
// clang++ -std=c++17 -stdlib=libc++ -fexperimental-library -fopenmp --offload-arch=gfx1036 mp_sample.cpp -O2 # -Xclang -ast-dump
// In file included from mp_sample.cpp:1:
// In file included from /home/tom/software/llvm-ompt/96adadf8f722/bin/../include/c++/v1/iostream:43:
// In file included from /home/tom/software/llvm-ompt/96adadf8f722/bin/../include/c++/v1/ios:222:
// In file included from /home/tom/software/llvm-ompt/96adadf8f722/bin/../include/c++/v1/__locale:16:
// In file included from /home/tom/software/llvm-ompt/96adadf8f722/bin/../include/c++/v1/__mutex/once_flag.h:21:
// In file included from /home/tom/software/llvm-ompt/96adadf8f722/bin/../include/c++/v1/tuple:268:
// In file included from /home/tom/software/llvm-ompt/96adadf8f722/bin/../include/c++/v1/compare:145:
// In file included from /home/tom/software/llvm-ompt/96adadf8f722/bin/../include/c++/v1/__compare/compare_partial_order_fallback.h:13:
// In file included from /home/tom/software/llvm-ompt/96adadf8f722/bin/../include/c++/v1/__compare/partial_order.h:14:
// In file included from /home/tom/software/llvm-ompt/96adadf8f722/bin/../include/c++/v1/__compare/weak_order.h:14:
// In file included from /home/tom/software/llvm-ompt/96adadf8f722/bin/../include/c++/v1/__compare/strong_order.h:20:
// In file included from /home/tom/software/llvm-ompt/96adadf8f722/lib/clang/18/include/openmp_wrappers/cmath:17:
// In file included from /home/tom/software/llvm-ompt/96adadf8f722/bin/../include/c++/v1/cmath:319:
// In file included from /home/tom/software/llvm-ompt/96adadf8f722/lib/clang/18/include/openmp_wrappers/math.h:55:
// In file included from /home/tom/software/llvm-ompt/96adadf8f722/lib/clang/18/include/__clang_hip_math.h:18:
// In file included from /home/tom/software/llvm-ompt/96adadf8f722/bin/../include/c++/v1/algorithm:1977:
// In file included from /home/tom/software/llvm-ompt/96adadf8f722/bin/../include/c++/v1/iterator:683:
// In file included from /home/tom/software/llvm-ompt/96adadf8f722/bin/../include/c++/v1/__iterator/common_iterator.h:31:
/// home/tom/software/llvm-ompt/96adadf8f722/bin/../include/c++/v1/variant:402:21: error: use of undeclared identifier '__not_found'
//  402 |   size_t __result = __not_found;
//      |                     ^
/// home/tom/software/llvm-ompt/96adadf8f722/bin/../include/c++/v1/variant:405:23: error: use of undeclared identifier '__not_found'
//  405 |       if (__result != __not_found) {
//      |                       ^
/// home/tom/software/llvm-ompt/96adadf8f722/bin/../include/c++/v1/variant:406:16: error: use of undeclared identifier '__ambiguous'
//  406 |         return __ambiguous;
//      |                ^
/// home/tom/software/llvm-ompt/96adadf8f722/bin/../include/c++/v1/variant:419:45: error: use of undeclared identifier '__not_found'
//  419 | struct __find_unambiguous_index_sfinae_impl<__not_found> {};
//      | ^
/// home/tom/software/llvm-ompt/96adadf8f722/bin/../include/c++/v1/variant:422:45: error: use of undeclared identifier '__ambiguous'
//  422 | struct __find_unambiguous_index_sfinae_impl<__ambiguous> {};
//      | ^
//  ... goes on...
```
Targeting nvptx works as expected.
Manually removing `#include <algorithm>`  in `__clang_hip_math.h` works, and so does adding `#include <algorithm>` before `#include <iostream>` in the reproducer program.

The actual cause seems to be an ordering issue, as `__not_found` is defined in `<tuple>` but that header's include chain eventually leads to `__iterator/common_iterator.h` which depends on `<variant>` in OpenMP target's amdgpu case, and `<variant>` expects some of the symbols from `<tuple>` (which isn't defined just yet).

In case it matters:
```shell
> clang++
clang version 18.0.0 (git@github.com:tom91136/llvm-project.git 96adadf8f7227f6543537056f27f98cb18bbe8ce)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/tom/software/llvm-ompt/96adadf8f722/bin
Found candidate GCC installation: /usr/lib/gcc/i686-redhat-linux/12
Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/12
Selected GCC installation: /usr/lib/gcc/x86_64-redhat-linux/12
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64
Found HIP installation: /opt/rocm, version 5.5.30202
```

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMWd-PmzoW_ms8L0dBYAIkD_OQTpq9lfbqXmn7sG_IsQ_gW2Mj28xk9q9f2UAmaaezt91WaVSpA9jHn7_zg-MP5pxsNeI9Kd6RYn_HRt8Ze-9Nv82yvLw7GvF8_7FDi-AQewfewBGBQYdMoAWpuRqdNBqOYwtSQ-f94Ei-I_RA6KGVvhuPCTc9oQelHpf_VoM1fyH3hB6OyhwJPbCqyCrMKGfsmFZFKnLRiIqnvGwY8i3jvMKC0TCUK6bbYEiGq98iEEfooa7jk7qTQ90z3yUdofk_sw1J9yTdfeyYX1A3UiEIg04TWvmXnXXsEYFpYNyPTIFGFNAYC6RMSf7AVGus9F1P8vekTOE4epDeXXDAmUMHDZNqtOhAmCcNvkNQUmMywTgYC3hi_aAwsBTvBfPTv-mS5tGkQCD5gzTOW2Rx0fhYag89k5rQDaFbINW787TBsrZnYPoBPLMt-ukJxEHVfrmy6EerISX5MnV59hmSB9MPUkndxl0M1rSW9cDZGLZ5jgGLinkUgNYaC0arZ3jqUM8QwmzWi3YYv77fECowu_Udoe9g5bwg-Z5Pl1kVbwR_53slj3wZ1eBpQCt71J6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