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

via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 12 08:33:15 PDT 2023


llvmbot wrote:

@llvm/pr-subscribers-clang-driver

<details>
<summary>Changes</summary>

Add an option to target SPIR-V to the clang-dxc driver, which sets the target triple's architecture to logical SPIR-V.
--
Full diff: https://github.com/llvm/llvm-project/pull/65989.diff

4 Files Affected:

- (modified) clang/include/clang/Driver/Options.td (+2) 
- (modified) clang/lib/Driver/Driver.cpp (+6) 
- (added) clang/test/Driver/dxc_spirv.hlsl (+4) 
- (modified) clang/test/Driver/hlsl-lang-targets.hlsl (+2-3) 


<pre>
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index a5f5ca29053b43b..a7b8f1d9a0e0f2d 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -8258,3 +8258,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 ba723eac2a7ee74..39b1df45a410db7 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -1302,6 +1302,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
diff --git a/clang/test/Driver/hlsl-lang-targets.hlsl b/clang/test/Driver/hlsl-lang-targets.hlsl
index a757e2a3cdf74de..9adfeb3d0753b40 100644
--- a/clang/test/Driver/hlsl-lang-targets.hlsl
+++ b/clang/test/Driver/hlsl-lang-targets.hlsl
@@ -1,7 +1,7 @@
 // RUN: not %clang -target x86_64-unknown-unknown %s 2>&1 | FileCheck %s --check-prefix=X86
 // RUN: not %clang -target dxil-unknown-unknown %s 2>&1 | FileCheck %s --check-prefix=DXIL
 // RUN: not %clang -target x86_64-unknown-shadermodel %s 2>&1 | FileCheck %s --check-prefix=SM
-// RUN: not %clang -target spirv64-unknown-unknown %s 2>&1 | FileCheck %s --check-prefix=SPIRV
+// RUN: not %clang -target spirv-unknown-unknown %s 2>&1 | FileCheck %s --check-prefix=SPIRV
 
 
 // A completely unsupported target...
@@ -10,5 +10,4 @@
 // Poorly specified targets
 // DXIL: error: HLSL code generation is unsupported for target 'dxil-unknown-unknown'
 // SM: error: HLSL code generation is unsupported for target 'x86_64-unknown-shadermodel'
-
-// FIXME// SPIRV: error: HLSL code generation is unsupported for target 'spirv64-unknown-unknown'
+// SPIRV: error: HLSL code generation is unsupported for target 'spirv-unknown-unknown'
</pre>

</details>

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


More information about the cfe-commits mailing list