Skip to content

[SM6.10] Add Matrix class methods and other LinAlg API to linalg.h#8299

Merged
hekota merged 10 commits intomicrosoft:mainfrom
hekota:linalg-matrix-header
Mar 31, 2026
Merged

[SM6.10] Add Matrix class methods and other LinAlg API to linalg.h#8299
hekota merged 10 commits intomicrosoft:mainfrom
hekota:linalg-matrix-header

Conversation

@hekota
Copy link
Copy Markdown
Member

@hekota hekota commented Mar 26, 2026

Implements Matrix class methods and other API functions related to linear algebra as specified in LinAlg spec here.

The header disables warning when an argument is marked as groupshared which is reported when the language version is other than 202x. In order to do that the warning needed to be moved to a separate diagnostic group. Also fixes couple of places in SemaHLSL that did not account for the linalg matrix tyle.

Related to #7839

Implements Matrix class methods and other API function related to linear algebra as specified in LinAlg spec (here)[https://github.com/microsoft/hlsl-specs/blob/main/proposals/0035-linalg-matrix.md#appendix-1-hlsl-header].

The header disables warning for a groupshared argument when the language version is other than 202x.

Related to microsoft#7839
@hekota hekota force-pushed the linalg-matrix-header branch from af16148 to c63a4b5 Compare March 26, 2026 23:17
…less input type interpretation is provided as a separate argument or in InterpretedVector.
@hekota hekota marked this pull request as ready for review March 27, 2026 19:11
@hekota hekota requested review from V-FEXrt and llvm-beanz March 27, 2026 19:11
@hekota hekota linked an issue Mar 30, 2026 that may be closed by this pull request
Copy link
Copy Markdown
Collaborator

@llvm-beanz llvm-beanz left a comment

Choose a reason for hiding this comment

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

This all looks sane to me. I have a few comments, but nothing that needs to block this PR.


template <typename T> struct enable_if<true, T> {
using type = T;
};
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

At some point we'll probably want to pull the traits and enable_if out into a separate header so that it can be reused without causing redeclaration issues. For now this is probably fine because I don't think we've really figured out the right way to handle implicit include paths.

Comment thread tools/clang/lib/Headers/hlsl/dx/linalg.h
Comment thread tools/clang/lib/Headers/hlsl/dx/linalg.h Outdated
Copy link
Copy Markdown
Contributor

@tex3d tex3d left a comment

Choose a reason for hiding this comment

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

I think this looks reasonable, though I haven't thoroughly verified that the SFINAE code encodes all the required rules.

It looks like there's a missing use of Transpose in the Cast operation though.

Comment thread tools/clang/lib/Headers/hlsl/dx/linalg.h Outdated
Comment thread tools/clang/test/CodeGenDXIL/hlsl/linalg/api/matrix-class.hlsl Outdated
Load(groupshared T Arr[Size], uint StartIdx, uint Stride,
MatrixLayoutEnum Layout) {
Matrix Result;
__builtin_LinAlg_MatrixLoadFromMemory(Result.__handle, Arr, StartIdx,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

From the spec:

For the Load operations on groupshared arrays, the Stride argument is the count of elements in the groupshared array.

Should Stride be removed here and replace with Size?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

There might be multiple matrices stored in one array, so Size of the array might not match the Stride. The StartIdx allows loading of different matrices from different places in the array.

Copy link
Copy Markdown
Contributor

@tex3d tex3d Mar 30, 2026

Choose a reason for hiding this comment

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

I don’t think it should be replaced with Size, but perhaps it should be clearer what Stride we’re specifying. This isn’t the matrix stride, it’s the row/col stride (whichever is major), right?

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 31, 2026

✅ With the latest revision this PR passed the C/C++ code formatter.

@hekota hekota enabled auto-merge (squash) March 31, 2026 20:55
Copy link
Copy Markdown
Contributor

@tex3d tex3d left a comment

Choose a reason for hiding this comment

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

I found that inconsistent naming for typename ...Ty vs. ComponentEnum ...Ty made this harder to read and review.

Matrix uses ComponentEnum MatrixDT, which helps, but DT isn't a convention used elsewhere, nor does it match the naming ComponentType which has a long history in HLSL and would better match the ComponentEnum naming.

Could we standardize on naming things with CT when referring to a ComponentType specified by the ComponentEnum and Ty when referring to an actual native type?

To be clear: for unblocking things, we can likely fix the issues I pointed out in a follow-up PR.

vector<OutputElTy, M> Result;
__builtin_LinAlg_MatrixVectorMultiply(Result, MatrixA.__handle, Vec,
__builtin_LinAlg_MatrixVectorMultiply(Result, MatrixA.__handle,
hlsl::is_signed<OutputElTy>::value, Vec,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I believe you need to construct the inputInterp input from the InputElTy , rather than passing in the MatrixDT.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think for non-interpreted vectors the assumption is that the interpretation type matches the native scalar type. @llvm-beanz - can you confirm?

MatrixDT, Bias, MatrixDT);
__builtin_LinAlg_MatrixVectorMultiplyAdd(Result, MatrixA.__handle,
hlsl::is_signed<OutputElTy>::value,
Vec, MatrixDT, Bias, MatrixDT);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I believe you need to construct the inputInterp and biasInterp inputs from the InputElTy and BiasElTy, rather than passing in the MatrixDT.

Result, MatrixA.__handle, InterpVec.Data, InterpVec.Interpretation, Bias,
MatrixDT);
Result, MatrixA.__handle, hlsl::is_signed<OutputElTy>::value,
InterpVec.Data, InterpVec.Interpretation, Bias, MatrixDT);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I believe you need to construct the biasInterp input from the BiasElTy, rather than passing in the MatrixDT.

MatrixDT, BiasVec, BiasElTy);
__builtin_LinAlg_MatrixVectorMultiplyAdd(Result, MatrixA.__handle,
hlsl::is_signed<OutputElTy>::value,
Vec, MatrixDT, BiasVec, BiasElTy);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I believe you need to construct the inputInterp input from the InputElTy , rather than passing in the MatrixDT.

@hekota
Copy link
Copy Markdown
Member Author

hekota commented Mar 31, 2026

Could we standardize on naming things with CT when referring to a ComponentType specified by the ComponentEnum and Ty when referring to an actual native type?

I've filed a follow-up issue: #8318

@hekota hekota merged commit 834978b into microsoft:main Mar 31, 2026
12 checks passed
@github-project-automation github-project-automation Bot moved this from New to Done in HLSL Roadmap Mar 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Finalize Linalg HLSL Headers

4 participants