Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 61e80a3

Browse files
committed
[HIP] Use -mlink-builtin-bitcode to link device library
Use -mlink-builtin-bitcode instead of llvm-link to link device library so that device library bitcode and user device code can be compiled in a consistent way. This is the same approach used by CUDA and OpenMP. Differential Revision: https://reviews.llvm.org/D60513 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@358290 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 7860756 commit 61e80a3

File tree

4 files changed

+60
-54
lines changed

4 files changed

+60
-54
lines changed

lib/Driver/ToolChains/HIP.cpp

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ using namespace llvm::opt;
3131

3232
namespace {
3333

34-
static void addBCLib(Compilation &C, const ArgList &Args,
34+
static void addBCLib(const Driver &D, const ArgList &Args,
3535
ArgStringList &CmdArgs, ArgStringList LibraryPaths,
3636
StringRef BCName) {
3737
StringRef FullName;
@@ -40,11 +40,12 @@ static void addBCLib(Compilation &C, const ArgList &Args,
4040
llvm::sys::path::append(Path, BCName);
4141
FullName = Path;
4242
if (llvm::sys::fs::exists(FullName)) {
43+
CmdArgs.push_back("-mlink-builtin-bitcode");
4344
CmdArgs.push_back(Args.MakeArgString(FullName));
4445
return;
4546
}
4647
}
47-
C.getDriver().Diag(diag::err_drv_no_such_file) << BCName;
48+
D.Diag(diag::err_drv_no_such_file) << BCName;
4849
}
4950

5051
} // namespace
@@ -58,44 +59,6 @@ const char *AMDGCN::Linker::constructLLVMLinkCommand(
5859
for (const auto &II : Inputs)
5960
CmdArgs.push_back(II.getFilename());
6061

61-
ArgStringList LibraryPaths;
62-
63-
// Find in --hip-device-lib-path and HIP_LIBRARY_PATH.
64-
for (auto Path : Args.getAllArgValues(options::OPT_hip_device_lib_path_EQ))
65-
LibraryPaths.push_back(Args.MakeArgString(Path));
66-
67-
addDirectoryList(Args, LibraryPaths, "-L", "HIP_DEVICE_LIB_PATH");
68-
69-
llvm::SmallVector<std::string, 10> BCLibs;
70-
71-
// Add bitcode library in --hip-device-lib.
72-
for (auto Lib : Args.getAllArgValues(options::OPT_hip_device_lib_EQ)) {
73-
BCLibs.push_back(Args.MakeArgString(Lib));
74-
}
75-
76-
// If --hip-device-lib is not set, add the default bitcode libraries.
77-
if (BCLibs.empty()) {
78-
// Get the bc lib file name for ISA version. For example,
79-
// gfx803 => oclc_isa_version_803.amdgcn.bc.
80-
std::string ISAVerBC =
81-
"oclc_isa_version_" + SubArchName.drop_front(3).str() + ".amdgcn.bc";
82-
83-
llvm::StringRef FlushDenormalControlBC;
84-
if (Args.hasArg(options::OPT_fcuda_flush_denormals_to_zero))
85-
FlushDenormalControlBC = "oclc_daz_opt_on.amdgcn.bc";
86-
else
87-
FlushDenormalControlBC = "oclc_daz_opt_off.amdgcn.bc";
88-
89-
BCLibs.append({"hip.amdgcn.bc", "opencl.amdgcn.bc",
90-
"ocml.amdgcn.bc", "ockl.amdgcn.bc",
91-
"oclc_finite_only_off.amdgcn.bc",
92-
FlushDenormalControlBC,
93-
"oclc_correctly_rounded_sqrt_on.amdgcn.bc",
94-
"oclc_unsafe_math_off.amdgcn.bc", ISAVerBC});
95-
}
96-
for (auto Lib : BCLibs)
97-
addBCLib(C, Args, CmdArgs, LibraryPaths, Lib);
98-
9962
// Add an intermediate output file.
10063
CmdArgs.push_back("-o");
10164
std::string TmpName =
@@ -324,6 +287,44 @@ void HIPToolChain::addClangTargetOptions(
324287
CC1Args.append({"-fvisibility", "hidden"});
325288
CC1Args.push_back("-fapply-global-visibility-to-externs");
326289
}
290+
ArgStringList LibraryPaths;
291+
292+
// Find in --hip-device-lib-path and HIP_LIBRARY_PATH.
293+
for (auto Path :
294+
DriverArgs.getAllArgValues(options::OPT_hip_device_lib_path_EQ))
295+
LibraryPaths.push_back(DriverArgs.MakeArgString(Path));
296+
297+
addDirectoryList(DriverArgs, LibraryPaths, "-L", "HIP_DEVICE_LIB_PATH");
298+
299+
llvm::SmallVector<std::string, 10> BCLibs;
300+
301+
// Add bitcode library in --hip-device-lib.
302+
for (auto Lib : DriverArgs.getAllArgValues(options::OPT_hip_device_lib_EQ)) {
303+
BCLibs.push_back(DriverArgs.MakeArgString(Lib));
304+
}
305+
306+
// If --hip-device-lib is not set, add the default bitcode libraries.
307+
if (BCLibs.empty()) {
308+
// Get the bc lib file name for ISA version. For example,
309+
// gfx803 => oclc_isa_version_803.amdgcn.bc.
310+
std::string ISAVerBC =
311+
"oclc_isa_version_" + GpuArch.drop_front(3).str() + ".amdgcn.bc";
312+
313+
llvm::StringRef FlushDenormalControlBC;
314+
if (DriverArgs.hasArg(options::OPT_fcuda_flush_denormals_to_zero))
315+
FlushDenormalControlBC = "oclc_daz_opt_on.amdgcn.bc";
316+
else
317+
FlushDenormalControlBC = "oclc_daz_opt_off.amdgcn.bc";
318+
319+
BCLibs.append({"hip.amdgcn.bc", "opencl.amdgcn.bc", "ocml.amdgcn.bc",
320+
"ockl.amdgcn.bc", "oclc_finite_only_off.amdgcn.bc",
321+
FlushDenormalControlBC,
322+
"oclc_correctly_rounded_sqrt_on.amdgcn.bc",
323+
"oclc_unsafe_math_off.amdgcn.bc", ISAVerBC});
324+
}
325+
for (auto Lib : BCLibs)
326+
addBCLib(getDriver(), DriverArgs, CC1Args, LibraryPaths, Lib);
327+
327328
}
328329

329330
llvm::opt::DerivedArgList *

test/Driver/hip-device-libs.hip

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020
// RUN: 2>&1 | FileCheck %s --check-prefixes=COM,NOFLUSHD
2121

2222

23-
// COM: [[LLVM_LINK:"*.llvm-link"]]
24-
// COM-SAME: "{{.*}}hip.amdgcn.bc" "{{.*}}opencl.amdgcn.bc"
25-
// COM-SAME: "{{.*}}ocml.amdgcn.bc" "{{.*}}ockl.amdgcn.bc"
26-
// FLUSHD-SAME: {{.*}} "{{.*}}oclc_daz_opt_on.amdgcn.bc"
27-
// NOFLUSHD-SAME: {{.*}} "{{.*}}oclc_daz_opt_off.amdgcn.bc"
28-
// COM-SAME: {{.*}} "-o" "{{.*}}-gfx900-linked-{{.*bc}}"
23+
// COM: {{"[^"]*clang[^"]*"}}
24+
// COM-SAME: "-mlink-builtin-bitcode" "{{.*}}hip.amdgcn.bc"
25+
// COM-SAME: "-mlink-builtin-bitcode" "{{.*}}opencl.amdgcn.bc"
26+
// COM-SAME: "-mlink-builtin-bitcode" "{{.*}}ocml.amdgcn.bc"
27+
// COM-SAME: "-mlink-builtin-bitcode" "{{.*}}ockl.amdgcn.bc"
28+
// FLUSHD-SAME: "-mlink-builtin-bitcode" "{{.*}}oclc_daz_opt_on.amdgcn.bc"
29+
// NOFLUSHD-SAME: "-mlink-builtin-bitcode" "{{.*}}oclc_daz_opt_off.amdgcn.bc"
2930

test/Driver/hip-toolchain-no-rdc.hip

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
// CHECK-SAME: {{.*}} "-main-file-name" "a.cu" {{.*}} "-target-cpu" "gfx803"
2323
// CHECK-SAME: "-fcuda-is-device" "-fvisibility" "hidden"
2424
// CHECK-SAME: "-fapply-global-visibility-to-externs"
25+
// CHECK-SAME: "{{.*}}lib1.bc" "{{.*}}lib2.bc"
2526
// CHECK-SAME: {{.*}} "-o" [[A_BC_803:".*bc"]] "-x" "hip"
2627
// CHECK-SAME: {{.*}} [[A_SRC:".*a.cu"]]
2728

2829
// CHECK: [[LLVM_LINK:"*.llvm-link"]] [[A_BC_803]]
29-
// CHECK-SAME: "{{.*}}lib1.bc" "{{.*}}lib2.bc"
3030
// CHECK-SAME: "-o" [[LINKED_BC_DEV_A_803:".*-gfx803-linked-.*bc"]]
3131

3232
// CHECK: [[OPT:".*opt"]] [[LINKED_BC_DEV_A_803]] "-mtriple=amdgcn-amd-amdhsa"
@@ -50,11 +50,11 @@
5050
// CHECK-SAME: {{.*}} "-main-file-name" "a.cu" {{.*}} "-target-cpu" "gfx900"
5151
// CHECK-SAME: "-fcuda-is-device" "-fvisibility" "hidden"
5252
// CHECK-SAME: "-fapply-global-visibility-to-externs"
53+
// CHECK-SAME: "{{.*}}lib1.bc" "{{.*}}lib2.bc"
5354
// CHECK-SAME: {{.*}} "-o" [[A_BC_900:".*bc"]] "-x" "hip"
5455
// CHECK-SAME: {{.*}} [[A_SRC]]
5556

5657
// CHECK: [[LLVM_LINK:"*.llvm-link"]] [[A_BC_900]]
57-
// CHECK-SAME: "{{.*}}lib1.bc" "{{.*}}lib2.bc"
5858
// CHECK-SAME: "-o" [[LINKED_BC_DEV_A_900:".*-gfx900-linked-.*bc"]]
5959

6060
// CHECK: [[OPT:".*opt"]] [[LINKED_BC_DEV_A_900]] "-mtriple=amdgcn-amd-amdhsa"
@@ -94,11 +94,11 @@
9494
// CHECK-SAME: {{.*}} "-main-file-name" "b.hip" {{.*}} "-target-cpu" "gfx803"
9595
// CHECK-SAME: "-fcuda-is-device" "-fvisibility" "hidden"
9696
// CHECK-SAME: "-fapply-global-visibility-to-externs"
97+
// CHECK-SAME: "{{.*}}lib1.bc" "{{.*}}lib2.bc"
9798
// CHECK-SAME: {{.*}} "-o" [[B_BC_803:".*bc"]] "-x" "hip"
9899
// CHECK-SAME: {{.*}} [[B_SRC:".*b.hip"]]
99100

100101
// CHECK: [[LLVM_LINK:"*.llvm-link"]] [[B_BC_803]]
101-
// CHECK-SAME: "{{.*}}lib1.bc" "{{.*}}lib2.bc"
102102
// CHECK-SAME: "-o" [[LINKED_BC_DEV_B_803:".*-gfx803-linked-.*bc"]]
103103

104104
// CHECK: [[OPT:".*opt"]] [[LINKED_BC_DEV_B_803]] "-mtriple=amdgcn-amd-amdhsa"
@@ -122,11 +122,11 @@
122122
// CHECK-SAME: {{.*}} "-main-file-name" "b.hip" {{.*}} "-target-cpu" "gfx900"
123123
// CHECK-SAME: "-fcuda-is-device" "-fvisibility" "hidden"
124124
// CHECK-SAME: "-fapply-global-visibility-to-externs"
125+
// CHECK-SAME: "{{.*}}lib1.bc" "{{.*}}lib2.bc"
125126
// CHECK-SAME: {{.*}} "-o" [[B_BC_900:".*bc"]] "-x" "hip"
126127
// CHECK-SAME: {{.*}} [[B_SRC]]
127128

128129
// CHECK: [[LLVM_LINK:"*.llvm-link"]] [[B_BC_900]]
129-
// CHECK-SAME: "{{.*}}lib1.bc" "{{.*}}lib2.bc"
130130
// CHECK-SAME: "-o" [[LINKED_BC_DEV_B_900:".*-gfx900-linked-.*bc"]]
131131

132132
// CHECK: [[OPT:".*opt"]] [[LINKED_BC_DEV_B_900]] "-mtriple=amdgcn-amd-amdhsa"

test/Driver/hip-toolchain-rdc.hip

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// CHECK-SAME: {{.*}} "-main-file-name" "a.cu" {{.*}} "-target-cpu" "gfx803"
1919
// CHECK-SAME: "-fcuda-is-device" "-fgpu-rdc" "-fvisibility" "hidden"
2020
// CHECK-SAME: "-fapply-global-visibility-to-externs"
21+
// CHECK-SAME: "{{.*}}lib1.bc" "{{.*}}lib2.bc"
2122
// CHECK-SAME: {{.*}} "-o" [[A_BC:".*bc"]] "-x" "hip"
2223
// CHECK-SAME: {{.*}} [[A_SRC:".*a.cu"]]
2324

@@ -27,11 +28,11 @@
2728
// CHECK-SAME: {{.*}} "-main-file-name" "b.hip" {{.*}} "-target-cpu" "gfx803"
2829
// CHECK-SAME: "-fcuda-is-device" "-fgpu-rdc" "-fvisibility" "hidden"
2930
// CHECK-SAME: "-fapply-global-visibility-to-externs"
31+
// CHECK-SAME: "{{.*}}lib1.bc" "{{.*}}lib2.bc"
3032
// CHECK-SAME: {{.*}} "-o" [[B_BC:".*bc"]] "-x" "hip"
3133
// CHECK-SAME: {{.*}} [[B_SRC:".*b.hip"]]
3234

3335
// CHECK: [[LLVM_LINK:"*.llvm-link"]] [[A_BC]] [[B_BC]]
34-
// CHECK-SAME: "{{.*}}lib1.bc" "{{.*}}lib2.bc"
3536
// CHECK-SAME: "-o" [[LINKED_BC_DEV1:".*-gfx803-linked-.*bc"]]
3637

3738
// CHECK: [[OPT:".*opt"]] [[LINKED_BC_DEV1]] "-mtriple=amdgcn-amd-amdhsa"
@@ -49,18 +50,21 @@
4950
// CHECK-SAME: "-aux-triple" "x86_64-unknown-linux-gnu"
5051
// CHECK-SAME: "-emit-llvm-bc"
5152
// CHECK-SAME: {{.*}} "-main-file-name" "a.cu" {{.*}} "-target-cpu" "gfx900"
52-
// CHECK-SAME: "-fcuda-is-device" {{.*}} "-o" [[A_BC:".*bc"]] "-x" "hip"
53+
// CHECK-SAME: "-fcuda-is-device" "-fgpu-rdc"
54+
// CHECK-SAME: "{{.*}}lib1.bc" "{{.*}}lib2.bc"
55+
// CHECK-SAME: {{.*}} "-o" [[A_BC:".*bc"]] "-x" "hip"
5356
// CHECK-SAME: {{.*}} [[A_SRC]]
5457

5558
// CHECK: [[CLANG]] "-cc1" "-triple" "amdgcn-amd-amdhsa"
5659
// CHECK-SAME: "-aux-triple" "x86_64-unknown-linux-gnu"
5760
// CHECK-SAME: "-emit-llvm-bc"
5861
// CHECK-SAME: {{.*}} "-main-file-name" "b.hip" {{.*}} "-target-cpu" "gfx900"
59-
// CHECK-SAME: "-fcuda-is-device" {{.*}} "-o" [[B_BC:".*bc"]] "-x" "hip"
62+
// CHECK-SAME: "-fcuda-is-device" "-fgpu-rdc"
63+
// CHECK-SAME: "{{.*}}lib1.bc" "{{.*}}lib2.bc"
64+
// CHECK-SAME: {{.*}} "-o" [[B_BC:".*bc"]] "-x" "hip"
6065
// CHECK-SAME: {{.*}} [[B_SRC]]
6166

6267
// CHECK: [[LLVM_LINK]] [[A_BC]] [[B_BC]]
63-
// CHECK-SAME: "{{.*}}lib1.bc" "{{.*}}lib2.bc"
6468
// CHECK-SAME: "-o" [[LINKED_BC_DEV2:".*-gfx900-linked-.*bc"]]
6569

6670
// CHECK: [[OPT]] [[LINKED_BC_DEV2]] "-mtriple=amdgcn-amd-amdhsa"

0 commit comments

Comments
 (0)