[clang] [Matrix] Add a row\col major toggle in the clang driver (PR #167628)
Farzon Lotfi via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 11 19:20:30 PST 2025
https://github.com/farzonl created https://github.com/llvm/llvm-project/pull/167628
fixes #167621
- define the new options in `Options.td` limit the naming to row-major or column-major.
- In `ToolChains/Clang.cpp` limit the opt usage to only when `-fenable-matrix` is used.
- make sure we set the flags llvm needs for the lower-matrix-intrinsics pass.
>From fd63d4ceb6b1bb3c824e34063af18af5f9514a1e Mon Sep 17 00:00:00 2001
From: Farzon Lotfi <farzonlotfi at microsoft.com>
Date: Tue, 11 Nov 2025 22:13:57 -0500
Subject: [PATCH] [Matrix] Add a row\col major toggle in the clang driver
fixes #167621
- define the new options in `Options.td` limit the naming to row-major or
column-major.
- In `ToolChains/Clang.cpp` limit the opt usage to only when
`-fenable-matrix` is used.
- make sure we set the flags llvm needs for the lower-matrix-intrinsics
pass.
---
clang/include/clang/Options/Options.td | 5 +++
clang/lib/Driver/ToolChains/Clang.cpp | 11 +++++++
clang/test/Driver/fmatrix-default-layout.c | 38 ++++++++++++++++++++++
3 files changed, 54 insertions(+)
create mode 100644 clang/test/Driver/fmatrix-default-layout.c
diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
index 2f7434d8afe11..e3f50c3187086 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -4642,6 +4642,11 @@ def fenable_matrix : Flag<["-"], "fenable-matrix">, Group<f_Group>,
HelpText<"Enable matrix data type and related builtin functions">,
MarshallingInfoFlag<LangOpts<"MatrixTypes">, hlsl.KeyPath>;
+def fmatrix_default_layout_EQ : Joined<["-"], "fmatrix-default-layout=">,
+ HelpText<"Set default matrix layout (row-major or column-major)">,
+ Values<"row-major,column-major">,
+ Group<f_Group>;
+
defm raw_string_literals : BoolFOption<"raw-string-literals",
LangOpts<"RawStringLiterals">, Default<std#".hasRawStringLiterals()">,
PosFlag<SetTrue, [], [], "Enable">,
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 80389937ee218..bcb97e8dfd897 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -5692,6 +5692,17 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
CmdArgs.push_back("-fenable-matrix");
CmdArgs.push_back("-mllvm");
CmdArgs.push_back("-enable-matrix");
+ // Only handle default layout if matrix is enabled
+ if (const Arg *A =
+ Args.getLastArg(options::OPT_fmatrix_default_layout_EQ)) {
+ StringRef Val = A->getValue();
+ if (Val == "row-major" || Val == "column-major") {
+ CmdArgs.push_back("-mllvm");
+ CmdArgs.push_back(Args.MakeArgString("-matrix-default-layout=" + Val));
+ } else {
+ D.Diag(diag::err_drv_invalid_value) << A->getAsString(Args) << Val;
+ }
+ }
}
CodeGenOptions::FramePointerKind FPKeepKind =
diff --git a/clang/test/Driver/fmatrix-default-layout.c b/clang/test/Driver/fmatrix-default-layout.c
new file mode 100644
index 0000000000000..c89396f4452f6
--- /dev/null
+++ b/clang/test/Driver/fmatrix-default-layout.c
@@ -0,0 +1,38 @@
+// RUN: %clang --target=x86_64-linux-gnu -fenable-matrix -fmatrix-default-layout=column-major %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-COL-MAJOR
+// CHECK-COL-MAJOR: -fenable-matrix
+// CHECK-COL-MAJOR: -mllvm
+// CHECK-COL-MAJOR: -enable-matrix
+// CHECK-COL-MAJOR: -mllvm
+// CHECK-COL-MAJOR: -matrix-default-layout=column-major
+
+// RUN: %clang --target=x86_64-linux-gnu -fenable-matrix -fmatrix-default-layout=row-major %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-ROW-MAJOR
+// CHECK-ROW-MAJOR: -fenable-matrix
+// CHECK-ROW-MAJOR: -mllvm
+// CHECK-ROW-MAJOR: -enable-matrix
+// CHECK-ROW-MAJOR: -mllvm
+// CHECK-ROW-MAJOR: -matrix-default-layout=row-major
+
+// RUN: not %clang --target=x86_64-linux-gnu -fenable-matrix -fmatrix-default-layout=error-major %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-ERROR-MAJOR
+// CHECK-ERROR-MAJOR: error: invalid value 'error-major' in '-fmatrix-default-layout=error-major'
+
+// RUN: %clang --target=x86_64-linux-gnu -fmatrix-default-layout=column-major %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-COL-MAJOR-DISABLED
+// CHECK-COL-MAJOR-DISABLED-NOT: -fenable-matrix
+// CHECK-COL-MAJOR-DISABLED-NOT: -mllvm
+// CHECK-COL-MAJOR-DISABLED-NOT: -enable-matrix
+// CHECK-COL-MAJOR-DISABLED-NOT: -mllvm
+// CHECK-COL-MAJOR-DISABLED-NOT: -matrix-default-layout=column-major
+
+// RUN: %clang --target=x86_64-linux-gnu -fmatrix-default-layout=row-major %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-ROW-MAJOR-DISABLED
+// CHECK-ROW-MAJOR-DISABLED-NOT: -fenable-matrix
+// CHECK-ROW-MAJOR-DISABLED-NOT: -mllvm
+// CHECK-ROW-MAJOR-DISABLED-NOT: -enable-matrix
+// CHECK-ROW-MAJOR-DISABLED-NOT: -mllvm
+// CHECK-ROW-MAJOR-DISABLED-NOT: -matrix-default-layout=row-major
+
+// RUN: %clang --target=x86_64-linux-gnu -fenable-matrix %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-MATRIX-ENABLED
+// CHECK-MATRIX-ENABLED: -fenable-matrix
+// CHECK-MATRIX-ENABLED: -mllvm
+// CHECK-MATRIX-ENABLED: -enable-matrix
+// CHECK-MATRIX-ENABLED-NOT: -mllvm
+// CHECK-MATRIX-ENABLED-NOT: -matrix-default-layout=row-major
+// CHECK-MATRIX-ENABLED-NOT: -matrix-default-layout=column-major
More information about the cfe-commits
mailing list