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

Joshua Batista via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 8 13:11:36 PST 2024


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

>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/6] 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/6] 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

>From 87ae56a7d129e0f04197757d50189a3921c75161 Mon Sep 17 00:00:00 2001
From: Joshua Batista <jbatista at microsoft.com>
Date: Tue, 5 Mar 2024 10:55:52 -0800
Subject: [PATCH 3/6] address Justin

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

diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index c4caf232887b56..541d3e954a4e2b 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_hlsl_version : 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/test/Options/HV.hlsl b/clang/test/Options/HV.hlsl
index 59158ff2f001ed..e17cdfec1127cc 100644
--- a/clang/test/Options/HV.hlsl
+++ b/clang/test/Options/HV.hlsl
@@ -1,7 +1,7 @@
 // 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
+// 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"

>From 79f5172bc97fb2c8591d46198e8eee7beaa66786 Mon Sep 17 00:00:00 2001
From: Joshua Batista <jbatista at microsoft.com>
Date: Tue, 5 Mar 2024 15:20:09 -0800
Subject: [PATCH 4/6] address Chris

---
 .../clang/Basic/DiagnosticDriverKinds.td      |  3 ++
 clang/include/clang/Driver/Options.td         |  3 +-
 clang/lib/Driver/ToolChains/HLSL.cpp          | 41 ++++++++++++++-----
 clang/test/Options/HV.hlsl                    |  7 +++-
 llvm/include/llvm/Option/OptTable.h           |  6 +++
 5 files changed, 47 insertions(+), 13 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index 1bc9885849d54b..c2ce342cac3bf1 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -753,6 +753,9 @@ def err_drv_hlsl_bad_shader_required_in_target : Error<
 
 def err_drv_hlsl_bad_shader_unsupported : Error<
   "%select{shader model|Vulkan environment|shader stage}0 '%1' in target '%2' is invalid for HLSL code generation">;
+def err_drv_hlsl_dxc_bad_argument_value : Error<
+  "'%0' dxc argument has invalid value '%1'">;
+
 def warn_drv_dxc_missing_dxv : Warning<"dxv not found. "
     "Resulting DXIL will not be validated or signed for use in release environments.">,
     InGroup<DXILValidation>;
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 541d3e954a4e2b..3f0781e3efb98b 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -8548,7 +8548,8 @@ def dxc_entrypoint : Option<["--", "/", "-"], "E", KIND_JOINED_OR_SEPARATE>,
 def dxc_hlsl_version : Option<["/", "-"], "HV", KIND_JOINED_OR_SEPARATE>,
                      Group<dxc_Group>,
                      Visibility<[DXCOption]>,
-                     HelpText<"HLSL Version">;
+                     HelpText<"HLSL Version">,
+                     Values<"2016, 2017, 2018, 2021, 202x">;
 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 fe258919dedf3e..5692d1ea061c9e 100644
--- a/clang/lib/Driver/ToolChains/HLSL.cpp
+++ b/clang/lib/Driver/ToolChains/HLSL.cpp
@@ -228,21 +228,40 @@ HLSLToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
     }
     if (A->getOption().getID() == options::OPT_dxc_hlsl_version) {
       // Translate -HV into -std for llvm
-      // 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) {
+      // depending on the value given
+      llvm::StringRef value(A->getValue());
+      llvm::StringRef acceptedValues(
+          Opts.getOptionValues(options::OPT_dxc_hlsl_version));
+      llvm::SmallVector<StringRef, 8> validValues;
+      acceptedValues.split(validValues, ", ");
+      if (value == validValues[0]) {
+        LangStandard l =
+            LangStandard::getLangStandardForKind(LangStandard::lang_hlsl2016);
         DAL->AddSeparateArg(nullptr, Opts.getOption(options::OPT_std_EQ),
-                            "hlsl2016");
-      } else if (strcmp(value, "2017") == 0) {
+                            l.getName());
+      } else if (value == validValues[1]) {
+        LangStandard l =
+            LangStandard::getLangStandardForKind(LangStandard::lang_hlsl2017);
         DAL->AddSeparateArg(nullptr, Opts.getOption(options::OPT_std_EQ),
-                            "hlsl2017");
-      } else if (strcmp(value, "2018") == 0) {
+                            l.getName());
+      } else if (value == validValues[2]) {
+        LangStandard l =
+            LangStandard::getLangStandardForKind(LangStandard::lang_hlsl2018);
         DAL->AddSeparateArg(nullptr, Opts.getOption(options::OPT_std_EQ),
-                            "hlsl2018");
-      } else if (strcmp(value, "2021") == 0) {
+                            l.getName());
+      } else if (value == validValues[3]) {
+        LangStandard l =
+            LangStandard::getLangStandardForKind(LangStandard::lang_hlsl2021);
         DAL->AddSeparateArg(nullptr, Opts.getOption(options::OPT_std_EQ),
-                            "hlsl2021");
+                            l.getName());
+      } else if (value == validValues[4]) {
+        LangStandard l =
+            LangStandard::getLangStandardForKind(LangStandard::lang_hlsl202x);
+        DAL->AddSeparateArg(nullptr, Opts.getOption(options::OPT_std_EQ),
+                            l.getName());
+      } else {
+        getDriver().Diag(diag::err_drv_hlsl_dxc_bad_argument_value)
+            << "HV" << value;
       }
 
       A->claim();
