[clang] [HLSL] Add validation for the -enable-16bit-types option (PR #85340)

Joshua Batista via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 25 12:59:40 PDT 2024


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

>From 3ba14cb55a85bf8bd0ed0cae43af657c6946acb8 Mon Sep 17 00:00:00 2001
From: Joshua Batista <jbatista at microsoft.com>
Date: Thu, 14 Mar 2024 17:04:12 -0700
Subject: [PATCH 01/15] add test

---
 .../clang/Basic/DiagnosticDriverKinds.td      |  3 +-
 clang/lib/Driver/ToolChains/HLSL.cpp          | 86 ++++++++++++++++---
 2 files changed, 74 insertions(+), 15 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index e33a1f4c45b949..fae9132bd0a9c9 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -753,7 +753,8 @@ def err_drv_hlsl_unsupported_target : Error<
   "HLSL code generation is unsupported for target '%0'">;
 def err_drv_hlsl_bad_shader_required_in_target : Error<
   "%select{shader model|Vulkan environment|shader stage}0 is required as %select{OS|environment}1 in target '%2' for HLSL code generation">;
-
+def err_drv_hlsl_enable_16bit_types_option_invalid: Error<
+  "enable_16bit_types option only valid when target shader model [-T] is >= 6.2 and Hlsl Version [-HV] is >= 2021">;
 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 warn_drv_dxc_missing_dxv : Warning<"dxv not found. "
diff --git a/clang/lib/Driver/ToolChains/HLSL.cpp b/clang/lib/Driver/ToolChains/HLSL.cpp
index 05aac9caa7fb29..bf8fc42a27816c 100644
--- a/clang/lib/Driver/ToolChains/HLSL.cpp
+++ b/clang/lib/Driver/ToolChains/HLSL.cpp
@@ -66,15 +66,48 @@ bool isLegalShaderModel(Triple &T) {
   return false;
 }
 
-std::optional<std::string> tryParseProfile(StringRef Profile) {
-  // [ps|vs|gs|hs|ds|cs|ms|as]_[major]_[minor]
+struct ShaderModel {
+  StringRef TargetKind;
+  unsigned Major;
+  unsigned Minor;
+  bool OfflineLibMinor = false;
+};
+
+std::optional<ShaderModel> GetShaderModelFromString(StringRef Profile) {
   SmallVector<StringRef, 3> Parts;
   Profile.split(Parts, "_");
   if (Parts.size() != 3)
     return std::nullopt;
 
+  unsigned long long Major = 0;
+  if (llvm::getAsUnsignedInteger(Parts[1], 0, Major))
+    return std::nullopt;
+
+  unsigned long long Minor = 0;
+  bool isOfflineLibMinor = false;
+  if (Parts[0] == "lib" && Parts[2] == "x")
+    isOfflineLibMinor = true;
+  else if (llvm::getAsUnsignedInteger(Parts[2], 0, Minor))
+    return std::nullopt;
+
+  ShaderModel ret;
+  ret.TargetKind = Parts[0];
+  ret.Major = Major;
+  ret.Minor = Minor;
+  ret.OfflineLibMinor = isOfflineLibMinor;
+
+  return ret;
+}
+
+std::optional<std::string> tryParseProfile(StringRef Profile) {
+  std::optional<ShaderModel> SM = GetShaderModelFromString(Profile);
+  if (!SM.has_value()) {
+    return std::nullopt;
+  }
+  // [ps|vs|gs|hs|ds|cs|ms|as]_[major]_[minor]
+
   Triple::EnvironmentType Kind =
-      StringSwitch<Triple::EnvironmentType>(Parts[0])
+      StringSwitch<Triple::EnvironmentType>(SM.value().TargetKind)
           .Case("ps", Triple::EnvironmentType::Pixel)
           .Case("vs", Triple::EnvironmentType::Vertex)
           .Case("gs", Triple::EnvironmentType::Geometry)
@@ -88,21 +121,11 @@ std::optional<std::string> tryParseProfile(StringRef Profile) {
   if (Kind == Triple::EnvironmentType::UnknownEnvironment)
     return std::nullopt;
 
-  unsigned long long Major = 0;
-  if (llvm::getAsUnsignedInteger(Parts[1], 0, Major))
-    return std::nullopt;
-
-  unsigned long long Minor = 0;
-  if (Parts[2] == "x" && Kind == Triple::EnvironmentType::Library)
-    Minor = OfflineLibMinor;
-  else if (llvm::getAsUnsignedInteger(Parts[2], 0, Minor))
-    return std::nullopt;
-
   // dxil-unknown-shadermodel-hull
   llvm::Triple T;
   T.setArch(Triple::ArchType::dxil);
   T.setOSName(Triple::getOSTypeName(Triple::OSType::ShaderModel).str() +
-              VersionTuple(Major, Minor).getAsString());
+              VersionTuple(SM.value().Major, SM.value().Minor).getAsString());
   T.setEnvironment(Kind);
   if (isLegalShaderModel(T))
     return T.getTriple();
@@ -258,6 +281,41 @@ HLSLToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
   // FIXME: add validation for enable_16bit_types should be after HLSL 2018 and
   // shader model 6.2.
   // See: https://github.com/llvm/llvm-project/issues/57876
+  if (DAL->hasArg(options::OPT_fnative_half_type)) {
+
+    bool HVArgIsValid = true;
+    bool TPArgIsValid = true;
+
+    const StringRef HVArg =
+        DAL->getLastArgValue(options::OPT_std_EQ, "hlsl2021");
+
+    const StringRef TPArg =
+        DAL->getLastArgValue(options::OPT_target_profile, "");
+    std::optional<ShaderModel> parsedTargetProfile =
+        GetShaderModelFromString(TPArg);
+
+    unsigned long long HV_year;
+    StringRef HV_year_str = HVArg.drop_front(4);
+    if (HV_year_str != "202x") {
+      llvm::getAsUnsignedInteger(HV_year_str, 0, HV_year);
+      if (HV_year < 2021)
+        HVArgIsValid = false;
+    }
+
+    if (!parsedTargetProfile.has_value())
+      return DAL;
+    else {
+      if (parsedTargetProfile.value().Major < 6 ||
+          (parsedTargetProfile.value().Major == 6 &&
+           parsedTargetProfile.value().Minor < 2))
+        TPArgIsValid = false;
+    }
+
+    // if the HLSL Version is not at least 2021, or the shader model is not at
+    // least 6.2, then enable-16bit-types is an invalid flag.
+    if (!(HVArgIsValid && TPArgIsValid))
+      getDriver().Diag(diag::err_drv_hlsl_enable_16bit_types_option_invalid);
+  }
   return DAL;
 }
 

>From f7d8f97b54c3b9448aa20bd9b4d971cf257ae3f7 Mon Sep 17 00:00:00 2001
From: Joshua Batista <jbatista at microsoft.com>
Date: Thu, 14 Mar 2024 17:17:04 -0700
Subject: [PATCH 02/15] add helpful comment that a condition is unreachable

---
 clang/lib/Driver/ToolChains/HLSL.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/lib/Driver/ToolChains/HLSL.cpp b/clang/lib/Driver/ToolChains/HLSL.cpp
index bf8fc42a27816c..0b7b101f4a8cb9 100644
--- a/clang/lib/Driver/ToolChains/HLSL.cpp
+++ b/clang/lib/Driver/ToolChains/HLSL.cpp
@@ -303,6 +303,8 @@ HLSLToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
     }
 
     if (!parsedTargetProfile.has_value())
+      // This should be unreachable, target profile validation happens
+      // before this point.
       return DAL;
     else {
       if (parsedTargetProfile.value().Major < 6 ||

>From 4e89fa6a4137acdda23b96b7138a44f0e5c5200d Mon Sep 17 00:00:00 2001
From: Joshua Batista <jbatista at microsoft.com>
Date: Fri, 15 Mar 2024 12:53:47 -0700
Subject: [PATCH 03/15] add test, remove fixme comment

---
 clang/lib/Driver/ToolChains/HLSL.cpp          |  4 +---
 .../enable_16bit_types_validation.hlsl        | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Options/enable_16bit_types_validation.hlsl

diff --git a/clang/lib/Driver/ToolChains/HLSL.cpp b/clang/lib/Driver/ToolChains/HLSL.cpp
index 0b7b101f4a8cb9..35422df2b34fea 100644
--- a/clang/lib/Driver/ToolChains/HLSL.cpp
+++ b/clang/lib/Driver/ToolChains/HLSL.cpp
@@ -278,9 +278,7 @@ HLSLToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
   if (!DAL->hasArg(options::OPT_O_Group)) {
     DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_O), "3");
   }
-  // FIXME: add validation for enable_16bit_types should be after HLSL 2018 and
-  // shader model 6.2.
-  // See: https://github.com/llvm/llvm-project/issues/57876
+
   if (DAL->hasArg(options::OPT_fnative_half_type)) {
 
     bool HVArgIsValid = true;
diff --git a/clang/test/Options/enable_16bit_types_validation.hlsl b/clang/test/Options/enable_16bit_types_validation.hlsl
new file mode 100644
index 00000000000000..ad97e68cc60abf
--- /dev/null
+++ b/clang/test/Options/enable_16bit_types_validation.hlsl
@@ -0,0 +1,19 @@
+// RUN: not %clang_dxc -enable-16bit-types -T cs_6_0 -HV 2016 %s 2>&1 -###   | FileCheck -check-prefix=both_invalid %s
+// RUN: not %clang_dxc -enable-16bit-types -T lib_6_4 -HV 2017 %s 2>&1 -###   | FileCheck -check-prefix=HV_invalid %s
+// RUN: not %clang_dxc -enable-16bit-types -T cs_6_0 /HV 2021 %s 2>&1 -###   | FileCheck -check-prefix=TP_invalid %s
+// RUN: %clang_dxc -enable-16bit-types -T lib_6_4 /HV 2021 %s 2>&1 -###   | FileCheck -check-prefix=valid %ss
+
+
+// both_invalid: error: enable_16bit_types option only valid when target shader model [-T] is >= 6.2 and Hlsl Version [-HV] is >= 2021
+// HV_invalid: error: enable_16bit_types option only valid when target shader model [-T] is >= 6.2 and Hlsl Version [-HV] is >= 2021
+// TP_invalid: error: enable_16bit_types option only valid when target shader model [-T] is >= 6.2 and Hlsl Version [-HV] is >= 2021
+// valid: "dxil-unknown-shadermodel6.4-library"
+// valid-SAME: "-std=hlsl2021"
+// valid-SAME: "-fnative-half-type"
+
+[numthreads(1,1,1)]
+void main()
+{
+  return;
+}
+

>From 54e1fc5e7cbcfefe34559830e628f877f7c9fb69 Mon Sep 17 00:00:00 2001
From: Joshua Batista <jbatista at microsoft.com>
Date: Mon, 18 Mar 2024 13:50:36 -0700
Subject: [PATCH 04/15] fix test typo

---
 clang/test/Options/enable_16bit_types_validation.hlsl | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/Options/enable_16bit_types_validation.hlsl b/clang/test/Options/enable_16bit_types_validation.hlsl
index ad97e68cc60abf..205383c01b5ae4 100644
--- a/clang/test/Options/enable_16bit_types_validation.hlsl
+++ b/clang/test/Options/enable_16bit_types_validation.hlsl
@@ -1,7 +1,7 @@
 // RUN: not %clang_dxc -enable-16bit-types -T cs_6_0 -HV 2016 %s 2>&1 -###   | FileCheck -check-prefix=both_invalid %s
 // RUN: not %clang_dxc -enable-16bit-types -T lib_6_4 -HV 2017 %s 2>&1 -###   | FileCheck -check-prefix=HV_invalid %s
 // RUN: not %clang_dxc -enable-16bit-types -T cs_6_0 /HV 2021 %s 2>&1 -###   | FileCheck -check-prefix=TP_invalid %s
-// RUN: %clang_dxc -enable-16bit-types -T lib_6_4 /HV 2021 %s 2>&1 -###   | FileCheck -check-prefix=valid %ss
+// RUN: %clang_dxc -enable-16bit-types -T lib_6_4 /HV 2021 %s 2>&1 -###   | FileCheck -check-prefix=valid %s
 
 
 // both_invalid: error: enable_16bit_types option only valid when target shader model [-T] is >= 6.2 and Hlsl Version [-HV] is >= 2021

>From a4c781703733ed832e0db0938c5702bea9d4b350 Mon Sep 17 00:00:00 2001
From: Joshua Batista <jbatista at microsoft.com>
Date: Mon, 18 Mar 2024 15:41:14 -0700
Subject: [PATCH 05/15] move implementation to compilerinvocation, adjust test

---
 clang/lib/Driver/ToolChains/HLSL.cpp          | 88 +++----------------
 clang/lib/Frontend/CompilerInvocation.cpp     | 12 +++
 .../enable_16bit_types_validation.hlsl        |  6 +-
 3 files changed, 29 insertions(+), 77 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/HLSL.cpp b/clang/lib/Driver/ToolChains/HLSL.cpp
index 35422df2b34fea..1169b5d8c92dd6 100644
--- a/clang/lib/Driver/ToolChains/HLSL.cpp
+++ b/clang/lib/Driver/ToolChains/HLSL.cpp
@@ -66,48 +66,15 @@ bool isLegalShaderModel(Triple &T) {
   return false;
 }
 
-struct ShaderModel {
-  StringRef TargetKind;
-  unsigned Major;
-  unsigned Minor;
-  bool OfflineLibMinor = false;
-};
-
-std::optional<ShaderModel> GetShaderModelFromString(StringRef Profile) {
+std::optional<std::string> tryParseProfile(StringRef Profile) {
+  // [ps|vs|gs|hs|ds|cs|ms|as]_[major]_[minor]
   SmallVector<StringRef, 3> Parts;
   Profile.split(Parts, "_");
   if (Parts.size() != 3)
     return std::nullopt;
 
-  unsigned long long Major = 0;
-  if (llvm::getAsUnsignedInteger(Parts[1], 0, Major))
-    return std::nullopt;
-
-  unsigned long long Minor = 0;
-  bool isOfflineLibMinor = false;
-  if (Parts[0] == "lib" && Parts[2] == "x")
-    isOfflineLibMinor = true;
-  else if (llvm::getAsUnsignedInteger(Parts[2], 0, Minor))
-    return std::nullopt;
-
-  ShaderModel ret;
-  ret.TargetKind = Parts[0];
-  ret.Major = Major;
-  ret.Minor = Minor;
-  ret.OfflineLibMinor = isOfflineLibMinor;
-
-  return ret;
-}
-
-std::optional<std::string> tryParseProfile(StringRef Profile) {
-  std::optional<ShaderModel> SM = GetShaderModelFromString(Profile);
-  if (!SM.has_value()) {
-    return std::nullopt;
-  }
-  // [ps|vs|gs|hs|ds|cs|ms|as]_[major]_[minor]
-
   Triple::EnvironmentType Kind =
-      StringSwitch<Triple::EnvironmentType>(SM.value().TargetKind)
+      StringSwitch<Triple::EnvironmentType>(Parts[0])
           .Case("ps", Triple::EnvironmentType::Pixel)
           .Case("vs", Triple::EnvironmentType::Vertex)
           .Case("gs", Triple::EnvironmentType::Geometry)
@@ -121,11 +88,21 @@ std::optional<std::string> tryParseProfile(StringRef Profile) {
   if (Kind == Triple::EnvironmentType::UnknownEnvironment)
     return std::nullopt;
 
+  unsigned long long Major = 0;
+  if (llvm::getAsUnsignedInteger(Parts[1], 0, Major))
+    return std::nullopt;
+
+  unsigned long long Minor = 0;
+  if (Parts[2] == "x" && Kind == Triple::EnvironmentType::Library)
+    Minor = OfflineLibMinor;
+  else if (llvm::getAsUnsignedInteger(Parts[2], 0, Minor))
+    return std::nullopt;
+
   // dxil-unknown-shadermodel-hull
   llvm::Triple T;
   T.setArch(Triple::ArchType::dxil);
   T.setOSName(Triple::getOSTypeName(Triple::OSType::ShaderModel).str() +
-              VersionTuple(SM.value().Major, SM.value().Minor).getAsString());
+              VersionTuple(Major, Minor).getAsString());
   T.setEnvironment(Kind);
   if (isLegalShaderModel(T))
     return T.getTriple();
@@ -279,43 +256,6 @@ HLSLToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
     DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_O), "3");
   }
 
-  if (DAL->hasArg(options::OPT_fnative_half_type)) {
-
-    bool HVArgIsValid = true;
-    bool TPArgIsValid = true;
-
-    const StringRef HVArg =
-        DAL->getLastArgValue(options::OPT_std_EQ, "hlsl2021");
-
-    const StringRef TPArg =
-        DAL->getLastArgValue(options::OPT_target_profile, "");
-    std::optional<ShaderModel> parsedTargetProfile =
-        GetShaderModelFromString(TPArg);
-
-    unsigned long long HV_year;
-    StringRef HV_year_str = HVArg.drop_front(4);
-    if (HV_year_str != "202x") {
-      llvm::getAsUnsignedInteger(HV_year_str, 0, HV_year);
-      if (HV_year < 2021)
-        HVArgIsValid = false;
-    }
-
-    if (!parsedTargetProfile.has_value())
-      // This should be unreachable, target profile validation happens
-      // before this point.
-      return DAL;
-    else {
-      if (parsedTargetProfile.value().Major < 6 ||
-          (parsedTargetProfile.value().Major == 6 &&
-           parsedTargetProfile.value().Minor < 2))
-        TPArgIsValid = false;
-    }
-
-    // if the HLSL Version is not at least 2021, or the shader model is not at
-    // least 6.2, then enable-16bit-types is an invalid flag.
-    if (!(HVArgIsValid && TPArgIsValid))
-      getDriver().Diag(diag::err_drv_hlsl_enable_16bit_types_option_invalid);
-  }
   return DAL;
 }
 
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index 0df6a82ccd8933..7c94c92204ae65 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -4283,6 +4283,18 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
       } else {
         llvm_unreachable("expected DXIL or SPIR-V target");
       }
