Skip to content
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

dxcopt: Support full container and restore extra data to module #4845

Merged
merged 30 commits into from
Dec 13, 2022
Merged
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
298aca7
dxcopt: Support full container and restore subobjects/root sig to module
tex3d Dec 3, 2022
9956bac
Move more code into the try block.
tex3d Dec 3, 2022
dea301d
Beginning outline of tests
tex3d Dec 3, 2022
dd5b6bd
Better return code when container doesn't contain DXIL
tex3d Dec 3, 2022
f91f943
Assemble to container, add a test
jeffnn Dec 3, 2022
5f72f78
More test
jeffnn Dec 3, 2022
7c13794
Fix ubuntu build?
jeffnn Dec 6, 2022
b91c86f
Assemble to container outside optimizer
jeffnn Dec 7, 2022
b4504f5
Copy PSV0 ViewID data back to DxilModule serialized data
tex3d Dec 7, 2022
99abb03
Restore resource names from shader reflection part (STAT)
tex3d Dec 8, 2022
ff66248
Restore Resource HLSL types and type annotations
tex3d Dec 8, 2022
ecc4330
Some error checking when copying resource info
tex3d Dec 8, 2022
2a4e96f
Remove existing type annotation if present, to force replacement
tex3d Dec 8, 2022
e2adc94
Start of view id tests
jeffnn Dec 8, 2022
5886db8
Merge branch 'dxcopt-from-container' of github.com:tex3d/DirectXShade…
jeffnn Dec 8, 2022
97beb9c
Test by comparing before and after
jeffnn Dec 8, 2022
92c66af
View instancing tests
jeffnn Dec 9, 2022
8c22fb4
GS test
jeffnn Dec 9, 2022
f1900dd
Move function restoring resource reflection to DxilModule
tex3d Dec 9, 2022
e3c7d67
Check target GlobalSymbol() before trying to set the name
tex3d Dec 9, 2022
4ca6522
Resource copy tests
jeffnn Dec 9, 2022
c31ab03
Merge branch 'dxcopt-from-container' of github.com:tex3d/DirectXShade…
jeffnn Dec 9, 2022
70ac5fd
Not should be not-not, initialize from the right data
jeffnn Dec 9, 2022
d07d3a2
ARE_EQUAL_STR
jeffnn Dec 9, 2022
a3733c9
Test fixes
jeffnn Dec 9, 2022
89ddbb7
test fixes
jeffnn Dec 9, 2022
e823397
Undo mistaken checkin
jeffnn Dec 9, 2022
190f138
Tests for i/o dependency map
jeffnn Dec 10, 2022
0182587
Test all tables
jeffnn Dec 10, 2022
1cfd5e7
NC: Update comment for what's restored from STAT
tex3d Dec 13, 2022
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
88 changes: 69 additions & 19 deletions lib/HLSL/DxcOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "llvm/Analysis/DxilValueCache.h"
#include "dxc/DXIL/DxilUtil.h"
#include "dxc/Support/dxcapi.impl.h"
#include "dxc/DxilContainer/DxilRuntimeReflection.h"