diff --git a/clang/test/Options/HV.hlsl b/clang/test/Options/HV.hlsl
index e17cdfec1127cc..497591aa5900f7 100644
--- a/clang/test/Options/HV.hlsl
+++ b/clang/test/Options/HV.hlsl
@@ -2,12 +2,17 @@
 // 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
+// RUN: %clang_dxc -T lib_6_4 /HV 202x %s 2>&1 -###   | FileCheck -check-prefix=202x %s
+// RUN: not %clang_dxc -T lib_6_4 /HV gibberish -### %s 2>&1 | FileCheck -check-prefix=CHECK-ERR %s
 
 // 2016: "-std=hlsl2016"
 // 2017: "-std=hlsl2017"
 // 2018: "-std=hlsl2018"
 // 2021: "-std=hlsl2021"
+// 202x: "-std=hlsl202x"
+// CHECK-ERR: error: 'HV' dxc argument has invalid value 'gibberish'
 float4 main(float4 a : A) : SV_TARGET
 {
   return -a.yxxx;
-}
\ No newline at end of file
+}
+
diff --git a/llvm/include/llvm/Option/OptTable.h b/llvm/include/llvm/Option/OptTable.h
index bb3b665a16319f..a3106374543302 100644
--- a/llvm/include/llvm/Option/OptTable.h
+++ b/llvm/include/llvm/Option/OptTable.h
@@ -154,6 +154,12 @@ class OptTable {
     return getInfo(id).MetaVar;
   }
 
+ /// Get the meta-variable name to use when describing
+  /// this options values in the help text.
+  const char *getOptionValues(OptSpecifier id) const {
+    return getInfo(id).Values;
+  }
+
   /// Specify the environment variable where initial options should be read.
   void setInitialOptionsFromEnvironment(const char *E) { EnvVar = E; }
 

>From 443e85523d612ed163af5d405262f9a5a8f271f2 Mon Sep 17 00:00:00 2001
From: Joshua Batista <jbatista at microsoft.com>
Date: Tue, 5 Mar 2024 15:27:33 -0800
Subject: [PATCH 5/6] clarify comment, format

---
 llvm/include/llvm/Option/OptTable.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/Option/OptTable.h b/llvm/include/llvm/Option/OptTable.h
index a3106374543302..11c528c95f150d 100644
--- a/llvm/include/llvm/Option/OptTable.h
+++ b/llvm/include/llvm/Option/OptTable.h
@@ -154,8 +154,8 @@ class OptTable {
     return getInfo(id).MetaVar;
   }
 
- /// Get the meta-variable name to use when describing
-  /// this options values in the help text.
+  /// Get the string of acceptable argument value choices
+  /// that could be supplied to this option.
   const char *getOptionValues(OptSpecifier id) const {
     return getInfo(id).Values;
   }

>From 1df7e9a8f5a3446bd4f36181733a40e8cbb62b7e Mon Sep 17 00:00:00 2001
From: Joshua Batista <jbatista at microsoft.com>
Date: Fri, 8 Mar 2024 13:11:18 -0800
Subject: [PATCH 6/6] address Xiang

---
 clang/include/clang/Basic/LangStandard.h |  1 +
 clang/lib/Basic/LangStandards.cpp        | 15 +++++++++++
 clang/lib/Driver/ToolChains/HLSL.cpp     | 33 +++---------------------
 3 files changed, 20 insertions(+), 29 deletions(-)

