Skip to content

Commit 08b9512

Browse files
committed
Invoke the SPIRV backend directly and add -ccc-print-phases and -ccc-print-bindings test cases.
1 parent 13d77d9 commit 08b9512

File tree

7 files changed

+54
-40
lines changed

7 files changed

+54
-40
lines changed

clang/include/clang/Driver/Types.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ TYPE("c++-module-cpp-output", PP_CXXModule, INVALID, "iim", phases
7878
TYPE("ada", Ada, INVALID, nullptr, phases::Compile, phases::Backend, phases::Assemble, phases::Link)
7979
TYPE("assembler", PP_Asm, INVALID, "s", phases::Assemble, phases::Link)
8080
TYPE("assembler-with-cpp", Asm, PP_Asm, "S", phases::Preprocess, phases::Assemble, phases::Link)
81+
TYPE("spv", SPV, INVALID, "spv", phases::Backend)
8182

8283
// Note: The `phases::Preprocess` phase is added to ".i" (i.e. Fortran
8384
// pre-processed) files. The reason is that the pre-processor "phase" has to be

clang/lib/Driver/Driver.cpp

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3672,29 +3672,45 @@ class OffloadingActionBuilder final {
36723672
// compiler phases, including backend and assemble phases.
36733673
ActionList AL;
36743674
Action *BackendAction = nullptr;
3675+
bool AssembleAndLink = true;
36753676
if (ToolChains.front()->getTriple().isSPIRV() ||
36763677
(ToolChains.front()->getTriple().isAMDGCN() &&
36773678
GpuArchList[I] == StringRef("amdgcnspirv"))) {
3678-
// Emit LLVM bitcode for SPIR-V targets. SPIR-V device tool chain
3679-
// (HIPSPVToolChain or HIPAMDToolChain) runs post-link LLVM IR
3680-
// passes.
3681-
types::ID Output = Args.hasArg(options::OPT_S)
3679+
3680+
bool UseSPIRVBackend =
3681+
Args.hasFlag(options::OPT_use_experimental_spirv_backend,
3682+
options::OPT_no_use_experimental_spirv_backend,
3683+
/*Default=*/false);
3684+
3685+
types::ID Output = UseSPIRVBackend ? types::TY_SPV
3686+
: Args.hasArg(options::OPT_S)
36823687
? types::TY_LLVM_IR
36833688
: types::TY_LLVM_BC;
3689+
36843690
BackendAction =
36853691
C.MakeAction<BackendJobAction>(CudaDeviceActions[I], Output);
3692+
3693+
if (UseSPIRVBackend) {
3694+
AssembleAndLink = false;
3695+
}
3696+
36863697
} else
36873698
BackendAction = C.getDriver().ConstructPhaseAction(
36883699
C, Args, phases::Backend, CudaDeviceActions[I],
36893700
AssociatedOffloadKind);
3690-
auto AssembleAction = C.getDriver().ConstructPhaseAction(
3691-
C, Args, phases::Assemble, BackendAction,
3692-
AssociatedOffloadKind);
3693-
AL.push_back(AssembleAction);
3694-
// Create a link action to link device IR with device library
3695-
// and generate ISA.
3696-
CudaDeviceActions[I] =
3697-
C.MakeAction<LinkJobAction>(AL, types::TY_Image);
3701+
3702+
if (AssembleAndLink) {
3703+
auto AssembleAction = C.getDriver().ConstructPhaseAction(
3704+
C, Args, phases::Assemble, BackendAction,
3705+
AssociatedOffloadKind);
3706+
AL.push_back(AssembleAction);
3707+
// Create a link action to link device IR with device library
3708+
// and generate ISA.
3709+
CudaDeviceActions[I] =
3710+
C.MakeAction<LinkJobAction>(AL, types::TY_Image);
3711+
} else {
3712+
CudaDeviceActions[I] = BackendAction;
3713+
}
36983714
}
36993715

37003716
// OffloadingActionBuilder propagates device arch until an offload

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5197,6 +5197,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
51975197
rewriteKind = RK_Fragile;
51985198
} else if (JA.getType() == types::TY_CIR) {
51995199
CmdArgs.push_back("-emit-cir");
5200+
} else if (JA.getType() == types::TY_SPV) {
5201+
CmdArgs.push_back("-emit-obj");
52005202
} else {
52015203
assert(JA.getType() == types::TY_PP_Asm && "Unexpected output type!");
52025204
}