+      // validate that if fnative-half-type is given, that
+      // the language standard is at least hlsl2021, and that
+      // the target shader model is at least 6.2
+      if (Args.getLastArg(OPT_fnative_half_type)) {
+        bool LangStdArgIsValid = Opts.LangStd >= LangStandard::lang_hlsl2021;
+        bool TPArgIsValid = T.getOSVersion() >= VersionTuple(6, 2);
+
+        // if the HLSL Version is not at least 2021, or the shader model is not
+        // at least 6.2, then enable-16bit-types is an invalid flag.
+        if (!(LangStdArgIsValid && TPArgIsValid))
+          Diags.Report(diag::err_drv_hlsl_enable_16bit_types_option_invalid);
+      }
     } else
       Diags.Report(diag::err_drv_hlsl_unsupported_target) << T.str();
   }
diff --git a/clang/test/Options/enable_16bit_types_validation.hlsl b/clang/test/Options/enable_16bit_types_validation.hlsl
index 205383c01b5ae4..6d5d2e51e68b25 100644
--- a/clang/test/Options/enable_16bit_types_validation.hlsl
+++ b/clang/test/Options/enable_16bit_types_validation.hlsl
@@ -1,6 +1,6 @@
-// RUN: not %clang_dxc -enable-16bit-types -T cs_6_0 -HV 2016 %s 2>&1 -###   | FileCheck -check-prefix=both_invalid %s
-// RUN: not %clang_dxc -enable-16bit-types -T lib_6_4 -HV 2017 %s 2>&1 -###   | FileCheck -check-prefix=HV_invalid %s
-// RUN: not %clang_dxc -enable-16bit-types -T cs_6_0 /HV 2021 %s 2>&1 -###   | FileCheck -check-prefix=TP_invalid %s
+// RUN: not %clang_dxc -enable-16bit-types -T cs_6_0 -HV 2016 %s 2>&1  | FileCheck -check-prefix=both_invalid %s
+// RUN: not %clang_dxc -enable-16bit-types -T lib_6_4 -HV 2017 %s 2>&1 | FileCheck -check-prefix=HV_invalid %s
+// RUN: not %clang_dxc -enable-16bit-types -T cs_6_0 /HV 2021 %s 2>&1  | FileCheck -check-prefix=TP_invalid %s
 // RUN: %clang_dxc -enable-16bit-types -T lib_6_4 /HV 2021 %s 2>&1 -###   | FileCheck -check-prefix=valid %s
 
 

