Skip to content

Commit f13b8d4

Browse files
committedOct 3, 2019
[HIP] Support -emit-llvm for device compilation
Sometimes it is useful to compile HIP device code to LLVM BC. It is not convenient to use clang -cc1 since there are lots of options needed. This patch allows clang driver to compile HIP device code to LLVM BC with -emit-llvm -c. Differential Revision: https://reviews.llvm.org/D68284 llvm-svn: 373561
1 parent f849f41 commit f13b8d4

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed
 

‎clang/lib/Driver/Driver.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -2312,6 +2312,8 @@ class OffloadingActionBuilder final {
23122312
/// compilation.
23132313
bool CompileHostOnly = false;
23142314
bool CompileDeviceOnly = false;
2315+
bool EmitLLVM = false;
2316+
bool EmitAsm = false;
23152317

23162318
/// List of GPU architectures to use in this compilation.
23172319
SmallVector<CudaArch, 4> GpuArchList;
@@ -2478,6 +2480,8 @@ class OffloadingActionBuilder final {
24782480
CompileDeviceOnly = PartialCompilationArg &&
24792481
PartialCompilationArg->getOption().matches(
24802482
options::OPT_cuda_device_only);
2483+
EmitLLVM = Args.getLastArg(options::OPT_emit_llvm);
2484+
EmitAsm = Args.getLastArg(options::OPT_S);
24812485

24822486
// Collect all cuda_gpu_arch parameters, removing duplicates.
24832487
std::set<CudaArch> GpuArchs;
@@ -2664,7 +2668,8 @@ class OffloadingActionBuilder final {
26642668
assert(!CompileHostOnly &&
26652669
"Not expecting CUDA actions in host-only compilation.");
26662670

2667-
if (!Relocatable && CurPhase == phases::Backend) {
2671+
if (!Relocatable && CurPhase == phases::Backend && !EmitLLVM &&
2672+
!EmitAsm) {
26682673
// If we are in backend phase, we attempt to generate the fat binary.
26692674
// We compile each arch to IR and use a link action to generate code
26702675
// object containing ISA. Then we use a special "link" action to create
@@ -2732,7 +2737,8 @@ class OffloadingActionBuilder final {
27322737
A = C.getDriver().ConstructPhaseAction(C, Args, CurPhase, A,
27332738
AssociatedOffloadKind);
27342739

2735-
return ABRT_Success;
2740+
return (CompileDeviceOnly && CurPhase == FinalPhase) ? ABRT_Ignore_Host
2741+
: ABRT_Success;
27362742
}
27372743

27382744
void appendLinkDependences(OffloadAction::DeviceDependences &DA) override {
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// REQUIRES: clang-driver
2+
// REQUIRES: x86-registered-target
3+
// REQUIRES: amdgpu-registered-target
4+
5+
// If -emit-llvm and/or -S is used in device only compilation,
6+
// the output should not be bundled.
7+
8+
// RUN: %clang -c -emit-llvm --cuda-device-only -### -target x86_64-linux-gnu \
9+
// RUN: -o a.bc -x hip --cuda-gpu-arch=gfx900 \
10+
// RUN: --hip-device-lib=lib1.bc \
11+
// RUN: --hip-device-lib-path=%S/Inputs/hip_multiple_inputs/lib1 \
12+
// RUN: %S/Inputs/hip_multiple_inputs/a.cu \
13+
// RUN: 2>&1 | FileCheck -check-prefixes=CHECK,BC %s
14+
15+
// RUN: %clang -c -S -emit-llvm --cuda-device-only -### -target x86_64-linux-gnu \
16+
// RUN: -o a.ll -x hip --cuda-gpu-arch=gfx900 \
17+
// RUN: --hip-device-lib=lib1.bc \
18+
// RUN: --hip-device-lib-path=%S/Inputs/hip_multiple_inputs/lib1 \
19+
// RUN: %S/Inputs/hip_multiple_inputs/a.cu \
20+
// RUN: 2>&1 | FileCheck -check-prefixes=CHECK,LL %s
21+
22+
// RUN: %clang -c -S --cuda-device-only -### -target x86_64-linux-gnu \
23+
// RUN: -o a.s -x hip --cuda-gpu-arch=gfx900 \
24+
// RUN: --hip-device-lib=lib1.bc \
25+
// RUN: --hip-device-lib-path=%S/Inputs/hip_multiple_inputs/lib1 \
26+
// RUN: %S/Inputs/hip_multiple_inputs/a.cu \
27+
// RUN: 2>&1 | FileCheck -check-prefixes=CHECK,ASM %s
28+
29+
// CHECK: {{".*clang.*"}} "-cc1" "-triple" "amdgcn-amd-amdhsa"
30+
// CHECK-SAME: "-aux-triple" "x86_64-unknown-linux-gnu"
31+
// BC-SAME: "-emit-llvm-bc"
32+
// LL-SAME: "-emit-llvm"
33+
// ASM-NOT: "-emit-llvm"
34+
// CHECK-SAME: "-main-file-name" "a.cu" {{.*}} "-target-cpu" "gfx900"
35+
// CHECK-SAME: "-fcuda-is-device"
36+
// CHECK-SAME: {{".*lib1.bc"}}
37+
// BC-SAME: "-o" "a.bc"
38+
// LL-SAME: "-o" "a.ll"
39+
// ASM-SAME: "-o" "a.s"
40+
// CHECK-SAME: {{".*a.cu"}}
41+
42+
// CHECK-NOT: {{"*.llvm-link"}}
43+
// CHECK-NOT: {{".*opt"}}
44+
// CHECK-NOT: {{".*llc"}}
45+
// CHECK-NOT: {{".*lld"}}
46+
// CHECK-NOT: {{".*clang-offload-bundler"}}
47+
// CHECK-NOT: {{".*ld.*"}}
48+
49+
// If neither -emit-llvm nor -S is used in device only compilation,
50+
// the output should be bundled.
51+
52+
// RUN: %clang -c --cuda-device-only -### -target x86_64-linux-gnu \
53+
// RUN: -o a.s -x hip --cuda-gpu-arch=gfx900 \
54+
// RUN: --hip-device-lib=lib1.bc \
55+
// RUN: --hip-device-lib-path=%S/Inputs/hip_multiple_inputs/lib1 \
56+
// RUN: %S/Inputs/hip_multiple_inputs/a.cu \
57+
// RUN: 2>&1 | FileCheck -check-prefixes=BUNDLE %s
58+
59+
// RUN: %clang --cuda-device-only -### -target x86_64-linux-gnu \
60+
// RUN: -o a.s -x hip --cuda-gpu-arch=gfx900 \
61+
// RUN: --hip-device-lib=lib1.bc \
62+
// RUN: --hip-device-lib-path=%S/Inputs/hip_multiple_inputs/lib1 \
63+
// RUN: %S/Inputs/hip_multiple_inputs/a.cu \
64+
// RUN: 2>&1 | FileCheck -check-prefixes=BUNDLE %s
65+
66+
// BUNDLE: {{"*.clang.*"}}
67+
// BUNDLE: {{"*.llvm-link"}}
68+
// BUNDLE: {{".*opt"}}
69+
// BUNDLE: {{".*llc"}}
70+
// BUNDLE: {{".*lld"}}
71+
// BUNDLE: {{".*clang-offload-bundler"}}
72+

0 commit comments

Comments
 (0)
Please sign in to comment.