Skip to content

Commit 67eb653

Browse files
committed
update resource range analysis to use retained source loc
1 parent f6d6d97 commit 67eb653

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

clang/include/clang/Sema/SemaHLSL.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ class SemaHLSL : public SemaBase {
139139
ArrayRef<hlsl::RootSignatureElement> Elements);
140140

141141
// Returns true when D is invalid and a diagnostic was produced
142-
bool handleRootSignatureDecl(HLSLRootSignatureDecl *D, SourceLocation Loc);
142+
bool
143+
handleRootSignatureElements(ArrayRef<hlsl::RootSignatureElement> Elements,
144+
SourceLocation Loc);
143145
void handleRootSignatureAttr(Decl *D, const ParsedAttr &AL);
144146
void handleNumThreadsAttr(Decl *D, const ParsedAttr &AL);
145147
void handleWaveSizeAttr(Decl *D, const ParsedAttr &AL);

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "clang/Basic/SourceLocation.h"
2929
#include "clang/Basic/Specifiers.h"
3030
#include "clang/Basic/TargetInfo.h"
31+
#include "clang/Parse/ParseHLSLRootSignature.h"
3132
#include "clang/Sema/Initialization.h"
3233
#include "clang/Sema/Lookup.h"
3334
#include "clang/Sema/ParsedAttr.h"
@@ -1066,6 +1067,9 @@ void SemaHLSL::ActOnFinishRootSignatureDecl(
10661067
SourceLocation Loc, IdentifierInfo *DeclIdent,
10671068
ArrayRef<hlsl::RootSignatureElement> RootElements) {
10681069

1070+
if (handleRootSignatureElements(RootElements, Loc))
1071+
return;
1072+
10691073
SmallVector<llvm::hlsl::rootsig::RootElement> Elements;
10701074
for (auto &RootSigElement : RootElements)
10711075
Elements.push_back(RootSigElement.getElement());
@@ -1074,15 +1078,12 @@ void SemaHLSL::ActOnFinishRootSignatureDecl(
10741078
SemaRef.getASTContext(), /*DeclContext=*/SemaRef.CurContext, Loc,
10751079
DeclIdent, SemaRef.getLangOpts().HLSLRootSigVer, Elements);
10761080

1077-
if (handleRootSignatureDecl(SignatureDecl, Loc))
1078-
return;
1079-
10801081
SignatureDecl->setImplicit();
10811082
SemaRef.PushOnScopeChains(SignatureDecl, SemaRef.getCurScope());
10821083
}
10831084