>From f0ed3fa9b30545dafefbd96cbce3b2ce76480e37 Mon Sep 17 00:00:00 2001
From: Joshua Batista <jbatista at microsoft.com>
Date: Tue, 19 Mar 2024 10:43:26 -0700
Subject: [PATCH 06/15] adjust test SM

---
 clang/test/CodeGenHLSL/builtins/RWBuffer-elementtype.hlsl | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/CodeGenHLSL/builtins/RWBuffer-elementtype.hlsl b/clang/test/CodeGenHLSL/builtins/RWBuffer-elementtype.hlsl
index 87002ccd462d3f..036c9c28ef2779 100644
--- a/clang/test/CodeGenHLSL/builtins/RWBuffer-elementtype.hlsl
+++ b/clang/test/CodeGenHLSL/builtins/RWBuffer-elementtype.hlsl
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -finclude-default-header -fnative-half-type -emit-llvm -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.2-compute -finclude-default-header -fnative-half-type -emit-llvm -o - %s | FileCheck %s
 
 RWBuffer<int16_t> BufI16;
 RWBuffer<uint16_t> BufU16;

>From 79234bf5f5f9b108428126d912596a5cc5642eff Mon Sep 17 00:00:00 2001
From: Joshua Batista <jbatista at microsoft.com>
Date: Thu, 21 Mar 2024 15:56:45 -0700
Subject: [PATCH 07/15] Update
 clang/include/clang/Basic/DiagnosticDriverKinds.td

