Skip to content

Commit 476a351

Browse files
authored
[SYCL][libdevice] Split MSVC specific math functions from cmath devicelib. (#6120)
In cmath devicelib, we disabled ldexpf, hypotf, frexpf for Windows, the reason was we included "math.h" in the source and MSVC "math.h" has inlined those 3 functions in header file which would block us to implement those 3 functions, otherwise "multiple definition" error would happen in building cmath devicelib. The reason why we had to include MSVC "math.h" was we implemented some MSVC internal math functions in cmath devicelib and those internal math functions depended on some constant which are defined in MSVC math header. Now, some users reported that they encountered "undefined symbol" error for "hypotf" on SYCL GPU device when using 3rd-party math headers instead of MSVC math headers. In order to support such use case, we need to provide ldexpf, hypotf, frexpf in SYCL device library for Windows too. And in order to resolve the multiple definition error introduced by inline for hypotf, ldexpf, frexpf in MSVC math header, we have to: * split all MSVC internal function into a new source file msvc-math.cpp and we will include "math.h" in this source. * enable hypotf, ldexpf, frexpf in cmath_wrapper.cpp since there is no "include <math.h>" in the source any more, building it using clang will succeed. * we will build msvc-math.cpp to get a new .obj file libsycl-msvc-math.obj and this devicelib will be added to device link list by default and controlled by fsycl-device-lib=libm-fp32 too. Signed-off-by: jinge90 <[email protected]>
1 parent 2e3f61e commit 476a351

File tree

7 files changed

+332
-303
lines changed

7 files changed

+332
-303
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4754,12 +4754,17 @@ class OffloadingActionBuilder final {
47544754
SYCLTC->SYCLInstallation.getSYCLDeviceLibPath(LibLocCandidates);
47554755
StringRef LibSuffix = isMSVCEnv ? ".obj" : ".o";
47564756
using SYCLDeviceLibsList = SmallVector<DeviceLibOptInfo, 5>;
4757+
47574758
const SYCLDeviceLibsList sycl_device_wrapper_libs = {
4758-
{"libsycl-crt", "libc"},
4759-
{"libsycl-complex", "libm-fp32"},
4760-
{"libsycl-complex-fp64", "libm-fp64"},
4761-
{"libsycl-cmath", "libm-fp32"},
4762-
{"libsycl-cmath-fp64", "libm-fp64"}};
4759+
{"libsycl-crt", "libc"},
4760+
{"libsycl-complex", "libm-fp32"},
4761+
{"libsycl-complex-fp64", "libm-fp64"},
4762+
{"libsycl-cmath", "libm-fp32"},
4763+
{"libsycl-cmath-fp64", "libm-fp64"},
4764+
#if defined(_WIN32)
4765+
{"libsycl-msvc-math", "libm-fp32"},
4766+
#endif
4767+
};
47634768
// For AOT compilation, we need to link sycl_device_fallback_libs as
47644769
// default too.
47654770
const SYCLDeviceLibsList sycl_device_fallback_libs = {

clang/lib/Driver/ToolChains/SYCL.cpp

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -130,21 +130,16 @@ void SYCL::constructLLVMForeachCommand(Compilation &C, const JobAction &JA,
130130
// The list should match pre-built SYCL device library files located in
131131
// compiler package. Once we add or remove any SYCL device library files,
132132
// the list should be updated accordingly.
133-
static llvm::SmallVector<StringRef, 16> SYCLDeviceLibList{
134-
"crt",
135-
"cmath",
136-
"cmath-fp64",
137-
"complex",
138-
"complex-fp64",
139-
"itt-compiler-wrappers",
140-
"itt-stubs",
141-
"itt-user-wrappers",
142-
"fallback-cassert",
143-
"fallback-cstring",
144-
"fallback-cmath",
145-
"fallback-cmath-fp64",
146-
"fallback-complex",
147-
"fallback-complex-fp64"};
133+
134+
static llvm::SmallVector<StringRef, 16> SYCLDeviceLibList {
135+
"crt", "cmath", "cmath-fp64", "complex", "complex-fp64",
136+
#if defined(_WIN32)
137+
"msvc-math",
138+
#endif
139+
"itt-compiler-wrappers", "itt-stubs", "itt-user-wrappers",
140+
"fallback-cassert", "fallback-cstring", "fallback-cmath",
141+
"fallback-cmath-fp64", "fallback-complex", "fallback-complex-fp64"
142+
};
148143

149144
const char *SYCL::Linker::constructLLVMLinkCommand(
150145
Compilation &C, const JobAction &JA, const InputInfo &Output,

clang/test/Driver/sycl-device-lib-win.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
// SYCL_DEVICE_LIB_UNBUNDLE_DEFAULT-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-complex-fp64.obj" "-output={{.*}}libsycl-complex-fp64-{{.*}}.o" "-unbundle"
2121
// SYCL_DEVICE_LIB_UNBUNDLE_DEFAULT-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-cmath.obj" "-output={{.*}}libsycl-cmath-{{.*}}.o" "-unbundle"
2222
// SYCL_DEVICE_LIB_UNBUNDLE_DEFAULT-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-cmath-fp64.obj" "-output={{.*}}libsycl-cmath-fp64-{{.*}}.o" "-unbundle"
23+
// SYCL_DEVICE_LIB_UNBUNDLE_DEFAULT-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-msvc-math.obj" "-output={{.*}}libsycl-msvc-math-{{.*}}.o" "-unbundle"
2324

2425
/// ###########################################################################
2526
/// test sycl fallback device libraries are not linked by default
@@ -46,6 +47,7 @@
4647
// SYCL_DEVICE_LIB_UNBUNDLE_WITH_FP64-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-complex-fp64.obj" "-output={{.*}}libsycl-complex-fp64-{{.*}}.o" "-unbundle"
4748
// SYCL_DEVICE_LIB_UNBUNDLE_WITH_FP64-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-cmath.obj" "-output={{.*}}libsycl-cmath-{{.*}}.o" "-unbundle"
4849
// SYCL_DEVICE_LIB_UNBUNDLE_WITH_FP64-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-cmath-fp64.obj" "-output={{.*}}libsycl-cmath-fp64-{{.*}}.o" "-unbundle"
50+
// SYCL_DEVICE_LIB_UNBUNDLE_WITH_FP64-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-msvc-math.obj" "-output={{.*}}libsycl-msvc-math-{{.*}}.o" "-unbundle"
4951

5052
/// ###########################################################################
5153

@@ -56,6 +58,8 @@
5658
// SYCL_DEVICE_LIB_UNBUNDLE_NO_LIBC: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-complex-fp64.obj" "-output={{.*}}libsycl-complex-fp64-{{.*}}.o" "-unbundle"
5759
// SYCL_DEVICE_LIB_UNBUNDLE_NO_LIBC-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-cmath.obj" "-output={{.*}}libsycl-cmath-{{.*}}.o" "-unbundle"
5860
// SYCL_DEVICE_LIB_UNBUNDLE_NO_LIBC-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-cmath-fp64.obj" "-output={{.*}}libsycl-cmath-fp64-{{.*}}.o" "-unbundle"
61+
// SYCL_DEVICE_LIB_UNBUNDLE_NO_LIBC-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-msvc-math.obj" "-output={{.*}}libsycl-msvc-math-{{.*}}.o" "-unbundle"
62+
5963
/// ###########################################################################
6064

6165
/// test behavior of -fno-sycl-device-lib=libm-fp32,libm-fp64
@@ -107,6 +111,7 @@
107111
// SYCL_LLVM_LINK_DEVICE_LIB-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-complex-fp64.obj" "-output={{.*}}libsycl-complex-fp64-{{.*}}.o" "-unbundle"
108112
// SYCL_LLVM_LINK_DEVICE_LIB-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-cmath.obj" "-output={{.*}}libsycl-cmath-{{.*}}.o" "-unbundle"
109113
// SYCL_LLVM_LINK_DEVICE_LIB-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-cmath-fp64.obj" "-output={{.*}}libsycl-cmath-fp64-{{.*}}.o" "-unbundle"
114+
// SYCL_LLVM_LINK_DEVICE_LIB-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-msvc-math.obj" "-output={{.*}}libsycl-msvc-math-{{.*}}.o" "-unbundle"
110115
// SYCL_LLVM_LINK_DEVICE_LIB-NEXT: llvm-link{{.*}} "-only-needed" "{{.*}}" "-o" "{{.*}}.bc" "--suppress-warnings"
111116

112117
/// ###########################################################################

libdevice/cmake/modules/SYCLLibdevice.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ add_devicelib_obj(libsycl-complex SRC complex_wrapper.cpp DEP ${complex_obj_deps
9898
add_devicelib_obj(libsycl-complex-fp64 SRC complex_wrapper_fp64.cpp DEP ${complex_obj_deps} )
9999
add_devicelib_obj(libsycl-cmath SRC cmath_wrapper.cpp DEP ${cmath_obj_deps})
100100
add_devicelib_obj(libsycl-cmath-fp64 SRC cmath_wrapper_fp64.cpp DEP ${cmath_obj_deps} )
101+
if(WIN32)
102+
add_devicelib_obj(libsycl-msvc-math SRC msvc_math.cpp DEP ${cmath_obj_deps})
103+
endif()
101104

102105
add_fallback_devicelib(libsycl-fallback-cassert SRC fallback-cassert.cpp DEP ${crt_obj_deps})
103106
add_fallback_devicelib(libsycl-fallback-cstring SRC fallback-cstring.cpp DEP ${crt_obj_deps})

0 commit comments

Comments
 (0)