clang/lib/Driver/ToolChains/HIPAMD.cpp

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -175,33 +175,15 @@ void AMDGCN::Linker::constructLinkAndEmitSpirvCommand(
175175

176176
constructLlvmLinkCommand(C, JA, Inputs, LinkedBCFile, Args);
177177

178-
bool UseSPIRVBackend = Args.hasFlag(
179-
options::OPT_use_experimental_spirv_backend,
180-
options::OPT_no_use_experimental_spirv_backend, /*Default=*/false);
181-
182-
// Emit SPIR-V binary either using the SPIRV backend or the translator.
183-
if (UseSPIRVBackend) {
184-
llvm::opt::ArgStringList CmdArgs;
185-
const char *Triple =
186-
C.getArgs().MakeArgString("-triple=spirv64-amd-amdhsa");
187-
CmdArgs.append({"-cc1", Triple, "-emit-obj", LinkedBCFile.getFilename(),
188-
"-o", Output.getFilename()});
189-
const char *Exec = getToolChain().getDriver().getClangProgramPath();
190-
C.addCommand(std::make_unique<Command>(JA, *this,
191-
ResponseFileSupport::None(), Exec,
192-
CmdArgs, LinkedBCFile, Output));
193-
} else {
194-
// Use the SPIRV translator for code gen.
195-
llvm::opt::ArgStringList TrArgs{
196-
"--spirv-max-version=1.6",
197-
"--spirv-ext=+all",
198-
"--spirv-allow-unknown-intrinsics",
199-
"--spirv-lower-const-expr",
200-
"--spirv-preserve-auxdata",
201-
"--spirv-debug-info-version=nonsemantic-shader-200"};
202-
SPIRV::constructTranslateCommand(C, *this, JA, Output, LinkedBCFile,
203-
TrArgs);
204-
}
178+
// Use the SPIRV translator for code gen.
179+
llvm::opt::ArgStringList TrArgs{
180+
"--spirv-max-version=1.6",
181+
"--spirv-ext=+all",
182+
"--spirv-allow-unknown-intrinsics",
183+
"--spirv-lower-const-expr",
184+
"--spirv-preserve-auxdata",
185+
"--spirv-debug-info-version=nonsemantic-shader-200"};
186+
SPIRV::constructTranslateCommand(C, *this, JA, Output, LinkedBCFile, TrArgs);
205187
}
206188

207189
// For amdgcn the inputs of the linker job are device bitcode and output is
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// RUN: %clang -x hip %s --cuda-device-only --offload-arch=amdgcnspirv -use-experimental-spirv-backend -nogpuinc -nogpulib -ccc-print-bindings 2>&1 | FileCheck %s
2+
3+
// CHECK: # "spirv64-amd-amdhsa" - "clang", inputs: ["{{.*}}.c"], output: "[[SPV_FILE:.*.spv]]"
4+
// CHECK: # "spirv64-amd-amdhsa" - "AMDGCN::Linker", inputs: ["[[SPV_FILE]]"], output: "{{.*.hipfb}}"

clang/test/Driver/hip-spirv-backend-opt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// Test that -use-experimental-spirv-backend calls clang -cc1 with the SPIRV triple.
44
// RUN: %clang -x hip %s --cuda-device-only --offload-arch=amdgcnspirv -use-experimental-spirv-backend -nogpuinc -nogpulib -### 2>&1 | FileCheck %s --check-prefix=CHECK-SPIRV-BACKEND
5-
// CHECK-SPIRV-BACKEND: "{{.*}}clang{{.*}}" "-cc1" "{{.*-triple=spirv64-amd-amdhsa}}"
5+
// CHECK-SPIRV-BACKEND: "{{.*}}clang{{.*}}" "-cc1" "{{.*-triple}}" "{{spirv64-amd-amdhsa}}"
66

77
// Test that -no-use-experimental-spirv-backend calls the SPIRV translator
88
// RUN: %clang -x hip %s --cuda-device-only --offload-arch=amdgcnspirv -no-use-experimental-spirv-backend -nogpuinc -nogpulib -### 2>&1 | FileCheck %s --check-prefix=CHECK-SPIRV-TRANSLATOR
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %clang -x hip %s --cuda-device-only --offload-arch=amdgcnspirv -use-experimental-spirv-backend -nogpuinc -nogpulib -ccc-print-phases 2>&1 | FileCheck %s
2+
3+
// CHECK: [[P0:[0-9]+]]: input, "{{.*}}.c", hip, (device-hip, amdgcnspirv)
4+
// CHECK: [[P1:[0-9]+]]: preprocessor, {[[P0]]}, hip-cpp-output, (device-hip, amdgcnspirv)
5+
// CHECK: [[P2:[0-9]+]]: compiler, {[[P1]]}, ir, (device-hip, amdgcnspirv)
6+
// CHECK: [[P3:[0-9]+]]: backend, {[[P2]]}, spv, (device-hip, amdgcnspirv)
7+
// CHECK: [[P4:[0-9]+]]: offload, "device-hip (spirv64-amd-amdhsa:amdgcnspirv)" {[[P3]]}, spv
8+
// CHECK: [[P5:[0-9]+]]: linker, {[[P4]]}, hip-fatbin, (device-hip, )
9+
// CHECK: [[P6:[0-9]+]]: offload, "device-hip (spirv64-amd-amdhsa:)" {[[P5]]}, hip-fatbin

0 commit comments

Comments
 (0)