Co-authored-by: Damyan Pepper <damyanp at microsoft.com>
---
 clang/include/clang/Basic/DiagnosticDriverKinds.td | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index fae9132bd0a9c9..5bcc5666ac70dd 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -754,7 +754,7 @@ def err_drv_hlsl_unsupported_target : Error<
 def err_drv_hlsl_bad_shader_required_in_target : Error<
   "%select{shader model|Vulkan environment|shader stage}0 is required as %select{OS|environment}1 in target '%2' for HLSL code generation">;
 def err_drv_hlsl_enable_16bit_types_option_invalid: Error<
-  "enable_16bit_types option only valid when target shader model [-T] is >= 6.2 and Hlsl Version [-HV] is >= 2021">;
+  "enable_16bit_types option only valid when target shader model [-T] is >= 6.2 and HLSL Version [-HV] is >= 2021">;
 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 warn_drv_dxc_missing_dxv : Warning<"dxv not found. "

>From acdf90b564985838ad93bb658b5463a041714cf6 Mon Sep 17 00:00:00 2001
From: Joshua Batista <jbatista at microsoft.com>
Date: Thu, 21 Mar 2024 16:53:47 -0700
Subject: [PATCH 08/15] address Damyang

---
 clang/lib/Frontend/CompilerInvocation.cpp     | 20 ++++++++-----------
 .../enable_16bit_types_validation.hlsl        |  6 +++---
 2 files changed, 11 insertions(+), 15 deletions(-)

diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index 7c94c92204ae65..d70f6463757b7a 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -4275,6 +4275,14 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
           Diags.Report(diag::err_drv_hlsl_bad_shader_unsupported)
               << ShaderModel << T.getOSName() << T.str();
         }
+        // validate that if fnative-half-type is given, that
+        // the language standard is at least hlsl2021, and that
+        // the target shader model is at least 6.2
+        if (Args.getLastArg(OPT_fnative_half_type)) {
+          if (!(Opts.LangStd >= LangStandard::lang_hlsl2021 &&
+                T.getOSVersion() >= VersionTuple(6, 2)))
+            Diags.Report(diag::err_drv_hlsl_enable_16bit_types_option_invalid);
+        }
       } else if (T.isSPIRVLogical()) {
         if (!T.isVulkanOS() || T.getVulkanVersion() == VersionTuple(0)) {
           Diags.Report(diag::err_drv_hlsl_bad_shader_unsupported)
@@ -4283,18 +4291,6 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
       } else {
         llvm_unreachable("expected DXIL or SPIR-V target");
       }
-      // validate that if fnative-half-type is given, that
-      // the language standard is at least hlsl2021, and that
-      // the target shader model is at least 6.2
-      if (Args.getLastArg(OPT_fnative_half_type)) {
-        bool LangStdArgIsValid = Opts.LangStd >= LangStandard::lang_hlsl2021;
-        bool TPArgIsValid = T.getOSVersion() >= VersionTuple(6, 2);
-
-        // if the HLSL Version is not at least 2021, or the shader model is not
-        // at least 6.2, then enable-16bit-types is an invalid flag.
-        if (!(LangStdArgIsValid && TPArgIsValid))
-          Diags.Report(diag::err_drv_hlsl_enable_16bit_types_option_invalid);
-      }
     } else
       Diags.Report(diag::err_drv_hlsl_unsupported_target) << T.str();
   }
diff --git a/clang/test/Options/enable_16bit_types_validation.hlsl b/clang/test/Options/enable_16bit_types_validation.hlsl
index 6d5d2e51e68b25..b833de2072b8d3 100644
--- a/clang/test/Options/enable_16bit_types_validation.hlsl
+++ b/clang/test/Options/enable_16bit_types_validation.hlsl
@@ -4,9 +4,9 @@
 // RUN: %clang_dxc -enable-16bit-types -T lib_6_4 /HV 2021 %s 2>&1 -###   | FileCheck -check-prefix=valid %s
 
 
-// both_invalid: error: enable_16bit_types option only valid when target shader model [-T] is >= 6.2 and Hlsl Version [-HV] is >= 2021
-// HV_invalid: error: enable_16bit_types option only valid when target shader model [-T] is >= 6.2 and Hlsl Version [-HV] is >= 2021
-// TP_invalid: error: enable_16bit_types option only valid when target shader model [-T] is >= 6.2 and Hlsl Version [-HV] is >= 2021
+// both_invalid: error: enable_16bit_types option only valid when target shader model [-T] is >= 6.2 and HLSL Version [-HV] is >= 2021
+// HV_invalid: error: enable_16bit_types option only valid when target shader model [-T] is >= 6.2 and HLSL Version [-HV] is >= 2021
+// TP_invalid: error: enable_16bit_types option only valid when target shader model [-T] is >= 6.2 and HLSL Version [-HV] is >= 2021
 // valid: "dxil-unknown-shadermodel6.4-library"
 // valid-SAME: "-std=hlsl2021"
 // valid-SAME: "-fnative-half-type"

>From 787fb85cacfe1819cd4dd3b46b32605e026ef214 Mon Sep 17 00:00:00 2001
From: Joshua Batista <jbatista at microsoft.com>
Date: Fri, 22 Mar 2024 12:57:01 -0700
Subject: [PATCH 09/15] add check for spirv

---
 clang/lib/Frontend/CompilerInvocation.cpp | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index d70f6463757b7a..bf9b1f7284765f 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -4288,6 +4288,11 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
           Diags.Report(diag::err_drv_hlsl_bad_shader_unsupported)
               << VulkanEnv << T.getOSName() << T.str();
         }
+        if (Args.getLastArg(OPT_fnative_half_type)) {
+          if (!(Opts.LangStd >= LangStandard::lang_hlsl2021))
+            Diags.Report(diag::err_drv_hlsl_bad_shader_unsupported)
+                << VulkanEnv << T.getOSName() << T.str();
+        }
       } else {
         llvm_unreachable("expected DXIL or SPIR-V target");
       }

>From a6203964ebbb7260095c9949d77e7d742b843b01 Mon Sep 17 00:00:00 2001
From: Joshua Batista <jbatista at microsoft.com>
Date: Mon, 25 Mar 2024 11:46:30 -0700
Subject: [PATCH 10/15] spirv test try 1

---
 clang/cmake/caches/HLSL.cmake                          |  2 +-
 .../Options/enable_16bit_types_validation_spirv.hlsl   | 10 ++++++++++
 2 files changed, 11 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/Options/enable_16bit_types_validation_spirv.hlsl

diff --git a/clang/cmake/caches/HLSL.cmake b/clang/cmake/caches/HLSL.cmake
index 71f81e53f6bd35..84850c86f12cd7 100644
--- a/clang/cmake/caches/HLSL.cmake
+++ b/clang/cmake/caches/HLSL.cmake
@@ -4,7 +4,7 @@ set(LLVM_TARGETS_TO_BUILD Native CACHE STRING "")
 
 # Include the DirectX target for DXIL code generation, eventually we'll include
 # SPIR-V here too.
-set(LLVM_EXPERIMENTAL_TARGETS_TO_BUILD DirectX CACHE STRING "")
+set(LLVM_EXPERIMENTAL_TARGETS_TO_BUILD "DirectX;SPIRV" CACHE STRING "")
 
 # HLSL support is currently limted to clang, eventually it will expand to
 # clang-tools-extra too.
