Skip to content

PIX:Descend Type Hierarchy for Alloca'd structs#5028

Merged
jeffnn merged 4 commits intomicrosoft:mainfrom
jeffnn:PIX_TypedefDescent
Feb 16, 2023
Merged

PIX:Descend Type Hierarchy for Alloca'd structs#5028
jeffnn merged 4 commits intomicrosoft:mainfrom
jeffnn:PIX_TypedefDescent

Conversation

@jeffnn
Copy link
Copy Markdown
Collaborator

@jeffnn jeffnn commented Feb 15, 2023

In some cases structs like the RayDesc built-in type (one of the arguments to TraceRays) will be placed in an alloca.
The PIX annotation code wasn't properly counting all the leaf-node values of aggregates within such structs when determining offsets for later members.
The result of this was erroneous attribution of writes to those members during PIX shader debugging.

@jeffnn jeffnn self-assigned this Feb 15, 2023
Comment thread lib/DxilPIXPasses/DxilAnnotateWithVirtualRegister.cpp Outdated
@AppVeyorBot
Copy link
Copy Markdown

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.

Please include a test case.

@jeffnn
Copy link
Copy Markdown
Collaborator Author

jeffnn commented Feb 15, 2023

Please include a test case.

Fair enough! This would be well-tested on the PIX side, but that's no reason not to have something here.

@jeffnn jeffnn requested a review from llvm-beanz February 15, 2023 18:27
@AppVeyorBot
Copy link
Copy Markdown

@llvm-beanz
Copy link
Copy Markdown
Collaborator

Fair enough! This would be well-tested on the PIX side, but that's no reason not to have something here.

I realize we haven't historically enforced this requirement as rigorously as we should, but our contribution guidelines do require that every change has a test case.

I am looking into adding automatic verification of that in our PR testing as well.

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.

I don't see anything obvious about this change that is problematic from the LLVM perspective. Whether or not it is correct for PIX I assume you're testing separately. It would be nice to have a simple unit test for this code that verifies the IR metadata generation. We have very few tests that verify the IR results of the PIX passes.

Adding a test file under tools/clang/test/HLSLFileCheck/pix that contained something like this would be useful to verify the correct metadata generation in the IR:

// RUN: %dxc -Od -T lib_6_6 %s | %opt -S -dxil-annotate-with-virtual-regs | FileCheck %s


/* To run locally run:
%dxc -Od -T lib_6_6 %s -Fc %t.ll
%opt %t.ll -S -dxil-annotate-with-virtual-regs | FileCheck %s
*/

RaytracingAccelerationStructure scene : register(t0);

struct RayPayload
{
    int3 color;
};

[shader("raygeneration")]
void ENTRY()
{
    RayDesc ray = {{0,0,0}, {0,0,1}, 0.05, 1000.0};
    RayPayload pld;
    TraceRay(scene, 0 /*rayFlags*/, 0xFF /*rayMask*/, 0 /*sbtRecordOffset*/, 1 /*sbtRecordStride*/, 0 /*missIndex*/, ray, pld);
}

// CHECK: {{.*}} = alloca %struct.RayDesc, align 4, !pix-dxil-inst-num {{.*}}, !pix-alloca-reg [[RDAlloca:![0-9]+]]
// CHECK: {{.*}} = alloca %struct.RayPayload, align 4, !pix-dxil-inst-num {{.*}}, !pix-alloca-reg [[RPAlloca:![0-9]+]]
// CHECK: {{.*}} = getelementptr inbounds %struct.RayDesc, %struct.RayDesc* {{.*}}, i32 0, i32 0, !pix-dxil-inst-num {{.*}}, !pix-dxil-reg [[RDGEP:![0-9]+]]
// CHECK: {{.*}} = load i32, i32* getelementptr inbounds ([1 x i32], [1 x i32]* @dx.nothing.a, i32 0, i32 0), !pix-dxil-inst-num {{.*}}, !pix-dxil-reg [[NothGEP:![0-9]+]]
// CHECK: {{.*}} = getelementptr inbounds %struct.RayDesc, %struct.RayDesc* {{.*}}, i32 0, i32 1, !pix-dxil-inst-num {{.*}}, !pix-dxil-reg [[RDGEP2:![0-9]+]]
// CHECK: {{.*}} = load i32, i32* getelementptr inbounds ([1 x i32], [1 x i32]* @dx.nothing.a, i32 0, i32 0), !pix-dxil-inst-num {{.*}}, !pix-dxil-reg [[NothGEP2:![0-9]+]]
// CHECK: {{.*}} = getelementptr inbounds %struct.RayDesc, %struct.RayDesc* {{.*}}, i32 0, i32 2, !pix-dxil-inst-num {{.*}}, !pix-dxil-reg [[RDGEP3:![0-9]+]]
// CHECK: {{.*}} = load i32, i32* getelementptr inbounds ([1 x i32], [1 x i32]* @dx.nothing.a, i32 0, i32 0), !pix-dxil-inst-num {{.*}}, !pix-dxil-reg [[NothGEP3:![0-9]+]]

// CHECK-DAG: [[RDAlloca]] = !{i32 1, i32 0, i32 8}
// CHECK-DAG: [[RPAlloca]] = !{i32 1, i32 8, i32 3}
// CHECK-DAG: [[RDGEP]] = !{i32 0, i32 0}
// CHECK-DAG: [[NothGEP]] = !{i32 0, i32 11}
// CHECK-DAG: [[RDGEP2]] = !{i32 0, i32 3}
// CHECK-DAG: [[NothGEP2]] = !{i32 0, i32 12}
// CHECK-DAG: [[RDGEP3]] = !{i32 0, i32 4}
// CHECK-DAG: [[NothGEP3]] = !{i32 0, i32 13}

An alternate testing strategy we could consider in the future would be building a pretty-printer that translated the IR metadata into a more human print format, but in the absence of that testing the raw IR is better than nothing.

@jeffnn
Copy link
Copy Markdown
Collaborator Author

jeffnn commented Feb 15, 2023

Hey thanks for the assist. I didn't know about that syntax for testing metadata nodes. Cool!

@AppVeyorBot
Copy link
Copy Markdown

@jeffnn jeffnn merged commit f98f8d6 into microsoft:main Feb 16, 2023
@jeffnn jeffnn deleted the PIX_TypedefDescent branch February 16, 2023 00:52
jeffnn added a commit to jeffnn/DirectXShaderCompiler that referenced this pull request Feb 16, 2023
In some cases structs like the RayDesc built-in type (one of the arguments to TraceRays) will be placed in an alloca.
The PIX annotation code wasn't properly counting all the leaf-node values of aggregates within such structs when determining offsets for later members.
The result of this was erroneous attribution of writes to those members during PIX shader debugging.

* PIX:Descend Type Hierarchy for Alloca'd structs

* Add test, vectors are scalar-only

* New file-check for metadata nodes

(cherry picked from commit f98f8d6)
jeffnn added a commit that referenced this pull request Feb 17, 2023
In some cases structs like the RayDesc built-in type (one of the arguments to TraceRays) will be placed in an alloca.
The PIX annotation code wasn't properly counting all the leaf-node values of aggregates within such structs when determining offsets for later members.
The result of this was erroneous attribution of writes to those members during PIX shader debugging.

* PIX:Descend Type Hierarchy for Alloca'd structs

* Add test, vectors are scalar-only

* New file-check for metadata nodes

(cherry picked from commit f98f8d6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants