From 55b43449978cc7050bef3ec255cbf5c5feee274e Mon Sep 17 00:00:00 2001 From: "Yaxun (Sam) Liu" Date: Thu, 27 Oct 2022 12:04:45 -0400 Subject: [PATCH] [HIP] add --offload-add-rpath Add an option --[no-]offload-add-rpath to control whether to pass -rpath to linker for HIP runtime library. By default it is off to match gcc/clang behavior for not adding -rpath for runtime library by default. Reviewed by: Artem Belevich Differential Revision: https://reviews.llvm.org/D136854 --- clang/include/clang/Driver/Options.td | 4 ++++ clang/lib/Driver/ToolChains/Linux.cpp | 10 +++++++--- clang/test/Driver/hip-runtime-libs-linux.hip | 9 +++++++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 7c59748..efbc758 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -4153,6 +4153,10 @@ def frtlib_add_rpath: Flag<["-"], "frtlib-add-rpath">, Flags<[NoArgumentUnused]> HelpText<"Add -rpath with architecture-specific resource directory to the linker flags">; def fno_rtlib_add_rpath: Flag<["-"], "fno-rtlib-add-rpath">, Flags<[NoArgumentUnused]>, HelpText<"Do not add -rpath with architecture-specific resource directory to the linker flags">; +def offload_add_rpath: Flag<["--"], "offload-add-rpath">, Flags<[NoArgumentUnused]>, + HelpText<"Add -rpath with HIP runtime library directory to the linker flags">; +def no_offload_add_rpath: Flag<["--"], "no-offload-add-rpath">, Flags<[NoArgumentUnused]>, + HelpText<"Do not add -rpath with HIP runtime library directory to the linker flags">; defm openmp_implicit_rpath: BoolFOption<"openmp-implicit-rpath", LangOpts<"OpenMP">, DefaultTrue, diff --git a/clang/lib/Driver/ToolChains/Linux.cpp b/clang/lib/Driver/ToolChains/Linux.cpp index ccbdda1..b6a93fe 100644 --- a/clang/lib/Driver/ToolChains/Linux.cpp +++ b/clang/lib/Driver/ToolChains/Linux.cpp @@ -689,9 +689,13 @@ void Linux::AddHIPIncludeArgs(const ArgList &DriverArgs, void Linux::AddHIPRuntimeLibArgs(const ArgList &Args, ArgStringList &CmdArgs) const { - CmdArgs.append( - {Args.MakeArgString(StringRef("-L") + RocmInstallation.getLibPath()), - "-rpath", Args.MakeArgString(RocmInstallation.getLibPath())}); + CmdArgs.push_back( + Args.MakeArgString(StringRef("-L") + RocmInstallation.getLibPath())); + + if (Args.hasFlag(options::OPT_offload_add_rpath, + options::OPT_no_offload_add_rpath, false)) + CmdArgs.append( + {"-rpath", Args.MakeArgString(RocmInstallation.getLibPath())}); CmdArgs.push_back("-lamdhip64"); } diff --git a/clang/test/Driver/hip-runtime-libs-linux.hip b/clang/test/Driver/hip-runtime-libs-linux.hip index 0b577ae..aa71798 100644 --- a/clang/test/Driver/hip-runtime-libs-linux.hip +++ b/clang/test/Driver/hip-runtime-libs-linux.hip @@ -12,6 +12,10 @@ // RUN: --target=x86_64-linux-gnu %t.o 2>&1 \ // RUN: | FileCheck -check-prefixes=ROCM-PATH %s +// RUN: %clang -### --hip-link --target=x86_64-linux-gnu \ +// RUN: --rocm-path=%S/Inputs/rocm %t.o --offload-add-rpath 2>&1 \ +// RUN: | FileCheck -check-prefixes=ROCM-RPATH %s + // Test detecting latest /opt/rocm-{release} directory. // RUN: rm -rf %t && mkdir -p %t/opt // RUN: cp -r %S/Inputs/rocm %t/opt/rocm-3.9.0-1234 @@ -35,8 +39,9 @@ // RUN: --rocm-path=%S/Inputs/rocm %t.o 2>&1 \ // RUN: | FileCheck -check-prefixes=NOHIPRT %s -// ROCM-PATH: "-L[[HIPRT:.*/Inputs/rocm/lib]]" "-rpath" "[[HIPRT]]" "-lamdhip64" -// ROCM-REL: "-L[[HIPRT:.*/opt/rocm-3.10.0/lib]]" "-rpath" "[[HIPRT]]" "-lamdhip64" +// ROCM-PATH: "-L[[HIPRT:.*/Inputs/rocm/lib]]" "-lamdhip64" +// ROCM-RPATH: "-L[[HIPRT:.*/Inputs/rocm/lib]]" "-rpath" "[[HIPRT]]" "-lamdhip64" +// ROCM-REL: "-L[[HIPRT:.*/opt/rocm-3.10.0/lib]]" "-lamdhip64" // NOHIPRT-NOT: "-L{{.*/Inputs/rocm/lib}}" // NOHIPRT-NOT: "-rpath" "{{.*/Inputs/rocm/lib}}" // NOHIPRT-NOT: "-lamdhip64" -- 2.7.4