[llvm-branch-commits] [clang] 7062cd6 - [Matrix] Add a row\col major toggle in the clang driver (#167628)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Dec 9 15:51:29 PST 2025
Author: Farzon Lotfi
Date: 2025-12-09T18:30:25-05:00
New Revision: 7062cd63d0c258ac499c82dc233259aecac29b06
URL: https://github.com/llvm/llvm-project/commit/7062cd63d0c258ac499c82dc233259aecac29b06
DIFF: https://github.com/llvm/llvm-project/commit/7062cd63d0c258ac499c82dc233259aecac29b06.diff
LOG: [Matrix] Add a row\col major toggle in the clang driver (#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.
---------
Co-authored-by: Florian Hahn <flo at fhahn.com>
Added:
clang/test/Driver/fmatrix-memory-layout.c
clang/test/Sema/matrix-col-major-builtin-disable.c
Modified:
clang/docs/LanguageExtensions.rst
clang/docs/MatrixTypes.rst
clang/docs/ReleaseNotes.rst
clang/include/clang/Basic/DiagnosticDriverKinds.td
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/include/clang/Basic/LangOptions.def
clang/include/clang/Basic/LangOptions.h
clang/include/clang/Options/Options.td
clang/lib/Driver/ToolChains/Clang.cpp
clang/lib/Frontend/CompilerInvocation.cpp
clang/lib/Sema/SemaChecking.cpp
clang/unittests/Frontend/CompilerInvocationTest.cpp
Removed:
################################################################################
diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst
index 87d38e7d99e50..c4b86b203d383 100644
--- a/clang/docs/LanguageExtensions.rst
+++ b/clang/docs/LanguageExtensions.rst
@@ -1073,6 +1073,12 @@ The matrix type extension supports explicit casts. Implicit type conversion betw
i = static_cast<matrix_5_5<int>>(d);
}
+The matrix type extension supports column and row major memory layouts, but not
+all builtins are supported with row-major layout. The layout defaults to column
+major and can be specified using `-fmatrix-memory-layout`. To enable column
+major layout, use `-fmatrix-memory-layout=column-major`, and for row major
+layout use `-fmatrix-memory-layout=row-major`
+
Half-Precision Floating Point
=============================
diff --git a/clang/docs/MatrixTypes.rst b/clang/docs/MatrixTypes.rst
index b3a2c8cf53670..8701baa2d0f1c 100644
--- a/clang/docs/MatrixTypes.rst
+++ b/clang/docs/MatrixTypes.rst
@@ -287,6 +287,10 @@ 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 memory layout to row major use the `-fmatrix-memory-layout` flag.
+This flag supports two flag argument values either `column-major` or
+`row-major` used like so `-fmatrix-memory-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 1cd465e25947a..690d2a389ef24 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -331,6 +331,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-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/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index aeffe96e806bd..3e6f7a2a430ff 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -792,6 +792,9 @@ def err_cc1_round_trip_mismatch : Error<
def err_cc1_unbounded_vscale_min : Error<
"minimum vscale must be an unsigned integer greater than 0">;
+def err_conflicting_matrix_layout_flags: Error<
+ "-fmatrix-memory-layout=%0 conflicts with -mllvm -matrix-default-layout=%1">;
+
def err_drv_using_omit_rtti_component_without_no_rtti : Error<
"-fexperimental-omit-vtable-rtti call only be used with -fno-rtti">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index fec278c21a89e..28803829f387d 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -13041,6 +13041,9 @@ def err_builtin_trivially_relocate_invalid_arg_type: Error <
def err_builtin_matrix_disabled: Error<
"matrix types extension is disabled. Pass -fenable-matrix to enable it">;
+def err_builtin_matrix_major_order_disabled: Error<
+ "matrix %select{row|column}0 major %select{load|store}1 is not supported with "
+ "-fmatrix-memory-layout=%select{column|row}0-major. Pass -fmatrix-memory-layout=%select{row|column}0-major to enable it">;
def err_matrix_index_not_integer: Error<
"matrix %select{row|column}0 index is not an integer">;
def err_matrix_index_outside_range: Error<
diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def
index 40fc66ea12e34..e515c0cee79eb 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, MatrixMemoryLayout::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 61ee0275283fc..859ed57996983 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 class 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 cac122d296624..e55146f0c7823 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -4692,6 +4692,12 @@ 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_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>;
+
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 7187d1a158e01..542b70b3e9d4c 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -5703,6 +5703,18 @@ 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_memory_layout_EQ)) {
+ StringRef Val = A->getValue();
+ if (Val == "row-major" || Val == "column-major") {
+ CmdArgs.push_back(Args.MakeArgString("-fmatrix-memory-layout=" + Val));
+ 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/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index b42c263abd2c7..477406f2526c0 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -3951,6 +3951,15 @@ void CompilerInvocationBase::GenerateLangArgs(const LangOptions &Opts,
StringRef S = llvm::getAllocTokenModeAsString(*Opts.AllocTokenMode);
GenerateArg(Consumer, OPT_falloc_token_mode_EQ, S);
}
+ // Generate args for matrix types.
+ if (Opts.MatrixTypes) {
+ if (Opts.getDefaultMatrixMemoryLayout() ==
+ LangOptions::MatrixMemoryLayout::MatrixColMajor)
+ GenerateArg(Consumer, OPT_fmatrix_memory_layout_EQ, "column-major");
+ if (Opts.getDefaultMatrixMemoryLayout() ==
+ LangOptions::MatrixMemoryLayout::MatrixRowMajor)
+ GenerateArg(Consumer, OPT_fmatrix_memory_layout_EQ, "row-major");
+ }
}
bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
@@ -4545,6 +4554,27 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
Diags.Report(diag::err_drv_invalid_value) << Arg->getAsString(Args) << S;
}
+ // Enable options for matrix types.
+ if (Opts.MatrixTypes) {
+ if (const Arg *A = Args.getLastArg(OPT_fmatrix_memory_layout_EQ)) {
+ StringRef ClangValue = A->getValue();
+ if (ClangValue == "row-major")
+ Opts.setDefaultMatrixMemoryLayout(
+ LangOptions::MatrixMemoryLayout::MatrixRowMajor);
+ else
+ Opts.setDefaultMatrixMemoryLayout(
+ LangOptions::MatrixMemoryLayout::MatrixColMajor);
+
+ for (Arg *A : Args.filtered(options::OPT_mllvm)) {
+ StringRef OptValue = A->getValue();
+ if (OptValue.consume_front("-matrix-default-layout=") &&
+ ClangValue != OptValue)
+ Diags.Report(diag::err_conflicting_matrix_layout_flags)
+ << ClangValue << OptValue;
+ }
+ }
+ }
+
// 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/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 02c838bc4a862..67482f1d56da1 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -16511,6 +16511,13 @@ ExprResult Sema::BuiltinMatrixColumnMajorLoad(CallExpr *TheCall,
return ExprError();
}
+ if (getLangOpts().getDefaultMatrixMemoryLayout() !=
+ LangOptions::MatrixMemoryLayout::MatrixColMajor) {
+ Diag(TheCall->getBeginLoc(), diag::err_builtin_matrix_major_order_disabled)
+ << /*column*/ 1 << /*load*/ 0;
+ return ExprError();
+ }
+
if (checkArgCount(TheCall, 4))
return ExprError();
@@ -16623,6 +16630,18 @@ ExprResult Sema::BuiltinMatrixColumnMajorLoad(CallExpr *TheCall,
ExprResult Sema::BuiltinMatrixColumnMajorStore(CallExpr *TheCall,
ExprResult CallResult) {
+ if (!getLangOpts().MatrixTypes) {
+ Diag(TheCall->getBeginLoc(), diag::err_builtin_matrix_disabled);
+ return ExprError();
+ }
+
+ if (getLangOpts().getDefaultMatrixMemoryLayout() !=
+ LangOptions::MatrixMemoryLayout::MatrixColMajor) {
+ Diag(TheCall->getBeginLoc(), diag::err_builtin_matrix_major_order_disabled)
+ << /*column*/ 1 << /*store*/ 1;
+ return ExprError();
+ }
+
if (checkArgCount(TheCall, 3))
return ExprError();
diff --git a/clang/test/Driver/fmatrix-memory-layout.c b/clang/test/Driver/fmatrix-memory-layout.c
new file mode 100644
index 0000000000000..f05cd8f26c004
--- /dev/null
+++ b/clang/test/Driver/fmatrix-memory-layout.c
@@ -0,0 +1,49 @@
+// 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
+// CHECK-COL-MAJOR: -mllvm
+// CHECK-COL-MAJOR: -matrix-default-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
+// CHECK-ROW-MAJOR: -mllvm
+// CHECK-ROW-MAJOR: -matrix-default-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
+// CHECK-COL-MAJOR-DISABLED-NOT: -mllvm
+// CHECK-COL-MAJOR-DISABLED-NOT: -matrix-default-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
+// 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: -fmatrix-memory-layout=row-major
+// CHECK-MATRIX-ENABLED-NOT: -fmatrix-memory-layout=column-major
+// CHECK-MATRIX-ENABLED-NOT: -mllvm
+// CHECK-MATRIX-ENABLED-NOT: -matrix-default-layout=row-major
+// CHECK-MATRIX-ENABLED-NOT: -matrix-default-layout=column-major
+
+// RUN: not %clang --target=x86_64-linux-gnu -fenable-matrix -fmatrix-memory-layout=column-major -mllvm -matrix-default-layout=row-major %s -fsyntax-only 2>&1 | FileCheck %s --check-prefix=CHECK-MISMATCH-MAJOR
+// CHECK-MISMATCH-MAJOR: error: -fmatrix-memory-layout=column-major conflicts with -mllvm -matrix-default-layout=row-major
diff --git a/clang/test/Sema/matrix-col-major-builtin-disable.c b/clang/test/Sema/matrix-col-major-builtin-disable.c
new file mode 100644
index 0000000000000..b86d4ebe54f93
--- /dev/null
+++ b/clang/test/Sema/matrix-col-major-builtin-disable.c
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 %s -fenable-matrix -fmatrix-memory-layout=row-major -pedantic -verify -triple=x86_64-apple-darwin9
+
+typedef float sx5x10_t __attribute__((matrix_type(5, 10)));
+
+void column_major_load(float *p1) {
+ sx5x10_t a1 = __builtin_matrix_column_major_load(p1, 5, 11, 5);
+ // expected-error at -1 {{matrix column major load is not supported with -fmatrix-memory-layout=row-major. Pass -fmatrix-memory-layout=column-major to enable it}}
+}
+
+void column_major_store(sx5x10_t *m1, float *p1) {
+ __builtin_matrix_column_major_store(*m1, p1, 1);
+ // expected-error at -1 {{matrix column major store is not supported with -fmatrix-memory-layout=row-major. Pass -fmatrix-memory-layout=column-major to enable it}}
+}
diff --git a/clang/unittests/Frontend/CompilerInvocationTest.cpp b/clang/unittests/Frontend/CompilerInvocationTest.cpp
index 1332422688fe6..64d0f4c38f11c 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::MatrixMemoryLayout::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::MatrixMemoryLayout::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::MatrixMemoryLayout::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::MatrixMemoryLayout::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::MatrixMemoryLayout::MatrixColMajor);
Invocation.generateCC1CommandLine(GeneratedArgs, *this);
}
More information about the llvm-branch-commits
mailing list