From cd601cd401d3d89f5de29a8db8908fe32352f30c Mon Sep 17 00:00:00 2001 From: Nicolas Miller Date: Tue, 14 Jun 2022 17:48:56 +0100 Subject: [PATCH 1/4] [SYCL] Add implicit rpath to libsycl directory This patch adds the flag `-f{no-}sycl-implicit-rpath`, which ensures that on Linux-like platforms the directory containing `libsycl.so` in the clang build or installation is added to the SYCL binaries' `RPATH`. This flag is enabled by default. This makes it easier for SYCL binaries to find `libsycl.so` and removes the need for user to set `LD_LIBRARY_PATH` in most cases. This flag is similar to the OpenMP `-fopenmp-implicit-rpath` flag. --- clang/include/clang/Basic/LangOptions.def | 1 + clang/include/clang/Driver/Options.td | 5 +++++ clang/lib/Driver/ToolChains/Gnu.cpp | 13 +++++++++++++ sycl/doc/UsersManual.md | 6 ++++++ 4 files changed, 25 insertions(+) diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def index 76de5207ab6d8..ded1775e0e48b 100644 --- a/clang/include/clang/Basic/LangOptions.def +++ b/clang/include/clang/Basic/LangOptions.def @@ -275,6 +275,7 @@ LANGOPT(SYCLIsHost , 1, 0, "SYCL host compilation") LANGOPT(SYCLAllowFuncPtr , 1, 0, "Allow function pointers in SYCL device code") LANGOPT(SYCLStdLayoutKernelParams, 1, 0, "Enable standard layout requirement for SYCL kernel parameters") LANGOPT(SYCLUnnamedLambda , 1, 0, "Allow unnamed lambda SYCL kernels") +LANGOPT(SYCLImplicitRPath , 1, 0, "Implicitely set RPath to SYCL library") ENUM_LANGOPT(SYCLVersion , SYCLMajorVersion, 2, SYCL_None, "Version of the SYCL standard used") LANGOPT(DeclareSPIRVBuiltins, 1, 0, "Declare SPIR-V builtin functions") LANGOPT(SYCLExplicitSIMD , 1, 0, "SYCL compilation with explicit SIMD extension") diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index a5759ec9a4b7e..58cd23fbe3e7c 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -4938,6 +4938,11 @@ def fno_sycl_device_lib_jit_link : Flag<["-"], "fno-sycl-device-lib-jit-link">, def fsycl_fp32_prec_sqrt : Flag<["-"], "fsycl-fp32-prec-sqrt">, Group, Flags<[CC1Option]>, HelpText<"SYCL only. Specify that single precision floating-point sqrt is correctly rounded.">, MarshallingInfoFlag>; +defm sycl_implicit_rpath: BoolFOption<"sycl-implicit-rpath", + LangOpts<"SYCLImplicitRPath">, + DefaultTrue, + PosFlag, + NegFlag>; //===----------------------------------------------------------------------===// // FLangOption + NoXarchOption diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp index cf3bab126e3e6..3a1b6153c73e6 100644 --- a/clang/lib/Driver/ToolChains/Gnu.cpp +++ b/clang/lib/Driver/ToolChains/Gnu.cpp @@ -694,6 +694,19 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA, !Args.hasArg(options::OPT_nolibsycl)) { CmdArgs.push_back("-lsycl"); CmdArgs.push_back("-lsycl-devicelib-host"); + + if (Args.hasFlag(options::OPT_fsycl_implicit_rpath, + options::OPT_fno_sycl_implicit_rpath, true)) { + // Add clang libdir to the rpath so that libsycl.so can be found at + // runtime. + SmallString<256> DefaultLibPath = + llvm::sys::path::parent_path(ToolChain.getDriver().Dir); + llvm::sys::path::append(DefaultLibPath, + Twine("lib") + CLANG_LIBDIR_SUFFIX); + CmdArgs.push_back("-rpath"); + CmdArgs.push_back(Args.MakeArgString(DefaultLibPath)); + } + // Use of -fintelfpga implies -lOpenCL. // FIXME: Adjust to use plugin interface when available. if (Args.hasArg(options::OPT_fintelfpga)) diff --git a/sycl/doc/UsersManual.md b/sycl/doc/UsersManual.md index 21a06c8968128..c490678fb6f5f 100644 --- a/sycl/doc/UsersManual.md +++ b/sycl/doc/UsersManual.md @@ -290,6 +290,12 @@ and not recommended to use in production environment. NOTE: This flag is currently only supported with the CUDA and HIP targets. +**`-fsycl-implicit-rpath, -fno-sycl-implicit-rpath`** + + Instructs the compiler to add the directory within the clang build or + installation containing the SYCL library to the `RPATH` of generated + binaries when building in SYCL mode. This flag is enabled by default. + # Example: SYCL device code compilation To invoke SYCL device compiler set `-fsycl-device-only` flag. From 426ee84d5a214d5c960cc89c7c3023f3d3acb77f Mon Sep 17 00:00:00 2001 From: Nicolas Miller Date: Wed, 15 Jun 2022 09:41:01 +0100 Subject: [PATCH 2/4] [SYCL] Switch to BooleanFFlag for rpath flag and add test --- clang/include/clang/Basic/LangOptions.def | 1 - clang/include/clang/Driver/Options.td | 7 ++----- clang/test/Driver/sycl-rpath.cpp | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 clang/test/Driver/sycl-rpath.cpp diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def index ded1775e0e48b..76de5207ab6d8 100644 --- a/clang/include/clang/Basic/LangOptions.def +++ b/clang/include/clang/Basic/LangOptions.def @@ -275,7 +275,6 @@ LANGOPT(SYCLIsHost , 1, 0, "SYCL host compilation") LANGOPT(SYCLAllowFuncPtr , 1, 0, "Allow function pointers in SYCL device code") LANGOPT(SYCLStdLayoutKernelParams, 1, 0, "Enable standard layout requirement for SYCL kernel parameters") LANGOPT(SYCLUnnamedLambda , 1, 0, "Allow unnamed lambda SYCL kernels") -LANGOPT(SYCLImplicitRPath , 1, 0, "Implicitely set RPath to SYCL library") ENUM_LANGOPT(SYCLVersion , SYCLMajorVersion, 2, SYCL_None, "Version of the SYCL standard used") LANGOPT(DeclareSPIRVBuiltins, 1, 0, "Declare SPIR-V builtin functions") LANGOPT(SYCLExplicitSIMD , 1, 0, "SYCL compilation with explicit SIMD extension") diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 58cd23fbe3e7c..0732e7ba36c2b 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -4938,11 +4938,8 @@ def fno_sycl_device_lib_jit_link : Flag<["-"], "fno-sycl-device-lib-jit-link">, def fsycl_fp32_prec_sqrt : Flag<["-"], "fsycl-fp32-prec-sqrt">, Group, Flags<[CC1Option]>, HelpText<"SYCL only. Specify that single precision floating-point sqrt is correctly rounded.">, MarshallingInfoFlag>; -defm sycl_implicit_rpath: BoolFOption<"sycl-implicit-rpath", - LangOpts<"SYCLImplicitRPath">, - DefaultTrue, - PosFlag, - NegFlag>; +defm sycl_implicit_rpath: BooleanFFlag<"sycl-implicit-rpath">, + Group, HelpText<"Set rpath on SYCL executables">; //===----------------------------------------------------------------------===// // FLangOption + NoXarchOption diff --git a/clang/test/Driver/sycl-rpath.cpp b/clang/test/Driver/sycl-rpath.cpp new file mode 100644 index 0000000000000..f21b15ac0540c --- /dev/null +++ b/clang/test/Driver/sycl-rpath.cpp @@ -0,0 +1,17 @@ +// RUN: %clang -### -fsycl %s 2>&1 | FileCheck --check-prefix=CHECK-DEFAULT %s + +// CHECK-DEFAULT: "-rpath" + +// RUN: %clang -### -fsycl \ +// RUN: -fsycl-implicit-rpath %s 2>&1 | FileCheck --check-prefix=CHECK-FLAG %s + +// CHECK-FLAG: "-rpath" + +// RUN: %clang -### -fsycl \ +// RUN: -fno-sycl-implicit-rpath %s 2>&1 | FileCheck --check-prefix=CHECK-NOFLAG %s + +// CHECK-NOFLAG-NOT: "-rpath" + +// REQUIRES: system-linux + +void func(){}; From 850533070acba4dd1654379abedc59769316d5b2 Mon Sep 17 00:00:00 2001 From: Nicolas Miller Date: Wed, 12 Oct 2022 17:30:17 +0100 Subject: [PATCH 3/4] [SYCL] Ensure absolute path for rpath --- clang/lib/Driver/ToolChains/Gnu.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp index e9cc67d453b63..db5a6af7bc63d 100644 --- a/clang/lib/Driver/ToolChains/Gnu.cpp +++ b/clang/lib/Driver/ToolChains/Gnu.cpp @@ -755,7 +755,9 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA, SmallString<256> DefaultLibPath = llvm::sys::path::parent_path(ToolChain.getDriver().Dir); llvm::sys::path::append(DefaultLibPath, - Twine("lib") + CLANG_LIBDIR_SUFFIX); + CLANG_INSTALL_LIBDIR_BASENAME); + std::error_code EC = llvm::sys::fs::make_absolute(DefaultLibPath); + assert(!EC && "Invalid aboslute path for SYCL implicit rpath"); CmdArgs.push_back("-rpath"); CmdArgs.push_back(Args.MakeArgString(DefaultLibPath)); } From 911a1206e932b0edfa02a5004d13f76580dbb54c Mon Sep 17 00:00:00 2001 From: Nicolas Miller Date: Thu, 10 Nov 2022 10:34:58 +0000 Subject: [PATCH 4/4] Update clang/test/Driver/sycl-rpath.cpp Remove unnecessary test body Co-authored-by: Alexey Bader --- clang/test/Driver/sycl-rpath.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/clang/test/Driver/sycl-rpath.cpp b/clang/test/Driver/sycl-rpath.cpp index f21b15ac0540c..c8cb01b2e0b9b 100644 --- a/clang/test/Driver/sycl-rpath.cpp +++ b/clang/test/Driver/sycl-rpath.cpp @@ -14,4 +14,3 @@ // REQUIRES: system-linux -void func(){};