Skip to content

Commit 65082f6

Browse files
committed
update resource range analysis to use retained source loc
1 parent 1708789 commit 65082f6

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,21 +1078,24 @@ 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
using RangeInfo = llvm::hlsl::rootsig::RangeInfo;
10871088
using OverlappingRanges = llvm::hlsl::rootsig::OverlappingRanges;
10881089

1090+
// Introduce a mapping from the collected RangeInfos back to the
1091+
// RootSignatureElement that will retain its diagnostics info
1092+
llvm::DenseMap<size_t, const hlsl::RootSignatureElement *> InfoIndexMap;
1093+
size_t InfoIndex = 0;
1094+
10891095
// 1. Collect RangeInfos
10901096
llvm::SmallVector<RangeInfo> Infos;
1091-
for (const llvm::hlsl::rootsig::RootElement &Elem : D->getRootElements()) {
1097+
for (const hlsl::RootSignatureElement &RootSigElem : Elements) {
1098+
const llvm::hlsl::rootsig::RootElement &Elem = RootSigElem.getElement();
10921099
if (const auto *Descriptor =
10931100
std::get_if<llvm::hlsl::rootsig::RootDescriptor>(&Elem)) {
10941101
RangeInfo Info;
@@ -1099,6 +1106,9 @@ bool SemaHLSL::handleRootSignatureDecl(HLSLRootSignatureDecl *D,
10991106
llvm::dxil::ResourceClass(llvm::to_underlying(Descriptor->Type));
11001107
Info.Space = Descriptor->Space;
11011108
Info.Visibility = Descriptor->Visibility;
1109+
1110+
Info.Index = InfoIndex++;
1111+
InfoIndexMap[Info.Index] = &RootSigElem;
11021112
Infos.push_back(Info);
11031113
} else if (const auto *Constants =
11041114
std::get_if<llvm::hlsl::rootsig::RootConstants>(&Elem)) {
@@ -1109,6 +1119,9 @@ bool SemaHLSL::handleRootSignatureDecl(HLSLRootSignatureDecl *D,
11091119
Info.Class = llvm::dxil::ResourceClass::CBuffer;
11101120
Info.Space = Constants->Space;
11111121
Info.Visibility = Constants->Visibility;
1122+
1123+
Info.Index = InfoIndex++;
1124+
InfoIndexMap[Info.Index] = &RootSigElem;
11121125
Infos.push_back(Info);
11131126
} else if (const auto *Sampler =
11141127
std::get_if<llvm::hlsl::rootsig::StaticSampler>(&Elem)) {
@@ -1119,6 +1132,9 @@ bool SemaHLSL::handleRootSignatureDecl(HLSLRootSignatureDecl *D,
11191132
Info.Class = llvm::dxil::ResourceClass::Sampler;
11201133
Info.Space = Sampler->Space;
11211134
Info.Visibility = Sampler->Visibility;
1135+
1136+
Info.Index = InfoIndex++;
1137+
InfoIndexMap[Info.Index] = &RootSigElem;
11221138
Infos.push_back(Info);
11231139
} else if (const auto *Clause =
11241140
std::get_if<llvm::hlsl::rootsig::DescriptorTableClause>(
@@ -1133,7 +1149,10 @@ bool SemaHLSL::handleRootSignatureDecl(HLSLRootSignatureDecl *D,
11331149

11341150
Info.Class = Clause->Type;
11351151
Info.Space = Clause->Space;
1152+
11361153
// Note: Clause does not hold the visibility this will need to
1154+
Info.Index = InfoIndex++;
1155+
InfoIndexMap[Info.Index] = &RootSigElem;
11371156
Infos.push_back(Info);
11381157
} else if (const auto *Table =
11391158
std::get_if<llvm::hlsl::rootsig::DescriptorTable>(&Elem)) {
@@ -1150,13 +1169,15 @@ bool SemaHLSL::handleRootSignatureDecl(HLSLRootSignatureDecl *D,
11501169
}
11511170

11521171
// Helper to report diagnostics
1153-
auto ReportOverlap = [this, Loc](OverlappingRanges Overlap) {
1172+
auto ReportOverlap = [this, &InfoIndexMap](OverlappingRanges Overlap) {
11541173
const RangeInfo *Info = Overlap.A;
11551174
const RangeInfo *OInfo = Overlap.B;
11561175
auto CommonVis = Info->Visibility == llvm::dxbc::ShaderVisibility::All
11571176
? OInfo->Visibility
11581177
: Info->Visibility;
1159-
this->Diag(Loc, diag::err_hlsl_resource_range_overlap)
1178+
const hlsl::RootSignatureElement *Elem = InfoIndexMap.at(Info->Index);
1179+
SourceLocation InfoLoc = Elem->getLocation();
1180+
this->Diag(InfoLoc, diag::err_hlsl_resource_range_overlap)
11601181
<< llvm::to_underlying(Info->Class) << Info->LowerBound
11611182
<< /*unbounded=*/(Info->UpperBound == RangeInfo::Unbounded)
11621183
<< 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
@@ -50,6 +50,9 @@ struct RangeInfo {
5050
llvm::dxil::ResourceClass Class;
5151
uint32_t Space;
5252
llvm::dxbc::ShaderVisibility Visibility;
53+
54+
// The index retains its original position before being sorted by group.
55+
size_t Index;
5356
};
5457

5558
class ResourceRange {

0 commit comments

Comments
 (0)