#include "llvm/Pass.h"
#include "llvm/PassInfo.h"
Expand Down Expand Up @@ -249,28 +250,77 @@ HRESULT STDMETHODCALLTYPE DxcOptimizer::RunOptimizer(
const char * pBlobContent = reinterpret_cast<const char *>(pBlob->GetBufferPointer());
unsigned blobSize = pBlob->GetBufferSize();
const DxilProgramHeader *pProgramHeader =
reinterpret_cast<const DxilProgramHeader *>(pBlobContent);
if (IsValidDxilProgramHeader(pProgramHeader, blobSize)) {
std::string DiagStr;
GetDxilProgramBitcode(pProgramHeader, &pBlobContent, &blobSize);
M = hlsl::dxilutil::LoadModuleFromBitcode(
llvm::StringRef(pBlobContent, blobSize), Context, DiagStr);
}
else {
StringRef bufStrRef(pBlobContent, blobSize);
memBuf = MemoryBuffer::getMemBufferCopy(bufStrRef);
M = parseIR(memBuf->getMemBufferRef(), Err, Context);
}
reinterpret_cast<const DxilProgramHeader *>(pBlobContent);
const DxilContainerHeader *pContainerHeader = IsDxilContainerLike(pBlobContent, blobSize);
bool bIsFullContainer = IsValidDxilContainer(pContainerHeader, blobSize);

if (M == nullptr) {
return DXC_E_IR_VERIFICATION_FAILED;
}
try {
tex3d marked this conversation as resolved.
Show resolved Hide resolved

if (bIsFullContainer) {
// Prefer debug module, if present.
pProgramHeader = GetDxilProgramHeader(pContainerHeader, DFCC_ShaderDebugInfoDXIL);
if (!pProgramHeader)
pProgramHeader = GetDxilProgramHeader(pContainerHeader, DFCC_DXIL);
}

legacy::PassManager ModulePasses;
legacy::FunctionPassManager FunctionPasses(M.get());
legacy::PassManagerBase *pPassManager = &ModulePasses;
if (IsValidDxilProgramHeader(pProgramHeader, blobSize)) {
std::string DiagStr;
GetDxilProgramBitcode(pProgramHeader, &pBlobContent, &blobSize);
M = hlsl::dxilutil::LoadModuleFromBitcode(
llvm::StringRef(pBlobContent, blobSize), Context, DiagStr);
} else if (!bIsFullContainer) {
StringRef bufStrRef(pBlobContent, blobSize);
memBuf = MemoryBuffer::getMemBufferCopy(bufStrRef);
M = parseIR(memBuf->getMemBufferRef(), Err, Context);
} else {
return E_INVALIDARG;
}

if (M == nullptr) {
return DXC_E_IR_VERIFICATION_FAILED;
}

if (bIsFullContainer) {
// Restore extra data from certain parts back into the module so that data isn't lost.
// Note: Only GetOrCreateDxilModule if one of these is present.
// - Subobjects from RDAT
// - RootSignature from RTS0
// - ViewID and I/O dependency data from PSV0

// RDAT
if (const DxilPartHeader *pPartHeader =
GetDxilPartByType(pContainerHeader, DFCC_RuntimeData)) {
DxilModule &DM = M->GetOrCreateDxilModule();
RDAT::DxilRuntimeData rdat(GetDxilPartData(pPartHeader), pPartHeader->PartSize);
auto table = rdat.GetSubobjectTable();
if (table && table.Count() > 0) {
DM.ResetSubobjects(new DxilSubobjects());
if (!LoadSubobjectsFromRDAT(*DM.GetSubobjects(), rdat)) {
return DXC_E_CONTAINER_INVALID;
}
}
}

// RST0
if (const DxilPartHeader *pPartHeader =
GetDxilPartByType(pContainerHeader, DFCC_RootSignature)) {
DxilModule &DM = M->GetOrCreateDxilModule();
const uint8_t* pPartData = (const uint8_t*)GetDxilPartData(pPartHeader);
DM.ResetSerializedRootSignature(
std::vector<uint8_t>(pPartData, pPartData + pPartHeader->PartSize));
}

// PSV0 - TBD
//if (const DxilPartHeader *pPartHeader = GetDxilPartByType(
tex3d marked this conversation as resolved.
Show resolved Hide resolved
// pContainerHeader, DFCC_PipelineStateValidation)) {
// DxilModule &DM = M->GetOrCreateDxilModule();
//}
}

legacy::PassManager ModulePasses;
legacy::FunctionPassManager FunctionPasses(M.get());
legacy::PassManagerBase *pPassManager = &ModulePasses;

try {
CComPtr<AbstractMemoryStream> pOutputStream;
CComPtr<IDxcBlob> pOutputBlob;

Expand Down