Skip to content

Conversation

@farzonl
Copy link
Member

@farzonl farzonl commented Dec 10, 2025

fixes #58676

  • Make /Zpr and /Zpc turn on the -fmatrix-memory-layout= row-major and column-major flags
  • Add the new DXC driver flags to Options.td
  • Error in the HLSL toolchain when both flags are specified
  • Add the new error diagnostic to DiagnosticDriverKinds.td
  • propogate the flag via the Clang toolchain

fixes llvm#58676

- Make /Zpr and /Zpc turn on the -fmatrix-memory-layout= row-major and column-major flags
- Add the new DXC driver flags to Options.td
- Error in the HLSL toolchain when  both flags are specified
- Add the new error diagnostic to DiagnosticDriverKinds.td
- propogate the flag via the Clang toolchain
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang:frontend Language frontend issues, e.g. anything involving "Sema" HLSL HLSL Language Support labels Dec 10, 2025
@llvmbot
Copy link
Member

llvmbot commented Dec 10, 2025

@llvm/pr-subscribers-hlsl
@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-driver

Author: Farzon Lotfi (farzonl)

Changes

fixes #58676

  • Make /Zpr and /Zpc turn on the -fmatrix-memory-layout= row-major and column-major flags
  • Add the new DXC driver flags to Options.td
  • Error in the HLSL toolchain when both flags are specified
  • Add the new error diagnostic to DiagnosticDriverKinds.td
  • propogate the flag via the Clang toolchain

Full diff: https://github.com/llvm/llvm-project/pull/171550.diff

5 Files Affected:

  • (modified) clang/include/clang/Basic/DiagnosticDriverKinds.td (+2)
  • (modified) clang/include/clang/Options/Options.td (+2)
  • (modified) clang/lib/Driver/ToolChains/Clang.cpp (+4)
  • (modified) clang/lib/Driver/ToolChains/HLSL.cpp (+4)
  • (added) clang/test/Driver/hlsl_matrix_pack_order.hlsl (+8)
diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index 3e6f7a2a430ff..fca84904326c9 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -826,6 +826,8 @@ def err_drv_target_variant_invalid : Error<
 
 def err_drv_invalid_directx_shader_module : Error<
   "invalid profile : %0">;
+def err_drv_dxc_invalid_matrix_layout : Error<
+  "cannot specify /Zpr and /Zpc together">;
 def err_drv_dxc_missing_target_profile : Error<
   "target profile option (-T) is missing">;
 def err_drv_hlsl_unsupported_target : Error<
diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
index e55146f0c7823..35fb9d5bd227a 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -9587,6 +9587,8 @@ class DXCJoinedOrSeparate<string name> : Option<["/", "-"], name,
   KIND_JOINED_OR_SEPARATE>, Group<dxc_Group>,
   Visibility<[DXCOption]>;
 
+def dxc_col_major : DXCFlag<"Zpc">, HelpText<"Pack matrices in column-major order">;
+def dxc_row_major : DXCFlag<"Zpr">, HelpText<"Pack matrices in row-major order">;
 def dxc_no_stdinc : DXCFlag<"hlsl-no-stdinc">,
   HelpText<"HLSL only. Disables all standard includes containing non-native compiler types and functions.">;
 def dxc_Fo : DXCJoinedOrSeparate<"Fo">,
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 542b70b3e9d4c..b0d1c994de160 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -3723,6 +3723,10 @@ static void RenderHLSLOptions(const ArgList &Args, ArgStringList &CmdArgs,
   if (!Args.hasArg(options::OPT_dxc_no_stdinc) &&
       !Args.hasArg(options::OPT_nostdinc))
     CmdArgs.push_back("-finclude-default-header");
+  if (Args.hasArg(options::OPT_dxc_col_major))
+    CmdArgs.push_back("-fmatrix-memory-layout=column-major");
+  if (Args.hasArg(options::OPT_dxc_row_major))
+    CmdArgs.push_back("-fmatrix-memory-layout=row-major");
 }
 
 static void RenderOpenACCOptions(const Driver &D, const ArgList &Args,
diff --git a/clang/lib/Driver/ToolChains/HLSL.cpp b/clang/lib/Driver/ToolChains/HLSL.cpp
index 5d7221b8718a9..8f58b2f449a9a 100644
--- a/clang/lib/Driver/ToolChains/HLSL.cpp
+++ b/clang/lib/Driver/ToolChains/HLSL.cpp
@@ -412,6 +412,10 @@ HLSLToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
 
   const OptTable &Opts = getDriver().getOpts();
 
+  if (Args.hasArg(options::OPT_dxc_col_major) &&
+      Args.hasArg(options::OPT_dxc_row_major))
+    getDriver().Diag(diag::err_drv_dxc_invalid_matrix_layout);
+
   for (Arg *A : Args) {
     if (A->getOption().getID() == options::OPT_dxil_validator_version) {
       StringRef ValVerStr = A->getValue();
diff --git a/clang/test/Driver/hlsl_matrix_pack_order.hlsl b/clang/test/Driver/hlsl_matrix_pack_order.hlsl
new file mode 100644
index 0000000000000..f7b6902fd02d8
--- /dev/null
+++ b/clang/test/Driver/hlsl_matrix_pack_order.hlsl
@@ -0,0 +1,8 @@
+// RUN: %clang_dxc -T lib_6_7  -Zpr -### %s 2>&1 | FileCheck %s  --check-prefix=CHECK-ROW-MAJOR
+// CHECK-ROW-MAJOR:  -fmatrix-memory-layout=row-major
+
+// RUN: %clang_dxc -T lib_6_7  -Zpc -### %s 2>&1 | FileCheck %s --check-prefix=CHECK-COL-MAJOR
+// CHECK-COL-MAJOR:  -fmatrix-memory-layout=column-major
+
+// RUN: not  %clang_dxc  -Tlib_6_7 -Zpr -Zpc -fcgl -Fo - %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-MISMATCH-MAJOR
+// CHECK-MISMATCH-MAJOR: cannot specify /Zpr and /Zpc together

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category HLSL HLSL Language Support

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[HLSL] support HLSL matrix default orientation flags

3 participants