Skip to content

Commit 6caaa41

Browse files
committed
refactoring & tests
1 parent d0baee2 commit 6caaa41

File tree

6 files changed

+146
-46
lines changed

6 files changed

+146
-46
lines changed

include/dxc/DXIL/DxilModule.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ class DxilModule {
205205
void ResetEntryPropsMap(DxilEntryPropsMap &&PropMap);
206206

207207
bool StripReflection();
208+
bool StripNamesSensitiveToDebug();
208209
void StripDebugRelatedCode();
209210
void RemoveUnusedTypeAnnotations();
210211

lib/DXIL/DxilModule.cpp

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,6 +1795,41 @@ bool DxilModule::StripReflection() {
17951795
}
17961796
}
17971797

1798+
// Resource
1799+
if (!bIsLib) {
1800+
bChanged |= StripResourcesReflection(m_CBuffers);
1801+
bChanged |= StripResourcesReflection(m_UAVs);
1802+
bChanged |= StripResourcesReflection(m_SRVs);
1803+
bChanged |= StripResourcesReflection(m_Samplers);
1804+
}
1805+
1806+
// Unused global.
1807+
SmallVector<GlobalVariable *, 2> UnusedGlobals;
1808+
for (GlobalVariable &GV : m_pModule->globals()) {
1809+
if (GV.use_empty()) {
1810+
// Need to preserve this global, otherwise we drop constructors
1811+
// for static globals.
1812+
if (!bIsLib || GV.getName().compare("llvm.global_ctors") != 0)
1813+
UnusedGlobals.emplace_back(&GV);
1814+
}
1815+
}
1816+
bChanged |= !UnusedGlobals.empty();
1817+
1818+
for (GlobalVariable *GV : UnusedGlobals) {
1819+
GV->eraseFromParent();
1820+
}
1821+
1822+
// ReEmit meta.
1823+
if (bChanged)
1824+
ReEmitDxilResources();
1825+
1826+
return bChanged;
1827+
}
1828+
1829+
bool DxilModule::StripNamesSensitiveToDebug() {
1830+
bool bChanged = false;
1831+
bool bIsLib = GetShaderModel()->IsLib();
1832+
17981833
if (!bIsLib) {
17991834
// Strip struct names
18001835
vector<StructType *> structTypes = m_pModule->getIdentifiedStructTypes();
@@ -1831,36 +1866,12 @@ bool DxilModule::StripReflection() {
18311866
bChanged = true;
18321867
}
18331868
}
1834-
}
1835-
1836-
// Resource
1837-
if (!bIsLib) {
1838-
bChanged |= StripResourcesReflection(m_CBuffers);
1839-
bChanged |= StripResourcesReflection(m_UAVs);
1840-
bChanged |= StripResourcesReflection(m_SRVs);
1841-
bChanged |= StripResourcesReflection(m_Samplers);
1842-
}
1843-
1844-
// Unused global.
1845-
SmallVector<GlobalVariable *, 2> UnusedGlobals;
1846-
for (GlobalVariable &GV : m_pModule->globals()) {
1847-
if (GV.use_empty()) {
1848-
// Need to preserve this global, otherwise we drop constructors
1849-
// for static globals.
1850-
if (!bIsLib || GV.getName().compare("llvm.global_ctors") != 0)
1851-
UnusedGlobals.emplace_back(&GV);
1852-
}
1853-
}
1854-
bChanged |= !UnusedGlobals.empty();
18551869

1856-
for (GlobalVariable *GV : UnusedGlobals) {
1857-
GV->eraseFromParent();
1870+
// ReEmit meta.
1871+
if (bChanged)
1872+
ReEmitDxilResources();
18581873
}
18591874

1860-
// ReEmit meta.
1861-
if (bChanged)
1862-
ReEmitDxilResources();
1863-
18641875
return bChanged;
18651876
}
18661877

