Skip to content

[DirectX] use DXILMetadataAnalysis to build PSVRuntimeInfo #107101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 5, 2024
Merged
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
29 changes: 22 additions & 7 deletions llvm/lib/Target/DirectX/DXContainerGlobals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Analysis/DXILMetadataAnalysis.h"
#include "llvm/BinaryFormat/DXContainer.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/IR/Constants.h"
Expand Down Expand Up @@ -57,6 +58,7 @@ class DXContainerGlobals : public llvm::ModulePass {
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.setPreservesAll();
AU.addRequired<ShaderFlagsAnalysisWrapper>();
AU.addRequired<DXILMetadataAnalysisWrapperPass>();
}
};

Expand Down Expand Up @@ -143,23 +145,35 @@ void DXContainerGlobals::addPipelineStateValidationInfo(
SmallString<256> Data;
raw_svector_ostream OS(Data);
PSVRuntimeInfo PSV;
Triple TT(M.getTargetTriple());
PSV.BaseData.MinimumWaveLaneCount = 0;
PSV.BaseData.MaximumWaveLaneCount = std::numeric_limits<uint32_t>::max();

dxil::ModuleMetadataInfo &MMI =
getAnalysis<DXILMetadataAnalysisWrapperPass>().getModuleMetadata();
assert(MMI.EntryPropertyVec.size() == 1 ||
MMI.ShaderStage == Triple::Library);
PSV.BaseData.ShaderStage =
static_cast<uint8_t>(TT.getEnvironment() - Triple::Pixel);
static_cast<uint8_t>(MMI.ShaderStage - Triple::Pixel);

// Hardcoded values here to unblock loading the shader into D3D.
//
// TODO: Lots more stuff to do here!
//
// See issue https://github.com/llvm/llvm-project/issues/96674.
PSV.BaseData.NumThreadsX = 1;
PSV.BaseData.NumThreadsY = 1;
PSV.BaseData.NumThreadsZ = 1;
PSV.EntryName = "main";
switch (MMI.ShaderStage) {
case Triple::Compute:
PSV.BaseData.NumThreadsX = MMI.EntryPropertyVec[0].NumThreadsX;
PSV.BaseData.NumThreadsY = MMI.EntryPropertyVec[0].NumThreadsY;
PSV.BaseData.NumThreadsZ = MMI.EntryPropertyVec[0].NumThreadsZ;
break;
default:
break;
}

if (MMI.ShaderStage != Triple::Library)
PSV.EntryName = MMI.EntryPropertyVec[0].Entry->getName();
Comment on lines +163 to +174
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems pretty unfortunate that we need to know which fields are valid in the analysis result based on what stage it is - I see you made a similar comment on the metadata analysis PR, but I didn't notice it at the time. We should probably have a discussion on what the API of the metadata analysis should look like (cc @bharadwajy).


PSV.finalize(TT.getEnvironment());
PSV.finalize(MMI.ShaderStage);
PSV.write(OS);
Constant *Constant =
ConstantDataArray::getString(M.getContext(), Data, /*AddNull*/ false);
Expand All @@ -170,6 +184,7 @@ char DXContainerGlobals::ID = 0;
INITIALIZE_PASS_BEGIN(DXContainerGlobals, "dxil-globals",
"DXContainer Global Emitter", false, true)
INITIALIZE_PASS_DEPENDENCY(ShaderFlagsAnalysisWrapper)
INITIALIZE_PASS_DEPENDENCY(DXILMetadataAnalysisWrapperPass)
INITIALIZE_PASS_END(DXContainerGlobals, "dxil-globals",
"DXContainer Global Emitter", false, true)

Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Target/DirectX/DXILPrepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Analysis/DXILMetadataAnalysis.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/IR/AttributeMask.h"
#include "llvm/IR/IRBuilder.h"
Expand Down Expand Up @@ -247,6 +248,7 @@ class DXILPrepareModule : public ModulePass {
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addPreserved<ShaderFlagsAnalysisWrapper>();
AU.addPreserved<DXILResourceMDWrapper>();
AU.addPreserved<DXILMetadataAnalysisWrapperPass>();
}
static char ID; // Pass identification.
};
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "DXILShaderFlags.h"
#include "DirectX.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Analysis/DXILMetadataAnalysis.h"
#include "llvm/Analysis/DXILResource.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Metadata.h"
Expand Down Expand Up @@ -103,6 +104,7 @@ class DXILTranslateMetadataLegacy : public ModulePass {
AU.addRequired<DXILResourceWrapperPass>();
AU.addRequired<DXILResourceMDWrapper>();
AU.addRequired<ShaderFlagsAnalysisWrapper>();
AU.addRequired<DXILMetadataAnalysisWrapperPass>();
Copy link
Contributor

@bharadwajy bharadwajy Sep 3, 2024

Choose a reason for hiding this comment

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

Does DXILMetadataAnalysisWrapperPass need to be added as required for DXILTranslateMetadata pass, given the changes in this PR do not consume results from DXILMetadataAnalysis pass?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The analysis needs to be run before preparePass which removes the attributes.
And we could consume result for translateMetadata pass as well.

}

bool runOnModule(Module &M) override {
Expand Down
41 changes: 41 additions & 0 deletions llvm/test/CodeGen/DirectX/ContainerData/RuntimeInfoCS.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
; RUN: opt %s -dxil-embed -dxil-globals -S -o - | FileCheck %s
; RUN: llc %s --filetype=obj -o - | obj2yaml | FileCheck %s --check-prefix=DXC
target triple = "dxil-unknown-shadermodel6.0-compute"

; CHECK: @dx.psv0 = private constant [80 x i8] c"{{.*}}", section "PSV0", align 4

define void @cs_main() #0 {
entry:
ret void
}

attributes #0 = { "hlsl.numthreads"="8,8,1" "hlsl.shader"="compute" }

!dx.valver = !{!0}

!0 = !{i32 1, i32 7}

; DXC: - Name: PSV0
; DXC-NEXT: Size: 80
; DXC-NEXT: PSVInfo:
; DXC-NEXT: Version: 3
; DXC-NEXT: ShaderStage: 5
; DXC-NEXT: MinimumWaveLaneCount: 0
; DXC-NEXT: MaximumWaveLaneCount: 4294967295
; DXC-NEXT: UsesViewID: 0
; DXC-NEXT: SigInputVectors: 0
; DXC-NEXT: SigOutputVectors: [ 0, 0, 0, 0 ]
; DXC-NEXT: NumThreadsX: 8
; DXC-NEXT: NumThreadsY: 8
; DXC-NEXT: NumThreadsZ: 1
; DXC-NEXT: EntryName: cs_main
; DXC-NEXT: ResourceStride: 24
; DXC-NEXT: Resources: []
; DXC-NEXT: SigInputElements: []
; DXC-NEXT: SigOutputElements: []
; DXC-NEXT: SigPatchOrPrimElements: []
; DXC-NEXT: InputOutputMap:
; DXC-NEXT: - [ ]
; DXC-NEXT: - [ ]
; DXC-NEXT: - [ ]
; DXC-NEXT: - [ ]
Loading