[clang] [HLSL] Add -HV option translation to clang-dxc.exe (PR #83938)

Joshua Batista via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 4 17:33:23 PST 2024


https://github.com/bob80905 created https://github.com/llvm/llvm-project/pull/83938

Previously, clang-dxc.exe would not recognize -HV as a valid argument to DXC, and would be unable to translate the argument to a legal clang argument. This PR implements a translation of the HV option and its value to the appropriate clang flag and the appropriate value. It adds a test by using the -### option to spit out the translated options, and checks to see that the correct option was generated.
Fixes #83479

>From 7453ffdea39c624221c9696394bbd47be7eec662 Mon Sep 17 00:00:00 2001
From: Joshua Batista <jbatista at microsoft.com>
Date: Mon, 4 Mar 2024 13:42:02 -0800
Subject: [PATCH 1/2] first try

---
 clang/include/clang/Driver/Options.td | 4 ++++
 clang/lib/Driver/ToolChains/HLSL.cpp  | 8 ++++++++
 2 files changed, 12 insertions(+)

diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index bef38738fde82e..6fe3dea1655b24 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -8545,6 +8545,10 @@ def dxc_entrypoint : Option<["--", "/", "-"], "E", KIND_JOINED_OR_SEPARATE>,
                      Group<dxc_Group>,
                      Visibility<[DXCOption]>,
                      HelpText<"Entry point name">;
+def dxc_HlslVersion : Option<["--", "/", "-"], "HV", KIND_JOINED_OR_SEPARATE>,
+                     Group<dxc_Group>,
+                     Visibility<[DXCOption]>,
+                     HelpText<"HLSL Version">;
 def dxc_validator_path_EQ : Joined<["--"], "dxv-path=">, Group<dxc_Group>,
   HelpText<"DXIL validator installation path">;
 def dxc_disable_validation : DXCFlag<"Vd">,
diff --git a/clang/lib/Driver/ToolChains/HLSL.cpp b/clang/lib/Driver/ToolChains/HLSL.cpp
index c6ad862b229420..0ffc15155e4db3 100644
--- a/clang/lib/Driver/ToolChains/HLSL.cpp
+++ b/clang/lib/Driver/ToolChains/HLSL.cpp
@@ -226,6 +226,14 @@ HLSLToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
       A->claim();
       continue;
     }
+    if (A->getOption().getID() == options::OPT_HLSL_Version) {
+      // Translate -HV into -std for llvm
+      DAL->AddSeparateArg(nullptr,
+                          Opts.getOption(options::OPT_stdlibxx_isystem),
+                          A->getValue());
+      A->claim();
+      continue;
+    }
     DAL->append(A);
   }
 

>From 69953d737b842f2144ebe0519d810c57b5c031b9 Mon Sep 17 00:00:00 2001
From: Joshua Batista <jbatista at microsoft.com>
Date: Mon, 4 Mar 2024 17:11:37 -0800
Subject: [PATCH 2/2] add HV option, and translation test

---
 clang/include/clang/Driver/Options.td |  2 +-
 clang/lib/Driver/ToolChains/HLSL.cpp  | 22 ++++++++++++++++++----
 clang/test/Options/HV.hlsl            | 13 +++++++++++++
 3 files changed, 32 insertions(+), 5 deletions(-)
 create mode 100644 clang/test/Options/HV.hlsl

diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 6fe3dea1655b24..c4caf232887b56 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -8545,7 +8545,7 @@ def dxc_entrypoint : Option<["--", "/", "-"], "E", KIND_JOINED_OR_SEPARATE>,
                      Group<dxc_Group>,
                      Visibility<[DXCOption]>,
                      HelpText<"Entry point name">;
-def dxc_HlslVersion : Option<["--", "/", "-"], "HV", KIND_JOINED_OR_SEPARATE>,
+def dxc_hlsl_version : Option<["--", "/", "-"], "HV", KIND_JOINED_OR_SEPARATE>,
                      Group<dxc_Group>,
                      Visibility<[DXCOption]>,
                      HelpText<"HLSL Version">;
diff --git a/clang/lib/Driver/ToolChains/HLSL.cpp b/clang/lib/Driver/ToolChains/HLSL.cpp
index 0ffc15155e4db3..fe258919dedf3e 100644
--- a/clang/lib/Driver/ToolChains/HLSL.cpp
+++ b/clang/lib/Driver/ToolChains/HLSL.cpp
@@ -226,11 +226,25 @@ HLSLToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
       A->claim();
       continue;
     }
-    if (A->getOption().getID() == options::OPT_HLSL_Version) {
+    if (A->getOption().getID() == options::OPT_dxc_hlsl_version) {
       // Translate -HV into -std for llvm
-      DAL->AddSeparateArg(nullptr,
-                          Opts.getOption(options::OPT_stdlibxx_isystem),
-                          A->getValue());
+      // depending on the value given, assign std to:
+      // c++14,c++17,c++20,c++latest,c11,c17
+      const char *value = A->getValue();
+      if (strcmp(value, "2016") == 0) {
+        DAL->AddSeparateArg(nullptr, Opts.getOption(options::OPT_std_EQ),
+                            "hlsl2016");
+      } else if (strcmp(value, "2017") == 0) {
+        DAL->AddSeparateArg(nullptr, Opts.getOption(options::OPT_std_EQ),
+                            "hlsl2017");
+      } else if (strcmp(value, "2018") == 0) {
+        DAL->AddSeparateArg(nullptr, Opts.getOption(options::OPT_std_EQ),
+                            "hlsl2018");
+      } else if (strcmp(value, "2021") == 0) {
+        DAL->AddSeparateArg(nullptr, Opts.getOption(options::OPT_std_EQ),
+                            "hlsl2021");
+      }
+
       A->claim();
       continue;
     }
diff --git a/clang/test/Options/HV.hlsl b/clang/test/Options/HV.hlsl
new file mode 100644
index 00000000000000..59158ff2f001ed
--- /dev/null
+++ b/clang/test/Options/HV.hlsl
@@ -0,0 +1,13 @@
+// RUN: %clang_dxc -T lib_6_4 -HV 2016 %s 2>&1 -###   | FileCheck -check-prefix=2016 %s
+// RUN: %clang_dxc -T lib_6_4 -HV 2017 %s 2>&1 -###   | FileCheck -check-prefix=2017 %s
+// RUN: %clang_dxc -T lib_6_4 -HV 2018 %s 2>&1 -###   | FileCheck -check-prefix=2018 %s
+// RUN: %clang_dxc -T lib_6_4 -HV 2021 %s 2>&1 -###   | FileCheck -check-prefix=2021 %s
+
+// 2016: "-std=hlsl2016"
+// 2017: "-std=hlsl2017"
+// 2018: "-std=hlsl2018"
+// 2021: "-std=hlsl2021"
+float4 main(float4 a : A) : SV_TARGET
+{
+  return -a.yxxx;
+}
\ No newline at end of file



More information about the cfe-commits mailing list