1084-
bool SemaHLSL::handleRootSignatureDecl(HLSLRootSignatureDecl *D,
1085-
SourceLocation Loc) {
1085+
bool SemaHLSL::handleRootSignatureElements(
1086+
ArrayRef<hlsl::RootSignatureElement> Elements, SourceLocation Loc) {
10861087
// The following conducts analysis on resource ranges to detect and report
10871088
// any overlaps in resource ranges.
10881089
//
@@ -1107,9 +1108,15 @@ bool SemaHLSL::handleRootSignatureDecl(HLSLRootSignatureDecl *D,
11071108
using ResourceRange = llvm::hlsl::rootsig::ResourceRange;
11081109
using GroupT = std::pair<ResourceClass, /*Space*/ uint32_t>;
11091110

1111+
// Introduce a mapping from the collected RangeInfos back to the
1112+
// RootSignatureElement that will retain its diagnostics info
1113+
llvm::DenseMap<size_t, const hlsl::RootSignatureElement *> InfoIndexMap;
1114+
size_t InfoIndex = 0;
1115+
11101116
// 1. Collect RangeInfos
11111117
llvm::SmallVector<RangeInfo> Infos;
1112-
for (const llvm::hlsl::rootsig::RootElement &Elem : D->getRootElements()) {
1118+
for (const hlsl::RootSignatureElement &RootSigElem : Elements) {
1119+
const llvm::hlsl::rootsig::RootElement &Elem = RootSigElem.getElement();
11131120
if (const auto *Descriptor =
11141121
std::get_if<llvm::hlsl::rootsig::RootDescriptor>(&Elem)) {
11151122
RangeInfo Info;
@@ -1120,6 +1127,9 @@ bool SemaHLSL::handleRootSignatureDecl(HLSLRootSignatureDecl *D,
11201127
llvm::dxil::ResourceClass(llvm::to_underlying(Descriptor->Type));
11211128
Info.Space = Descriptor->Space;
11221129
Info.Visibility = Descriptor->Visibility;
1130+
1131+
Info.Index = InfoIndex++;
1132+
InfoIndexMap[Info.Index] = &RootSigElem;
11231133
Infos.push_back(Info);
11241134
} else if (const auto *Constants =
11251135
std::get_if<llvm::hlsl::rootsig::RootConstants>(&Elem)) {
@@ -1130,6 +1140,9 @@ bool SemaHLSL::handleRootSignatureDecl(HLSLRootSignatureDecl *D,
11301140
Info.Class = llvm::dxil::ResourceClass::CBuffer;
11311141
Info.Space = Constants->Space;
11321142
Info.Visibility = Constants->Visibility;
1143+
1144+
Info.Index = InfoIndex++;
1145+
InfoIndexMap[Info.Index] = &RootSigElem;
11331146
Infos.push_back(Info);
11341147
} else if (const auto *Sampler =
11351148
std::get_if<llvm::hlsl::rootsig::StaticSampler>(&Elem)) {
@@ -1140,6 +1153,9 @@ bool SemaHLSL::handleRootSignatureDecl(HLSLRootSignatureDecl *D,
11401153
Info.Class = llvm::dxil::ResourceClass::Sampler;
11411154
Info.Space = Sampler->Space;
11421155
Info.Visibility = Sampler->Visibility;
1156+
1157+
Info.Index = InfoIndex++;
1158+
InfoIndexMap[Info.Index] = &RootSigElem;
11431159
Infos.push_back(Info);
11441160
} else if (const auto *Clause =
11451161
std::get_if<llvm::hlsl::rootsig::DescriptorTableClause>(
@@ -1154,7 +1170,10 @@ bool SemaHLSL::handleRootSignatureDecl(HLSLRootSignatureDecl *D,
11541170

11551171
Info.Class = Clause->Type;
11561172
Info.Space = Clause->Space;
1173+
11571174
// Note: Clause does not hold the visibility this will need to
1175+
Info.Index = InfoIndex++;
1176+
InfoIndexMap[Info.Index] = &RootSigElem;
11581177
Infos.push_back(Info);
11591178
} else if (const auto *Table =
11601179
std::get_if<llvm::hlsl::rootsig::DescriptorTable>(&Elem)) {
@@ -1201,13 +1220,15 @@ bool SemaHLSL::handleRootSignatureDecl(HLSLRootSignatureDecl *D,
12011220
};
12021221

12031222
// Helper to report diagnostics
1204-
auto ReportOverlap = [this, Loc, &HadOverlap](const RangeInfo *Info,
1223+
auto ReportOverlap = [this, InfoIndexMap, &HadOverlap](const RangeInfo *Info,
12051224
const RangeInfo *OInfo) {
12061225
HadOverlap = true;
12071226
auto CommonVis = Info->Visibility == llvm::dxbc::ShaderVisibility::All
12081227
? OInfo->Visibility
12091228
: Info->Visibility;
1210-
this->Diag(Loc, diag::err_hlsl_resource_range_overlap)
1229+
const hlsl::RootSignatureElement *Elem = InfoIndexMap.at(Info->Index);
1230+
SourceLocation InfoLoc = Elem->getLocation();
1231+
this->Diag(InfoLoc, diag::err_hlsl_resource_range_overlap)
12111232
<< llvm::to_underlying(Info->Class) << Info->LowerBound
12121233
<< /*unbounded=*/(Info->UpperBound == RangeInfo::Unbounded)
12131234
<< Info->UpperBound << llvm::to_underlying(OInfo->Class)

llvm/include/llvm/Frontend/HLSL/RootSignatureValidations.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ struct RangeInfo {
3232
llvm::dxil::ResourceClass Class;
3333
uint32_t Space;
3434
llvm::dxbc::ShaderVisibility Visibility;
35+
36+
// The index retains its original position before being sorted by group.
37+
size_t Index;
3538
};
3639

3740
class ResourceRange {

0 commit comments

Comments
 (0)