diff --git a/clang/test/Options/enable_16bit_types_validation_spirv.hlsl b/clang/test/Options/enable_16bit_types_validation_spirv.hlsl
new file mode 100644
index 00000000000000..5ed75dc0147000
--- /dev/null
+++ b/clang/test/Options/enable_16bit_types_validation_spirv.hlsl
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -internal-isystem D:\llvm-project\build\x64-Release\lib\clang\19\include -nostdsysteminc -triple spirv-vulkan-library -x hlsl -std=hlsl2016 -emit-llvm -disable-llvm-passes  -o - %s | FileCheck %s --check-prefix=SPIRV
+// kRUN: %clang_dxc -T lib_6_4 -HV 2016 -enable-16bit-types %s | FileCheck %s --check-prefix=SPIRV
+// SPIRV: error: enable_16bit_types option only valid when target shader model [-T] is >= 6.2 and HLSL Version [-HV] is >= 2021
+
+[numthreads(1,1,1)]
+void main()
+{
+  return;
+}
+

>From b6d6814abd4af2287f93196aa9a21744ea23bd14 Mon Sep 17 00:00:00 2001
From: Joshua Batista <jbatista at microsoft.com>
Date: Thu, 14 Mar 2024 17:04:12 -0700
Subject: [PATCH 11/15] add test

---
 clang/lib/Driver/ToolChains/HLSL.cpp | 89 +++++++++++++++++++++++-----
 1 file changed, 75 insertions(+), 14 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/HLSL.cpp b/clang/lib/Driver/ToolChains/HLSL.cpp
index 1169b5d8c92dd6..d14ab93e656aed 100644
--- a/clang/lib/Driver/ToolChains/HLSL.cpp
+++ b/clang/lib/Driver/ToolChains/HLSL.cpp
@@ -66,15 +66,48 @@ bool isLegalShaderModel(Triple &T) {
   return false;
 }
 
-std::optional<std::string> tryParseProfile(StringRef Profile) {
-  // [ps|vs|gs|hs|ds|cs|ms|as]_[major]_[minor]
+struct ShaderModel {
+  StringRef TargetKind;
+  unsigned Major;
+  unsigned Minor;
+  bool OfflineLibMinor = false;
+};
+
+std::optional<ShaderModel> GetShaderModelFromString(StringRef Profile) {
   SmallVector<StringRef, 3> Parts;
   Profile.split(Parts, "_");
   if (Parts.size() != 3)
     return std::nullopt;
 
+  unsigned long long Major = 0;
+  if (llvm::getAsUnsignedInteger(Parts[1], 0, Major))
+    return std::nullopt;
+
+  unsigned long long Minor = 0;
+  bool isOfflineLibMinor = false;
+  if (Parts[0] == "lib" && Parts[2] == "x")
+    isOfflineLibMinor = true;
+  else if (llvm::getAsUnsignedInteger(Parts[2], 0, Minor))
+    return std::nullopt;
+
+  ShaderModel ret;
+  ret.TargetKind = Parts[0];
+  ret.Major = Major;
+  ret.Minor = Minor;
+  ret.OfflineLibMinor = isOfflineLibMinor;
+
+  return ret;
+}
+
+std::optional<std::string> tryParseProfile(StringRef Profile) {
+  std::optional<ShaderModel> SM = GetShaderModelFromString(Profile);
+  if (!SM.has_value()) {
+    return std::nullopt;
+  }
+  // [ps|vs|gs|hs|ds|cs|ms|as]_[major]_[minor]
+
   Triple::EnvironmentType Kind =
-      StringSwitch<Triple::EnvironmentType>(Parts[0])
+      StringSwitch<Triple::EnvironmentType>(SM.value().TargetKind)
           .Case("ps", Triple::EnvironmentType::Pixel)
           .Case("vs", Triple::EnvironmentType::Vertex)
           .Case("gs", Triple::EnvironmentType::Geometry)
@@ -88,21 +121,11 @@ std::optional<std::string> tryParseProfile(StringRef Profile) {
   if (Kind == Triple::EnvironmentType::UnknownEnvironment)
     return std::nullopt;
 
-  unsigned long long Major = 0;
-  if (llvm::getAsUnsignedInteger(Parts[1], 0, Major))
-    return std::nullopt;
-
-  unsigned long long Minor = 0;
-  if (Parts[2] == "x" && Kind == Triple::EnvironmentType::Library)
-    Minor = OfflineLibMinor;
-  else if (llvm::getAsUnsignedInteger(Parts[2], 0, Minor))
-    return std::nullopt;
-
   // dxil-unknown-shadermodel-hull
   llvm::Triple T;
   T.setArch(Triple::ArchType::dxil);
   T.setOSName(Triple::getOSTypeName(Triple::OSType::ShaderModel).str() +
-              VersionTuple(Major, Minor).getAsString());
+              VersionTuple(SM.value().Major, SM.value().Minor).getAsString());
   T.setEnvironment(Kind);
   if (isLegalShaderModel(T))
     return T.getTriple();
@@ -256,6 +279,44 @@ HLSLToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
     DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_O), "3");
   }
 
+  // FIXME: add validation for enable_16bit_types should be after HLSL 2018 and
+  // shader model 6.2.
+  // See: https://github.com/llvm/llvm-project/issues/57876
+  if (DAL->hasArg(options::OPT_fnative_half_type)) {
+
+    bool HVArgIsValid = true;
+    bool TPArgIsValid = true;
+
+    const StringRef HVArg =
+        DAL->getLastArgValue(options::OPT_std_EQ, "hlsl2021");
+
+    const StringRef TPArg =
+        DAL->getLastArgValue(options::OPT_target_profile, "");
+    std::optional<ShaderModel> parsedTargetProfile =
+        GetShaderModelFromString(TPArg);
+
+    unsigned long long HV_year;
+    StringRef HV_year_str = HVArg.drop_front(4);
+    if (HV_year_str != "202x") {
+      llvm::getAsUnsignedInteger(HV_year_str, 0, HV_year);
+      if (HV_year < 2021)
+        HVArgIsValid = false;
+    }
+
+    if (!parsedTargetProfile.has_value())
+      return DAL;
+    else {
+      if (parsedTargetProfile.value().Major < 6 ||
+          (parsedTargetProfile.value().Major == 6 &&
+           parsedTargetProfile.value().Minor < 2))
+        TPArgIsValid = false;
+    }
+
+    // if the HLSL Version is not at least 2021, or the shader model is not at
+    // least 6.2, then enable-16bit-types is an invalid flag.
+    if (!(HVArgIsValid && TPArgIsValid))
+      getDriver().Diag(diag::err_drv_hlsl_enable_16bit_types_option_invalid);
+  }
   return DAL;
 }
 

