Skip to content

Commit 60be049

Browse files
committed
Generate descriptions for resources with no names (#3598)
Module with stripped reflection will have no name for error messages. Try to get the name from the debug module. Failing that, use ID, type and binding info. (cherry picked from commit 2791c51)
1 parent a75ab9e commit 60be049

1 file changed

Lines changed: 46 additions & 5 deletions

File tree

lib/HLSL/DxilValidation.cpp

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -687,16 +687,57 @@ struct ValidationContext {
687687
Failed = true;
688688
}
689689

690+
// Use this instead of DxilResourceBase::GetGlobalName
691+
std::string GetResourceName(const hlsl::DxilResourceBase *Res) {
692+
if (!Res)
693+
return "nullptr";
694+
std::string resName = Res->GetGlobalName();
695+
if (!resName.empty())
696+
return resName;
697+
if (pDebugModule) {
698+
DxilModule &DM = pDebugModule->GetOrCreateDxilModule();
699+
switch (Res->GetClass()) {
700+
case DXIL::ResourceClass::CBuffer: return DM.GetCBuffer(Res->GetID()).GetGlobalName();
701+
case DXIL::ResourceClass::Sampler: return DM.GetSampler(Res->GetID()).GetGlobalName();
702+
case DXIL::ResourceClass::SRV: return DM.GetSRV(Res->GetID()).GetGlobalName();
703+
case DXIL::ResourceClass::UAV: return DM.GetUAV(Res->GetID()).GetGlobalName();
704+
default: return "Invalid Resource";
705+
}
706+
}
707+
// When names have been stripped, use class and binding location to
708+
// identify the resource. Format is roughly:
709+
// Allocated: (CB|T|U|S)<ID>: <ResourceKind> ((cb|t|u|s)<LB>[<RangeSize>] space<SpaceID>)
710+
// Unallocated: (CB|T|U|S)<ID>: <ResourceKind> (no bind location)
711+
// Example: U0: TypedBuffer (u5[2] space1)
712+
// [<RangeSize>] and space<SpaceID> skipped if 1 and 0 respectively.
713+
return (Twine(Res->GetResIDPrefix()) + Twine(Res->GetID()) + ": " +
714+
Twine(Res->GetResKindName()) +
715+
(Res->IsAllocated()
716+
? (" (" + Twine(Res->GetResBindPrefix()) +
717+
Twine(Res->GetLowerBound()) +
718+
(Res->IsUnbounded()
719+
? Twine("[unbounded]")
720+
: (Res->GetRangeSize() != 1)
721+
? "[" + Twine(Res->GetRangeSize()) + "]"
722+
: Twine()) +
723+
((Res->GetSpaceID() != 0)
724+
? " space" + Twine(Res->GetSpaceID())
725+
: Twine()) +
726+
")")
727+
: Twine(" (no bind location)")))
728+
.str();
729+
}
730+
690731
void EmitResourceError(const hlsl::DxilResourceBase *Res, ValidationRule rule) {
691-
std::string QuotedRes = " '" + Res->GetGlobalName() + "'";
732+
std::string QuotedRes = " '" + GetResourceName(Res) + "'";
692733
dxilutil::EmitErrorOnContext(M.getContext(), GetValidationRuleText(rule) + QuotedRes);
693734
Failed = true;
694735
}
695736

696737
void EmitResourceFormatError(const hlsl::DxilResourceBase *Res,
697738
ValidationRule rule,
698739
ArrayRef<StringRef> args) {
699-
std::string QuotedRes = " '" + Res->GetGlobalName() + "'";
740+
std::string QuotedRes = " '" + GetResourceName(Res) + "'";
700741
std::string ruleText = GetValidationRuleText(rule);
701742
FormatRuleText(ruleText, args);
702743
dxilutil::EmitErrorOnContext(M.getContext(), ruleText + QuotedRes);
@@ -3996,7 +4037,7 @@ static void ValidateResourceOverlap(
39964037
if (conflictRes) {
39974038
ValCtx.EmitFormatError(
39984039
ValidationRule::SmResourceRangeOverlap,
3999-
{res.GetGlobalName(), std::to_string(base),
4040+
{ValCtx.GetResourceName(&res), std::to_string(base),
40004041
std::to_string(size),
40014042
std::to_string(conflictRes->GetLowerBound()),
40024043
std::to_string(conflictRes->GetRangeSize()),
@@ -4208,7 +4249,7 @@ static void ValidateCBuffer(DxilCBuffer &cb, ValidationContext &ValCtx) {
42084249
DXIL::kMaxCBufferSize << 4);
42094250
CollectCBufferRanges(annotation, constAllocator,
42104251
0, typeSys,
4211-
cb.GetGlobalName(), ValCtx);
4252+
ValCtx.GetResourceName(&cb), ValCtx);
42124253
}
42134254

42144255
static void ValidateResources(ValidationContext &ValCtx) {
@@ -4240,7 +4281,7 @@ static void ValidateResources(ValidationContext &ValCtx) {
42404281
if (uav->HasCounter() && uav->IsGloballyCoherent())
42414282
ValCtx.EmitResourceFormatError(uav.get(),
42424283
ValidationRule::MetaGlcNotOnAppendConsume,
4243-
{uav.get()->GetGlobalName()});
4284+
{ValCtx.GetResourceName(uav.get())});
42444285

42454286
ValidateResource(*uav, ValCtx);
42464287
ValidateResourceOverlap(*uav, uavAllocator, ValCtx);

0 commit comments

Comments
 (0)