[HIP] link HIP runtime library without --hip-link

When doing combined compilation/link for HIP source
files, clang should link the HIP runtime library
automatically without --hip-link.

Reviewed by: Siu Chi Chan, Joseph Huber

Differential Revision: https://reviews.llvm.org/D156426
This commit is contained in:
Yaxun (Sam) Liu 2023-07-27 01:18:30 -04:00
parent 1d340250a8
commit 932c63550a
8 changed files with 26 additions and 12 deletions

View File

@ -67,11 +67,14 @@ To link a HIP program, use this command:
clang++ --hip-link --offload-arch=gfx906 sample.o -o sample
In the above command, the ``--hip-link`` flag instructs Clang to link the HIP runtime library. However,
the use of this flag is unnecessary if a HIP input file is already present in your program.
For convenience, Clang also supports compiling and linking in a single step:
.. code-block:: shell
clang++ --hip-link --offload-arch=gfx906 -xhip sample.cpp -o sample
clang++ --offload-arch=gfx906 -xhip sample.cpp -o sample
In the above commands, ``gfx906`` is the GPU architecture that the code is being compiled for. The supported GPU
architectures can be found in the `AMDGPU Processor Table <https://llvm.org/docs/AMDGPUUsage.html#processors>`_.
@ -85,7 +88,7 @@ You can use ``--offload-arch=native`` to automatically detect the GPU architectu
.. code-block:: shell
clang++ --hip-link --offload-arch=native -xhip sample.cpp -o sample
clang++ --offload-arch=native -xhip sample.cpp -o sample
Path Setting for Dependencies

View File

@ -2428,10 +2428,10 @@ void tools::addOpenMPDeviceRTL(const Driver &D,
<< LibOmpTargetName << ArchPrefix;
}
}
void tools::addHIPRuntimeLibArgs(const ToolChain &TC,
void tools::addHIPRuntimeLibArgs(const ToolChain &TC, Compilation &C,
const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs) {
if (Args.hasArg(options::OPT_hip_link) &&
if ((C.getActiveOffloadKinds() & Action::OFK_HIP) &&
!Args.hasArg(options::OPT_nostdlib) &&
!Args.hasArg(options::OPT_no_hip_rt)) {
TC.AddHIPRuntimeLibArgs(Args, CmdArgs);

View File

@ -138,7 +138,8 @@ void addFortranRuntimeLibraryPath(const ToolChain &TC,
const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs);
void addHIPRuntimeLibArgs(const ToolChain &TC, const llvm::opt::ArgList &Args,
void addHIPRuntimeLibArgs(const ToolChain &TC, Compilation &C,
const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs);
const char *getAsNeededOption(const ToolChain &TC, bool as_needed);

View File

@ -549,7 +549,7 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
addLinkerCompressDebugSectionsOption(ToolChain, Args, CmdArgs);
AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
addHIPRuntimeLibArgs(ToolChain, Args, CmdArgs);
addHIPRuntimeLibArgs(ToolChain, C, Args, CmdArgs);
// The profile runtime also needs access to system libraries.
getToolChain().addProfileRTLibs(Args, CmdArgs);

View File

@ -315,7 +315,7 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
A.renderAsInput(Args, CmdArgs);
}
addHIPRuntimeLibArgs(TC, Args, CmdArgs);
addHIPRuntimeLibArgs(TC, C, Args, CmdArgs);
TC.addProfileRTLibs(Args, CmdArgs);

View File

@ -43,6 +43,11 @@
// RUN: --rocm-path=%S/Inputs/rocm %t.o 2>&1 \
// RUN: | FileCheck -check-prefixes=NOHIPRT %s
// Test HIP runtime lib is linked without hip-link if there is HIP input file.
// RUN: %clang -### --target=x86_64-linux-gnu -nogpuinc -nogpulib \
// RUN: --rocm-path=%S/Inputs/rocm %s 2>&1 \
// RUN: | FileCheck -check-prefixes=ROCM-PATH %s
// 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"

View File

@ -7,4 +7,9 @@
// RUN: --rocm-path=%S/Inputs/rocm %t.o 2>&1 \
// RUN: | FileCheck %s
// Test HIP runtime lib is linked without --hip-link when there is HIP input file.
// RUN: %clang -### --target=x86_64-pc-windows-msvc \
// RUN: --rocm-path=%S/Inputs/rocm %s 2>&1 \
// RUN: | FileCheck %s
// CHECK: "-libpath:{{.*Inputs.*rocm.*lib}}" "amdhip64.lib"

View File

@ -35,32 +35,32 @@
// Test HIP_PATH overrides ROCM_PATH.
// RUN: env ROCM_PATH=%S/Inputs/rocm HIP_PATH=%t/myhip \
// RUN: %clang -### -target x86_64-linux-gnu --offload-arch=gfx1010 --hip-link \
// RUN: %clang -### -target x86_64-linux-gnu --offload-arch=gfx1010 \
// RUN: --print-rocm-search-dirs %s 2>&1 \
// RUN: | FileCheck -check-prefixes=ROCM-ENV,HIP-PATH %s
// Test --hip-path overrides ROCM_PATH.
// RUN: env ROCM_PATH=%S/Inputs/rocm \
// RUN: %clang -### -target x86_64-linux-gnu --offload-arch=gfx1010 --hip-link \
// RUN: %clang -### -target x86_64-linux-gnu --offload-arch=gfx1010 \
// RUN: --hip-path=%t/myhip \
// RUN: --print-rocm-search-dirs %s 2>&1 \
// RUN: | FileCheck -check-prefixes=ROCM-ENV,HIP-PATH %s
// Test --hip-path overrides --rocm-path.
// RUN: %clang -### -target x86_64-linux-gnu --offload-arch=gfx1010 --hip-link \
// RUN: %clang -### -target x86_64-linux-gnu --offload-arch=gfx1010 \
// RUN: --hip-path=%t/myhip --rocm-path=%S/Inputs/rocm \
// RUN: --print-rocm-search-dirs %s 2>&1 \
// RUN: | FileCheck -check-prefixes=ROCM-ENV,HIP-PATH %s
// Test HIP_PATH overrides --rocm-path.
// RUN: env HIP_PATH=%t/myhip %clang -### -target x86_64-linux-gnu --offload-arch=gfx1010 --hip-link \
// RUN: env HIP_PATH=%t/myhip %clang -### -target x86_64-linux-gnu --offload-arch=gfx1010 \
// RUN: --rocm-path=%S/Inputs/rocm \
// RUN: --print-rocm-search-dirs %s 2>&1 \
// RUN: | FileCheck -check-prefixes=ROCM-ENV,HIP-PATH %s
// Test empty HIP_PATH does not override --rocm-path.
// RUN: env HIP_PATH= \
// RUN: %clang -### -target x86_64-linux-gnu --offload-arch=gfx1010 --hip-link \
// RUN: %clang -### -target x86_64-linux-gnu --offload-arch=gfx1010 \
// RUN: --rocm-path=%S/Inputs/rocm --print-rocm-search-dirs %s 2>&1 \
// RUN: | FileCheck -check-prefixes=ROCM-PATH %s