Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions clang/docs/LanguageExtensions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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-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
=============================

Expand Down
4 changes: 4 additions & 0 deletions clang/docs/MatrixTypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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-default-layout` flag.
This flag supports two flag argument values either `column-major` or `row-major`
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*).

Expand Down
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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-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
^^^^^^^^^^^^^^
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/Basic/LangOptions.def
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
7 changes: 7 additions & 0 deletions clang/include/clang/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions clang/include/clang/Options/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -4642,6 +4642,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">,
Expand Down
9 changes: 9 additions & 0 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5692,6 +5692,15 @@ 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));
} else {
D.Diag(diag::err_drv_invalid_value) << A->getAsString(Args) << Val;
}
}
}

CodeGenOptions::FramePointerKind FPKeepKind =
Expand Down
19 changes: 19 additions & 0 deletions clang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3976,6 +3976,13 @@ void CompilerInvocationBase::GenerateLangArgs(const LangOptions &Opts,
}
GenerateArg(Consumer, OPT_falloc_token_mode_EQ, S);
}
// Enable options for Matrices
Copy link
Member Author

@farzonl farzonl Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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");
if (Opts.DefaultMatrixMemoryLayout == LangOptions::MatrixRowMajor)
GenerateArg(Consumer, OPT_fmatrix_memory_layout_EQ, "row-major");
}
}

bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
Expand Down Expand Up @@ -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) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (Opts.MatrixTypes) {
// Enable options for Matrices
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
Expand Down
35 changes: 35 additions & 0 deletions clang/test/Driver/fmatrix-memory-layout.c
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions clang/unittests/Frontend/CompilerInvocationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
Loading