Skip to content

Commit e89ce89

Browse files
[HIP][SPIRV] Enable the SPIRV backend instead of the translator through an experimental flag.
Co-authored-by: Juan Manuel Martinez Caamaño <[email protected]>
1 parent 36bce68 commit e89ce89

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5483,6 +5483,20 @@ defm wavefrontsize64 : SimpleMFlag<"wavefrontsize64",
54835483
defm amdgpu_precise_memory_op
54845484
: SimpleMFlag<"amdgpu-precise-memory-op", "Enable", "Disable",
54855485
" precise memory mode (AMDGPU only)">;
5486+
def amdgpu_use_experimental_spirv_backend
5487+
: Flag<["-"], "amdgpu-use-experimental-spirv-backend">,
5488+
Group<m_amdgpu_Features_Group>,
5489+
Flags<[HelpHidden]>,
5490+
Visibility<[ClangOption]>,
5491+
HelpText<"Use experimental SPIRV backend for AMDGPU compilation (AMDGPU "
5492+
"only)">;
5493+
def no_amdgpu_use_experimental_spirv_backend
5494+
: Flag<["-"], "no-amdgpu-use-experimental-spirv-backend">,
5495+
Group<m_amdgpu_Features_Group>,
5496+
Flags<[HelpHidden]>,
5497+
Visibility<[ClangOption]>,
5498+
HelpText<"Do not use experimental SPIRV backend for AMDGPU compilation "
5499+
"(AMDGPU only)">;
54865500

54875501
def munsafe_fp_atomics : Flag<["-"], "munsafe-fp-atomics">,
54885502
Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>, Alias<fatomic_ignore_denormal_mode>;

clang/lib/Driver/ToolChains/HIPAMD.cpp

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

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

178-
// Emit SPIR-V binary.
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);
178+
bool UseSPIRVBackend = Args.hasFlag(
179+
options::OPT_amdgpu_use_experimental_spirv_backend,
180+
options::OPT_no_amdgpu_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+
}
187205
}
188206

189207
// For amdgcn the inputs of the linker job are device bitcode and output is
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// COM: This test case validates the behavior of -amdgpu-use-experimental-spirv-backend
2+
3+
// COM: Test that -amdgpu-use-experimental-spirv-backend calls clang -cc1 with the SPIRV triple.
4+
// RUN: %clang -x hip %s --cuda-device-only --offload-arch=amdgcnspirv -amdgpu-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}}"
6+
7+
// COM: Test that -no-amdgpu-use-experimental-spirv-backend calls the SPIRV translator
8+
// RUN: %clang -x hip %s --cuda-device-only --offload-arch=amdgcnspirv -no-amdgpu-use-experimental-spirv-backend -nogpuinc -nogpulib -### 2>&1 | FileCheck %s --check-prefix=CHECK-SPIRV-TRANSLATOR
9+
// CHECK-SPIRV-TRANSLATOR: "{{.*llvm-spirv.*}}" "{{--spirv-max-version=[0-9]+\.[0-9]}}"
10+
11+
// COM: Test that by default we use the translator
12+
// RUN: %clang -x hip %s --cuda-device-only --offload-arch=amdgcnspirv -nogpuinc -nogpulib -### 2>&1 | FileCheck %s --check-prefix=CHECK-SPIRV-TRANSLATOR

0 commit comments

Comments
 (0)