diff --git a/clang/include/clang/Basic/LangStandard.h b/clang/include/clang/Basic/LangStandard.h
index bc49669a82ad2d..199e24c6731603 100644
--- a/clang/include/clang/Basic/LangStandard.h
+++ b/clang/include/clang/Basic/LangStandard.h
@@ -139,6 +139,7 @@ struct LangStandard {
   bool isOpenCL() const { return Flags & OpenCL; }
 
   static Kind getLangKind(StringRef Name);
+  static Kind getHLSLLangKind(StringRef Name);
   static const LangStandard &getLangStandardForKind(Kind K);
   static const LangStandard *getLangStandardForName(StringRef Name);
 };
diff --git a/clang/lib/Basic/LangStandards.cpp b/clang/lib/Basic/LangStandards.cpp
index ab09c7221dda92..87c078c2e38622 100644
--- a/clang/lib/Basic/LangStandards.cpp
+++ b/clang/lib/Basic/LangStandards.cpp
@@ -69,6 +69,21 @@ LangStandard::Kind LangStandard::getLangKind(StringRef Name) {
       .Default(lang_unspecified);
 }
 
+LangStandard::Kind LangStandard::getHLSLLangKind(StringRef Name) {
+  if (Name == "2016")
+    return LangStandard::lang_hlsl2016;
+  if (Name == "2017")
+    return LangStandard::lang_hlsl2017;
+  if (Name == "2018")
+    return LangStandard::lang_hlsl2018;
+  if (Name == "2021")
+    return LangStandard::lang_hlsl2021;
+  if (Name == "202x")
+    return LangStandard::lang_hlsl202x;
+
+  return LangStandard::lang_unspecified;
+}
+
 const LangStandard *LangStandard::getLangStandardForName(StringRef Name) {
   Kind K = getLangKind(Name);
   if (K == lang_unspecified)
diff --git a/clang/lib/Driver/ToolChains/HLSL.cpp b/clang/lib/Driver/ToolChains/HLSL.cpp
index 5692d1ea061c9e..85eedab9e54389 100644
--- a/clang/lib/Driver/ToolChains/HLSL.cpp
+++ b/clang/lib/Driver/ToolChains/HLSL.cpp
@@ -229,39 +229,14 @@ HLSLToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
     if (A->getOption().getID() == options::OPT_dxc_hlsl_version) {
       // Translate -HV into -std for llvm
       // depending on the value given
-      llvm::StringRef value(A->getValue());
-      llvm::StringRef acceptedValues(
-          Opts.getOptionValues(options::OPT_dxc_hlsl_version));
-      llvm::SmallVector<StringRef, 8> validValues;
-      acceptedValues.split(validValues, ", ");
-      if (value == validValues[0]) {
-        LangStandard l =
-            LangStandard::getLangStandardForKind(LangStandard::lang_hlsl2016);
-        DAL->AddSeparateArg(nullptr, Opts.getOption(options::OPT_std_EQ),
-                            l.getName());
-      } else if (value == validValues[1]) {
-        LangStandard l =
-            LangStandard::getLangStandardForKind(LangStandard::lang_hlsl2017);
-        DAL->AddSeparateArg(nullptr, Opts.getOption(options::OPT_std_EQ),
-                            l.getName());
-      } else if (value == validValues[2]) {
-        LangStandard l =
-            LangStandard::getLangStandardForKind(LangStandard::lang_hlsl2018);
-        DAL->AddSeparateArg(nullptr, Opts.getOption(options::OPT_std_EQ),
-                            l.getName());
-      } else if (value == validValues[3]) {
-        LangStandard l =
-            LangStandard::getLangStandardForKind(LangStandard::lang_hlsl2021);
-        DAL->AddSeparateArg(nullptr, Opts.getOption(options::OPT_std_EQ),
-                            l.getName());
-      } else if (value == validValues[4]) {
-        LangStandard l =
-            LangStandard::getLangStandardForKind(LangStandard::lang_hlsl202x);
+      LangStandard::Kind LangStd = LangStandard::getHLSLLangKind(A->getValue());
+      if (LangStd != LangStandard::lang_unspecified) {
+        LangStandard l = LangStandard::getLangStandardForKind(LangStd);
         DAL->AddSeparateArg(nullptr, Opts.getOption(options::OPT_std_EQ),
                             l.getName());
       } else {
         getDriver().Diag(diag::err_drv_hlsl_dxc_bad_argument_value)
-            << "HV" << value;
+            << "HV" << A->getValue();
       }
 
       A->claim();



More information about the cfe-commits mailing list