[clang] [SPIRV] Add -spirv option to DXC driver (PR #65989)

Natalie Chouinard via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 24 08:17:23 PDT 2023


https://github.com/sudonatalie updated https://github.com/llvm/llvm-project/pull/65989

>From 46b615e1c40ca89b8d4b9e924c139a5e9c430565 Mon Sep 17 00:00:00 2001
From: Natalie Chouinard <chouinard at google.com>
Date: Mon, 11 Sep 2023 18:08:17 +0000
Subject: [PATCH 1/4] [SPIRV] Add -spirv option to DXC driver

Add an option to target SPIR-V to the clang-dxc driver, which sets the
target triple's architecture to logical SPIR-V.
---
 clang/include/clang/Driver/Options.td | 2 ++
 clang/lib/Driver/Driver.cpp           | 6 ++++++
 clang/test/Driver/dxc_spirv.hlsl      | 4 ++++
 3 files changed, 12 insertions(+)
 create mode 100644 clang/test/Driver/dxc_spirv.hlsl

diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index e63158fb0e5333a..81b22d755f014ed 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -8352,3 +8352,5 @@ def dxc_disable_validation : DXCFlag<"Vd">,
 def : Option<["/", "-"], "Qembed_debug", KIND_FLAG>, Group<dxc_Group>,
   Flags<[Ignored, NoXarchOption]>, Visibility<[DXCOption]>,
   HelpText<"Embed PDB in shader container (ignored)">;
+def spirv : DXCFlag<"spirv">,
+  HelpText<"Generate SPIR-V code">;
\ No newline at end of file
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index f5fd900a6447fe3..38791955d4a972b 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -1303,6 +1303,12 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
         Diag(diag::err_drv_invalid_directx_shader_module) << TargetProfile;
 
       A->claim();
+
+      if (Args.hasArg(options::OPT_spirv)) {
+        llvm::Triple T(TargetTriple);
+        T.setArch(llvm::Triple::spirv);
+        TargetTriple = T.str();
+      }
     } else {
       Diag(diag::err_drv_dxc_missing_target_profile);
     }
diff --git a/clang/test/Driver/dxc_spirv.hlsl b/clang/test/Driver/dxc_spirv.hlsl
new file mode 100644
index 000000000000000..387b71ca58ada18
--- /dev/null
+++ b/clang/test/Driver/dxc_spirv.hlsl
@@ -0,0 +1,4 @@
+// RUN: %clang_dxc -T cs_6_0 -spirv -### %s 2>&1 | FileCheck %s
+
+// CHECK: "-triple" "spirv-unknown-shadermodel6.0-compute"
+// CHECK-SAME: "-x" "hlsl"
\ No newline at end of file

>From 2b7154784fe742a4f86684f474cd658e5be121ab Mon Sep 17 00:00:00 2001
From: Natalie Chouinard <chouinard at google.com>
Date: Tue, 24 Oct 2023 14:40:36 +0000
Subject: [PATCH 2/4] Add TODO

---
 clang/lib/Driver/Driver.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 38791955d4a972b..f9339b0a9c2bfac 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -1304,6 +1304,7 @@ 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);

>From 714829fb4f7fc076025747334e82cc48340db84e Mon Sep 17 00:00:00 2001
From: Natalie Chouinard <chouinard at google.com>
Date: Tue, 24 Oct 2023 14:51:20 +0000
Subject: [PATCH 3/4] EOF newlines

---
 clang/include/clang/Driver/Options.td | 2 +-
 clang/test/Driver/dxc_spirv.hlsl      | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 81b22d755f014ed..af4790b6f6a7cfe 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -8353,4 +8353,4 @@ def : Option<["/", "-"], "Qembed_debug", KIND_FLAG>, Group<dxc_Group>,
   Flags<[Ignored, NoXarchOption]>, Visibility<[DXCOption]>,
   HelpText<"Embed PDB in shader container (ignored)">;
 def spirv : DXCFlag<"spirv">,
-  HelpText<"Generate SPIR-V code">;
\ No newline at end of file
+  HelpText<"Generate SPIR-V code">;
diff --git a/clang/test/Driver/dxc_spirv.hlsl b/clang/test/Driver/dxc_spirv.hlsl
index 387b71ca58ada18..a3c5c30af06e3c8 100644
--- a/clang/test/Driver/dxc_spirv.hlsl
+++ b/clang/test/Driver/dxc_spirv.hlsl
@@ -1,4 +1,4 @@
 // RUN: %clang_dxc -T cs_6_0 -spirv -### %s 2>&1 | FileCheck %s
 
 // CHECK: "-triple" "spirv-unknown-shadermodel6.0-compute"
-// CHECK-SAME: "-x" "hlsl"
\ No newline at end of file
+// CHECK-SAME: "-x" "hlsl"

>From 2a54d11bcf2eaf211113e170f2dc152288957308 Mon Sep 17 00:00:00 2001
From: Natalie Chouinard <chouinard at google.com>
Date: Tue, 24 Oct 2023 15:16:38 +0000
Subject: [PATCH 4/4] Fixup test

---
 clang/test/Driver/hlsl-lang-targets-spirv.hlsl | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/test/Driver/hlsl-lang-targets-spirv.hlsl b/clang/test/Driver/hlsl-lang-targets-spirv.hlsl
index ff29f143ba1dc8e..e04d71263770bbb 100644
--- a/clang/test/Driver/hlsl-lang-targets-spirv.hlsl
+++ b/clang/test/Driver/hlsl-lang-targets-spirv.hlsl
@@ -2,8 +2,8 @@
 
 // Supported targets
 //
-// RUN: %clang -target dxil-unknown-shadermodel6.2-pixel %s -S -o /dev/null 2>&1 | FileCheck --check-prefix=CHECK-VALID %s
-// RUN: %clang -target spirv-unknown-shadermodel6.2-library %s -S -o /dev/null 2>&1 | FileCheck --check-prefix=CHECK-VALID %s
+// RUN: %clang -target dxil-unknown-shadermodel6.2-compute %s -S -o /dev/null 2>&1 | FileCheck --allow-empty --check-prefix=CHECK-VALID %s
+// RUN: %clang -target spirv-unknown-shadermodel6.2-compute %s -S -o /dev/null 2>&1 | FileCheck --allow-empty --check-prefix=CHECK-VALID %s
 
 // Empty shader model
 //
@@ -27,5 +27,5 @@
 // CHECK-NO-ENV: error: shader stage is required in target '{{.*}}' for HLSL code generation
 // CHECK-BAD-ENV: error: shader stage '{{.*}}' in target '{{.*}}' is invalid for HLSL code generation
 
-[shader("pixel")]
+[shader("compute"), numthreads(1,1,1)]
 void main() {}



More information about the cfe-commits mailing list