Skip to content

Commit ce519b5

Browse files
authored
[HLSL][SPIR-V] Add support -fspv-target-env opt (llvm#78611)
Add the -fspv-target-env option to the clang-dxc compatibility driver to specify the SPIR-V target environment, which is propagated to the target Triple.
1 parent 55cb52b commit ce519b5

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8479,3 +8479,6 @@ def : Option<["/", "-"], "Qembed_debug", KIND_FLAG>, Group<dxc_Group>,
84798479
HelpText<"Embed PDB in shader container (ignored)">;
84808480
def spirv : DXCFlag<"spirv">,
84818481
HelpText<"Generate SPIR-V code">;
8482+
def fspv_target_env_EQ : Joined<["-"], "fspv-target-env=">, Group<dxc_Group>,
8483+
HelpText<"Specify the target environment">,
8484+
Values<"vulkan1.2, vulkan1.3">;

clang/lib/Driver/Driver.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1310,10 +1310,23 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
13101310

13111311
A->claim();
13121312

1313-
// TODO: Specify Vulkan target environment somewhere in the triple.
13141313
if (Args.hasArg(options::OPT_spirv)) {
13151314
llvm::Triple T(TargetTriple);
13161315
T.setArch(llvm::Triple::spirv);
1316+
T.setOS(llvm::Triple::Vulkan);
1317+
1318+
// Set specific Vulkan version if applicable.
1319+
if (const Arg *A = Args.getLastArg(options::OPT_fspv_target_env_EQ)) {
1320+
const llvm::StringSet<> ValidValues = {"vulkan1.2", "vulkan1.3"};
1321+
if (ValidValues.contains(A->getValue())) {
1322+
T.setOSName(A->getValue());
1323+
} else {
1324+
Diag(diag::err_drv_invalid_value)
1325+
<< A->getAsString(Args) << A->getValue();
1326+
}
1327+
A->claim();
1328+
}
1329+
13171330
TargetTriple = T.str();
13181331
}
13191332
} else {

clang/test/Driver/dxc_spirv.hlsl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
// RUN: %clang_dxc -T cs_6_0 -spirv -### %s 2>&1 | FileCheck %s
2+
// RUN: %clang_dxc -T cs_6_0 -spirv -fspv-target-env=vulkan1.2 -### %s 2>&1 | FileCheck %s --check-prefix=CHECK-VULKAN12
3+
// RUN: %clang_dxc -T cs_6_0 -spirv -fspv-target-env=vulkan1.3 -### %s 2>&1 | FileCheck %s --check-prefix=CHECK-VULKAN13
4+
// RUN: not %clang_dxc -T cs_6_0 -spirv -fspv-target-env=vulkan1.0 -### %s 2>&1 | FileCheck %s --check-prefix=CHECK-ERROR
25

3-
// CHECK: "-triple" "spirv-unknown-shadermodel6.0-compute"
6+
// CHECK: "-triple" "spirv-unknown-vulkan-compute"
47
// CHECK-SAME: "-x" "hlsl"
8+
9+
// CHECK-VULKAN12: "-triple" "spirv-unknown-vulkan1.2-compute"
10+
11+
// CHECK-VULKAN13: "-triple" "spirv-unknown-vulkan1.3-compute"
12+
13+
// CHECK-ERROR: error: invalid value 'vulkan1.0' in '-fspv-target-env=vulkan1.0'

0 commit comments

Comments
 (0)