>From ef683409e4aca2f6ae1493fb808e3286156ed87d Mon Sep 17 00:00:00 2001
From: Joshua Batista <jbatista at microsoft.com>
Date: Thu, 14 Mar 2024 17:17:04 -0700
Subject: [PATCH 12/15] add helpful comment that a condition is unreachable

---
 clang/lib/Driver/ToolChains/HLSL.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/lib/Driver/ToolChains/HLSL.cpp b/clang/lib/Driver/ToolChains/HLSL.cpp
index d14ab93e656aed..04dd2b611bcb9d 100644
--- a/clang/lib/Driver/ToolChains/HLSL.cpp
+++ b/clang/lib/Driver/ToolChains/HLSL.cpp
@@ -304,6 +304,8 @@ HLSLToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
     }
 
     if (!parsedTargetProfile.has_value())
+      // This should be unreachable, target profile validation happens
+      // before this point.
       return DAL;
     else {
       if (parsedTargetProfile.value().Major < 6 ||

>From 6fd2af542093cf51713c4d7c55411b13f8281034 Mon Sep 17 00:00:00 2001
From: Joshua Batista <jbatista at microsoft.com>
Date: Fri, 15 Mar 2024 12:53:47 -0700
Subject: [PATCH 13/15] add test, remove fixme comment

---
 clang/lib/Driver/ToolChains/HLSL.cpp                  | 3 ---
 clang/test/Options/enable_16bit_types_validation.hlsl | 1 +
 2 files changed, 1 insertion(+), 3 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/HLSL.cpp b/clang/lib/Driver/ToolChains/HLSL.cpp
index 04dd2b611bcb9d..35422df2b34fea 100644
--- a/clang/lib/Driver/ToolChains/HLSL.cpp
+++ b/clang/lib/Driver/ToolChains/HLSL.cpp
@@ -279,9 +279,6 @@ HLSLToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
     DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_O), "3");
   }
 
-  // FIXME: add validation for enable_16bit_types should be after HLSL 2018 and
-  // shader model 6.2.
-  // See: https://github.com/llvm/llvm-project/issues/57876
   if (DAL->hasArg(options::OPT_fnative_half_type)) {
 
     bool HVArgIsValid = true;
diff --git a/clang/test/Options/enable_16bit_types_validation.hlsl b/clang/test/Options/enable_16bit_types_validation.hlsl
index b833de2072b8d3..b7f293fb6cbc66 100644
--- a/clang/test/Options/enable_16bit_types_validation.hlsl
+++ b/clang/test/Options/enable_16bit_types_validation.hlsl
@@ -7,6 +7,7 @@
 // both_invalid: error: enable_16bit_types option only valid when target shader model [-T] is >= 6.2 and HLSL Version [-HV] is >= 2021
 // HV_invalid: error: enable_16bit_types option only valid when target shader model [-T] is >= 6.2 and HLSL Version [-HV] is >= 2021
 // TP_invalid: error: enable_16bit_types option only valid when target shader model [-T] is >= 6.2 and HLSL Version [-HV] is >= 2021
+
 // valid: "dxil-unknown-shadermodel6.4-library"
 // valid-SAME: "-std=hlsl2021"
 // valid-SAME: "-fnative-half-type"

>From 974c83355e58be839c349837d736f47e90e99fd5 Mon Sep 17 00:00:00 2001
From: Joshua Batista <jbatista at microsoft.com>
Date: Mon, 18 Mar 2024 15:41:14 -0700
Subject: [PATCH 14/15] move implementation to compilerinvocation, adjust test

---
 clang/lib/Driver/ToolChains/HLSL.cpp      | 88 ++++-------------------
 clang/lib/Frontend/CompilerInvocation.cpp | 12 ++++
 2 files changed, 26 insertions(+), 74 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/HLSL.cpp b/clang/lib/Driver/ToolChains/HLSL.cpp
index 35422df2b34fea..1169b5d8c92dd6 100644
--- a/clang/lib/Driver/ToolChains/HLSL.cpp
+++ b/clang/lib/Driver/ToolChains/HLSL.cpp
@@ -66,48 +66,15 @@ bool isLegalShaderModel(Triple &T) {
   return false;
 }
 
-struct ShaderModel {
-  StringRef TargetKind;
-  unsigned Major;
-  unsigned Minor;
-  bool OfflineLibMinor = false;
-};
-
-std::optional<ShaderModel> GetShaderModelFromString(StringRef Profile) {
+std::optional<std::string> tryParseProfile(StringRef Profile) {
+  // [ps|vs|gs|hs|ds|cs|ms|as]_[major]_[minor]
   SmallVector<StringRef, 3> Parts;
   Profile.split(Parts, "_");
   if (Parts.size() != 3)
     return std::nullopt;
 
-  unsigned long long Major = 0;
-  if (llvm::getAsUnsignedInteger(Parts[1], 0, Major))
-    return std::nullopt;
-
-  unsigned long long Minor = 0;
-  bool isOfflineLibMinor = false;
-  if (Parts[0] == "lib" && Parts[2] == "x")
-    isOfflineLibMinor = true;
-  else if (llvm::getAsUnsignedInteger(Parts[2], 0, Minor))
-    return std::nullopt;
-
-  ShaderModel ret;
-  ret.TargetKind = Parts[0];
-  ret.Major = Major;
-  ret.Minor = Minor;
-  ret.OfflineLibMinor = isOfflineLibMinor;
-
-  return ret;
-}
-
-std::optional<std::string> tryParseProfile(StringRef Profile) {
-  std::optional<ShaderModel> SM = GetShaderModelFromString(Profile);
-  if (!SM.has_value()) {
-    return std::nullopt;
-  }
-  // [ps|vs|gs|hs|ds|cs|ms|as]_[major]_[minor]
-
   Triple::EnvironmentType Kind =
-      StringSwitch<Triple::EnvironmentType>(SM.value().TargetKind)
+      StringSwitch<Triple::EnvironmentType>(Parts[0])
           .Case("ps", Triple::EnvironmentType::Pixel)
           .Case("vs", Triple::EnvironmentType::Vertex)
           .Case("gs", Triple::EnvironmentType::Geometry)
@@ -121,11 +88,21 @@ std::optional<std::string> tryParseProfile(StringRef Profile) {
   if (Kind == Triple::EnvironmentType::UnknownEnvironment)
     return std::nullopt;
 
+  unsigned long long Major = 0;
+  if (llvm::getAsUnsignedInteger(Parts[1], 0, Major))
+    return std::nullopt;
+
+  unsigned long long Minor = 0;
+  if (Parts[2] == "x" && Kind == Triple::EnvironmentType::Library)
+    Minor = OfflineLibMinor;
+  else if (llvm::getAsUnsignedInteger(Parts[2], 0, Minor))
+    return std::nullopt;
+
   // dxil-unknown-shadermodel-hull
   llvm::Triple T;
   T.setArch(Triple::ArchType::dxil);
   T.setOSName(Triple::getOSTypeName(Triple::OSType::ShaderModel).str() +
-              VersionTuple(SM.value().Major, SM.value().Minor).getAsString());
+              VersionTuple(Major, Minor).getAsString());
   T.setEnvironment(Kind);
   if (isLegalShaderModel(T))
     return T.getTriple();
@@ -279,43 +256,6 @@ HLSLToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
     DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_O), "3");
   }
 
