diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index 11ddac412a074..dcb3d28e9efa4 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -3772,16 +3772,6 @@ class OffloadingActionBuilder final { for (auto SDA : SYCLDeviceActions) SYCLLinkBinaryList.push_back(SDA); if (WrapDeviceOnlyBinary) { - // If used without -fintelfpga, -fsycl-link is used to wrap device - // objects for future host link. Device libraries should be linked - // by default to resolve any undefined reference. - if (!Args.hasArg(options::OPT_fintelfpga)) { - const auto *TC = ToolChains.front(); - addSYCLDeviceLibs(TC, SYCLLinkBinaryList, true, - C.getDefaultToolChain() - .getTriple() - .isWindowsMSVCEnvironment()); - } // -fsycl-link behavior does the following to the unbundled device // binaries: // 1) Link them together using llvm-link @@ -3948,92 +3938,6 @@ class OffloadingActionBuilder final { SYCLDeviceActions.clear(); } - void addSYCLDeviceLibs(const ToolChain *TC, ActionList &DeviceLinkObjects, - bool isSpirvAOT, bool isMSVCEnv) { - enum SYCLDeviceLibType { - sycl_devicelib_wrapper, - sycl_devicelib_fallback - }; - struct DeviceLibOptInfo { - StringRef devicelib_name; - StringRef devicelib_option; - }; - - bool NoDeviceLibs = false; - // Currently, libc, libm-fp32 will be linked in by default. In order - // to use libm-fp64, -fsycl-device-lib=libm-fp64/all should be used. - llvm::StringMap devicelib_link_info = { - {"libc", true}, {"libm-fp32", true}, {"libm-fp64", false}}; - if (Arg *A = Args.getLastArg(options::OPT_fsycl_device_lib_EQ, - options::OPT_fno_sycl_device_lib_EQ)) { - if (A->getValues().size() == 0) - C.getDriver().Diag(diag::warn_drv_empty_joined_argument) - << A->getAsString(Args); - else { - if (A->getOption().matches(options::OPT_fno_sycl_device_lib_EQ)) - NoDeviceLibs = true; - - for (StringRef Val : A->getValues()) { - if (Val == "all") { - for (auto &K : devicelib_link_info.keys()) - devicelib_link_info[K] = true && !NoDeviceLibs; - break; - } - auto LinkInfoIter = devicelib_link_info.find(Val); - if (LinkInfoIter == devicelib_link_info.end()) { - C.getDriver().Diag(diag::err_drv_unsupported_option_argument) - << A->getOption().getName() << Val; - } - devicelib_link_info[Val] = true && !NoDeviceLibs; - } - } - } - - SmallString<128> LibLoc(TC->getDriver().Dir); - llvm::sys::path::append(LibLoc, "/../lib"); - StringRef LibSuffix = isMSVCEnv ? ".obj" : ".o"; - SmallVector sycl_device_wrapper_libs = { - {"libsycl-crt", "libc"}, - {"libsycl-complex", "libm-fp32"}, - {"libsycl-complex-fp64", "libm-fp64"}, - {"libsycl-cmath", "libm-fp32"}, - {"libsycl-cmath-fp64", "libm-fp64"}}; - // For AOT compilation, we need to link sycl_device_fallback_libs as - // default too. - SmallVector sycl_device_fallback_libs = { - {"libsycl-fallback-cassert", "libc"}, - {"libsycl-fallback-complex", "libm-fp32"}, - {"libsycl-fallback-complex-fp64", "libm-fp64"}, - {"libsycl-fallback-cmath", "libm-fp32"}, - {"libsycl-fallback-cmath-fp64", "libm-fp64"}}; - auto addInputs = [&](SYCLDeviceLibType t) { - auto sycl_libs = (t == sycl_devicelib_wrapper) - ? sycl_device_wrapper_libs - : sycl_device_fallback_libs; - for (const DeviceLibOptInfo &Lib : sycl_libs) { - if (!devicelib_link_info[Lib.devicelib_option]) - continue; - SmallString<128> LibName(LibLoc); - llvm::sys::path::append(LibName, Lib.devicelib_name); - llvm::sys::path::replace_extension(LibName, LibSuffix); - if (llvm::sys::fs::exists(LibName)) { - Arg *InputArg = MakeInputArg(Args, C.getDriver().getOpts(), - Args.MakeArgString(LibName)); - auto *SYCLDeviceLibsInputAction = - C.MakeAction(*InputArg, types::TY_Object); - auto *SYCLDeviceLibsUnbundleAction = - C.MakeAction( - SYCLDeviceLibsInputAction); - addDeviceDepences(SYCLDeviceLibsUnbundleAction); - DeviceLinkObjects.push_back(SYCLDeviceLibsUnbundleAction); - } - } - }; - addInputs(sycl_devicelib_wrapper); - if (isSpirvAOT) - addInputs(sycl_devicelib_fallback); - } - void appendLinkDependences(OffloadAction::DeviceDependences &DA) override { assert(ToolChains.size() == DeviceLinkerInputs.size() && "Toolchains and linker inputs sizes do not match."); @@ -4125,15 +4029,7 @@ class OffloadingActionBuilder final { else LinkObjects.push_back(Input); } - // FIXME: Link all wrapper and fallback device libraries as default, - // When spv online link is supported by all backends, the fallback - // device libraries are only needed when current toolchain is using - // AOT compilation. - if (!isNVPTX) { - addSYCLDeviceLibs( - *TC, LinkObjects, true, - C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment()); - } + // The linkage actions subgraph leading to the offload wrapper. // [cond] Means incoming/outgoing dependence is created only when cond // is true. A function of: diff --git a/clang/lib/Driver/ToolChains/SYCL.cpp b/clang/lib/Driver/ToolChains/SYCL.cpp index efe54a0445f23..6137a3cb636d5 100644 --- a/clang/lib/Driver/ToolChains/SYCL.cpp +++ b/clang/lib/Driver/ToolChains/SYCL.cpp @@ -5,7 +5,6 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// - #include "SYCL.h" #include "CommonArgs.h" #include "InputInfo.h" @@ -417,10 +416,96 @@ SYCLToolChain::SYCLToolChain(const Driver &D, const llvm::Triple &Triple, getProgramPaths().push_back(getDriver().Dir); } +static bool doLLVMBCEmit(llvm::opt::ArgStringList &CC1Args) { + for (auto StrArg : CC1Args) + if (std::string(StrArg) == "-emit-llvm-bc") + return true; + return false; +} + +void SYCLToolChain::AddSYCLDeviceLibs(const llvm::opt::ArgList &Args, + llvm::opt::ArgStringList &CC1Args) const { + enum SYCLDeviceLibType { sycl_devicelib_wrapper, sycl_devicelib_fallback }; + struct DeviceLibOptInfo { + StringRef devicelib_name; + StringRef devicelib_option; + }; + + bool NoDeviceLibs = false; + // Currently, libc, libm-fp32 will be linked in by default. In order + // to use libm-fp64, -fsycl-device-lib=libm-fp64/all should be used. + llvm::StringMap devicelib_link_info = { + {"libc", true}, {"libm-fp32", true}, {"libm-fp64", true}}; + if (Arg *A = Args.getLastArg(options::OPT_fsycl_device_lib_EQ, + options::OPT_fno_sycl_device_lib_EQ)) { + if (A->getValues().size() == 0) + getDriver().Diag(diag::warn_drv_empty_joined_argument) + << A->getAsString(Args); + else { + if (A->getOption().matches(options::OPT_fno_sycl_device_lib_EQ)) + NoDeviceLibs = true; + + for (StringRef Val : A->getValues()) { + if (Val == "all") { + for (auto &K : devicelib_link_info.keys()) + devicelib_link_info[K] = true && !NoDeviceLibs; + break; + } + auto LinkInfoIter = devicelib_link_info.find(Val); + if (LinkInfoIter == devicelib_link_info.end()) { + getDriver().Diag(diag::err_drv_unsupported_option_argument) + << A->getOption().getName() << Val; + } + devicelib_link_info[Val] = true && !NoDeviceLibs; + } + } + } + bool isSpirvAOT = true; + SmallString<128> LibLoc(getDriver().Dir); + llvm::sys::path::append(LibLoc, "/../lib"); + SmallVector sycl_device_wrapper_libs = { + {"libsycl-crt", "libc"}, + {"libsycl-complex", "libm-fp32"}, + {"libsycl-complex-fp64", "libm-fp64"}, + {"libsycl-cmath", "libm-fp32"}, + {"libsycl-cmath-fp64", "libm-fp64"}}; + // For AOT compilation, we need to link sycl_device_fallback_libs as + // default too. + SmallVector sycl_device_fallback_libs = { + {"libsycl-fallback-cassert", "libc"}, + {"libsycl-fallback-complex", "libm-fp32"}, + {"libsycl-fallback-complex-fp64", "libm-fp64"}, + {"libsycl-fallback-cmath", "libm-fp32"}, + {"libsycl-fallback-cmath-fp64", "libm-fp64"}}; + + auto addInputs = [&](SYCLDeviceLibType t, StringRef SYCLArchName) { + auto sycl_libs = (t == sycl_devicelib_wrapper) ? sycl_device_wrapper_libs + : sycl_device_fallback_libs; + for (const DeviceLibOptInfo &Lib : sycl_libs) { + if (!devicelib_link_info[Lib.devicelib_option]) + continue; + SmallString<128> LibName(LibLoc); + llvm::sys::path::append(LibName, Lib.devicelib_name); + LibName.append("-"); + LibName.append(SYCLArchName); + llvm::sys::path::replace_extension(LibName, ".bc"); + if (llvm::sys::fs::exists(LibName)) { + CC1Args.push_back("-mlink-builtin-bitcode"); + CC1Args.push_back(Args.MakeArgString(LibName.c_str())); + } + } + }; + addInputs(sycl_devicelib_wrapper, getTriple().getArchName()); + if (isSpirvAOT) + addInputs(sycl_devicelib_fallback, getTriple().getArchName()); +} + void SYCLToolChain::addClangTargetOptions( const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, Action::OffloadKind DeviceOffloadingKind) const { + if (DeviceOffloadingKind == Action::OFK_SYCL && doLLVMBCEmit(CC1Args)) + AddSYCLDeviceLibs(DriverArgs, CC1Args); HostTC.addClangTargetOptions(DriverArgs, CC1Args, DeviceOffloadingKind); } diff --git a/clang/lib/Driver/ToolChains/SYCL.h b/clang/lib/Driver/ToolChains/SYCL.h index 975c9127c89f8..a33d1b76691a0 100644 --- a/clang/lib/Driver/ToolChains/SYCL.h +++ b/clang/lib/Driver/ToolChains/SYCL.h @@ -162,6 +162,8 @@ class LLVM_LIBRARY_VISIBILITY SYCLToolChain : public ToolChain { void TranslateTargetOpt(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, llvm::opt::OptSpecifier Opt, llvm::opt::OptSpecifier Opt_EQ) const; + void AddSYCLDeviceLibs(const llvm::opt::ArgList &DriverArgs, + llvm::opt::ArgStringList &CC1Args) const; }; } // end namespace toolchains diff --git a/clang/test/Driver/sycl-intelfpga-static-lib.cpp b/clang/test/Driver/sycl-intelfpga-static-lib.cpp index 1a77afd98465a..a6d7149d014fb 100644 --- a/clang/test/Driver/sycl-intelfpga-static-lib.cpp +++ b/clang/test/Driver/sycl-intelfpga-static-lib.cpp @@ -6,7 +6,7 @@ // make dummy archive // Build a fat static lib that will be used for all tests // RUN: echo "void foo(void) {}" > %t1.cpp -// RUN: %clangxx -target x86_64-unknown-linux-gnu -fintelfpga -fsycl %t1.cpp -c -o %t1_bundle.o +// RUN: %clangxx -target x86_64-unknown-linux-gnu -fintelfpga -fsycl -fno-sycl-device-lib=all %t1.cpp -c -o %t1_bundle.o // RUN: llvm-ar cr %t.a %t1_bundle.o /// Check phases with static lib diff --git a/clang/test/Driver/sycl-offload-static-lib.cpp b/clang/test/Driver/sycl-offload-static-lib.cpp index 95cb56245d539..0a3fe36ef03fa 100644 --- a/clang/test/Driver/sycl-offload-static-lib.cpp +++ b/clang/test/Driver/sycl-offload-static-lib.cpp @@ -7,7 +7,7 @@ /// test behaviors of passing a fat static lib // Build a fat static lib that will be used for all tests // RUN: echo "void foo(void) {}" > %t1.cpp -// RUN: %clangxx -target x86_64-unknown-linux-gnu -fsycl %t1.cpp -c -o %t1_bundle.o +// RUN: %clangxx -target x86_64-unknown-linux-gnu -fsycl -fno-sycl-device-lib=all %t1.cpp -c -o %t1_bundle.o // RUN: llvm-ar cr %t.a %t1_bundle.o // RUN: llvm-ar cr %t_2.a %t1_bundle.o @@ -76,7 +76,7 @@ /// ########################################################################### // RUN: touch %t.a -// RUN: %clangxx -target x86_64-unknown-linux-gnu -fsycl -foffload-static-lib=%t.a -### %s 2>&1 \ +// RUN: %clangxx -target x86_64-unknown-linux-gnu -fsycl -fno-sycl-device-lib=all -foffload-static-lib=%t.a -### %s 2>&1 \ // RUN: | FileCheck %s -check-prefix=FOFFLOAD_STATIC_LIB_SRC2 // FOFFLOAD_STATIC_LIB_SRC2: clang{{.*}} "-emit-obj" {{.*}} "-o" "[[HOSTOBJ:.+\.o]]" // FOFFLOAD_STATIC_LIB_SRC2: ld{{(.exe)?}}" "-r" "-o" {{.*}} "[[HOSTOBJ]]" "[[INPUT:.+\.a]]" @@ -86,7 +86,7 @@ /// ########################################################################### // RUN: touch %t.a -// RUN: %clangxx -target x86_64-unknown-linux-gnu -fsycl -foffload-static-lib=%t.a -o output_name -lOpenCL -### %s 2>&1 \ +// RUN: %clangxx -target x86_64-unknown-linux-gnu -fsycl -fno-sycl-device-lib=all -foffload-static-lib=%t.a -o output_name -lOpenCL -### %s 2>&1 \ // RUN: | FileCheck %s -check-prefix=FOFFLOAD_STATIC_LIB_SRC3 // FOFFLOAD_STATIC_LIB_SRC3: ld{{(.exe)?}}" "-r" "-o" {{.*}} "[[INPUT:.+\.a]]" // FOFFLOAD_STATIC_LIB_SRC3: clang-offload-bundler{{.*}} "-type=oo" diff --git a/libdevice/cmake/modules/SYCLLibdevice.cmake b/libdevice/cmake/modules/SYCLLibdevice.cmake index 00bb9435a1b37..c26dc9d8d00fc 100644 --- a/libdevice/cmake/modules/SYCLLibdevice.cmake +++ b/libdevice/cmake/modules/SYCLLibdevice.cmake @@ -1,22 +1,13 @@ set(obj_binary_dir "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}") if (WIN32) - set(lib-suffix obj) set(spv_binary_dir "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}") - set(lib_crt_source msvc_wrapper.cpp) + set(libcrt_source msvc_wrapper.cpp) else() - set(lib-suffix o) set(spv_binary_dir "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}") - set(lib_crt_source glibc_wrapper.cpp) + set(libcrt_source glibc_wrapper.cpp) endif() set(clang $) -string(CONCAT sycl_targets_opt - "-fsycl-targets=" - "spir64_x86_64-unknown-unknown-sycldevice," - "spir64_gen-unknown-unknown-sycldevice," - "spir64_fpga-unknown-unknown-sycldevice," - "spir64-unknown-unknown-sycldevice") - set(compile_opts # suppress an error about SYCL_EXTERNAL being used for # a function with a raw pointer parameter. @@ -29,153 +20,81 @@ set(compile_opts -sycl-std=2017 ) -set(devicelib-obj-file ${obj_binary_dir}/libsycl-crt.${lib-suffix}) -add_custom_command(OUTPUT ${devicelib-obj-file} - COMMAND ${clang} -fsycl -c - ${compile_opts} ${sycl_targets_opt} - ${CMAKE_CURRENT_SOURCE_DIR}/${lib_crt_source} - -o ${devicelib-obj-file} - MAIN_DEPENDENCY ${lib_crt_source} - DEPENDS wrapper.h device.h spirv_vars.h clang clang-offload-bundler - VERBATIM) - -set(devicelib-obj-complex ${obj_binary_dir}/libsycl-complex.${lib-suffix}) -add_custom_command(OUTPUT ${devicelib-obj-complex} - COMMAND ${clang} -fsycl -c - ${compile_opts} ${sycl_targets_opt} - ${CMAKE_CURRENT_SOURCE_DIR}/complex_wrapper.cpp - -o ${devicelib-obj-complex} - MAIN_DEPENDENCY complex_wrapper.cpp - DEPENDS device_complex.h device.h clang clang-offload-bundler - VERBATIM) - -set(devicelib-obj-complex-fp64 ${obj_binary_dir}/libsycl-complex-fp64.${lib-suffix}) -add_custom_command(OUTPUT ${devicelib-obj-complex-fp64} - COMMAND ${clang} -fsycl -c - ${compile_opts} ${sycl_targets_opt} - ${CMAKE_CURRENT_SOURCE_DIR}/complex_wrapper_fp64.cpp - -o ${devicelib-obj-complex-fp64} - MAIN_DEPENDENCY complex_wrapper_fp64.cpp - DEPENDS device_complex.h device.h clang clang-offload-bundler - VERBATIM) - -set(devicelib-obj-cmath ${obj_binary_dir}/libsycl-cmath.${lib-suffix}) -add_custom_command(OUTPUT ${devicelib-obj-cmath} - COMMAND ${clang} -fsycl -c - ${compile_opts} ${sycl_targets_opt} - ${CMAKE_CURRENT_SOURCE_DIR}/cmath_wrapper.cpp - -o ${devicelib-obj-cmath} - MAIN_DEPENDENCY cmath_wrapper.cpp - DEPENDS device_math.h device.h clang clang-offload-bundler - VERBATIM) - -set(devicelib-obj-cmath-fp64 ${obj_binary_dir}/libsycl-cmath-fp64.${lib-suffix}) -add_custom_command(OUTPUT ${devicelib-obj-cmath-fp64} - COMMAND ${clang} -fsycl -c - ${compile_opts} ${sycl_targets_opt} - ${CMAKE_CURRENT_SOURCE_DIR}/cmath_wrapper_fp64.cpp - -o ${devicelib-obj-cmath-fp64} - MAIN_DEPENDENCY cmath_wrapper_fp64.cpp - DEPENDS device_math.h device.h clang clang-offload-bundler - VERBATIM) - -add_custom_command(OUTPUT ${spv_binary_dir}/libsycl-fallback-cassert.spv - COMMAND ${clang} -S -fsycl-device-only -fno-sycl-use-bitcode - ${compile_opts} - ${CMAKE_CURRENT_SOURCE_DIR}/fallback-cassert.cpp - -o ${spv_binary_dir}/libsycl-fallback-cassert.spv - MAIN_DEPENDENCY fallback-cassert.cpp - DEPENDS wrapper.h device.h clang spirv_vars.h llvm-spirv - VERBATIM) - -add_custom_command(OUTPUT ${obj_binary_dir}/libsycl-fallback-cassert.${lib-suffix} - COMMAND ${clang} -fsycl -c - ${compile_opts} ${sycl_targets_opt} - ${CMAKE_CURRENT_SOURCE_DIR}/fallback-cassert.cpp - -o ${obj_binary_dir}/libsycl-fallback-cassert.${lib-suffix} - MAIN_DEPENDENCY fallback-cassert.cpp - DEPENDS wrapper.h device.h clang spirv_vars.h clang-offload-bundler - VERBATIM) - -add_custom_command(OUTPUT ${spv_binary_dir}/libsycl-fallback-complex.spv - COMMAND ${clang} -S -fsycl-device-only -fno-sycl-use-bitcode - ${compile_opts} - ${CMAKE_CURRENT_SOURCE_DIR}/fallback-complex.cpp - -o ${spv_binary_dir}/libsycl-fallback-complex.spv - MAIN_DEPENDENCY fallback-complex.cpp - DEPENDS device_math.h device_complex.h device.h clang llvm-spirv - VERBATIM) - -add_custom_command(OUTPUT ${obj_binary_dir}/libsycl-fallback-complex.${lib-suffix} - COMMAND ${clang} -fsycl -c - ${compile_opts} ${sycl_targets_opt} - ${CMAKE_CURRENT_SOURCE_DIR}/fallback-complex.cpp - -o ${obj_binary_dir}/libsycl-fallback-complex.${lib-suffix} - MAIN_DEPENDENCY fallback-complex.cpp - DEPENDS device_math.h device_complex.h device.h clang clang-offload-bundler - VERBATIM) - -add_custom_command(OUTPUT ${spv_binary_dir}/libsycl-fallback-complex-fp64.spv - COMMAND ${clang} -S -fsycl-device-only -fno-sycl-use-bitcode - ${compile_opts} - ${CMAKE_CURRENT_SOURCE_DIR}/fallback-complex-fp64.cpp - -o ${spv_binary_dir}/libsycl-fallback-complex-fp64.spv - MAIN_DEPENDENCY fallback-complex-fp64.cpp - DEPENDS device_math.h device_complex.h device.h clang llvm-spirv - VERBATIM) - -add_custom_command(OUTPUT ${obj_binary_dir}/libsycl-fallback-complex-fp64.${lib-suffix} - COMMAND ${clang} -fsycl -c - ${compile_opts} ${sycl_targets_opt} - ${CMAKE_CURRENT_SOURCE_DIR}/fallback-complex-fp64.cpp - -o ${obj_binary_dir}/libsycl-fallback-complex-fp64.${lib-suffix} - MAIN_DEPENDENCY fallback-complex-fp64.cpp - DEPENDS device_math.h device_complex.h device.h clang clang-offload-bundler - VERBATIM) - -add_custom_command(OUTPUT ${spv_binary_dir}/libsycl-fallback-cmath.spv - COMMAND ${clang} -S -fsycl-device-only -fno-sycl-use-bitcode - ${compile_opts} - ${CMAKE_CURRENT_SOURCE_DIR}/fallback-cmath.cpp - -o ${spv_binary_dir}/libsycl-fallback-cmath.spv - MAIN_DEPENDENCY fallback-cmath.cpp - DEPENDS device_math.h device.h clang llvm-spirv - VERBATIM) - -add_custom_command(OUTPUT ${obj_binary_dir}/libsycl-fallback-cmath.${lib-suffix} - COMMAND ${clang} -fsycl -c - ${compile_opts} ${sycl_targets_opt} - ${CMAKE_CURRENT_SOURCE_DIR}/fallback-cmath.cpp - -o ${obj_binary_dir}/libsycl-fallback-cmath.${lib-suffix} - MAIN_DEPENDENCY fallback-cmath.cpp - DEPENDS device_math.h device.h clang clang-offload-bundler - VERBATIM) - -add_custom_command(OUTPUT ${spv_binary_dir}/libsycl-fallback-cmath-fp64.spv - COMMAND ${clang} -S -fsycl-device-only -fno-sycl-use-bitcode - ${compile_opts} - ${CMAKE_CURRENT_SOURCE_DIR}/fallback-cmath-fp64.cpp - -o ${spv_binary_dir}/libsycl-fallback-cmath-fp64.spv - MAIN_DEPENDENCY fallback-cmath-fp64.cpp - DEPENDS device_math.h device.h clang llvm-spirv - VERBATIM) - -add_custom_command(OUTPUT ${obj_binary_dir}/libsycl-fallback-cmath-fp64.${lib-suffix} - COMMAND ${clang} -fsycl -c - ${compile_opts} ${sycl_targets_opt} - ${CMAKE_CURRENT_SOURCE_DIR}/fallback-cmath-fp64.cpp - -o ${obj_binary_dir}/libsycl-fallback-cmath-fp64.${lib-suffix} - MAIN_DEPENDENCY fallback-cmath-fp64.cpp - DEPENDS device_math.h device.h clang clang-offload-bundler - VERBATIM) - -add_custom_target(libsycldevice-obj DEPENDS - ${devicelib-obj-file} - ${devicelib-obj-complex} - ${devicelib-obj-complex-fp64} - ${devicelib-obj-cmath} - ${devicelib-obj-cmath-fp64} -) +function(BuildSYCLDeviceLib) + cmake_parse_arguments(DeviceLib "" "Path;Target;Source" "Depends" ${ARGN}) + add_custom_command(OUTPUT ${DeviceLib_Path} + COMMAND ${clang} -fsycl -fsycl-device-only -emit-llvm + ${compile_opts} -fsycl-targets=${DeviceLib_Target} + ${CMAKE_CURRENT_SOURCE_DIR}/${DeviceLib_Source} + -o ${DeviceLib_Path} -fno-sycl-device-lib=all + MAIN_DEPENDENCY ${DeviceLib_Source} + DEPENDS ${DeviceLib_Depends} clang device.h + VERBATIM) +endfunction() + +set(SYCLWrapperLibs crt complex complex-fp64 cmath cmath-fp64) +set(SYCLFallbackLibs cassert complex complex-fp64 cmath cmath-fp64) +set(SYCLTargetTriples spir64 spir64_x86_64 spir64_gen spir64_fpga) + +foreach(TTriple ${SYCLTargetTriples}) + BuildSYCLDeviceLib(Path ${obj_binary_dir}/libsycl-crt-${TTriple}.bc Target ${TTriple}-unknown-unknown-sycldevice Source ${libcrt_source} Depends devicelib_assert.h spirv_vars.h) + BuildSYCLDeviceLib(Path ${obj_binary_dir}/libsycl-fallback-cassert-${TTriple}.bc Target ${TTriple}-unknown-unknown-sycldevice Source fallback-cassert.cpp Depends devicelib_assert.h spirv_vars.h) +endforeach() + +foreach(TTriple ${SYCLTargetTriples}) + BuildSYCLDeviceLib(Path ${obj_binary_dir}/libsycl-complex-${TTriple}.bc Target ${TTriple}-unknown-unknown-sycldevice Source complex_wrapper.cpp Depends device_complex.h) + BuildSYCLDeviceLib(Path ${obj_binary_dir}/libsycl-fallback-complex-${TTriple}.bc Target ${TTriple}-unknown-unknown-sycldevice Source fallback-complex.cpp Depends device_math.h device_complex.h) +endforeach() + +foreach(TTriple ${SYCLTargetTriples}) + BuildSYCLDeviceLib(Path ${obj_binary_dir}/libsycl-complex-fp64-${TTriple}.bc Target ${TTriple}-unknown-unknown-sycldevice Source complex_wrapper_fp64.cpp Depends device_complex.h) + BuildSYCLDeviceLib(Path ${obj_binary_dir}/libsycl-fallback-complex-fp64-${TTriple}.bc Target ${TTriple}-unknown-unknown-sycldevice Source fallback-complex-fp64.cpp Depends device_math.h device_complex.h) +endforeach() + +foreach(TTriple ${SYCLTargetTriples}) + BuildSYCLDeviceLib(Path ${obj_binary_dir}/libsycl-cmath-${TTriple}.bc Target ${TTriple}-unknown-unknown-sycldevice Source cmath_wrapper.cpp Depends device_math.h) + BuildSYCLDeviceLib(Path ${obj_binary_dir}/libsycl-fallback-cmath-${TTriple}.bc Target ${TTriple}-unknown-unknown-sycldevice Source fallback-cmath.cpp Depends device_math.h) +endforeach() + +foreach(TTriple ${SYCLTargetTriples}) + BuildSYCLDeviceLib(Path ${obj_binary_dir}/libsycl-cmath-fp64-${TTriple}.bc Target ${TTriple}-unknown-unknown-sycldevice Source cmath_wrapper_fp64.cpp Depends device_math.h) + BuildSYCLDeviceLib(Path ${obj_binary_dir}/libsycl-fallback-cmath-fp64-${TTriple}.bc Target ${TTriple}-unknown-unknown-sycldevice Source fallback-cmath-fp64.cpp Depends device_math.h) +endforeach() + +function(BuildSYCLFallbackDeviceLib) + cmake_parse_arguments(DeviceLib "" "Path;Source" "Depends" ${ARGN}) + add_custom_command(OUTPUT ${DeviceLib_Path} + COMMAND ${clang} -fsycl -fsycl-device-only -fno-sycl-use-bitcode + ${compile_opts} + ${CMAKE_CURRENT_SOURCE_DIR}/${DeviceLib_Source} + -o ${DeviceLib_Path} -fno-sycl-device-lib=all + MAIN_DEPENDENCY ${DeviceLib_Source} + DEPENDS ${DeviceLib_Depends} llvm-spirv clang device.h + VERBATIM) +endfunction() + +BuildSYCLFallbackDeviceLib(Path ${spv_binary_dir}/libsycl-fallback-cassert.spv Source fallback-cassert.cpp Depends devicelib_assert.h spirv_vars.h) +BuildSYCLFallbackDeviceLib(Path ${spv_binary_dir}/libsycl-fallback-complex.spv Source fallback-complex.cpp Depends device_math.h device_complex.h) +BuildSYCLFallbackDeviceLib(Path ${spv_binary_dir}/libsycl-fallback-complex-fp64.spv Source fallback-complex-fp64.cpp Depends device_math.h device_complex.h) +BuildSYCLFallbackDeviceLib(Path ${spv_binary_dir}/libsycl-fallback-cmath.spv Source fallback-cmath.cpp Depends device_math.h) +BuildSYCLFallbackDeviceLib(Path ${spv_binary_dir}/libsycl-fallback-cmath-fp64.spv Source fallback-cmath-fp64.cpp Depends device_math.h) + +set(sycl-wrapper-lib-files "") +foreach(SYCLLib ${SYCLWrapperLibs}) + foreach(TTriple ${SYCLTargetTriples}) + list(APPEND sycl-wrapper-lib-files ${obj_binary_dir}/libsycl-${SYCLLib}-${TTriple}.bc) + endforeach() +endforeach() + +set(sycl-fallback-lib-files "") +foreach(SYCLLib ${SYCLFallbackLibs}) + foreach(TTriple ${SYCLTargetTriples}) + list(APPEND sycl-fallback-lib-files ${obj_binary_dir}/libsycl-fallback-${SYCLLib}-${TTriple}.bc) + endforeach() +endforeach() + +add_custom_target(libsycldevice-obj DEPENDS ${sycl-wrapper-lib-files}) +add_custom_target(libsycldevice-fallback-obj DEPENDS ${sycl-fallback-lib-files}) add_custom_target(libsycldevice-spv DEPENDS ${spv_binary_dir}/libsycl-fallback-cassert.spv ${spv_binary_dir}/libsycl-fallback-complex.spv @@ -183,13 +102,6 @@ add_custom_target(libsycldevice-spv DEPENDS ${spv_binary_dir}/libsycl-fallback-cmath.spv ${spv_binary_dir}/libsycl-fallback-cmath-fp64.spv ) -add_custom_target(libsycldevice-fallback-obj DEPENDS - ${obj_binary_dir}/libsycl-fallback-cassert.${lib-suffix} - ${obj_binary_dir}/libsycl-fallback-complex.${lib-suffix} - ${obj_binary_dir}/libsycl-fallback-complex-fp64.${lib-suffix} - ${obj_binary_dir}/libsycl-fallback-cmath.${lib-suffix} - ${obj_binary_dir}/libsycl-fallback-cmath-fp64.${lib-suffix} -) add_custom_target(libsycldevice DEPENDS libsycldevice-obj libsycldevice-fallback-obj @@ -205,16 +117,7 @@ endif() set(install_dest_lib lib${LLVM_LIBDIR_SUFFIX}) -install(FILES ${devicelib-obj-file} - ${obj_binary_dir}/libsycl-fallback-cassert.${lib-suffix} - ${devicelib-obj-complex} - ${obj_binary_dir}/libsycl-fallback-complex.${lib-suffix} - ${devicelib-obj-complex-fp64} - ${obj_binary_dir}/libsycl-fallback-complex-fp64.${lib-suffix} - ${devicelib-obj-cmath} - ${obj_binary_dir}/libsycl-fallback-cmath.${lib-suffix} - ${devicelib-obj-cmath-fp64} - ${obj_binary_dir}/libsycl-fallback-cmath-fp64.${lib-suffix} +install(FILES ${sycl-wrapper-lib-files} ${sycl-fallback-lib-files} DESTINATION ${install_dest_lib} COMPONENT libsycldevice) diff --git a/libdevice/wrapper.h b/libdevice/devicelib_assert.h similarity index 82% rename from libdevice/wrapper.h rename to libdevice/devicelib_assert.h index 2116d1e55a226..d182c3a501b01 100644 --- a/libdevice/wrapper.h +++ b/libdevice/devicelib_assert.h @@ -1,4 +1,4 @@ -//==--- wrapper.h - declarations for devicelib functions -----*- C++ -*-----==// +//==- devicelib_assert.h - declarations for devicelib functions --*- C++ -*-==// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -6,8 +6,8 @@ // //===----------------------------------------------------------------------===// -#ifndef __LIBDEVICE_WRAPPER_H__ -#define __LIBDEVICE_WRAPPER_H__ +#ifndef __LIBDEVICE_ASSERT_H__ +#define __LIBDEVICE_ASSERT_H__ #include "device.h" @@ -25,4 +25,4 @@ void __devicelib_assert_fail(const char *expr, const char *file, int32_t line, uint64_t gid2, uint64_t lid0, uint64_t lid1, uint64_t lid2); #endif // __SPIR__ -#endif // __LIBDEVICE_WRAPPER_H__ +#endif // __LIBDEVICE_ASSERT_H__ diff --git a/libdevice/fallback-cassert.cpp b/libdevice/fallback-cassert.cpp index 724d4635fb0b5..c218db6383b84 100644 --- a/libdevice/fallback-cassert.cpp +++ b/libdevice/fallback-cassert.cpp @@ -6,7 +6,7 @@ // //===----------------------------------------------------------------------===// -#include "wrapper.h" +#include "devicelib_assert.h" #ifdef __SPIR__ static const __attribute__((opencl_constant)) char assert_fmt[] = diff --git a/libdevice/glibc_wrapper.cpp b/libdevice/glibc_wrapper.cpp index 6003c520cf0ff..69fdf7efe28ac 100644 --- a/libdevice/glibc_wrapper.cpp +++ b/libdevice/glibc_wrapper.cpp @@ -6,7 +6,7 @@ // //===----------------------------------------------------------------------===// -#include "wrapper.h" +#include "devicelib_assert.h" #ifdef __SPIR__ DEVICE_EXTERN_C diff --git a/libdevice/msvc_wrapper.cpp b/libdevice/msvc_wrapper.cpp index d96bc973dffc4..c35ea94f9f6a3 100644 --- a/libdevice/msvc_wrapper.cpp +++ b/libdevice/msvc_wrapper.cpp @@ -6,7 +6,7 @@ // //===----------------------------------------------------------------------===// -#include "wrapper.h" +#include "devicelib_assert.h" #ifdef __SPIR__ // Truncates a wide (16 or 32 bit) string (wstr) into an ASCII string (str).