Skip to content

Support SV_DispatchGrid semantic in a nested record#6931

Merged
damyanp merged 6 commits intomicrosoft:mainfrom
tcorringham:issue-6928
May 14, 2025
Merged

Support SV_DispatchGrid semantic in a nested record#6931
damyanp merged 6 commits intomicrosoft:mainfrom
tcorringham:issue-6928

Conversation

@tcorringham
Copy link
Copy Markdown
Collaborator

The SV_DispatchGrid DXIL metadata for a node input record was not generated in cases where:

  • the field with the SV_DispatchGrid semantic was in a nested record
  • the field with the SV_DispatchGrid semantic was in a record field
  • the field with the SV_DispatchGrid semantic was inherited from a base record
  • in any combinations of the above

Added FindDispatchGridSemantic() to be used by the AddHLSLNodeRecordTypeInfo() function, and added a test case.

Fixes #6928

@tcorringham tcorringham self-assigned this Sep 24, 2024
@tcorringham tcorringham requested a review from a team as a code owner September 24, 2024 15:31
@damyanp damyanp assigned tex3d and unassigned tcorringham Sep 26, 2024
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.

We should also make sure we have error detection in Sema for the case of multiple fields in a single or nested structure definition having SV_DispatchGrid applied. Since the CodeGen can only handle a single declaration, we should error if a structure has more than one annotated field.

