[clang] [HLSL] Add diagnostic for enabling 16 bit types (PR #84537)

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


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

The -enable-16bit-types option was previously added without checking for a couple essential conditions. First, the target shader model must be greater than 6.2, and secondly, the hlsl version must be after HLSL 2018. 
This PR adds a check after option translation, to check that if the enable_16bit_types option exists, that the target shader model and hlsl versions are satisfactory. If not, a new diagnostic is emitted.
Fixes https://github.com/llvm/llvm-project/issues/57876

>From 55395c5c8a5b92e200aa2f1bf06774d20ac4afd9 Mon Sep 17 00:00:00 2001
From: Joshua Batista <jbatista at microsoft.com>
Date: Fri, 8 Mar 2024 11:03:37 -0800
Subject: [PATCH] add check for enable 16 bit types

---
 clang/include/clang/Basic/DiagnosticDriverKinds.td |  3 ++-
 clang/lib/Driver/ToolChains/HLSL.cpp               | 14 ++++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index 1bc9885849d54b..23009117af0f37 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -750,7 +750,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 : Error<
+  "enable_16bit_types cannot be applied when shader model is <= 6.2 and hlsl version <= 2018. (SM: %0, HV: %1)">;
 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 c6ad862b229420..e30ae78ca6553c 100644
--- a/clang/lib/Driver/ToolChains/HLSL.cpp
+++ b/clang/lib/Driver/ToolChains/HLSL.cpp
@@ -243,6 +243,20 @@ 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_enable_16bit_types)) {
+    llvm::opt::Arg *hvArg = DAL->getLastArg(options::OPT_dxc_hlsl_version);
+    llvm::VersionTuple versionTuple = getTriple().getOSVersion();
+    unsigned int major = versionTuple.getMajor();
+    auto minor = versionTuple.getMinor();
+
+    if (major < 6 || (major == 6 && minor.value() <= 2) ||
+        strcmp(hvArg->getValue(), "2016") == 0 ||
+        strcmp(hvArg->getValue(), "2017") == 0 ||
+        strcmp(hvArg->getValue(), "2018") == 0) {
+      getDriver().Diag(diag::err_drv_hlsl_enable_16bit_types)
+          << versionTuple.getAsString() << hvArg->getValue();
+    }
+  }
   return DAL;
 }
 



More information about the cfe-commits mailing list