Skip to content
Merged
2 changes: 1 addition & 1 deletion clang/include/clang/Sema/SemaHLSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class ResourceBindings {
ResourceClass ResClass);
DeclBindingInfo *getDeclBindingInfo(const VarDecl *VD,
ResourceClass ResClass);
bool hasBindingInfoForDecl(const VarDecl *VD);
bool hasBindingInfoForDecl(const VarDecl *VD) const;

private:
// List of all resource bindings required by the shader.
Expand Down
21 changes: 17 additions & 4 deletions clang/lib/Sema/SemaHLSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,19 @@ DeclBindingInfo *ResourceBindings::addDeclBindingInfo(const VarDecl *VD,
ResourceClass ResClass) {
assert(getDeclBindingInfo(VD, ResClass) == nullptr &&
"DeclBindingInfo already added");
#ifndef NDEBUG
// Verify that existing bindings for this decl are stored sequentially
// and at the end of the BindingsList
auto I = DeclToBindingListIndex.find(VD);
if (I != DeclToBindingListIndex.end()) {
for (unsigned Index = I->getSecond(); Index < BindingsList.size(); ++Index)
assert(BindingsList[Index].Decl == VD);
}
#endif
Comment on lines +113 to +121
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking that assert(!hasBindingInfoForDecl(VD) || BindingsList.back().Decl == VD); would cover most bases?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shoot! I have completed this PR before realizing I did not push this change up.
Here is a separate PR: #112661

// VarDecl may have multiple entries for different resource classes.
// DeclToBindingListIndex stores the index of the first binding we saw
// for this decl. If there are any additional ones then that index
// shouldn't be updated.
DeclToBindingListIndex.try_emplace(VD, BindingsList.size());
return &BindingsList.emplace_back(VD, ResClass);
}
Expand All @@ -118,17 +131,17 @@ DeclBindingInfo *ResourceBindings::getDeclBindingInfo(const VarDecl *VD,
ResourceClass ResClass) {
auto Entry = DeclToBindingListIndex.find(VD);
if (Entry != DeclToBindingListIndex.end()) {
unsigned Index = Entry->getSecond();
while (Index < BindingsList.size() && BindingsList[Index].Decl == VD) {
for (unsigned Index = Entry->getSecond();
Index < BindingsList.size() && BindingsList[Index].Decl == VD;
++Index) {
if (BindingsList[Index].ResClass == ResClass)
return &BindingsList[Index];
Index++;
}
}
return nullptr;
}

bool ResourceBindings::hasBindingInfoForDecl(const VarDecl *VD) {
bool ResourceBindings::hasBindingInfoForDecl(const VarDecl *VD) const {
return DeclToBindingListIndex.contains(VD);
}

Expand Down