Skip to content

Commit d723587

Browse files
authored
[HLSL] Explicitly set the SPIR-V version with spv-target-env (llvm#121961)
In DXC, setting the vulkan version automatically sets the target spir-v version to the maximum spir-v version that the vulkan version must support. So for Vulkan 1.2, we set the spir-v version to spirv 1.5 because every implementation of Vulkan 1.2 must support spirv 1.5, but not spir-v 1.6.
1 parent d2b78c6 commit d723587

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,9 +1503,14 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
15031503

15041504
// Set specific Vulkan version if applicable.
15051505
if (const Arg *A = Args.getLastArg(options::OPT_fspv_target_env_EQ)) {
1506-
const llvm::StringSet<> ValidValues = {"vulkan1.2", "vulkan1.3"};
1507-
if (ValidValues.contains(A->getValue())) {
1508-
T.setOSName(A->getValue());
1506+
const llvm::StringMap<llvm::Triple::SubArchType> ValidTargets = {
1507+
{"vulkan1.2", llvm::Triple::SPIRVSubArch_v15},
1508+
{"vulkan1.3", llvm::Triple::SPIRVSubArch_v16}};
1509+
1510+
auto TargetInfo = ValidTargets.find(A->getValue());
1511+
if (TargetInfo != ValidTargets.end()) {
1512+
T.setOSName(TargetInfo->getKey());
1513+
T.setArch(llvm::Triple::spirv, TargetInfo->getValue());
15091514
} else {
15101515
Diag(diag::err_drv_invalid_value)
15111516
<< A->getAsString(Args) << A->getValue();

clang/test/Driver/dxc_spirv.hlsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
// CHECK: "-triple" "spirv-unknown-vulkan-compute"
77
// CHECK-SAME: "-x" "hlsl"
88

9-
// CHECK-VULKAN12: "-triple" "spirv-unknown-vulkan1.2-compute"
9+
// CHECK-VULKAN12: "-triple" "spirv1.5-unknown-vulkan1.2-compute"
1010

11-
// CHECK-VULKAN13: "-triple" "spirv-unknown-vulkan1.3-compute"
11+
// CHECK-VULKAN13: "-triple" "spirv1.6-unknown-vulkan1.3-compute"
1212

1313
// CHECK-ERROR: error: invalid value 'vulkan1.0' in '-fspv-target-env=vulkan1.0'

llvm/lib/TargetParser/Triple.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,26 @@ StringRef Triple::getArchName(ArchType Kind, SubArchType SubArch) {
113113
if (SubArch == AArch64SubArch_arm64e)
114114
return "arm64e";
115115
break;
116+
case Triple::spirv:
117+
switch (SubArch) {
118+
case Triple::SPIRVSubArch_v10:
119+
return "spirv1.0";
120+
case Triple::SPIRVSubArch_v11:
121+
return "spirv1.1";
122+
case Triple::SPIRVSubArch_v12:
123+
return "spirv1.2";
124+
case Triple::SPIRVSubArch_v13:
125+
return "spirv1.3";
126+
case Triple::SPIRVSubArch_v14:
127+
return "spirv1.4";
128+
case Triple::SPIRVSubArch_v15:
129+
return "spirv1.5";
130+
case Triple::SPIRVSubArch_v16:
131+
return "spirv1.6";
132+
default:
133+
break;
134+
}
135+
break;
116136
case Triple::dxil:
117137
switch (SubArch) {
118138
case Triple::NoSubArch:

0 commit comments

Comments
 (0)