Skip to content

Commit 87c30cf

Browse files
authored
Remove ResourceAttribute metadata node from DxilFieldAnnotation (microsoft#4671)
Currently, ResourceAttribute is only used to capture resource type information during CodeGen for the annotate handle generated during AddOpcodeParamForIntrinsic. Going through a metadata node is totally unnecessary, just adding code and complexity. This change just stores the DxilResourceProperties instead, renaming `*ResourceAttribute` methods to `*ResourceProperties`. This change also emits the resource properties to new field annotaion metadata in validator version 1.8+ to preserve through serialization.
1 parent aaa30e2 commit 87c30cf

File tree

12 files changed

+48
-230
lines changed

12 files changed

+48
-230
lines changed

include/dxc/DXIL/DxilMetadataHelper.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ class DxilMDHelper {
226226
static const unsigned kDxilFieldAnnotationCompTypeTag = 7;
227227
static const unsigned kDxilFieldAnnotationPreciseTag = 8;
228228
static const unsigned kDxilFieldAnnotationCBUsedTag = 9;
229+
static const unsigned kDxilFieldAnnotationResPropTag = 10;
229230

230231
// DXR Payload Annotations
231232
static const unsigned kDxilPayloadAnnotationStructTag = 0;
@@ -241,12 +242,6 @@ class DxilMDHelper {
241242
// Control flow hint.
242243
static const char kDxilControlFlowHintMDName[];
243244

244-
// Resource attribute.
245-
static const char kHLDxilResourceAttributeMDName[];
246-
static const unsigned kHLDxilResourceAttributeNumFields = 2;
247-
static const unsigned kHLDxilResourceAttributeClass = 0;
248-
static const unsigned kHLDxilResourceAttributeMeta = 1;
249-
250245
// Precise attribute.
251246
static const char kDxilPreciseAttributeMDName[];
252247

@@ -411,9 +406,6 @@ class DxilMDHelper {
411406
llvm::MDTuple *EmitDxilSampler(const DxilSampler &S);
412407
void LoadDxilSampler(const llvm::MDOperand &MDO, DxilSampler &S);
413408
const llvm::MDOperand &GetResourceClass(llvm::MDNode *MD, DXIL::ResourceClass &RC);
414-
void LoadDxilResourceBaseFromMDNode(llvm::MDNode *MD, DxilResourceBase &R);
415-
void LoadDxilResourceFromMDNode(llvm::MDNode *MD, DxilResource &R);
416-
void LoadDxilSamplerFromMDNode(llvm::MDNode *MD, DxilSampler &S);
417409

418410
// Type system.
419411
void EmitDxilTypeSystem(DxilTypeSystem &TypeSystem, std::vector<llvm::GlobalVariable *> &LLVMUsed);

include/dxc/DXIL/DxilModule.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,6 @@ class DxilModule {
109109
const DxilResource &GetUAV(unsigned idx) const;
110110
const std::vector<std::unique_ptr<DxilResource> > &GetUAVs() const;
111111

112-
void LoadDxilResourceBaseFromMDNode(llvm::MDNode *MD, DxilResourceBase &R);
113-
void LoadDxilResourceFromMDNode(llvm::MDNode *MD, DxilResource &R);
114-
void LoadDxilSamplerFromMDNode(llvm::MDNode *MD, DxilSampler &S);
115-
116112
void RemoveUnusedResources();
117113
void RemoveResourcesWithUnusedSymbols();
118114
void RemoveFunction(llvm::Function *F);

include/dxc/DXIL/DxilTypeSystem.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "dxc/DXIL/DxilConstants.h"
1616
#include "dxc/DXIL/DxilCompType.h"
1717
#include "dxc/DXIL/DxilInterpolationMode.h"
18+
#include "dxc/DXIL/DxilResourceProperties.h"
1819

1920
#include <memory>
2021
#include <string>
@@ -54,9 +55,12 @@ class DxilFieldAnnotation {
5455
const DxilMatrixAnnotation &GetMatrixAnnotation() const;
5556
void SetMatrixAnnotation(const DxilMatrixAnnotation &MA);
5657

57-
bool HasResourceAttribute() const;
58-
llvm::MDNode *GetResourceAttribute() const;
59-
void SetResourceAttribute(llvm::MDNode *MD);
58+
// Currently, ResourceProperties is only used to capture resource type
59+
// information during CodeGen for the annotate handle generated during
60+
// AddOpcodeParamForIntrinsic.
61+
bool HasResourceProperties() const;
62+
const DxilResourceProperties &GetResourceProperties() const;
63+
void SetResourceProperties(const DxilResourceProperties &RP);
6064

6165
bool HasCBufferOffset() const;
6266
unsigned GetCBufferOffset() const;
@@ -86,7 +90,7 @@ class DxilFieldAnnotation {
8690
bool m_bPrecise;
8791
CompType m_CompType;
8892
DxilMatrixAnnotation m_Matrix;
89-
llvm::MDNode *m_ResourceAttribute;
93+
DxilResourceProperties m_ResourceProps;
9094
unsigned m_CBufferOffset;
9195
std::string m_Semantic;
9296
InterpolationMode m_InterpMode;

include/dxc/HLSL/HLModule.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,7 @@ class HLModule {
176176
void LoadHLMetadata();
177177
/// Delete any HLDXIR from the specified module.
178178
static void ClearHLMetadata(llvm::Module &M);
179-
/// Create Metadata from a resource.
180-
llvm::MDNode *DxilSamplerToMDNode(const DxilSampler &S);
181-
llvm::MDNode *DxilSRVToMDNode(const DxilResource &SRV);
182-
llvm::MDNode *DxilUAVToMDNode(const DxilResource &UAV);
183-
llvm::MDNode *DxilCBufferToMDNode(const DxilCBuffer &CB);
184-
void LoadDxilResourceBaseFromMDNode(llvm::MDNode *MD, DxilResourceBase &R);
185-
void LoadDxilResourceFromMDNode(llvm::MDNode *MD, DxilResource &R);
186-
void LoadDxilSamplerFromMDNode(llvm::MDNode *MD, DxilSampler &S);
179+
187180
DxilResourceBase *
188181
AddResourceWithGlobalVariableAndProps(llvm::Constant *GV,
189182
DxilResourceProperties &RP);

lib/DXIL/DxilMetadataHelper.cpp

Lines changed: 15 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "dxc/DXIL/DxilFunctionProps.h"
2121
#include "dxc/DXIL/DxilShaderFlags.h"
2222
#include "dxc/DXIL/DxilSubobject.h"
23+
#include "dxc/DXIL/DxilModule.h"
24+
#include "dxc/DXIL/DxilOperations.h"
2325

2426
#include "llvm/IR/Constants.h"
2527
#include "llvm/IR/Function.h"
@@ -86,7 +88,6 @@ const char DxilMDHelper::kDxilPreciseAttributeMDName[] = "dx.prec
8688
const char DxilMDHelper::kDxilVariableDebugLayoutMDName[] = "dx.dbg.varlayout";
8789
const char DxilMDHelper::kDxilTempAllocaMDName[] = "dx.temp";
8890
const char DxilMDHelper::kDxilNonUniformAttributeMDName[] = "dx.nonuniform";
89-
const char DxilMDHelper::kHLDxilResourceAttributeMDName[] = "dx.hl.resource.attribute";
9091
const char DxilMDHelper::kDxilValidatorVersionMDName[] = "dx.valver";
9192
const char DxilMDHelper::kDxilDxrPayloadAnnotationsMDName[] = "dx.dxrPayloadAnnotations";
9293

@@ -1205,6 +1206,14 @@ Metadata *DxilMDHelper::EmitDxilFieldAnnotation(const DxilFieldAnnotation &FA) {
12051206
MDVals.emplace_back(Uint32ToConstMD(kDxilFieldAnnotationCBUsedTag));
12061207
MDVals.emplace_back(BoolToConstMD(true));
12071208
}
1209+
if (FA.HasResourceProperties() &&
1210+
DXIL::CompareVersions(m_MinValMajor, m_MinValMinor, 1, 8) >= 0) {
1211+
MDVals.emplace_back(Uint32ToConstMD(kDxilFieldAnnotationResPropTag));
1212+
MDVals.emplace_back(ValueAsMetadata::get(resource_helper::getAsConstant(
1213+
FA.GetResourceProperties(),
1214+
m_pModule->GetDxilModule().GetOP()->GetResourcePropertiesType(),
1215+
*m_pSM)));
1216+
}
12081217

12091218
return MDNode::get(m_Ctx, MDVals);
12101219
}
@@ -1253,6 +1262,11 @@ void DxilMDHelper::LoadDxilFieldAnnotation(const MDOperand &MDO, DxilFieldAnnota
12531262
case kDxilFieldAnnotationCBUsedTag:
12541263
FA.SetCBVarUsed(ConstMDToBool(MDO));
12551264
break;
1265+
case kDxilFieldAnnotationResPropTag:
1266+
if (Constant *C = dyn_cast_or_null<Constant>(
1267+
dyn_cast<ValueAsMetadata>(MDO)->getValue()))
1268+
FA.SetResourceProperties(resource_helper::loadPropsFromConstant(*C));
1269+
break;
12561270
default:
12571271
DXASSERT(false, "Unknown extended shader properties tag");
12581272
m_bExtraMetadata = true;
@@ -2032,76 +2046,6 @@ void DxilMDHelper::LoadDxilSampler(const MDOperand &MDO, DxilSampler &S) {
20322046
m_bExtraMetadata |= m_ExtraPropertyHelper->m_bExtraMetadata;
20332047
}
20342048

2035-
const MDOperand &DxilMDHelper::GetResourceClass(llvm::MDNode *MD,
2036-
DXIL::ResourceClass &RC) {
2037-
IFTBOOL(MD->getNumOperands() >=
2038-
DxilMDHelper::kHLDxilResourceAttributeNumFields,
2039-
DXC_E_INCORRECT_DXIL_METADATA);
2040-
RC = static_cast<DxilResource::Class>(ConstMDToUint32(
2041-
MD->getOperand(DxilMDHelper::kHLDxilResourceAttributeClass)));
2042-
return MD->getOperand(DxilMDHelper::kHLDxilResourceAttributeMeta);
2043-
}
2044-
2045-
void DxilMDHelper::LoadDxilResourceBaseFromMDNode(llvm::MDNode *MD,
2046-
DxilResourceBase &R) {
2047-
DxilResource::Class RC = DxilResource::Class::Invalid;
2048-
const MDOperand &Meta = GetResourceClass(MD, RC);
2049-
2050-
switch (RC) {
2051-
case DxilResource::Class::CBuffer: {
2052-
DxilCBuffer CB;
2053-
LoadDxilCBuffer(Meta, CB);
2054-
R = CB;
2055-
} break;
2056-
case DxilResource::Class::Sampler: {
2057-
DxilSampler S;
2058-
LoadDxilSampler(Meta, S);
2059-
R = S;
2060-
} break;
2061-
case DxilResource::Class::SRV: {
2062-
DxilResource Res;
2063-
LoadDxilSRV(Meta, Res);
2064-
R = Res;
2065-
} break;
2066-
case DxilResource::Class::UAV: {
2067-
DxilResource Res;
2068-
LoadDxilUAV(Meta, Res);
2069-
R = Res;
2070-
} break;
2071-
default:
2072-
DXASSERT(0, "Invalid metadata");
2073-
}
2074-
}
2075-
2076-
void DxilMDHelper::LoadDxilResourceFromMDNode(llvm::MDNode *MD,
2077-
DxilResource &R) {
2078-
DxilResource::Class RC = DxilResource::Class::Invalid;
2079-
const MDOperand &Meta = GetResourceClass(MD, RC);
2080-
2081-
switch (RC) {
2082-
case DxilResource::Class::SRV: {
2083-
LoadDxilSRV(Meta, R);
2084-
} break;
2085-
case DxilResource::Class::UAV: {
2086-
LoadDxilUAV(Meta, R);
2087-
} break;
2088-
default:
2089-
DXASSERT(0, "Invalid metadata");
2090-
}
2091-
}
2092-
2093-
void DxilMDHelper::LoadDxilSamplerFromMDNode(llvm::MDNode *MD, DxilSampler &S) {
2094-
DxilResource::Class RC = DxilResource::Class::Invalid;
2095-
const MDOperand &Meta = GetResourceClass(MD, RC);
2096-
2097-
switch (RC) {
2098-
case DxilResource::Class::Sampler: {
2099-
LoadDxilSampler(Meta, S);
2100-
} break;
2101-
default:
2102-
DXASSERT(0, "Invalid metadata");
2103-
}
2104-
}
21052049

21062050
//
21072051
// DxilExtraPropertyHelper shader-specific methods.

lib/DXIL/DxilModule.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -916,16 +916,6 @@ const vector<unique_ptr<DxilResource> > &DxilModule::GetUAVs() const {
916916
return m_UAVs;
917917
}
918918

919-
void DxilModule::LoadDxilResourceBaseFromMDNode(MDNode *MD, DxilResourceBase &R) {
920-
return m_pMDHelper->LoadDxilResourceBaseFromMDNode(MD, R);
921-
}
922-
void DxilModule::LoadDxilResourceFromMDNode(llvm::MDNode *MD, DxilResource &R) {
923-
return m_pMDHelper->LoadDxilResourceFromMDNode(MD, R);
924-
}
925-
void DxilModule::LoadDxilSamplerFromMDNode(llvm::MDNode *MD, DxilSampler &S) {
926-
return m_pMDHelper->LoadDxilSamplerFromMDNode(MD, S);
927-
}
928-
929919
template <typename TResource>
930920
static void RemoveResources(std::vector<std::unique_ptr<TResource>> &vec,
931921
std::unordered_set<unsigned> &immResID) {

lib/DXIL/DxilTypeSystem.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ DxilMatrixAnnotation::DxilMatrixAnnotation()
4343
//
4444
DxilFieldAnnotation::DxilFieldAnnotation()
4545
: m_bPrecise(false)
46-
, m_ResourceAttribute(nullptr)
4746
, m_CBufferOffset(UINT_MAX)
4847
, m_bCBufferVarUsed(false)
4948
{}
@@ -53,14 +52,14 @@ void DxilFieldAnnotation::SetPrecise(bool b) { m_bPrecise = b; }
5352
bool DxilFieldAnnotation::HasMatrixAnnotation() const { return m_Matrix.Cols != 0; }
5453
const DxilMatrixAnnotation &DxilFieldAnnotation::GetMatrixAnnotation() const { return m_Matrix; }
5554
void DxilFieldAnnotation::SetMatrixAnnotation(const DxilMatrixAnnotation &MA) { m_Matrix = MA; }
56-
bool DxilFieldAnnotation::HasResourceAttribute() const {
57-
return m_ResourceAttribute;
55+
bool DxilFieldAnnotation::HasResourceProperties() const {
56+
return m_ResourceProps.isValid();
5857
}
59-
llvm::MDNode *DxilFieldAnnotation::GetResourceAttribute() const {
60-
return m_ResourceAttribute;
58+
const DxilResourceProperties &DxilFieldAnnotation::GetResourceProperties() const {
59+
return m_ResourceProps;
6160
}
62-
void DxilFieldAnnotation::SetResourceAttribute(llvm::MDNode *MD) {
63-
m_ResourceAttribute = MD;
61+
void DxilFieldAnnotation::SetResourceProperties(const DxilResourceProperties &RP) {
62+
m_ResourceProps = RP;
6463
}
6564
bool DxilFieldAnnotation::HasCBufferOffset() const { return m_CBufferOffset != UINT_MAX; }
6665
unsigned DxilFieldAnnotation::GetCBufferOffset() const { return m_CBufferOffset; }

lib/HLSL/HLModule.cpp

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -664,45 +664,6 @@ void HLModule::LoadHLShaderProperties(const MDOperand &MDO) {
664664
return;
665665
}
666666

667-
MDNode *HLModule::DxilSamplerToMDNode(const DxilSampler &S) {
668-
MDNode *MD = m_pMDHelper->EmitDxilSampler(S);
669-
ValueAsMetadata *ResClass =
670-
m_pMDHelper->Uint32ToConstMD((unsigned)DXIL::ResourceClass::Sampler);
671-
672-
return MDNode::get(m_Ctx, {ResClass, MD});
673-
}
674-
MDNode *HLModule::DxilSRVToMDNode(const DxilResource &SRV) {
675-
MDNode *MD = m_pMDHelper->EmitDxilSRV(SRV);
676-
ValueAsMetadata *ResClass =
677-
m_pMDHelper->Uint32ToConstMD((unsigned)DXIL::ResourceClass::SRV);
678-
679-
return MDNode::get(m_Ctx, {ResClass, MD});
680-
}
681-
MDNode *HLModule::DxilUAVToMDNode(const DxilResource &UAV) {
682-
MDNode *MD = m_pMDHelper->EmitDxilUAV(UAV);
683-
ValueAsMetadata *ResClass =
684-
m_pMDHelper->Uint32ToConstMD((unsigned)DXIL::ResourceClass::UAV);
685-
686-
return MDNode::get(m_Ctx, {ResClass, MD});
687-
}
688-
MDNode *HLModule::DxilCBufferToMDNode(const DxilCBuffer &CB) {
689-
MDNode *MD = m_pMDHelper->EmitDxilCBuffer(CB);
690-
ValueAsMetadata *ResClass =
691-
m_pMDHelper->Uint32ToConstMD((unsigned)DXIL::ResourceClass::CBuffer);
692-
693-
return MDNode::get(m_Ctx, {ResClass, MD});
694-
}
695-
696-
void HLModule::LoadDxilResourceBaseFromMDNode(MDNode *MD, DxilResourceBase &R) {
697-
return m_pMDHelper->LoadDxilResourceBaseFromMDNode(MD, R);
698-
}
699-
void HLModule::LoadDxilResourceFromMDNode(llvm::MDNode *MD, DxilResource &R) {
700-
return m_pMDHelper->LoadDxilResourceFromMDNode(MD, R);
701-
}
702-
void HLModule::LoadDxilSamplerFromMDNode(llvm::MDNode *MD, DxilSampler &S) {
703-
return m_pMDHelper->LoadDxilSamplerFromMDNode(MD, S);
704-
}
705-
706667
DxilResourceBase *
707668
HLModule::AddResourceWithGlobalVariableAndProps(llvm::Constant *GV,
708669
DxilResourceProperties &RP) {

lib/Transforms/Scalar/ScalarReplAggregatesHLSL.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5268,7 +5268,7 @@ void SROA_Parameter_HLSL::flattenArgument(
52685268
flatParamAnnotation.SetCompType(annotation.GetCompType().GetKind());
52695269
flatParamAnnotation.SetMatrixAnnotation(annotation.GetMatrixAnnotation());
52705270
flatParamAnnotation.SetPrecise(annotation.IsPrecise());
5271-
flatParamAnnotation.SetResourceAttribute(annotation.GetResourceAttribute());
5271+
flatParamAnnotation.SetResourceProperties(annotation.GetResourceProperties());
52725272

52735273
// Add debug info.
52745274
if (DDIs.size() && V != Arg) {

tools/clang/lib/CodeGen/CGHLSLMS.cpp

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ class CGMSHLSLRuntime : public CGHLSLRuntime {
8080
llvm::DataLayout dataLayout;
8181
// decl map to constant id for program
8282
llvm::DenseMap<HLSLBufferDecl *, uint32_t> constantBufMap;
83-
// Map for resource type to resource metadata value.
84-
std::unordered_map<llvm::Type *, MDNode*> resMetadataMap;
8583
// Map from Constant to register bindings.
8684
llvm::DenseMap<llvm::Constant *,
8785
llvm::SmallVector<std::pair<DXIL::ResourceClass, unsigned>, 1>>
@@ -211,7 +209,6 @@ class CGMSHLSLRuntime : public CGHLSLRuntime {
211209
DxilTypeSystem &dxilTypeSys);
212210
unsigned AddTypeAnnotation(QualType Ty, DxilTypeSystem &dxilTypeSys,
213211
unsigned &arrayEltSize);
214-
MDNode *GetOrAddResTypeMD(QualType resTy, bool bCreate);
215212
DxilResourceProperties BuildResourceProperty(QualType resTy);
216213
void ConstructFieldAttributedAnnotation(DxilFieldAnnotation &fieldAnnotation,
217214
QualType fieldTy,
@@ -671,56 +668,6 @@ static DxilSampler::SamplerKind KeywordToSamplerKind(llvm::StringRef keyword) {
671668
.Default(DxilSampler::SamplerKind::Invalid);
672669
}
673670

674-
MDNode *CGMSHLSLRuntime::GetOrAddResTypeMD(QualType resTy, bool bCreate) {
675-
const RecordType *RT = resTy->getAs<RecordType>();
676-
if (!RT)
677-
return nullptr;
678-
RecordDecl *RD = RT->getDecl();
679-
SourceLocation loc = RD->getLocation();
680-
681-
hlsl::DxilResourceBase::Class resClass = TypeToClass(resTy);
682-
llvm::Type *Ty = CGM.getTypes().ConvertType(resTy);
683-
auto it = resMetadataMap.find(Ty);
684-
if (!bCreate && it != resMetadataMap.end())
685-
return it->second;
686-
687-
// Save resource type metadata.
688-
switch (resClass) {
689-
case DXIL::ResourceClass::UAV: {
690-
DxilResource UAV;
691-
// TODO: save globalcoherent to variable in EmitHLSLBuiltinCallExpr.
692-
SetUAVSRV(loc, resClass, &UAV, resTy);
693-
// Set global symbol to save type.
694-
UAV.SetGlobalSymbol(UndefValue::get(Ty->getPointerTo()));
695-
MDNode *MD = m_pHLModule->DxilUAVToMDNode(UAV);
696-
resMetadataMap[Ty] = MD;
697-
return MD;
698-
} break;
699-
case DXIL::ResourceClass::SRV: {
700-
DxilResource SRV;
701-
SetUAVSRV(loc, resClass, &SRV, resTy);
702-
// Set global symbol to save type.
703-
SRV.SetGlobalSymbol(UndefValue::get(Ty->getPointerTo()));
704-
MDNode *MD = m_pHLModule->DxilSRVToMDNode(SRV);
705-
resMetadataMap[Ty] = MD;
706-
return MD;
707-
} break;
708-
case DXIL::ResourceClass::Sampler: {
709-
DxilSampler S;
710-
DxilSampler::SamplerKind kind = KeywordToSamplerKind(RD->getName());
711-
S.SetSamplerKind(kind);
712-
// Set global symbol to save type.
713-
S.SetGlobalSymbol(UndefValue::get(Ty->getPointerTo()));
714-
MDNode *MD = m_pHLModule->DxilSamplerToMDNode(S);
715-
resMetadataMap[Ty] = MD;
716-
return MD;
717-
}
718-
default:
719-
// Skip OutputStream for GS.
720-
return nullptr;
721-
}
722-
}
723-
724671

725672
namespace {
726673
MatrixOrientation GetMatrixMajor(QualType Ty, bool bDefaultRowMajor) {
@@ -839,10 +786,7 @@ void CGMSHLSLRuntime::ConstructFieldAttributedAnnotation(
839786
EltTy = hlsl::GetHLSLVecElementType(Ty);
840787

841788
if (IsHLSLResourceType(Ty)) {
842-
// Always create for llvm::Type could be same for different QualType.
843-
// TODO: change to DxilProperties.
844-
MDNode *MD = GetOrAddResTypeMD(Ty, /*bCreate*/ true);
845-
fieldAnnotation.SetResourceAttribute(MD);
789+
fieldAnnotation.SetResourceProperties(BuildResourceProperty(Ty));
846790
}
847791

848792
bool bSNorm = false;

0 commit comments

Comments
 (0)