Index: clang/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ clang/include/clang/Driver/Options.td @@ -4153,6 +4153,10 @@ 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, Index: clang/lib/Driver/ToolChains/Linux.cpp =================================================================== --- clang/lib/Driver/ToolChains/Linux.cpp +++ clang/lib/Driver/ToolChains/Linux.cpp @@ -689,9 +689,13 @@ 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"); } Index: clang/test/Driver/hip-runtime-libs-linux.hip =================================================================== --- clang/test/Driver/hip-runtime-libs-linux.hip +++ 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"