Skip to content

Commit e6c27b3

Browse files
authored
[Driver][AMDGPU][HIP][SPIRV] Disable optimizations for AMDGCN SPIR-V (#154765)
SPIR-V specific optimizations can inadvertently remove information that is important for the AMDGPU BE / break certain code patterns we rely on. Therefore, for AMDGCN flavoured SPIR-V we disable optimizations over IR, to ensure that we operate directly on the output of Clang CodeGen when we finalise.
1 parent 0ac13af commit e6c27b3

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

clang/lib/Driver/ToolChains/AMDGPU.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,20 @@ void AMDGPUToolChain::addClangTargetOptions(
858858
CC1Args.push_back("-fapply-global-visibility-to-externs");
859859
}
860860

861+
// For SPIR-V we want to retain the pristine output of Clang CodeGen, since
862+
// optimizations might lose structure / information that is necessary for
863+
// generating optimal concrete AMDGPU code.
864+
// TODO: using the below option is a temporary placeholder until Clang
865+
// provides the required functionality, which essentially boils down to
866+
// -O0 being refactored / reworked to not imply optnone / remove TBAA.
867+
// Once that is added, we should pivot to that functionality, being
868+
// mindful to not corrupt the user provided and subsequently embedded
869+
// command-line (i.e. if the user asks for -O3 this is what the
870+
// finalisation should use).
871+
if (getTriple().isSPIRV() &&
872+
!DriverArgs.hasArg(options::OPT_disable_llvm_optzns))
873+
CC1Args.push_back("-disable-llvm-optzns");
874+
861875
if (DeviceOffloadingKind == Action::OFK_None)
862876
addOpenCLBuiltinsLib(getDriver(), DriverArgs, CC1Args);
863877
}

clang/lib/Driver/ToolChains/HIPAMD.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,12 @@ void HIPAMDToolChain::addClangTargetOptions(
261261
// with options that match the user-supplied ones.
262262
if (!DriverArgs.hasArg(options::OPT_fembed_bitcode_marker))
263263
CC1Args.push_back("-fembed-bitcode=marker");
264+
// For SPIR-V we want to retain the pristine output of Clang CodeGen, since
265+
// optimizations might lose structure / information that is necessary for
266+
// generating optimal concrete AMDGPU code. We duplicate this because the
267+
// HIP TC doesn't invoke the base AMDGPU TC addClangTargetOptions.
268+
if (!DriverArgs.hasArg(options::OPT_disable_llvm_passes))
269+
CC1Args.push_back("-disable-llvm-passes");
264270
return; // No DeviceLibs for SPIR-V.
265271
}
266272

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@
206206
// Check mixed AMDGCNSPIRV and concrete GPU arch.
207207
//
208208

209-
// AMDGCNSPIRV: "-cc1" "-triple" "spirv64-amd-amdhsa" {{.*}}"-emit-llvm-bc" {{.*}} "-o" "[[AMDGCNSPV_BC:.*bc]]"
209+
// AMDGCNSPIRV: "-cc1" "-triple" "spirv64-amd-amdhsa" {{.*}}"-emit-llvm-bc" {{.*}}"-fembed-bitcode=marker" "-disable-llvm-passes" {{.*}} "-o" "[[AMDGCNSPV_BC:.*bc]]"
210210
// AMDGCNSPIRV: {{".*llvm-link.*"}} "-o" "[[AMDGCNSPV_TMP:.*out]]" "[[AMDGCNSPV_BC]]"
211211
// AMDGCNSPIRV: {{".*llvm-spirv.*"}} "--spirv-max-version=1.6" "--spirv-ext=+all" {{.*}} "[[AMDGCNSPV_TMP]]" {{.*}}"-o" "[[AMDGCNSPV_CO:.*out]]"
212212
// AMDGCNSPIRV: "-cc1" "-triple" "amdgcn-amd-amdhsa" {{.*}}"-emit-obj" {{.*}}"-target-cpu" "gfx900"{{.*}} "-o" "[[GFX900_OBJ:.*o]]"

clang/test/Driver/spirv-amd-toolchain.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414

1515
// RUN: %clang -### --target=spirv64-amd-amdhsa %s -nogpulib -nogpuinc 2>&1 \
1616
// RUN: | FileCheck %s --check-prefix=INVOCATION
17-
// INVOCATION: "-cc1" "-triple" "spirv64-amd-amdhsa" {{.*}} "-o" "[[OUTPUT:.+]]" "-x" "c"
17+
// INVOCATION: "-cc1" "-triple" "spirv64-amd-amdhsa" {{.*}}"-disable-llvm-optzns" {{.*}} "-o" "[[OUTPUT:.+]]" "-x" "c"
1818
// INVOCATION: "{{.*}}llvm-link" "-o" "a.out" "[[OUTPUT]]"
1919
// INVOCATION: "{{.*}}llvm-spirv" "--spirv-max-version=1.6" "--spirv-ext=+all" "--spirv-allow-unknown-intrinsics" "--spirv-lower-const-expr" "--spirv-preserve-auxdata" "--spirv-debug-info-version=nonsemantic-shader-200" "a.out" "-o" "a.out"

0 commit comments

Comments
 (0)