Skip to content

[CUDA] std::clamp doesn't compile with latest clang and gcc #95183

Closed

Description

@pateldeev

Using std::clamp fails to compile with clang 18.1.7 and gcc 14.1 libstd. It works with gcc 13.2.

Error message is

external/local_config_clang/crosstool/extra_tools/lib/gcc/x86_64-linux-gnu/14.1.0/../../../../include/c++/14.1.0/bits/stl_algo.h:3625:7: error: reference to __host__ function '__glibcxx_assert_fail' in __host__ __device__ function
 3625 |       __glibcxx_assert(!(__hi < __lo));
      |       ^
external/local_config_clang/crosstool/extra_tools/lib/gcc/x86_64-linux-gnu/14.1.0/../../../../include/x86_64-linux-gnu/c++/14.1.0/bits/c++config.h:612:7: note: expanded from macro '__glibcxx_assert'
  612 |         std::__glibcxx_assert_fail();                                   \
      |              ^
quantize.cu.cc:12:13: note: called by 'quantize_z'
   12 |       std::clamp(z, kMinZ, kMaxZ);
      |            ^

Activity

  1. added
    clangClang issues not falling into any other category
    on Jun 12, 2024
  2. added
    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"
    libstdc++GNU libstdc++ C++ standard library
    and removed
    clangClang issues not falling into any other category
    on Jun 12, 2024
  3. llvmbot commented on Jun 12, 2024

    @llvmbot
    Member

    @llvm/issue-subscribers-clang-frontend

    Author: Deev Patel (pateldeev)

    Details Using `std::clamp` fails to compile with clang 18.1.7 and gcc 14.1 libstd. It works with gcc 13.2.

    Error message is

    external/local_config_clang/crosstool/extra_tools/lib/gcc/x86_64-linux-gnu/14.1.0/../../../../include/c++/14.1.0/bits/stl_algo.h:3625:7: error: reference to __host__ function '__glibcxx_assert_fail' in __host__ __device__ function
     3625 |       __glibcxx_assert(!(__hi &lt; __lo));
          |       ^
    external/local_config_clang/crosstool/extra_tools/lib/gcc/x86_64-linux-gnu/14.1.0/../../../../include/x86_64-linux-gnu/c++/14.1.0/bits/c++config.h:612:7: note: expanded from macro '__glibcxx_assert'
      612 |         std::__glibcxx_assert_fail();                                   \
          |              ^
    quantize.cu.cc:12:13: note: called by 'quantize_z'
       12 |       std::clamp(z, kMinZ, kMaxZ);
          |            ^
    
  4. pinskia commented on Jul 1, 2024

    @pinskia

    Same bug as before:
    #49727

  5. pinskia commented on Jul 1, 2024

    @pinskia

    But it looks like there is some fundamental issue with how cuda handles this.

  6. pinskia commented on Jul 1, 2024

    @pinskia
  7. AngryLoki commented on Jun 11, 2025

    @AngryLoki
    Contributor

    The same error occurs now with std::array, as libstdc++-15 adds __glibcxx_requires_subscript there too.
    https://github.com/gcc-mirror/gcc/blob/2f2e51b6596c3796b907c6289d3e631508d8774e/libstdc%2B%2B-v3/include/std/array#L207-L222

    This breaks libraries like composable-kernel - https://bugs.gentoo.org/955623. Note: works with stdlibc++-14 and libc++.

    Looks like it is possible to undo __glibcxx_requires_subscript both for std::clamp and std::array by adding this:

    #if !defined(__HIPCC_RTC__) && defined(__cplusplus) && defined(__clang__) && defined(__HIP__) \
    && defined(__HIP_PLATFORM_AMD__) && defined(__GLIBCXX__) \
    && defined(_GLIBCXX_RELEASE) && (_GLIBCXX_RELEASE == 15)
    #include <debug/assertions.h>
    #define __glibcxx_requires_subscript (void)
    #endif
    

    to https://github.com/llvm/llvm-project/blob/main/clang/lib/Headers/__clang_hip_runtime_wrapper.h at least before #include <complex> (which transitively includes <array>). Are there any better solutions?

  8. zhaorv commented on Jun 11, 2025

    @zhaorv

    There is ongoing discussion to fix this in libstdc++: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115740

  9. AngryLoki commented on Jul 13, 2025

    @AngryLoki
    Contributor

    Looks like the issue was addressed in #136133 by @jmmartinez

  10. self-assigned this
    on Jul 15, 2025
  11. jmmartinez commented on Jul 15, 2025

    @jmmartinez
    Contributor

    The issue should have been solved by the patch mentioned above.

    @pateldeev Could you try with a nightly version of clang to confirm the issue has been solved for you?

    Also, a workaround this issue could be to define the macro _GLIBCXX_ASSERTIONS to 0 on the command line.

  12. jmmartinez commented on Jul 25, 2025

    @jmmartinez
    Contributor

    Since the issue seems to be solved in the latest LLVM version by #136133 I've closed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

clang:frontendLanguage frontend issues, e.g. anything involving "Sema"cudalibstdc++GNU libstdc++ C++ standard library

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions