Skip to content

Commit 062317f

Browse files
committed
[HIP] Add HIP runtime library arguments for linker
Add -L -l options for linker. Reviewed by: Artem Belevich Differential Revision: https://reviews.llvm.org/D124545
1 parent 9861ca0 commit 062317f

File tree

11 files changed

+103
-0
lines changed

11 files changed

+103
-0
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,8 @@ def cuda_gpu_arch_EQ : Joined<["--"], "cuda-gpu-arch=">, Flags<[NoXarchOption]>,
927927
Alias<offload_arch_EQ>;
928928
def hip_link : Flag<["--"], "hip-link">,
929929
HelpText<"Link clang-offload-bundler bundles for HIP">;
930+
def no_hip_rt: Flag<["-"], "no-hip-rt">,
931+
HelpText<"Do not link against HIP runtime libraries">;
930932
def no_offload_arch_EQ : Joined<["--"], "no-offload-arch=">, Flags<[NoXarchOption]>,
931933
HelpText<"Remove CUDA/HIP offloading device architecture (e.g. sm_35, gfx906) from the list of devices to compile for. "
932934
"'all' resets the list to its default value.">;

clang/include/clang/Driver/ToolChain.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,11 @@ class ToolChain {
696696
virtual llvm::SmallVector<BitCodeLibraryInfo, 12>
697697
getHIPDeviceLibs(const llvm::opt::ArgList &Args) const;
698698

699+
/// Add the system specific linker arguments to use
700+
/// for the given HIP runtime library type.
701+
virtual void AddHIPRuntimeLibArgs(const llvm::opt::ArgList &Args,
702+
llvm::opt::ArgStringList &CmdArgs) const {}
703+
699704
/// Return sanitizers which are available in this toolchain.
700705
virtual SanitizerMask getSupportedSanitizers() const;
701706

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,3 +2089,17 @@ void tools::addOpenMPDeviceRTL(const Driver &D,
20892089
<< LibOmpTargetName << ArchPrefix;
20902090
}
20912091
}
2092+
void tools::addHIPRuntimeLibArgs(const ToolChain &TC,
2093+
const llvm::opt::ArgList &Args,
2094+
llvm::opt::ArgStringList &CmdArgs) {
2095+
if (Args.hasArg(options::OPT_hip_link) &&
2096+
!Args.hasArg(options::OPT_nostdlib) &&
2097+
!Args.hasArg(options::OPT_no_hip_rt)) {
2098+
TC.AddHIPRuntimeLibArgs(Args, CmdArgs);
2099+
} else {
2100+
// Claim "no HIP libraries" arguments if any
2101+
for (auto Arg : Args.filtered(options::OPT_no_hip_rt)) {
2102+
Arg->claim();
2103+
}
2104+
}
2105+
}

clang/lib/Driver/ToolChains/CommonArgs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ bool addOpenMPRuntime(llvm::opt::ArgStringList &CmdArgs, const ToolChain &TC,
120120
bool ForceStaticHostRuntime = false,
121121
bool IsOffloadingHost = false, bool GompNeedsRT = false);
122122

123+
void addHIPRuntimeLibArgs(const ToolChain &TC, const llvm::opt::ArgList &Args,
124+
llvm::opt::ArgStringList &CmdArgs);
125+
123126
const char *getAsNeededOption(const ToolChain &TC, bool as_needed);
124127

125128
llvm::opt::Arg *getLastProfileUseArg(const llvm::opt::ArgList &Args);

clang/lib/Driver/ToolChains/Gnu.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,9 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
580580
bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs);
581581
addLinkerCompressDebugSectionsOption(ToolChain, Args, CmdArgs);
582582
AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
583+
584+
addHIPRuntimeLibArgs(ToolChain, Args, CmdArgs);
585+
583586
// The profile runtime also needs access to system libraries.
584587
getToolChain().addProfileRTLibs(Args, CmdArgs);
585588

clang/lib/Driver/ToolChains/Linux.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,15 @@ void Linux::AddHIPIncludeArgs(const ArgList &DriverArgs,
676676
RocmInstallation.AddHIPIncludeArgs(DriverArgs, CC1Args);
677677
}
678678

679+
void Linux::AddHIPRuntimeLibArgs(const ArgList &Args,
680+
ArgStringList &CmdArgs) const {
681+
CmdArgs.append(
682+
{Args.MakeArgString(StringRef("-L") + RocmInstallation.getLibPath()),
683+
"-rpath", Args.MakeArgString(RocmInstallation.getLibPath())});
684+
685+
CmdArgs.push_back("-lamdhip64");
686+
}
687+
679688
void Linux::AddIAMCUIncludeArgs(const ArgList &DriverArgs,
680689
ArgStringList &CC1Args) const {
681690
if (GCCInstallation.isValid()) {

clang/lib/Driver/ToolChains/Linux.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class LLVM_LIBRARY_VISIBILITY Linux : public Generic_ELF {
3737
llvm::opt::ArgStringList &CC1Args) const override;
3838
void AddHIPIncludeArgs(const llvm::opt::ArgList &DriverArgs,
3939
llvm::opt::ArgStringList &CC1Args) const override;
40+
void AddHIPRuntimeLibArgs(const llvm::opt::ArgList &Args,
41+
llvm::opt::ArgStringList &CmdArgs) const override;
4042
void AddIAMCUIncludeArgs(const llvm::opt::ArgList &DriverArgs,
4143
llvm::opt::ArgStringList &CC1Args) const override;
4244
RuntimeLibType GetDefaultRuntimeLibType() const override;

clang/lib/Driver/ToolChains/MSVC.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
286286
A.renderAsInput(Args, CmdArgs);
287287
}
288288

289+
addHIPRuntimeLibArgs(TC, Args, CmdArgs);
290+
289291
TC.addProfileRTLibs(Args, CmdArgs);
290292

291293
std::vector<const char *> Environment;
@@ -475,6 +477,13 @@ void MSVCToolChain::AddHIPIncludeArgs(const ArgList &DriverArgs,
475477
RocmInstallation.AddHIPIncludeArgs(DriverArgs, CC1Args);
476478
}
477479

480+
void MSVCToolChain::AddHIPRuntimeLibArgs(const ArgList &Args,
481+
ArgStringList &CmdArgs) const {
482+
CmdArgs.append({Args.MakeArgString(StringRef("-libpath:") +
483+
RocmInstallation.getLibPath()),
484+
"amdhip64.lib"});
485+
}
486+
478487
void MSVCToolChain::printVerboseInfo(raw_ostream &OS) const {
479488
CudaInstallation.print(OS);
480489
RocmInstallation.print(OS);

clang/lib/Driver/ToolChains/MSVC.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ class LLVM_LIBRARY_VISIBILITY MSVCToolChain : public ToolChain {
9696
void AddHIPIncludeArgs(const llvm::opt::ArgList &DriverArgs,
9797
llvm::opt::ArgStringList &CC1Args) const override;
9898

99+
void AddHIPRuntimeLibArgs(const llvm::opt::ArgList &Args,
100+
llvm::opt::ArgStringList &CmdArgs) const override;
101+
99102
bool getWindowsSDKLibraryPath(
100103
const llvm::opt::ArgList &Args, std::string &path) const;
101104
bool getUniversalCRTLibraryPath(const llvm::opt::ArgList &Args,
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// UNSUPPORTED: system-windows
2+
3+
// RUN: touch %t.o
4+
5+
// Test HIP runtime lib args specified by --rocm-path.
6+
// RUN: %clang -### --hip-link -target x86_64-linux-gnu \
7+
// RUN: --rocm-path=%S/Inputs/rocm %t.o 2>&1 \
8+
// RUN: | FileCheck -check-prefixes=ROCM-PATH %s
9+
10+
// Test HIP runtime lib args specified by environment variable ROCM_PATH.
11+
// RUN: env ROCM_PATH=%S/Inputs/rocm %clang -### --hip-link \
12+
// RUN: -target x86_64-linux-gnu %t.o 2>&1 \
13+
// RUN: | FileCheck -check-prefixes=ROCM-PATH %s
14+
15+
// Test detecting latest /opt/rocm-{release} directory.
16+
// RUN: rm -rf %T/opt
17+
// RUN: mkdir -p %T/opt
18+
// RUN: cp -r %S/Inputs/rocm %T/opt/rocm-3.9.0-1234
19+
// RUN: cp -r %S/Inputs/rocm %T/opt/rocm-3.10.0
20+
// RUN: %clang -### --hip-link -target x86_64-linux-gnu \
21+
// RUN: --sysroot=%T %t.o 2>&1 \
22+
// RUN: | FileCheck -check-prefixes=ROCM-REL %s
23+
24+
// Test HIP runtime lib is not linked without --hip-link.
25+
// RUN: %clang -### -target x86_64-linux-gnu \
26+
// RUN: --rocm-path=%S/Inputs/rocm %t.o 2>&1 \
27+
// RUN: | FileCheck -check-prefixes=NOHIPRT %s
28+
29+
// Test HIP runtime lib is not linked with -nostdlib.
30+
// RUN: %clang -### --hip-link -nostdlib -target x86_64-linux-gnu \
31+
// RUN: --rocm-path=%S/Inputs/rocm %t.o 2>&1 \
32+
// RUN: | FileCheck -check-prefixes=NOHIPRT %s
33+
34+
// Test HIP runtime lib is not linked with -no-hip-rt.
35+
// RUN: %clang -### --hip-link -no-hip-rt -target x86_64-linux-gnu \
36+
// RUN: --rocm-path=%S/Inputs/rocm %t.o 2>&1 \
37+
// RUN: | FileCheck -check-prefixes=NOHIPRT %s
38+
39+
// ROCM-PATH: "-L[[HIPRT:.*/Inputs/rocm/lib]]" "-rpath" "[[HIPRT]]" "-lamdhip64"
40+
// ROCM-REL: "-L[[HIPRT:.*/opt/rocm-3.10.0/lib]]" "-rpath" "[[HIPRT]]" "-lamdhip64"
41+
// NOHIPRT-NOT: "-L{{.*/Inputs/rocm/lib}}"
42+
// NOHIPRT-NOT: "-rpath" "{{.*/Inputs/rocm/lib}}"
43+
// NOHIPRT-NOT: "-lamdhip64"

0 commit comments

Comments
 (0)