Skip to content

[SYCL] Add implicit rpath to libsycl directory #6306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -5116,6 +5116,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<sycl_Group>, Flags<[CC1Option]>,
HelpText<"SYCL only. Specify that single precision floating-point sqrt is correctly rounded.">,
MarshallingInfoFlag<CodeGenOpts<"SYCLFp32PrecSqrt">>;
defm sycl_implicit_rpath: BooleanFFlag<"sycl-implicit-rpath">,
Group<sycl_Group>, HelpText<"Set rpath on SYCL executables">;

//===----------------------------------------------------------------------===//
// FLangOption + NoXarchOption
Expand Down
15 changes: 15 additions & 0 deletions clang/lib/Driver/ToolChains/Gnu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,21 @@ 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,
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));
}

// Use of -fintelfpga implies -lOpenCL.
// FIXME: Adjust to use plugin interface when available.
if (Args.hasArg(options::OPT_fintelfpga))
Expand Down
16 changes: 16 additions & 0 deletions clang/test/Driver/sycl-rpath.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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

5 changes: 5 additions & 0 deletions sycl/doc/UsersManual.md
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,11 @@ 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.
Comment on lines +343 to +345
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which path is going to be used if I have both of them: build and installation directories?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's tied to the clang binary, so it will point to the associated lib directory, for example:

$ ./build/bin/clang++ -fsycl a.cpp -o a
# RPATH will point to "/path/to/build/lib"
$ ./install/bin/clang++ -fsycl a.cpp -o a
# RPATH will point to "/path/to/install/lib"

And when it comes to resolving which libraries to use RPATH is looked up before the system libraries, so even if you have DPC++ installed on your system, binaries generated with the clang++ from your build directory will use the libsycl.so from your build directory as well.


**`-f[no-]sycl-esimd-force-stateless-mem`** [EXPERIMENTAL]

Expand Down