Skip to content

Commit 7062cd6

Browse files
farzonlfhahn
andauthored
[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@fhahn.com>
1 parent 2765113 commit 7062cd6

File tree

14 files changed

+190
-0
lines changed

14 files changed

+190
-0
lines changed

clang/docs/LanguageExtensions.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,12 @@ The matrix type extension supports explicit casts. Implicit type conversion betw
10731073
i = static_cast<matrix_5_5<int>>(d);
10741074
}
10751075

1076+
The matrix type extension supports column and row major memory layouts, but not
1077+
all builtins are supported with row-major layout. The layout defaults to column
1078+
major and can be specified using `-fmatrix-memory-layout`. To enable column
1079+
major layout, use `-fmatrix-memory-layout=column-major`, and for row major
1080+
layout use `-fmatrix-memory-layout=row-major`
1081+
10761082
Half-Precision Floating Point
10771083
=============================
10781084

clang/docs/MatrixTypes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ part of the draft specification.
287287
The elements of a value of a matrix type are laid out in column-major order
288288
without padding.
289289

290+
To change memory layout to row major use the `-fmatrix-memory-layout` flag.
291+
This flag supports two flag argument values either `column-major` or
292+
`row-major` used like so `-fmatrix-memory-layout=column-major`.`
293+
290294
We propose to provide a Clang option to override this behavior and allow
291295
contraction of those operations (e.g. *-ffp-contract=matrix*).
292296

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ New Compiler Flags
331331
- 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``).
332332
- New options for enabling allocation token instrumentation: ``-fsanitize=alloc-token``, ``-falloc-token-max=``, ``-fsanitize-alloc-token-fast-abi``, ``-fsanitize-alloc-token-extended``.
333333
- The ``-resource-dir`` option is now displayed in the list of options shown by ``--help``.
334+
- 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``).
334335

335336
Lanai Support
336337
^^^^^^^^^^^^^^

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,9 @@ def err_cc1_round_trip_mismatch : Error<
792792
def err_cc1_unbounded_vscale_min : Error<
793793
"minimum vscale must be an unsigned integer greater than 0">;
794794

795+
def err_conflicting_matrix_layout_flags: Error<
796+
"-fmatrix-memory-layout=%0 conflicts with -mllvm -matrix-default-layout=%1">;
797+
795798
def err_drv_using_omit_rtti_component_without_no_rtti : Error<
796799
"-fexperimental-omit-vtable-rtti call only be used with -fno-rtti">;
797800

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13041,6 +13041,9 @@ def err_builtin_trivially_relocate_invalid_arg_type: Error <
1304113041

1304213042
def err_builtin_matrix_disabled: Error<
1304313043
"matrix types extension is disabled. Pass -fenable-matrix to enable it">;
13044+
def err_builtin_matrix_major_order_disabled: Error<
13045+
"matrix %select{row|column}0 major %select{load|store}1 is not supported with "
13046+
"-fmatrix-memory-layout=%select{column|row}0-major. Pass -fmatrix-memory-layout=%select{row|column}0-major to enable it">;
1304413047
def err_matrix_index_not_integer: Error<
1304513048
"matrix %select{row|column}0 index is not an integer">;
1304613049
def err_matrix_index_outside_range: Error<

clang/include/clang/Basic/LangOptions.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ ENUM_LANGOPT(RegisterStaticDestructors, RegisterStaticDestructorsKind, 2,
434434
LANGOPT(RegCall4, 1, 0, NotCompatible, "Set __regcall4 as a default calling convention to respect __regcall ABI v.4")
435435

436436
LANGOPT(MatrixTypes, 1, 0, NotCompatible, "Enable or disable the builtin matrix type")
437+
ENUM_LANGOPT(DefaultMatrixMemoryLayout, MatrixMemoryLayout, 1, MatrixMemoryLayout::MatrixColMajor, NotCompatible, "Defines the default memory Layout for matrices")
437438
VALUE_LANGOPT(MaxMatrixDimension, 32, (1 << 20) - 1, NotCompatible, "maximum allowed matrix dimension")
438439

439440
LANGOPT(CXXAssumptions, 1, 1, NotCompatible, "Enable or disable codegen and compile-time checks for C++23's [[assume]] attribute")

clang/include/clang/Basic/LangOptions.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,13 @@ class LangOptionsBase {
251251
FEM_UnsetOnCommandLine = 3
252252
};
253253

254+
enum class MatrixMemoryLayout : unsigned {
255+
// Use column-major layout for matrices
256+
MatrixColMajor = 0,
257+
// Use row-major layout for matrices
258+
MatrixRowMajor = 1,
259+
};
260+
254261
enum ExcessPrecisionKind { FPP_Standard, FPP_Fast, FPP_None };
255262

256263
enum class LaxVectorConversionKind {

clang/include/clang/Options/Options.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4692,6 +4692,12 @@ def fenable_matrix : Flag<["-"], "fenable-matrix">, Group<f_Group>,
46924692
HelpText<"Enable matrix data type and related builtin functions">,
46934693
MarshallingInfoFlag<LangOpts<"MatrixTypes">, hlsl.KeyPath>;
46944694

4695+
def fmatrix_memory_layout_EQ : Joined<["-"], "fmatrix-memory-layout=">,
4696+
Visibility<[ClangOption, CC1Option]>,
4697+
HelpText<"Sets the matrix memory layout (row-major or column-major)">,
4698+
Values<"row-major,column-major">,
4699+
Group<f_Group>;
4700+
46954701
defm raw_string_literals : BoolFOption<"raw-string-literals",
46964702
LangOpts<"RawStringLiterals">, Default<std#".hasRawStringLiterals()">,
46974703
PosFlag<SetTrue, [], [], "Enable">,

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5703,6 +5703,18 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
57035703
CmdArgs.push_back("-fenable-matrix");
57045704
CmdArgs.push_back("-mllvm");
57055705
CmdArgs.push_back("-enable-matrix");
5706+
// Only handle default layout if matrix is enabled
5707+
if (const Arg *A = Args.getLastArg(options::OPT_fmatrix_memory_layout_EQ)) {
5708+
StringRef Val = A->getValue();
5709+
if (Val == "row-major" || Val == "column-major") {
5710+
CmdArgs.push_back(Args.MakeArgString("-fmatrix-memory-layout=" + Val));
5711+
CmdArgs.push_back("-mllvm");
5712+
CmdArgs.push_back(Args.MakeArgString("-matrix-default-layout=" + Val));
5713+
5714+
} else {
5715+
D.Diag(diag::err_drv_invalid_value) << A->getAsString(Args) << Val;
5716+
}
5717+
}
57065718
}
57075719

57085720
CodeGenOptions::FramePointerKind FPKeepKind =

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3951,6 +3951,15 @@ void CompilerInvocationBase::GenerateLangArgs(const LangOptions &Opts,
39513951
StringRef S = llvm::getAllocTokenModeAsString(*Opts.AllocTokenMode);
39523952
GenerateArg(Consumer, OPT_falloc_token_mode_EQ, S);
39533953
}
3954+
// Generate args for matrix types.
3955+
if (Opts.MatrixTypes) {
3956+
if (Opts.getDefaultMatrixMemoryLayout() ==
3957+
LangOptions::MatrixMemoryLayout::MatrixColMajor)
3958+
GenerateArg(Consumer, OPT_fmatrix_memory_layout_EQ, "column-major");
3959+
if (Opts.getDefaultMatrixMemoryLayout() ==
3960+
LangOptions::MatrixMemoryLayout::MatrixRowMajor)
3961+
GenerateArg(Consumer, OPT_fmatrix_memory_layout_EQ, "row-major");
3962+
}
39543963
}
39553964

39563965
bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
@@ -4545,6 +4554,27 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
45454554
Diags.Report(diag::err_drv_invalid_value) << Arg->getAsString(Args) << S;
45464555
}
45474556

4557+
// Enable options for matrix types.
4558+
if (Opts.MatrixTypes) {
4559+
if (const Arg *A = Args.getLastArg(OPT_fmatrix_memory_layout_EQ)) {
4560+
StringRef ClangValue = A->getValue();
4561+
if (ClangValue == "row-major")
4562+
Opts.setDefaultMatrixMemoryLayout(
4563+
LangOptions::MatrixMemoryLayout::MatrixRowMajor);
4564+
else
4565+
Opts.setDefaultMatrixMemoryLayout(
4566+
LangOptions::MatrixMemoryLayout::MatrixColMajor);
4567+
4568+
for (Arg *A : Args.filtered(options::OPT_mllvm)) {
4569+
StringRef OptValue = A->getValue();
4570+
if (OptValue.consume_front("-matrix-default-layout=") &&
4571+
ClangValue != OptValue)
4572+
Diags.Report(diag::err_conflicting_matrix_layout_flags)
4573+
<< ClangValue << OptValue;
4574+
}
4575+
}
4576+
}
4577+
45484578
// Validate options for HLSL
45494579
if (Opts.HLSL) {
45504580
// TODO: Revisit restricting SPIR-V to logical once we've figured out how to

0 commit comments

Comments
 (0)