[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
Mon Nov 17 09:07:24 PST 2025
https://github.com/farzonl updated https://github.com/llvm/llvm-project/pull/167628
>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 1/4] [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
>From e2f0a2d03d060e29df500ab13033caf5f4ca5290 Mon Sep 17 00:00:00 2001
From: Farzon Lotfi <farzonlotfi at microsoft.com>
Date: Wed, 12 Nov 2025 18:51:44 -0500
Subject: [PATCH 2/4] add new documentation to address pr concerns
---
clang/docs/LanguageExtensions.rst | 5 +++++
clang/docs/MatrixTypes.rst | 5 +++++
clang/docs/ReleaseNotes.rst | 1 +
3 files changed, 11 insertions(+)
diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst
index a3db3e5d356b3..88abc270fccf6 100644
--- a/clang/docs/LanguageExtensions.rst
+++ b/clang/docs/LanguageExtensions.rst
@@ -1073,6 +1073,11 @@ The matrix type extension supports explicit casts. Implicit type conversion betw
i = static_cast<matrix_5_5<int>>(d);
}
+The matrix type extension will support column and row major layouts. The flag
+to change this behavior is `-fmatrix-default-layout` used like so
+`-fmatrix-default-layout=column-major` for column major and like so
+`-fmatrix-default-layout=row-major` for row major.
+
Half-Precision Floating Point
=============================
diff --git a/clang/docs/MatrixTypes.rst b/clang/docs/MatrixTypes.rst
index b3a2c8cf53670..6c5392149a814 100644
--- a/clang/docs/MatrixTypes.rst
+++ b/clang/docs/MatrixTypes.rst
@@ -287,6 +287,11 @@ part of the draft specification.
The elements of a value of a matrix type are laid out in column-major order
without padding.
+To change the default order to row major use the `-fmatrix-default-layout` flag.
+This flag supports two flag argument values either `column-major` or `row-major`
+used like so `-fmatrix-default-layout=column-major`.` This flag controls the
+memory layout of matrix types.
+
We propose to provide a Clang option to override this behavior and allow
contraction of those operations (e.g. *-ffp-contract=matrix*).
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 88a05affebf9e..9a53274a0fe1c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -316,6 +316,7 @@ New Compiler Flags
- New option ``-fsanitize-debug-trap-reasons=`` added to control emitting trap reasons into the debug info when compiling with trapping UBSan (e.g. ``-fsanitize-trap=undefined``).
- New options for enabling allocation token instrumentation: ``-fsanitize=alloc-token``, ``-falloc-token-max=``, ``-fsanitize-alloc-token-fast-abi``, ``-fsanitize-alloc-token-extended``.
- The ``-resource-dir`` option is now displayed in the list of options shown by ``--help``.
+- New option ``-fmatrix-default-layout`` added to control the memory layout of Clang matrix types. (e.g. ``-fmatrix-default-layout=column-major`` or ``-fmatrix-default-layout=row-major``).
Lanai Support
^^^^^^^^^^^^^^
>From 49a8726884cdb2089c464332fcf3585e4348dd56 Mon Sep 17 00:00:00 2001
From: Farzon Lotfi <farzonlotfi at microsoft.com>
Date: Fri, 14 Nov 2025 14:57:54 -0500
Subject: [PATCH 3/4] Address pr comments, drop the -mllvm flags make this just
about memory layout. So this change actually does something move the lang opt
changes into this pr
---
clang/docs/LanguageExtensions.rst | 6 +--
clang/docs/MatrixTypes.rst | 5 +--
clang/docs/ReleaseNotes.rst | 2 +-
clang/include/clang/Basic/LangOptions.def | 1 +
clang/include/clang/Basic/LangOptions.h | 7 ++++
clang/include/clang/Options/Options.td | 5 ++-
clang/lib/Driver/ToolChains/Clang.cpp | 6 +--
clang/lib/Frontend/CompilerInvocation.cpp | 19 ++++++++++
clang/test/Driver/fmatrix-default-layout.c | 38 -------------------
clang/test/Driver/fmatrix-memory-layout.c | 35 +++++++++++++++++
.../Frontend/CompilerInvocationTest.cpp | 36 ++++++++++++++++++
11 files changed, 109 insertions(+), 51 deletions(-)
delete mode 100644 clang/test/Driver/fmatrix-default-layout.c
create mode 100644 clang/test/Driver/fmatrix-memory-layout.c
diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst
index 88abc270fccf6..cc5ae719080f0 100644
--- a/clang/docs/LanguageExtensions.rst
+++ b/clang/docs/LanguageExtensions.rst
@@ -1074,9 +1074,9 @@ The matrix type extension supports explicit casts. Implicit type conversion betw
}
The matrix type extension will support column and row major layouts. The flag
-to change this behavior is `-fmatrix-default-layout` used like so
-`-fmatrix-default-layout=column-major` for column major and like so
-`-fmatrix-default-layout=row-major` for row major.
+to change this behavior is `-fmatrix-memory-layout` used like so
+`-fmatrix-memory-layout=column-major` for column major and like so
+`-fmatrix-memory-layout=row-major` for row major.
Half-Precision Floating Point
=============================
diff --git a/clang/docs/MatrixTypes.rst b/clang/docs/MatrixTypes.rst
index 6c5392149a814..bc36857800aa4 100644
--- a/clang/docs/MatrixTypes.rst
+++ b/clang/docs/MatrixTypes.rst
@@ -287,10 +287,9 @@ part of the draft specification.
The elements of a value of a matrix type are laid out in column-major order
without padding.
-To change the default order to row major use the `-fmatrix-default-layout` flag.
+To change memory layout to row major use the `-fmatrix-default-layout` flag.
This flag supports two flag argument values either `column-major` or `row-major`
-used like so `-fmatrix-default-layout=column-major`.` This flag controls the
-memory layout of matrix types.
+used like so `-fmatrix-default-layout=column-major`.`
We propose to provide a Clang option to override this behavior and allow
contraction of those operations (e.g. *-ffp-contract=matrix*).
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 9a53274a0fe1c..8383b2ded756f 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -316,7 +316,7 @@ New Compiler Flags
- New option ``-fsanitize-debug-trap-reasons=`` added to control emitting trap reasons into the debug info when compiling with trapping UBSan (e.g. ``-fsanitize-trap=undefined``).
- New options for enabling allocation token instrumentation: ``-fsanitize=alloc-token``, ``-falloc-token-max=``, ``-fsanitize-alloc-token-fast-abi``, ``-fsanitize-alloc-token-extended``.
- The ``-resource-dir`` option is now displayed in the list of options shown by ``--help``.
-- New option ``-fmatrix-default-layout`` added to control the memory layout of Clang matrix types. (e.g. ``-fmatrix-default-layout=column-major`` or ``-fmatrix-default-layout=row-major``).
+- New option ``-fmatrix-memory-layout`` added to control the memory layout of Clang matrix types. (e.g. ``-fmatrix-memory-layout=column-major`` or ``-fmatrix-memory-layout=row-major``).
Lanai Support
^^^^^^^^^^^^^^
diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def
index 40fc66ea12e34..bc2226942f7e4 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -434,6 +434,7 @@ ENUM_LANGOPT(RegisterStaticDestructors, RegisterStaticDestructorsKind, 2,
LANGOPT(RegCall4, 1, 0, NotCompatible, "Set __regcall4 as a default calling convention to respect __regcall ABI v.4")
LANGOPT(MatrixTypes, 1, 0, NotCompatible, "Enable or disable the builtin matrix type")
+ENUM_LANGOPT(DefaultMatrixMemoryLayout, MatrixMemoryLayout, 1, MatrixColMajor, NotCompatible, "Defines the default memory Layout for matrices")
VALUE_LANGOPT(MaxMatrixDimension, 32, (1 << 20) - 1, NotCompatible, "maximum allowed matrix dimension")
LANGOPT(CXXAssumptions, 1, 1, NotCompatible, "Enable or disable codegen and compile-time checks for C++23's [[assume]] attribute")
diff --git a/clang/include/clang/Basic/LangOptions.h b/clang/include/clang/Basic/LangOptions.h
index 8aa89d8c8c807..3f697ad6b05a8 100644
--- a/clang/include/clang/Basic/LangOptions.h
+++ b/clang/include/clang/Basic/LangOptions.h
@@ -251,6 +251,13 @@ class LangOptionsBase {
FEM_UnsetOnCommandLine = 3
};
+ enum MatrixMemoryLayout : unsigned {
+ // Use column-major layout for matrices
+ MatrixColMajor = 0,
+ // Use row-major layout for matrices
+ MatrixRowMajor = 1,
+ };
+
enum ExcessPrecisionKind { FPP_Standard, FPP_Fast, FPP_None };
enum class LaxVectorConversionKind {
diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
index e3f50c3187086..db497f9c314f5 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -4642,8 +4642,9 @@ 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)">,
+def fmatrix_memory_layout_EQ : Joined<["-"], "fmatrix-memory-layout=">,
+ Visibility<[ClangOption, CC1Option]>,
+ HelpText<"Sets the matrix memory layout (row-major or column-major)">,
Values<"row-major,column-major">,
Group<f_Group>;
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index bcb97e8dfd897..c5f03a12851cd 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -5693,12 +5693,10 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
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)) {
+ if (const Arg *A = Args.getLastArg(options::OPT_fmatrix_memory_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));
+ CmdArgs.push_back(Args.MakeArgString("-fmatrix-memory-layout=" + Val));
} else {
D.Diag(diag::err_drv_invalid_value) << A->getAsString(Args) << Val;
}
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index f584a2a5824b2..389dfd2d36f47 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -3976,6 +3976,13 @@ void CompilerInvocationBase::GenerateLangArgs(const LangOptions &Opts,
}
GenerateArg(Consumer, OPT_falloc_token_mode_EQ, S);
}
+ // Enable options for Matrices
+ if (Opts.MatrixTypes) {
+ if (Opts.DefaultMatrixMemoryLayout == LangOptions::MatrixColMajor)
+ GenerateArg(Consumer, OPT_fmatrix_memory_layout_EQ, "column-major");
+ if (Opts.DefaultMatrixMemoryLayout == LangOptions::MatrixRowMajor)
+ GenerateArg(Consumer, OPT_fmatrix_memory_layout_EQ, "row-major");
+ }
}
bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
@@ -4571,6 +4578,18 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
Diags.Report(diag::err_drv_invalid_value) << Arg->getAsString(Args) << S;
}
+ if (Opts.MatrixTypes) {
+ if (const Arg *A = Args.getLastArg(OPT_fmatrix_memory_layout_EQ)) {
+ StringRef Val = A->getValue();
+ if (Val == "row-major")
+ Opts.setDefaultMatrixMemoryLayout(
+ LangOptions::MatrixMemoryLayout::MatrixRowMajor);
+ else
+ Opts.setDefaultMatrixMemoryLayout(
+ LangOptions::MatrixMemoryLayout::MatrixColMajor);
+ }
+ }
+
// Validate options for HLSL
if (Opts.HLSL) {
// TODO: Revisit restricting SPIR-V to logical once we've figured out how to
diff --git a/clang/test/Driver/fmatrix-default-layout.c b/clang/test/Driver/fmatrix-default-layout.c
deleted file mode 100644
index c89396f4452f6..0000000000000
--- a/clang/test/Driver/fmatrix-default-layout.c
+++ /dev/null
@@ -1,38 +0,0 @@
-// 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
diff --git a/clang/test/Driver/fmatrix-memory-layout.c b/clang/test/Driver/fmatrix-memory-layout.c
new file mode 100644
index 0000000000000..b6ed6ac9d9061
--- /dev/null
+++ b/clang/test/Driver/fmatrix-memory-layout.c
@@ -0,0 +1,35 @@
+// RUN: %clang --target=x86_64-linux-gnu -fenable-matrix -fmatrix-memory-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: -fmatrix-memory-layout=column-major
+
+// RUN: %clang --target=x86_64-linux-gnu -fenable-matrix -fmatrix-memory-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: -fmatrix-memory-layout=row-major
+
+// RUN: not %clang --target=x86_64-linux-gnu -fenable-matrix -fmatrix-memory-layout=error-major %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-ERROR-MAJOR
+// CHECK-ERROR-MAJOR: error: invalid value 'error-major' in '-fmatrix-memory-layout=error-major'
+
+// RUN: %clang --target=x86_64-linux-gnu -fmatrix-memory-layout=column-major %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-COL-MAJOR-DISABLED
+// CHECK-COL-MAJOR-DISABLED: clang: warning: argument unused during compilation: '-fmatrix-memory-layout=column-major'
+// 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: -fmatrix-memory-layout=column-major
+
+// RUN: %clang --target=x86_64-linux-gnu -fmatrix-memory-layout=row-major %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-ROW-MAJOR-DISABLED
+// CHECK-ROW-MAJOR-DISABLED: clang: warning: argument unused during compilation: '-fmatrix-memory-layout=row-major'
+// 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: -fmatrix-memory-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: -fmatrix-memory-layout=row-major
+// CHECK-MATRIX-ENABLED-NOT: -fmatrix-memory-layout=column-major
diff --git a/clang/unittests/Frontend/CompilerInvocationTest.cpp b/clang/unittests/Frontend/CompilerInvocationTest.cpp
index 1332422688fe6..f03e1db6644be 100644
--- a/clang/unittests/Frontend/CompilerInvocationTest.cpp
+++ b/clang/unittests/Frontend/CompilerInvocationTest.cpp
@@ -738,6 +738,18 @@ TEST_F(CommandLineTest, ConditionalParsingIfHLSLFlagPresent) {
CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags);
ASSERT_EQ(Invocation.getLangOpts().MaxMatrixDimension, 4u);
+ ASSERT_EQ(Invocation.getLangOpts().getDefaultMatrixMemoryLayout(),
+ LangOptions::MatrixColMajor);
+
+ Invocation.generateCC1CommandLine(GeneratedArgs, *this);
+}
+
+TEST_F(CommandLineTest, ConditionalParsingHLSLRowMajor) {
+ const char *Args[] = {"-xhlsl", "-fmatrix-memory-layout=row-major"};
+
+ CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags);
+ ASSERT_EQ(Invocation.getLangOpts().getDefaultMatrixMemoryLayout(),
+ LangOptions::MatrixRowMajor);
Invocation.generateCC1CommandLine(GeneratedArgs, *this);
}
@@ -748,6 +760,30 @@ TEST_F(CommandLineTest, ConditionalParsingIfHLSLFlagNotPresent) {
CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags);
ASSERT_EQ(Invocation.getLangOpts().MaxMatrixDimension, 1048575u);
+ ASSERT_EQ(Invocation.getLangOpts().getDefaultMatrixMemoryLayout(),
+ LangOptions::MatrixColMajor);
+
+ Invocation.generateCC1CommandLine(GeneratedArgs, *this);
+}
+
+TEST_F(CommandLineTest, ConditionalParsingClangRowMajor) {
+ const char *Args[] = {"-fenable-matrix", "-fmatrix-memory-layout=row-major"};
+
+ CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags);
+
+ ASSERT_EQ(Invocation.getLangOpts().getDefaultMatrixMemoryLayout(),
+ LangOptions::MatrixRowMajor);
+
+ Invocation.generateCC1CommandLine(GeneratedArgs, *this);
+}
+
+TEST_F(CommandLineTest, ConditionalParsingIgnoreRowMajorIfMatrixNotEnabled) {
+ const char *Args[] = {"-fmatrix-memory-layout=row-major"};
+
+ CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags);
+
+ ASSERT_EQ(Invocation.getLangOpts().getDefaultMatrixMemoryLayout(),
+ LangOptions::MatrixColMajor);
Invocation.generateCC1CommandLine(GeneratedArgs, *this);
}
>From fa843ede3973f20bb431a47ef01a23e37bf6573c Mon Sep 17 00:00:00 2001
From: Farzon Lotfi <farzonl at gmail.com>
Date: Mon, 17 Nov 2025 12:07:14 -0500
Subject: [PATCH 4/4] Update comments in CompilerInvocation.cpp
---
clang/lib/Frontend/CompilerInvocation.cpp | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index fa4bd82e51328..565e09b6d74b8 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -3976,7 +3976,7 @@ void CompilerInvocationBase::GenerateLangArgs(const LangOptions &Opts,
}
GenerateArg(Consumer, OPT_falloc_token_mode_EQ, S);
}
- // Enable options for Matrices
+ // Generate args for Matrices
if (Opts.MatrixTypes) {
if (Opts.DefaultMatrixMemoryLayout == LangOptions::MatrixColMajor)
GenerateArg(Consumer, OPT_fmatrix_memory_layout_EQ, "column-major");
@@ -4578,6 +4578,7 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
Diags.Report(diag::err_drv_invalid_value) << Arg->getAsString(Args) << S;
}
+ // Enable options for Matrices
if (Opts.MatrixTypes) {
if (const Arg *A = Args.getLastArg(OPT_fmatrix_memory_layout_EQ)) {
StringRef Val = A->getValue();
More information about the cfe-commits
mailing list