https://github.com/saman-amd updated https://github.com/llvm/llvm-project/pull/243733
>From bf3adcccc86530faf5ff9349170716291b8f76b5 Mon Sep 17 00:00:00 2001 From: "Khatir, Saman" <[email protected]> Date: Wed, 18 Nov 2026 00:22:35 -0500 Subject: [PATCH 1/2] [HIP] Support MSVC <complex> in device code and hipRTC On Windows, HIP device code is compiled against the MSVC STL, and so is hipRTC code that includes standard headers. std::complex from MSVC's <complex> did not work in device code: - hipRTC: std::signbit(long double), which <complex> calls, was ambiguous between the float and double overloads; with C++20, std::fma(long double) in MSVC's <cmath> too. - The __device__ versions of the <ymath.h> functions that MSVC's <complex> calls (_Cosh, _Sinh, ...) were only defined outside hipRTC, and _Exp and _FExp were missing, so std::exp(std::complex) did not compile for the device (with hipRTC: "undefined hidden symbol: _Exp" at link). - std::swap, which <complex> calls in abs, sqrt and log, is constexpr, and so usable in device code, only from C++20 on. In __clang_hip_cmath.h, for MSVC: define signbit and fma for long double (the same format as double there), add _Exp and _FExp, and define the <ymath.h> functions for hipRTC too. In the cuda_wrappers <complex>, for the MSVC STL: include <ymath.h> before the force_cuda_host_device pragma, as __clang_hip_cmath.h does outside hipRTC, so that its functions stay __host__ next to the __device__ versions, and add __device__ std::swap for float, double and long double before C++20. Reported in https://github.com/ROCm/TheRock/issues/9845. --- clang/lib/Headers/__clang_hip_cmath.h | 56 ++++++++++++++-- clang/lib/Headers/cuda_wrappers/complex | 31 +++++++++ clang/test/Headers/Inputs/include/complex | 5 ++ clang/test/Headers/Inputs/include/ymath.h | 19 ++++++ clang/test/Headers/hip-header-msvc.hip | 79 +++++++++++++++++++++++ 5 files changed, 185 insertions(+), 5 deletions(-) create mode 100644 clang/test/Headers/Inputs/include/ymath.h create mode 100644 clang/test/Headers/hip-header-msvc.hip diff --git a/clang/lib/Headers/__clang_hip_cmath.h b/clang/lib/Headers/__clang_hip_cmath.h index 8dbde4291fff51..08c3d0a8e9f346 100644 --- a/clang/lib/Headers/__clang_hip_cmath.h +++ b/clang/lib/Headers/__clang_hip_cmath.h @@ -801,16 +801,27 @@ _GLIBCXX_END_NAMESPACE_VERSION #endif // !defined(__HIPCC_RTC__) // Define device-side math functions from <ymath.h> on MSVC. -#if !defined(__HIPCC_RTC__) #if defined(_MSC_VER) - +#if !defined(__HIPCC_RTC__) // Before VS2019, `<ymath.h>` is also included in `<limits>` and other headers. // But, from VS2019, it's only included in `<complex>`. Need to include // `<ymath.h>` here to ensure C functions declared there won't be markded as // `__host__` and `__device__` through `<complex>` wrapper. #include <ymath.h> +#endif // !defined(__HIPCC_RTC__) #if defined(__cplusplus) +// MSVC's long double has the same format as double. MSVC's <complex> calls +// std::signbit, and its <cmath> std::fma, with long double arguments, which +// must not be ambiguous between the float and double overloads. +__DEVICE__ __CONSTEXPR__ bool signbit(long double __x) { + return ::__signbit(__x); +} +__DEVICE__ __CONSTEXPR__ long double fma(long double __x, long double __y, + long double __z) { + return ::fma((double)__x, (double)__y, (double)__z); +} + extern "C" { #endif // defined(__cplusplus) __DEVICE__ __CONSTEXPR__ __attribute__((overloadable)) double _Cosh(double x, @@ -821,11 +832,14 @@ __DEVICE__ __CONSTEXPR__ __attribute__((overloadable)) float _FCosh(float x, float y) { return coshf(x) * y; } +// The <ymath.h> classification codes: _NANCODE (2), _INFCODE (1), _FINITE +// (-1), _DENORM (-2) and 0 for zero, the FP_* values of MSVC's <math.h>, which +// hipRTC does not include. __DEVICE__ __CONSTEXPR__ __attribute__((overloadable)) short _Dtest(double *p) { - return fpclassify(*p); + return __builtin_fpclassify(2, 1, -1, -2, 0, *p); } __DEVICE__ __CONSTEXPR__ __attribute__((overloadable)) short _FDtest(float *p) { - return fpclassify(*p); + return __builtin_fpclassify(2, 1, -1, -2, 0, *p); } __DEVICE__ __CONSTEXPR__ __attribute__((overloadable)) double _Sinh(double x, double y) { @@ -835,11 +849,43 @@ __DEVICE__ __CONSTEXPR__ __attribute__((overloadable)) float _FSinh(float x, float y) { return sinhf(x) * y; } +// Store __y * e^(*__px) * 2^__eoff in *__px and return its classification. +// e^(*__px) may overflow where the product does not; then use e^(*__px / 2) +// twice. +__DEVICE__ __CONSTEXPR__ __attribute__((overloadable)) short +_Exp(double *__px, double __y, short __eoff) { + if (__y == 0) { + *__px = __y; + } else { + double __e = exp(*__px); + if (__builtin_isinf(__e)) { + double __h = exp(*__px / 2); + *__px = ldexp(__h * __y * __h, __eoff); + } else { + *__px = ldexp(__e * __y, __eoff); + } + } + return _Dtest(__px); +} +__DEVICE__ __CONSTEXPR__ __attribute__((overloadable)) short +_FExp(float *__px, float __y, short __eoff) { + if (__y == 0) { + *__px = __y; + } else { + float __e = expf(*__px); + if (__builtin_isinf(__e)) { + float __h = expf(*__px / 2); + *__px = ldexpf(__h * __y * __h, __eoff); + } else { + *__px = ldexpf(__e * __y, __eoff); + } + } + return _FDtest(__px); +} #if defined(__cplusplus) } #endif // defined(__cplusplus) #endif // defined(_MSC_VER) -#endif // !defined(__HIPCC_RTC__) #endif // ifndef __OPENMP_AMDGCN__ #pragma pop_macro("__DEVICE__") diff --git a/clang/lib/Headers/cuda_wrappers/complex b/clang/lib/Headers/cuda_wrappers/complex index e6805b6044e988..1e07aac219e029 100644 --- a/clang/lib/Headers/cuda_wrappers/complex +++ b/clang/lib/Headers/cuda_wrappers/complex @@ -36,6 +36,37 @@ #include <cmath> #include <sstream> +#ifdef _MSVC_STL_VERSION +// MSVC's <complex> calls C functions declared in <ymath.h> (_Exp, _Cosh, ...). +// Include it here, so that they stay __host__; __clang_hip_cmath.h defines +// their __device__ versions for HIP. +#if __has_include(<ymath.h>) +#include <ymath.h> +#endif + +// MSVC's <complex> also calls std::swap on its value type. std::swap is +// constexpr, and thus implicitly __host__ __device__, only from C++20 on. +#if !_HAS_CXX20 +namespace std { +inline __device__ void swap(float &__a, float &__b) noexcept { + float __t = __a; + __a = __b; + __b = __t; +} +inline __device__ void swap(double &__a, double &__b) noexcept { + double __t = __a; + __a = __b; + __b = __t; +} +inline __device__ void swap(long double &__a, long double &__b) noexcept { + long double __t = __a; + __a = __b; + __b = __t; +} +} // namespace std +#endif // !_HAS_CXX20 +#endif // _MSVC_STL_VERSION + // Next, include our <algorithm> wrapper, to ensure that device overloads of // std::min/max are available. #include <algorithm> diff --git a/clang/test/Headers/Inputs/include/complex b/clang/test/Headers/Inputs/include/complex index bd43cd952d7cdf..b228ac5b723803 100644 --- a/clang/test/Headers/Inputs/include/complex +++ b/clang/test/Headers/Inputs/include/complex @@ -2,6 +2,11 @@ #include <cmath> +#ifdef _MSVC_STL_VERSION +// Like MSVC's <complex>. +#include <ymath.h> +#endif + #define INFINITY (__builtin_inff()) #define NAN (__builtin_nanf ("")) diff --git a/clang/test/Headers/Inputs/include/ymath.h b/clang/test/Headers/Inputs/include/ymath.h new file mode 100644 index 00000000000000..c89cbc60b182ff --- /dev/null +++ b/clang/test/Headers/Inputs/include/ymath.h @@ -0,0 +1,19 @@ +#pragma once + +// Simplified MSVC STL <ymath.h>: C functions of the MSVC runtime that MSVC's +// <complex> calls. + +#ifdef __cplusplus +extern "C" { +#endif + +double _Cosh(double, double); +double _Sinh(double, double); +short _Exp(double *, double, short); +float _FCosh(float, float); +float _FSinh(float, float); +short _FExp(float *, float, short); + +#ifdef __cplusplus +} +#endif diff --git a/clang/test/Headers/hip-header-msvc.hip b/clang/test/Headers/hip-header-msvc.hip new file mode 100644 index 00000000000000..61837a0d4824b2 --- /dev/null +++ b/clang/test/Headers/hip-header-msvc.hip @@ -0,0 +1,79 @@ +// REQUIRES: amdgpu-registered-target + +// Device versions of what MSVC's <complex> calls, with an MSVC host (_MSC_VER +// and _MSVC_STL_VERSION), for HIP and hipRTC. + +// RUN: %clang_cc1 -include __clang_hip_runtime_wrapper.h \ +// RUN: -internal-isystem %S/../../lib/Headers/cuda_wrappers \ +// RUN: -internal-isystem %S/Inputs/include \ +// RUN: -triple amdgcn-amd-amdhsa -aux-triple x86_64-pc-windows-msvc \ +// RUN: -fms-extensions -fms-compatibility -fms-compatibility-version=19.33 \ +// RUN: -D_MSVC_STL_VERSION=144 -D_HAS_CXX20=0 -std=c++17 \ +// RUN: -fcuda-is-device -O1 -emit-llvm %s -o - -verify | FileCheck %s +// RUN: %clang_cc1 -include __clang_hip_runtime_wrapper.h \ +// RUN: -internal-isystem %S/../../lib/Headers/cuda_wrappers \ +// RUN: -internal-isystem %S/Inputs/include \ +// RUN: -triple amdgcn-amd-amdhsa -aux-triple x86_64-pc-windows-msvc \ +// RUN: -fms-extensions -fms-compatibility -fms-compatibility-version=19.33 \ +// RUN: -D_MSVC_STL_VERSION=144 -D_HAS_CXX20=0 -std=c++17 \ +// RUN: -fcuda-is-device -O1 -emit-llvm %s -o - -verify \ +// RUN: -D__HIPCC_RTC__ | FileCheck %s + +// expected-no-diagnostics + +// Under hipRTC, this <complex> is the first to include <ymath.h>, and must do +// so before it makes the functions of the wrapped <complex> __host__ +// __device__. +#include <complex> + +// CHECK-LABEL: define{{.*}} i1 @test_signbit_ld( +// CHECK-NOT: call +// CHECK: ret i1 +extern "C" __device__ bool test_signbit_ld(long double x) { + return signbit(x); +} + +// CHECK-LABEL: define{{.*}} double @test_fma_ld( +// CHECK: call{{.*}} double @llvm.fma.f64( +extern "C" __device__ long double test_fma_ld(long double x, long double y, + long double z) { + return fma(x, y, z); +} + +// CHECK-LABEL: define{{.*}} i16 @test_exp( +// CHECK: call{{.*}} @llvm.exp.f64( +// CHECK: call{{.*}} @llvm.ldexp.f64 +extern "C" __device__ short test_exp(double *x, double y, short e) { + return _Exp(x, y, e); +} + +// CHECK-LABEL: define{{.*}} i16 @test_fexp( +// CHECK: call{{.*}} @llvm.exp.f32( +// CHECK: call{{.*}} @llvm.ldexp.f32 +extern "C" __device__ short test_fexp(float *x, float y, short e) { + return _FExp(x, y, e); +} + +// CHECK-LABEL: define{{.*}} double @test_cosh( +// CHECK: call{{.*}} @__ocml_cosh_f64( +extern "C" __device__ double test_cosh(double x, double y) { + return _Cosh(x, y); +} + +// CHECK-LABEL: define{{.*}} float @test_fsinh( +// CHECK: call{{.*}} @__ocml_sinh_f32( +extern "C" __device__ float test_fsinh(float x, float y) { + return _FSinh(x, y); +} + +// CHECK-LABEL: define{{.*}} void @test_swap( +// CHECK-NOT: call +// CHECK: ret void +extern "C" __device__ void test_swap(double &x, double &y) { + std::swap(x, y); +} + +// CHECK-NOT: @_Exp( +// CHECK-NOT: @_FExp( +// CHECK-NOT: @_Cosh( +// CHECK-NOT: @_FSinh( >From 32364cde46972b364c7f8a852c182e0234669887 Mon Sep 17 00:00:00 2001 From: "Khatir, Saman" <[email protected]> Date: Wed, 18 Nov 2026 01:07:19 -0500 Subject: [PATCH 2/2] [HIP] Add long double classification, fix _Exp scaling, test host side - Define isfinite, isinf and isnan for long double on MSVC next to signbit and fma. __clang_cuda_math_forward_declares.h declares them, so device code that called them did not link, and with hipRTC the calls were ambiguous. - _Exp and _FExp: compute e^x as e^r * 2^k like the MSVC runtime, so that only the final scaling by 2^(k + eoff) can overflow or underflow (for example, x = 750 with eoff = -200 gave infinity), and return _FINITE for subnormal results, as the MSVC runtime does. - Use reserved names for the parameters of the <ymath.h> functions, which hipRTC now sees. - Test: the fake MSVC headers now declare host std::swap, signbit and fma for long double, and noexcept <ymath.h> functions, and the test calls them from __host__ __device__ functions, like <complex>, checking that the host compilation still calls the host functions, with C++17 and C++20. --- clang/lib/Headers/__clang_hip_cmath.h | 117 +++++++------ clang/test/Headers/Inputs/include/cmath | 5 + clang/test/Headers/Inputs/include/type_traits | 15 ++ clang/test/Headers/Inputs/include/ymath.h | 12 +- clang/test/Headers/hip-header-msvc.hip | 154 +++++++++++++----- 5 files changed, 203 insertions(+), 100 deletions(-) diff --git a/clang/lib/Headers/__clang_hip_cmath.h b/clang/lib/Headers/__clang_hip_cmath.h index 08c3d0a8e9f346..92aa793e7b9638 100644 --- a/clang/lib/Headers/__clang_hip_cmath.h +++ b/clang/lib/Headers/__clang_hip_cmath.h @@ -811,9 +811,16 @@ _GLIBCXX_END_NAMESPACE_VERSION #endif // !defined(__HIPCC_RTC__) #if defined(__cplusplus) -// MSVC's long double has the same format as double. MSVC's <complex> calls -// std::signbit, and its <cmath> std::fma, with long double arguments, which -// must not be ambiguous between the float and double overloads. +// MSVC's long double has the same format as double. Device code calls these +// with long double arguments (MSVC's <complex> calls std::signbit, its <cmath> +// std::fma), which must not be ambiguous between the float and double +// overloads. __clang_cuda_math_forward_declares.h also declares isfinite, +// isinf and isnan for long double on MSVC. +__DEVICE__ __CONSTEXPR__ bool isfinite(long double __x) { + return ::__finite(__x); +} +__DEVICE__ __CONSTEXPR__ bool isinf(long double __x) { return ::__isinf(__x); } +__DEVICE__ __CONSTEXPR__ bool isnan(long double __x) { return ::__isnan(__x); } __DEVICE__ __CONSTEXPR__ bool signbit(long double __x) { return ::__signbit(__x); } @@ -824,63 +831,75 @@ __DEVICE__ __CONSTEXPR__ long double fma(long double __x, long double __y, extern "C" { #endif // defined(__cplusplus) -__DEVICE__ __CONSTEXPR__ __attribute__((overloadable)) double _Cosh(double x, - double y) { - return cosh(x) * y; -} -__DEVICE__ __CONSTEXPR__ __attribute__((overloadable)) float _FCosh(float x, - float y) { - return coshf(x) * y; -} -// The <ymath.h> classification codes: _NANCODE (2), _INFCODE (1), _FINITE -// (-1), _DENORM (-2) and 0 for zero, the FP_* values of MSVC's <math.h>, which -// hipRTC does not include. -__DEVICE__ __CONSTEXPR__ __attribute__((overloadable)) short _Dtest(double *p) { - return __builtin_fpclassify(2, 1, -1, -2, 0, *p); -} -__DEVICE__ __CONSTEXPR__ __attribute__((overloadable)) short _FDtest(float *p) { - return __builtin_fpclassify(2, 1, -1, -2, 0, *p); -} -__DEVICE__ __CONSTEXPR__ __attribute__((overloadable)) double _Sinh(double x, - double y) { - return sinh(x) * y; -} -__DEVICE__ __CONSTEXPR__ __attribute__((overloadable)) float _FSinh(float x, - float y) { - return sinhf(x) * y; -} -// Store __y * e^(*__px) * 2^__eoff in *__px and return its classification. -// e^(*__px) may overflow where the product does not; then use e^(*__px / 2) -// twice. +__DEVICE__ __CONSTEXPR__ + __attribute__((overloadable)) double _Cosh(double __x, double __y) { + return cosh(__x) * __y; +} +__DEVICE__ __CONSTEXPR__ __attribute__((overloadable)) float _FCosh(float __x, + float __y) { + return coshf(__x) * __y; +} +// The classification codes of the MSVC runtime: _NANCODE (2), _INFCODE (1), +// _FINITE (-1), _DENORM (-2) and 0 for zero. They are the FP_* values of the +// UCRT <math.h>, which hipRTC does not include. +__DEVICE__ __CONSTEXPR__ __attribute__((overloadable)) short +_Dtest(double *__p) { + return __builtin_fpclassify(2, 1, -1, -2, 0, *__p); +} +__DEVICE__ __CONSTEXPR__ __attribute__((overloadable)) short +_FDtest(float *__p) { + return __builtin_fpclassify(2, 1, -1, -2, 0, *__p); +} +__DEVICE__ __CONSTEXPR__ __attribute__((overloadable)) double +_Sinh(double __x, double __y) { + return sinh(__x) * __y; +} +__DEVICE__ __CONSTEXPR__ __attribute__((overloadable)) float _FSinh(float __x, + float __y) { + return sinhf(__x) * __y; +} +// Store __y * e^(*__px) * 2^__eoff in *__px and return its classification: +// 0 for zero, _INFCODE, _NANCODE, else _FINITE (also for a subnormal result). +// Like the MSVC runtime, give 0 or infinity for |*__px| above 1842 (229 for +// float), and else compute e^(*__px) as e^__r * 2^__k with |__r| <= ln(2) / 2, +// so that, with __y = __m * 2^__ey, only the final scaling of e^__r * __m can +// overflow or underflow. __DEVICE__ __CONSTEXPR__ __attribute__((overloadable)) short _Exp(double *__px, double __y, short __eoff) { + double __x = *__px; if (__y == 0) { *__px = __y; - } else { - double __e = exp(*__px); - if (__builtin_isinf(__e)) { - double __h = exp(*__px / 2); - *__px = ldexp(__h * __y * __h, __eoff); - } else { - *__px = ldexp(__e * __y, __eoff); - } + } else if (__x < -1842) { + *__px = __builtin_copysign(0.0, __y); + } else if (__x > 1842) { + *__px = __builtin_copysign(__builtin_huge_val(), __y); + } else if (!__builtin_isnan(__x)) { + double __k = rint(__x * 1.44269504088896340736); + double __r = (__x - __k * 6.93147180369123816490e-01) - + __k * 1.90821492927058770002e-10; + int __ey = 0; + double __m = __builtin_isfinite(__y) ? frexp(__y, &__ey) : __y; + *__px = ldexp(exp(__r) * __m, (int)__k + __ey + __eoff); } - return _Dtest(__px); + return __builtin_fpclassify(2, 1, -1, -1, 0, *__px); } __DEVICE__ __CONSTEXPR__ __attribute__((overloadable)) short _FExp(float *__px, float __y, short __eoff) { + float __x = *__px; if (__y == 0) { *__px = __y; - } else { - float __e = expf(*__px); - if (__builtin_isinf(__e)) { - float __h = expf(*__px / 2); - *__px = ldexpf(__h * __y * __h, __eoff); - } else { - *__px = ldexpf(__e * __y, __eoff); - } + } else if (__x < -229) { + *__px = __builtin_copysignf(0.0f, __y); + } else if (__x > 229) { + *__px = __builtin_copysignf(__builtin_huge_valf(), __y); + } else if (!__builtin_isnan(__x)) { + float __k = rintf(__x * 1.44269504f); + float __r = (__x - __k * 6.93145752e-01f) - __k * 1.42860677e-06f; + int __ey = 0; + float __m = __builtin_isfinite(__y) ? frexpf(__y, &__ey) : __y; + *__px = ldexpf(expf(__r) * __m, (int)__k + __ey + __eoff); } - return _FDtest(__px); + return __builtin_fpclassify(2, 1, -1, -1, 0, *__px); } #if defined(__cplusplus) } diff --git a/clang/test/Headers/Inputs/include/cmath b/clang/test/Headers/Inputs/include/cmath index 20e34898b55359..6f437cd44f58cc 100644 --- a/clang/test/Headers/Inputs/include/cmath +++ b/clang/test/Headers/Inputs/include/cmath @@ -140,6 +140,11 @@ double scalbn(double, int); float scalbn(float, int); bool signbit(double); bool signbit(float); +#ifdef _MSVC_STL_VERSION +// MSVC's <cmath> and <math.h> declare these for long double too. +bool signbit(long double); +long double fma(long double, long double, long double); +#endif long double sin(long double); double sin(double); float sin(float); diff --git a/clang/test/Headers/Inputs/include/type_traits b/clang/test/Headers/Inputs/include/type_traits index 9fd02d51eff133..6b5508a041936e 100644 --- a/clang/test/Headers/Inputs/include/type_traits +++ b/clang/test/Headers/Inputs/include/type_traits @@ -41,3 +41,18 @@ template <typename _Tp> struct enable_if<true, _Tp>{ }; } + +#ifdef _MSVC_STL_VERSION +namespace std { +// Like MSVC's std::swap, which is constexpr from C++20 on. +template <class _Tp> +#if _HAS_CXX20 +constexpr +#endif +void swap(_Tp &__a, _Tp &__b) noexcept { + _Tp __t = __a; + __a = __b; + __b = __t; +} +} +#endif diff --git a/clang/test/Headers/Inputs/include/ymath.h b/clang/test/Headers/Inputs/include/ymath.h index c89cbc60b182ff..21476d17a5e831 100644 --- a/clang/test/Headers/Inputs/include/ymath.h +++ b/clang/test/Headers/Inputs/include/ymath.h @@ -7,12 +7,12 @@ extern "C" { #endif -double _Cosh(double, double); -double _Sinh(double, double); -short _Exp(double *, double, short); -float _FCosh(float, float); -float _FSinh(float, float); -short _FExp(float *, float, short); +double _Cosh(double, double) noexcept; +double _Sinh(double, double) noexcept; +short _Exp(double *, double, short) noexcept; +float _FCosh(float, float) noexcept; +float _FSinh(float, float) noexcept; +short _FExp(float *, float, short) noexcept; #ifdef __cplusplus } diff --git a/clang/test/Headers/hip-header-msvc.hip b/clang/test/Headers/hip-header-msvc.hip index 61837a0d4824b2..fa7ece2e2c5b57 100644 --- a/clang/test/Headers/hip-header-msvc.hip +++ b/clang/test/Headers/hip-header-msvc.hip @@ -1,23 +1,56 @@ // REQUIRES: amdgpu-registered-target -// Device versions of what MSVC's <complex> calls, with an MSVC host (_MSC_VER -// and _MSVC_STL_VERSION), for HIP and hipRTC. +// What MSVC's <complex> and <cmath> call, from __host__ __device__ functions +// like those of <complex>, with an MSVC host (_MSC_VER and _MSVC_STL_VERSION): +// device code, for HIP and hipRTC, gets the __device__ versions, host code +// still the host functions of the MSVC STL and runtime. // RUN: %clang_cc1 -include __clang_hip_runtime_wrapper.h \ // RUN: -internal-isystem %S/../../lib/Headers/cuda_wrappers \ // RUN: -internal-isystem %S/Inputs/include \ // RUN: -triple amdgcn-amd-amdhsa -aux-triple x86_64-pc-windows-msvc \ -// RUN: -fms-extensions -fms-compatibility -fms-compatibility-version=19.33 \ +// RUN: -fms-extensions -fms-compatibility -fms-compatibility-version=19.44 \ // RUN: -D_MSVC_STL_VERSION=144 -D_HAS_CXX20=0 -std=c++17 \ -// RUN: -fcuda-is-device -O1 -emit-llvm %s -o - -verify | FileCheck %s +// RUN: -fcuda-is-device -O1 -emit-llvm %s -o - -verify \ +// RUN: | FileCheck %s --check-prefix=DEV // RUN: %clang_cc1 -include __clang_hip_runtime_wrapper.h \ // RUN: -internal-isystem %S/../../lib/Headers/cuda_wrappers \ // RUN: -internal-isystem %S/Inputs/include \ // RUN: -triple amdgcn-amd-amdhsa -aux-triple x86_64-pc-windows-msvc \ -// RUN: -fms-extensions -fms-compatibility -fms-compatibility-version=19.33 \ +// RUN: -fms-extensions -fms-compatibility -fms-compatibility-version=19.44 \ // RUN: -D_MSVC_STL_VERSION=144 -D_HAS_CXX20=0 -std=c++17 \ +// RUN: -fcuda-is-device -O1 -emit-llvm %s -o - -verify -D__HIPCC_RTC__ \ +// RUN: | FileCheck %s --check-prefix=DEV +// RUN: %clang_cc1 -include __clang_hip_runtime_wrapper.h \ +// RUN: -internal-isystem %S/../../lib/Headers/cuda_wrappers \ +// RUN: -internal-isystem %S/Inputs/include \ +// RUN: -triple amdgcn-amd-amdhsa -aux-triple x86_64-pc-windows-msvc \ +// RUN: -fms-extensions -fms-compatibility -fms-compatibility-version=19.44 \ +// RUN: -D_MSVC_STL_VERSION=144 -D_HAS_CXX20=1 -std=c++20 \ // RUN: -fcuda-is-device -O1 -emit-llvm %s -o - -verify \ -// RUN: -D__HIPCC_RTC__ | FileCheck %s +// RUN: | FileCheck %s --check-prefix=DEV +// RUN: %clang_cc1 -include __clang_hip_runtime_wrapper.h \ +// RUN: -internal-isystem %S/../../lib/Headers/cuda_wrappers \ +// RUN: -internal-isystem %S/Inputs/include \ +// RUN: -triple amdgcn-amd-amdhsa -aux-triple x86_64-pc-windows-msvc \ +// RUN: -fms-extensions -fms-compatibility -fms-compatibility-version=19.44 \ +// RUN: -D_MSVC_STL_VERSION=144 -D_HAS_CXX20=1 -std=c++20 \ +// RUN: -fcuda-is-device -O1 -emit-llvm %s -o - -verify -D__HIPCC_RTC__ \ +// RUN: | FileCheck %s --check-prefix=DEV +// RUN: %clang_cc1 -include __clang_hip_runtime_wrapper.h \ +// RUN: -internal-isystem %S/../../lib/Headers/cuda_wrappers \ +// RUN: -internal-isystem %S/Inputs/include \ +// RUN: -triple x86_64-pc-windows-msvc -aux-triple amdgcn-amd-amdhsa \ +// RUN: -fms-extensions -fms-compatibility -fms-compatibility-version=19.44 \ +// RUN: -D_MSVC_STL_VERSION=144 -D_HAS_CXX20=0 -std=c++17 \ +// RUN: -emit-llvm %s -o - -verify | FileCheck %s --check-prefix=HOST +// RUN: %clang_cc1 -include __clang_hip_runtime_wrapper.h \ +// RUN: -internal-isystem %S/../../lib/Headers/cuda_wrappers \ +// RUN: -internal-isystem %S/Inputs/include \ +// RUN: -triple x86_64-pc-windows-msvc -aux-triple amdgcn-amd-amdhsa \ +// RUN: -fms-extensions -fms-compatibility -fms-compatibility-version=19.44 \ +// RUN: -D_MSVC_STL_VERSION=144 -D_HAS_CXX20=1 -std=c++20 \ +// RUN: -emit-llvm %s -o - -verify | FileCheck %s --check-prefix=HOST // expected-no-diagnostics @@ -26,54 +59,85 @@ // __device__. #include <complex> -// CHECK-LABEL: define{{.*}} i1 @test_signbit_ld( -// CHECK-NOT: call -// CHECK: ret i1 -extern "C" __device__ bool test_signbit_ld(long double x) { - return signbit(x); -} +#pragma clang force_cuda_host_device begin + +// DEV-LABEL: define{{.*}} i1 @test_signbit( +// DEV-NOT: call{{.*}} @{{[^l]}} +// DEV: ret i1 +// HOST-LABEL: define{{.*}} i1 @test_signbit( +// HOST: call{{.*}} @"?signbit@@YA_NO@Z"( +extern "C" bool test_signbit(long double x) { return std::signbit(x); } + +// DEV-LABEL: define{{.*}} i1 @test_isfinite( +// DEV-NOT: call{{.*}} @{{[^l]}} +// DEV: ret i1 +// HOST-LABEL: define{{.*}} i1 @test_isfinite( +// HOST: call{{.*}} @"?isfinite@@YA_NO@Z"( +extern "C" bool test_isfinite(long double x) { return std::isfinite(x); } -// CHECK-LABEL: define{{.*}} double @test_fma_ld( -// CHECK: call{{.*}} double @llvm.fma.f64( -extern "C" __device__ long double test_fma_ld(long double x, long double y, - long double z) { - return fma(x, y, z); +// DEV-LABEL: define{{.*}} i1 @test_isinf( +// DEV-NOT: call{{.*}} @{{[^l]}} +// DEV: ret i1 +// HOST-LABEL: define{{.*}} i1 @test_isinf( +// HOST: call{{.*}} @"?isinf@@YA_NO@Z"( +extern "C" bool test_isinf(long double x) { return std::isinf(x); } + +// DEV-LABEL: define{{.*}} i1 @test_isnan( +// DEV-NOT: call{{.*}} @{{[^l]}} +// DEV: ret i1 +// HOST-LABEL: define{{.*}} i1 @test_isnan( +// HOST: call{{.*}} @"?isnan@@YA_NO@Z"( +extern "C" bool test_isnan(long double x) { return std::isnan(x); } + +// DEV-LABEL: define{{.*}} double @test_fma( +// DEV: call{{.*}} double @llvm.fma.f64( +// HOST-LABEL: define{{.*}} double @test_fma( +// HOST: call{{.*}} @"?fma@@YAOOOO@Z"( +extern "C" long double test_fma(long double x, long double y, long double z) { + return std::fma(x, y, z); } -// CHECK-LABEL: define{{.*}} i16 @test_exp( -// CHECK: call{{.*}} @llvm.exp.f64( -// CHECK: call{{.*}} @llvm.ldexp.f64 -extern "C" __device__ short test_exp(double *x, double y, short e) { +// DEV-LABEL: define{{.*}} i16 @test_exp( +// DEV: call{{.*}} @llvm.exp.f64( +// DEV: call{{.*}} @llvm.ldexp.f64 +// HOST-LABEL: define{{.*}} i16 @test_exp( +// HOST: call{{.*}} i16 @_Exp( +extern "C" short test_exp(double *x, double y, short e) { return _Exp(x, y, e); } -// CHECK-LABEL: define{{.*}} i16 @test_fexp( -// CHECK: call{{.*}} @llvm.exp.f32( -// CHECK: call{{.*}} @llvm.ldexp.f32 -extern "C" __device__ short test_fexp(float *x, float y, short e) { +// DEV-LABEL: define{{.*}} i16 @test_fexp( +// DEV: call{{.*}} @llvm.exp.f32( +// DEV: call{{.*}} @llvm.ldexp.f32 +// HOST-LABEL: define{{.*}} i16 @test_fexp( +// HOST: call{{.*}} i16 @_FExp( +extern "C" short test_fexp(float *x, float y, short e) { return _FExp(x, y, e); } -// CHECK-LABEL: define{{.*}} double @test_cosh( -// CHECK: call{{.*}} @__ocml_cosh_f64( -extern "C" __device__ double test_cosh(double x, double y) { - return _Cosh(x, y); -} +// DEV-LABEL: define{{.*}} double @test_cosh( +// DEV: call{{.*}} @__ocml_cosh_f64( +// HOST-LABEL: define{{.*}} double @test_cosh( +// HOST: call{{.*}} double @_Cosh( +extern "C" double test_cosh(double x, double y) { return _Cosh(x, y); } -// CHECK-LABEL: define{{.*}} float @test_fsinh( -// CHECK: call{{.*}} @__ocml_sinh_f32( -extern "C" __device__ float test_fsinh(float x, float y) { - return _FSinh(x, y); -} +// DEV-LABEL: define{{.*}} float @test_fsinh( +// DEV: call{{.*}} @__ocml_sinh_f32( +// HOST-LABEL: define{{.*}} float @test_fsinh( +// HOST: call{{.*}} float @_FSinh( +extern "C" float test_fsinh(float x, float y) { return _FSinh(x, y); } -// CHECK-LABEL: define{{.*}} void @test_swap( -// CHECK-NOT: call -// CHECK: ret void -extern "C" __device__ void test_swap(double &x, double &y) { - std::swap(x, y); -} +// MSVC's std::swap is constexpr only from C++20 on. +// DEV-LABEL: define{{.*}} void @test_swap( +// DEV-NOT: call{{.*}} @{{[^l]}} +// DEV: ret void +// HOST-LABEL: define{{.*}} void @test_swap( +// HOST: call void @"??$swap@N@std@@YAXAEAN0@Z"( +extern "C" void test_swap(double &x, double &y) { std::swap(x, y); } + +#pragma clang force_cuda_host_device end -// CHECK-NOT: @_Exp( -// CHECK-NOT: @_FExp( -// CHECK-NOT: @_Cosh( -// CHECK-NOT: @_FSinh( +// DEV-NOT: @_Exp( +// DEV-NOT: @_FExp( +// DEV-NOT: @_Cosh( +// DEV-NOT: @_FSinh( _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