lib/DxilContainer/DxilContainerAssembler.cpp

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,6 @@ class DxilPSVWriter : public DxilPartWriter {
691691
std::vector<PSVSignatureElement0> m_SigOutputElements;
692692
std::vector<PSVSignatureElement0> m_SigPatchConstOrPrimElements;
693693
unsigned EntryFunctionName = 0;
694-
bool m_bStripReflection = false;
695694

696695
void SetPSVSigElement(PSVSignatureElement0 &E,
697696
const DxilSignatureElement &SE) {
@@ -737,10 +736,8 @@ class DxilPSVWriter : public DxilPartWriter {
737736
}
738737

739738
public:
740-
DxilPSVWriter(const DxilModule &mod, uint32_t PSVVersion = UINT_MAX,
741-
bool bStripReflection = false)
742-
: m_Module(mod), m_PSVInitInfo(PSVVersion),
743-
m_bStripReflection(bStripReflection) {
739+
DxilPSVWriter(const DxilModule &mod, uint32_t PSVVersion = UINT_MAX)
740+
: m_Module(mod), m_PSVInitInfo(PSVVersion) {
744741
m_Module.GetValidatorVersion(m_ValMajor, m_ValMinor);
745742
hlsl::SetupPSVInitInfo(m_PSVInitInfo, m_Module);
746743

@@ -769,13 +766,6 @@ class DxilPSVWriter : public DxilPartWriter {
769766
if (m_PSVInitInfo.PSVVersion > 2) {
770767
EntryFunctionName = (uint32_t)m_StringBuffer.size();
771768
StringRef Name(m_Module.GetEntryFunctionName());
772-
773-
// Strip entry function name
774-
std::string StrippedName;
775-
if (m_bStripReflection) {
776-
StrippedName = "dx.strip.entry.";
777-
Name = StrippedName;
778-
}
779769
m_StringBuffer.append(Name.size() + 1, '\0');
780770
memcpy(m_StringBuffer.data() + EntryFunctionName, Name.data(),
781771
Name.size());
@@ -2017,10 +2007,7 @@ void hlsl::SerializeDxilContainerForModule(
20172007
pModule->ResetSubobjects(nullptr);
20182008
} else {
20192009
// Write the DxilPipelineStateValidation (PSV0) part.
2020-
bool bStripReflection =
2021-
(Flags & SerializeDxilFlags::StripReflectionFromDxilPart) != 0;
2022-
pPSVWriter =
2023-
llvm::make_unique<DxilPSVWriter>(*pModule, UINT_MAX, bStripReflection);
2010+
pPSVWriter = llvm::make_unique<DxilPSVWriter>(*pModule);
20242011
writer.AddPart(
20252012
DFCC_PipelineStateValidation, pPSVWriter->size(),
20262013
[&](AbstractMemoryStream *pStream) { pPSVWriter->write(pStream); });

tools/clang/tools/dxcompiler/dxclinker.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,11 @@ HRESULT STDMETHODCALLTYPE DxcLinker::Link(
415415
SerializeFlags, pOutputStream, 0, opts.DebugFile, &Diag,
416416
&ShaderHashContent, pReflectionStream, pRootSigStream, nullptr,
417417
nullptr);
418+
419+
if (opts.StripDebug) {
420+
inputs.pM->GetOrCreateDxilModule().StripNamesSensitiveToDebug();
421+
}
422+
418423
if (needsValidation) {
419424
valHR = dxcutil::ValidateAndAssembleToContainer(inputs);
420425
} else {

tools/clang/tools/dxcompiler/dxcompilerobj.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,10 @@ class DxcCompiler : public IDxcCompiler3,
10481048

10491049
inputs.pVersionInfo = static_cast<IDxcVersionInfo *>(this);
10501050

1051+
if (opts.StripDebug) {
1052+
inputs.pM->GetOrCreateDxilModule().StripNamesSensitiveToDebug();
1053+
}
1054+
10511055
if (needsValidation) {
10521056
valHR = dxcutil::ValidateAndAssembleToContainer(inputs);
10531057
} else {

tools/clang/unittests/HLSL/DxilContainerTest.cpp

Lines changed: 95 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,11 @@ class DxilContainerTest : public ::testing::Test {
118118
TEST_METHOD(DxilContainerUnitTest)
119119
TEST_METHOD(DxilContainerCompilerVersionTest)
120120
TEST_METHOD(ContainerBuilder_AddPrivateForceLast)
121-
122121
TEST_METHOD(ReflectionMatchesDXBC_CheckIn)
122+
TEST_METHOD(StripReflectionRemovesStructNames)
123+
TEST_METHOD(StripReflectionRemovesEntryFunctionName)
124+
TEST_METHOD(StripReflectionRemovesGroupsharedNames)
125+
TEST_METHOD(StripReflectionLibraryDoesNotStripNames)
123126
BEGIN_TEST_METHOD(ReflectionMatchesDXBC_Full)
124127
TEST_METHOD_PROPERTY(L"Priority", L"1")
125128
END_TEST_METHOD()
@@ -607,12 +610,14 @@ class DxilContainerTest : public ::testing::Test {
607610
}
608611

609612
std::string DisassembleProgram(LPCSTR program, LPCWSTR entryPoint,
610-
LPCWSTR target) {
613+
LPCWSTR target, LPCWSTR *pArguments = nullptr,
614+
UINT32 argCount = 0) {
611615
CComPtr<IDxcCompiler> pCompiler;
612616
CComPtr<IDxcBlob> pProgram;
613617
CComPtr<IDxcBlobEncoding> pDisassembly;
614618

615-
CompileToProgram(program, entryPoint, target, nullptr, 0, &pProgram);
619+
CompileToProgram(program, entryPoint, target, pArguments, argCount,
620+
&pProgram);
616621
VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
617622
VERIFY_SUCCEEDED(pCompiler->Disassemble(pProgram, &pDisassembly));
618623
return BlobToUtf8(pDisassembly);
@@ -3081,3 +3086,90 @@ TEST_F(DxilContainerTest, DxilContainerUnitTest) {
30813086
hlsl::GetDxilProgramHeader(&header, hlsl::DxilFourCC::DFCC_DXIL));
30823087
VERIFY_IS_NULL(hlsl::GetDxilPartByType(&header, hlsl::DxilFourCC::DFCC_DXIL));
30833088
}
3089+
3090+
TEST_F(DxilContainerTest, StripReflectionRemovesStructNames) {
3091+
const char *Code = R"(
3092+
struct MyCustomStruct {
3093+
float4 position;
3094+
float4 color;
3095+
};
3096+
3097+
cbuffer MyCBuffer : register(b0) {
3098+
float4 data;
3099+
};
3100+
3101+
float4 main() : SV_Target {
3102+
MyCustomStruct s;
3103+
s.position = float4(0, 0, 0, 1);
3104+
s.color = float4(1, 1, 1, 1);
3105+
return s.color;
3106+
}
3107+
)";
3108+
3109+
LPCWSTR StripDebug = L"-Qstrip_debug";
3110+
3111+
std::string disassembly =
3112+
DisassembleProgram(Code, L"main", L"ps_6_0", &StripDebug, 1);
3113+
3114+
VERIFY_IS_TRUE(disassembly.find("MyCustomStruct") == std::string::npos);
3115+
}
3116+
3117+
TEST_F(DxilContainerTest, StripReflectionRemovesEntryFunctionName) {
3118+
const char *Code = R"(
3119+
float4 MyCustomEntryPoint() : SV_Target {
3120+
return float4(1, 0, 0, 1);
3121+
}
3122+
)";
3123+
3124+
LPCWSTR StripDebug = L"-Qstrip_debug";
3125+
3126+
std::string disassembly = DisassembleProgram(Code, L"MyCustomEntryPoint",
3127+
L"ps_6_0", &StripDebug, 1);
3128+
3129+
VERIFY_IS_TRUE(disassembly.find("MyCustomEntryPoint") == std::string::npos);
3130+
VERIFY_IS_TRUE(disassembly.find("dx.strip.entry.") != std::string::npos);
3131+
}
3132+
3133+
TEST_F(DxilContainerTest, StripReflectionRemovesGroupsharedNames) {
3134+
const char *Code = R"(
3135+
groupshared float mySharedData[256];
3136+
groupshared int mySharedCounter;
3137+
3138+
[numthreads(8, 8, 1)]
3139+
void CSMain(uint3 dispatchThreadID : SV_DispatchThreadID) {
3140+
mySharedData[dispatchThreadID.x] = 1.0f;
3141+
mySharedCounter = 42;
3142+
}
3143+
)";
3144+
3145+
LPCWSTR StripDebug = L"-Qstrip_debug";
3146+
3147+
std::string disassembly =
3148+
DisassembleProgram(Code, L"CSMain", L"cs_6_0", &StripDebug, 1);
3149+
3150+
VERIFY_IS_TRUE(disassembly.find("mySharedData") == std::string::npos);
3151+
VERIFY_IS_TRUE(disassembly.find("mySharedCounter") == std::string::npos);
3152+
VERIFY_IS_TRUE(disassembly.find("dx.strip.tgsm.") != std::string::npos);
3153+
}
3154+
3155+
TEST_F(DxilContainerTest, StripReflectionLibraryDoesNotStripNames) {
3156+
const char *Code = R"(
3157+
struct MyLibStruct {
3158+
float val;
3159+
};
3160+
3161+
[shader("raygeneration")]
3162+
void MyRayGenEntry() {
3163+
MyLibStruct s;
3164+
s.val = 1.0f;
3165+
}
3166+
)";
3167+
3168+
LPCWSTR StripDebug = L"-Qstrip_debug";
3169+
3170+
std::string disassembly =
3171+
DisassembleProgram(Code, L"", L"lib_6_3", &StripDebug, 1);
3172+
3173+
VERIFY_IS_TRUE(disassembly.find("MyLibStruct") != std::string::npos ||
3174+
disassembly.find("dx.strip.struct.") == std::string::npos);
3175+
}

0 commit comments

Comments
 (0)