-  if (DAL->hasArg(options::OPT_fnative_half_type)) {
-
-    bool HVArgIsValid = true;
-    bool TPArgIsValid = true;
-
-    const StringRef HVArg =
-        DAL->getLastArgValue(options::OPT_std_EQ, "hlsl2021");
-
-    const StringRef TPArg =
-        DAL->getLastArgValue(options::OPT_target_profile, "");
-    std::optional<ShaderModel> parsedTargetProfile =
-        GetShaderModelFromString(TPArg);
-
-    unsigned long long HV_year;
-    StringRef HV_year_str = HVArg.drop_front(4);
-    if (HV_year_str != "202x") {
-      llvm::getAsUnsignedInteger(HV_year_str, 0, HV_year);
-      if (HV_year < 2021)
-        HVArgIsValid = false;
-    }
-
-    if (!parsedTargetProfile.has_value())
-      // This should be unreachable, target profile validation happens
-      // before this point.
-      return DAL;
-    else {
-      if (parsedTargetProfile.value().Major < 6 ||
-          (parsedTargetProfile.value().Major == 6 &&
-           parsedTargetProfile.value().Minor < 2))
-        TPArgIsValid = false;
-    }
-
-    // if the HLSL Version is not at least 2021, or the shader model is not at
-    // least 6.2, then enable-16bit-types is an invalid flag.
-    if (!(HVArgIsValid && TPArgIsValid))
-      getDriver().Diag(diag::err_drv_hlsl_enable_16bit_types_option_invalid);
-  }
   return DAL;
 }
 
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index bf9b1f7284765f..37e70e3521c87e 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -4296,6 +4296,18 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
       } else {
         llvm_unreachable("expected DXIL or SPIR-V target");
       }
+      // validate that if fnative-half-type is given, that
+      // the language standard is at least hlsl2021, and that
+      // the target shader model is at least 6.2
+      if (Args.getLastArg(OPT_fnative_half_type)) {
+        bool LangStdArgIsValid = Opts.LangStd >= LangStandard::lang_hlsl2021;
+        bool TPArgIsValid = T.getOSVersion() >= VersionTuple(6, 2);
+
+        // if the HLSL Version is not at least 2021, or the shader model is not
+        // at least 6.2, then enable-16bit-types is an invalid flag.
+        if (!(LangStdArgIsValid && TPArgIsValid))
+          Diags.Report(diag::err_drv_hlsl_enable_16bit_types_option_invalid);
+      }
     } else
       Diags.Report(diag::err_drv_hlsl_unsupported_target) << T.str();
   }

>From 012a90feab22b9e4d04b82bc8ba938cc850fb682 Mon Sep 17 00:00:00 2001
From: Joshua Batista <jbatista at microsoft.com>
Date: Thu, 21 Mar 2024 16:53:47 -0700
Subject: [PATCH 15/15] address Damyang

---
 clang/lib/Frontend/CompilerInvocation.cpp | 12 ------------
 1 file changed, 12 deletions(-)

diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index 37e70e3521c87e..bf9b1f7284765f 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -4296,18 +4296,6 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
       } else {
         llvm_unreachable("expected DXIL or SPIR-V target");
       }
-      // validate that if fnative-half-type is given, that
-      // the language standard is at least hlsl2021, and that
-      // the target shader model is at least 6.2
-      if (Args.getLastArg(OPT_fnative_half_type)) {
-        bool LangStdArgIsValid = Opts.LangStd >= LangStandard::lang_hlsl2021;
-        bool TPArgIsValid = T.getOSVersion() >= VersionTuple(6, 2);
-
-        // if the HLSL Version is not at least 2021, or the shader model is not
-        // at least 6.2, then enable-16bit-types is an invalid flag.
-        if (!(LangStdArgIsValid && TPArgIsValid))
-          Diags.Report(diag::err_drv_hlsl_enable_16bit_types_option_invalid);
-      }
     } else
       Diags.Report(diag::err_drv_hlsl_unsupported_target) << T.str();
   }



More information about the cfe-commits mailing list