Skip to content

Commit 25307b4

Browse files
authored
Remove unused type annotations (microsoft#4689)
Types no longer in use, potentially due to type translation, are still kept around just because they are referenced in the type annotation metadata. This change adds a method DxilModule::RemoveUnusedTypeAnnotations() that removes any type annotations not used by resources or function arguments or return types. Types outside this set should no longer need any type annotations. This will get rid of extra unused types often left over in final dxil before.
1 parent 87c30cf commit 25307b4

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

include/dxc/DXIL/DxilModule.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ class DxilModule {
202202

203203
bool StripReflection();
204204
void StripDebugRelatedCode();
205+
void RemoveUnusedTypeAnnotations();
205206

206207
// Helper to remove dx.* metadata with source and compile options.
207208
// If the parameter `bReplaceWithDummyData` is true, the named metadata

lib/DXIL/DxilModule.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1824,6 +1824,55 @@ bool DxilModule::StripReflection() {
18241824
return bChanged;
18251825
}
18261826

1827+
static void RemoveTypesFromSet(Type *Ty, SetVector<const StructType*> &typeSet) {
1828+
if (Ty->isPointerTy())
1829+
Ty = Ty->getPointerElementType();
1830+
while (Ty->isArrayTy())
1831+
Ty = Ty->getArrayElementType();
1832+
if (StructType *ST = dyn_cast<StructType>(Ty)) {
1833+
if (typeSet.count(ST)) {
1834+
typeSet.remove(ST);
1835+
for (unsigned i = 0; i < ST->getNumElements(); i++) {
1836+
RemoveTypesFromSet(ST->getElementType(i), typeSet);
1837+
}
1838+
}
1839+
}
1840+
}
1841+
1842+
template <typename TResource>
1843+
static void
1844+
RemoveUsedTypesFromSet(std::vector<std::unique_ptr<TResource>> &vec, SetVector<const StructType*> &typeSet) {
1845+
for (auto &p : vec) {
1846+
RemoveTypesFromSet(p->GetHLSLType(), typeSet);
1847+
}
1848+
}
1849+
1850+
void DxilModule::RemoveUnusedTypeAnnotations() {
1851+
// Collect annotated types
1852+
const DxilTypeSystem::StructAnnotationMap &SAMap = m_pTypeSystem->GetStructAnnotationMap();
1853+
SetVector<const StructType*> types;
1854+
for (const auto &it : SAMap)
1855+
types.insert(it.first);
1856+
1857+
// Iterate resource types and remove any HLSL types from set
1858+
RemoveUsedTypesFromSet(m_CBuffers, types);
1859+
RemoveUsedTypesFromSet(m_UAVs, types);
1860+
RemoveUsedTypesFromSet(m_SRVs, types);
1861+
1862+
// Iterate Function parameters and return types, removing any HLSL types found from set
1863+
for (Function &F : m_pModule->functions()) {
1864+
FunctionType *FT = F.getFunctionType();
1865+
RemoveTypesFromSet(FT->getReturnType(), types);
1866+
for (Type *PTy : FT->params())
1867+
RemoveTypesFromSet(PTy, types);
1868+
}
1869+
1870+
// Remove remaining set of types
1871+
for (const StructType *ST : types)
1872+
m_pTypeSystem->EraseStructAnnotation(ST);
1873+
}
1874+
1875+
18271876
void DxilModule::LoadDxilResources(const llvm::MDOperand &MDO) {
18281877
if (MDO.get() == nullptr)
18291878
return;

lib/HLSL/DxilPreparePasses.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,9 @@ class DxilFinalizeModule : public ModulePass {
836836
// Strip parameters of entry function.
837837
StripEntryParameters(M, DM, IsLib);
838838

839+
// Remove unused types from type annotations
840+
DM.RemoveUnusedTypeAnnotations();
841+
839842
// Update flags to reflect any changes.
840843
DM.CollectShaderFlagsForModule();
841844

tools/clang/test/HLSLFileCheck/hlsl/types/enum/enum6.hlsl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// RUN: %dxc -E main -T vs_6_0 -HV 2017 %s | FileCheck %s
2-
// CHECK: %struct.PSInput = type { <4 x float>, <4 x float>, i16 }
32
// CHECK: call void @dx.op.storeOutput.i16(i32 5, i32 2, i32 0, i8 0, i16 3)
43

54
enum class E : min16int {

tools/clang/test/HLSLFileCheck/shader_targets/mesh/mesh.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// CHECK: dx.op.emitIndices(i32 169,
1010
// CHECK: dx.op.storePrimitiveOutput.f32(i32 172,
1111
// CHECK: dx.op.storeVertexOutput.f32(i32 171,
12-
// CHECK: !"cullPrimitive", i32 3, i32 100, i32 4, !"SV_CullPrimitive", i32 7, i32 1}
12+
// CHECK: !{i32 5, !"SV_CullPrimitive", i8 1, i8 30, !{{[0-9]+}}, i8 1, i32 1, i8 1, i32 -1, i8 -1, !{{[0-9]+}}}
1313

1414
#define MAX_VERT 32
1515
#define MAX_PRIM 16

0 commit comments

Comments
 (0)