Comment thread tools/clang/lib/CodeGen/CGHLSLMS.cpp Outdated
// Collect any non-virtual bases.
SmallVector<const CXXRecordDecl *, 4> Bases;
for (const CXXBaseSpecifier &Base : RD->bases()) {
if (!Base.isVirtual() && !Base.getType()->isDependentType())
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.

I assume it isn't actually possible in HLSL to have virtual base classes right? The language certainly shouldn't allow them, so I think this check is probably unnecessary.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

No, virtual base classes aren't possible - but the dependent type is, so we do need that check here. This condition just follows the style and layout of similar checks elsewhere - the isVirtual() check could safely be removed.

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.

Yea, I think we should remove the isVirtual check since that will always be false and I can't imagine any world where we'd be adding virtual inheritance to HLSL...

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done.

@tcorringham
Copy link
Copy Markdown
Collaborator Author

The presence of more than one SV_DispatchGrid field is already detected in Sema, and covers the cases of base classes and nested records.

[NumThreads(32,1,1)]
void node5(DispatchNodeInputRecord<Record5> input) {}
// CHECK: , i32 1, ![[SVDG_5:[0-9]+]]
// CHECK: [[SVDG_5]] = !{i32 20, i32 5, i32 2}
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.

What about some test cases with templates?

Something like:

template <typename T>
struct Base {
  T DG : SV_DispatchGrid;
};

struct Derived1 : Base<uint3> {
  int4 x;
};

[Shader("node")]
[NodeLaunch("broadcasting")]
[NodeMaxDispatchGrid(32,16,1)]
[NumThreads(32,1,1)]
void node6(DispatchNodeInputRecord<Derived1 > input) {}

template <typename T>
struct Derived2 : Base<T> {
  T Y;
};

[Shader("node")]
[NodeLaunch("broadcasting")]
[NodeMaxDispatchGrid(32,16,1)]
[NumThreads(32,1,1)]
void node7(DispatchNodeInputRecord<Derived2<uint2> > input) {}


template <typename T>
struct Derived3 {
  Derived2<T> V;
};

[Shader("node")]
[NodeLaunch("broadcasting")]
[NodeMaxDispatchGrid(32,16,1)]
[NumThreads(32,1,1)]
void node8(DispatchNodeInputRecord< Derived3 <uint3> > input) {}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Great idea! I've updated the test to include these cases.

@damyanp damyanp requested a review from llvm-beanz January 8, 2025 18:43
@damyanp damyanp requested review from bob80905 and pow2clk January 30, 2025 19:29
@damyanp
Copy link
Copy Markdown
Member

damyanp commented Jan 30, 2025

@llvm-beanz - could you have a look at this with some urgency please (we want it in the upcoming release) when you're back?

Copy link
Copy Markdown
Collaborator

@pow2clk pow2clk left a comment

Choose a reason for hiding this comment

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

Looks good! I think the parts I'm most concerned about are the repeated checks of parent classes and that I might not understand the motive for sorting base offsets. The other stuff is minor.

Comment thread tools/clang/lib/CodeGen/CGHLSLMS.cpp Outdated
Comment thread tools/clang/lib/CodeGen/CGHLSLMS.cpp
Comment thread tools/clang/lib/CodeGen/CGHLSLMS.cpp
Comment thread tools/clang/lib/CodeGen/CGHLSLMS.cpp Outdated
Comment thread tools/clang/lib/CodeGen/CGHLSLMS.cpp
@damyanp damyanp added this to the Release 1.8.2502 milestone Jan 31, 2025
Comment thread tools/clang/lib/CodeGen/CGHLSLMS.cpp Outdated
Comment thread tools/clang/lib/CodeGen/CGHLSLMS.cpp Outdated
@damyanp damyanp moved this from Needs Review to Active in HLSL Support Apr 25, 2025
Copy link
Copy Markdown
Collaborator

@pow2clk pow2clk left a comment

Choose a reason for hiding this comment

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

I don't mind the unneeded virtual check, but perhaps @llvm-beanz feels differently.

Comment thread tools/clang/lib/CodeGen/CGHLSLMS.cpp Outdated
if (FindDispatchGridSemantic(Base, SDGRec, BaseOffset))
DXASSERT(!Base.getType()->isDependentType(),
"Node Record with dependent base class not caught by Sema");
if (Base.isVirtual() || Base.getType()->isDependentType())
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.

It's minor since it should never fire, but I think the agreement per #6931 (comment) was to remove the virtual check.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I had removed this unnecessary check, but it looks as if it was accidentally reinstated with a subsequent commit. I have removed it again.

Tim Corringham and others added 5 commits May 2, 2025 10:05
The SV_DispatchGrid DXIL metadata for a node input record was not generated
in cases where:
- the field with the SV_DispatchGrid semantic was in a nested record
- the field with the SV_DispatchGrid semantic was in a record field
- the field with the SV_DispatchGrid semantic was inherited from a base record
- in any combinations of the above

Added FindDispatchGridSemantic() to be used by the AddHLSLNodeRecordTypeInfo()
function, and added a test case.

Fixes microsoft#6928
Remove the check for a virtual base class from the code in
FindDispatchGridSemantic() as virtual classes can't appear
in HLSL code.
Added test cases to cover nested SV_DispatchGrid used in
records using templates.
Adopting code suggested in review - there can only be one concrete base so the sort step isn't required.

Co-authored-by: Tex Riddell <[email protected]>
The unnecessary check for a virtual base class had previously been
removed but was apparently accidentally reintroduced by a subsequent
commit.
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.

Looks good. Only one minor nit on new variable names not following coding standard.

Comment thread tools/clang/lib/CodeGen/CGHLSLMS.cpp Outdated
Trivial change to amend local variable names to follow the coding
standard.
Copy link
Copy Markdown
Member

@damyanp damyanp left a comment

Choose a reason for hiding this comment

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

I see two approvals that have been made stale by a change to some variable names. So I'm going to reapprove and merge this.

@damyanp damyanp merged commit f5214f1 into microsoft:main May 14, 2025
12 checks passed
@github-project-automation github-project-automation Bot moved this from Active to Closed in HLSL Support May 14, 2025
@github-project-automation github-project-automation Bot moved this from New to Done in HLSL Roadmap May 14, 2025
@damyanp damyanp removed this from HLSL Support Jun 25, 2025
@tcorringham tcorringham deleted the issue-6928 branch January 26, 2026 15:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project
Status: Triaged

Development

Successfully merging this pull request may close these issues.

SV_DispatchGrid semantic in a nested record

5 participants