[clang] [HLSL][SPIR-V] Add support -fspv-target-env opt (PR #78611)
Natalie Chouinard via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 18 10:37:36 PST 2024
https://github.com/sudonatalie created https://github.com/llvm/llvm-project/pull/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.
>From d950de9ff25efdb81492eb630ba190a75c2762cf Mon Sep 17 00:00:00 2001
From: Natalie Chouinard <chouinard at google.com>
Date: Thu, 18 Jan 2024 15:42:37 +0000
Subject: [PATCH] [HLSL][SPIR-V] Add support -fspv-target-env opt
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.
---
clang/include/clang/Driver/Options.td | 3 +++
clang/lib/Driver/Driver.cpp | 15 ++++++++++++++-
clang/test/Driver/dxc_spirv.hlsl | 11 ++++++++++-
3 files changed, 27 insertions(+), 2 deletions(-)
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index e4fdad8265c8637..3b348800c779d28 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -8463,3 +8463,6 @@ def : Option<["/", "-"], "Qembed_debug", KIND_FLAG>, Group<dxc_Group>,
HelpText<"Embed PDB in shader container (ignored)">;
def spirv : DXCFlag<"spirv">,
HelpText<"Generate SPIR-V code">;
+def fspv_target_env_EQ : Joined<["-"], "fspv-target-env=">, Group<dxc_Group>,
+ HelpText<"Specify the target environment">,
+ Values<"vulkan1.2, vulkan1.3">;
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 35d563b9a87fac4..539e3206bdefac5 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -1310,10 +1310,23 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
A->claim();
- // TODO: Specify Vulkan target environment somewhere in the triple.
if (Args.hasArg(options::OPT_spirv)) {
llvm::Triple T(TargetTriple);
T.setArch(llvm::Triple::spirv);
+ T.setOS(llvm::Triple::Vulkan);
+
+ // Set specific Vulkan version if applicable.
+ if (const Arg *A = Args.getLastArg(options::OPT_fspv_target_env_EQ)) {
+ const llvm::StringSet<> ValidValues = {"vulkan1.2", "vulkan1.3"};
+ if (ValidValues.contains(A->getValue())) {
+ T.setOSName(A->getValue());
+ } else {
+ Diag(diag::err_drv_invalid_value)
+ << A->getAsString(Args) << A->getValue();
+ }
+ A->claim();
+ }
+
TargetTriple = T.str();
}
} else {
diff --git a/clang/test/Driver/dxc_spirv.hlsl b/clang/test/Driver/dxc_spirv.hlsl
index a3c5c30af06e3c8..c087ea4b0d709f5 100644
--- a/clang/test/Driver/dxc_spirv.hlsl
+++ b/clang/test/Driver/dxc_spirv.hlsl
@@ -1,4 +1,13 @@
// RUN: %clang_dxc -T cs_6_0 -spirv -### %s 2>&1 | FileCheck %s
+// RUN: %clang_dxc -T cs_6_0 -spirv -fspv-target-env=vulkan1.2 -### %s 2>&1 | FileCheck %s --check-prefix=CHECK-VULKAN12
+// RUN: %clang_dxc -T cs_6_0 -spirv -fspv-target-env=vulkan1.3 -### %s 2>&1 | FileCheck %s --check-prefix=CHECK-VULKAN13
+// RUN: not %clang_dxc -T cs_6_0 -spirv -fspv-target-env=vulkan1.0 -### %s 2>&1 | FileCheck %s --check-prefix=CHECK-ERROR
-// CHECK: "-triple" "spirv-unknown-shadermodel6.0-compute"
+// CHECK: "-triple" "spirv-unknown-vulkan-compute"
// CHECK-SAME: "-x" "hlsl"
+
+// CHECK-VULKAN12: "-triple" "spirv-unknown-vulkan1.2-compute"
+
+// CHECK-VULKAN13: "-triple" "spirv-unknown-vulkan1.3-compute"
+
+// CHECK-ERROR: error: invalid value 'vulkan1.0' in '-fspv-target-env=vulkan1.0'
More information about the cfe-commits
mailing list