Skip to content

Commit 6a8899c

Browse files
authored
[HLSL][RootSignature] Enable resource range analysis for remaining RootElements (#145109)
As implemented previously #140962. We now have a validation pass to ensure that there is no overlap in the register ranges of the associated resources. However, in the previous pr, for the sake of brevity, we only "collected `RangeInfo`" for Root Descriptors. This means the analysis is not run on any other `RootElement` type. This pr simply implements the collection of `RangeInfo` for the remaining types so that the analysis is run account for all `RootElement` types. Additionally, we improve the diagnostics message to display `unbounded` ranges. Part 3 of and Resolves #129942.
1 parent 559218f commit 6a8899c

File tree

5 files changed

+108
-5
lines changed

5 files changed

+108
-5
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13054,10 +13054,13 @@ def err_invalid_hlsl_resource_type: Error<
1305413054
def err_hlsl_spirv_only: Error<"%0 is only available for the SPIR-V target">;
1305513055
def err_hlsl_vk_literal_must_contain_constant: Error<"the argument to vk::Literal must be a vk::integral_constant">;
1305613056

13057+
def subst_hlsl_format_ranges: TextSubstitution<
13058+
"%select{t|u|b|s}0[%1;%select{%3]|unbounded)}2">;
13059+
1305713060
def err_hlsl_resource_range_overlap: Error<
13058-
"resource ranges %select{t|u|b|s}0[%1;%2] and %select{t|u|b|s}3[%4;%5] "
13059-
"overlap within space = %6 and visibility = "
13060-
"%select{All|Vertex|Hull|Domain|Geometry|Pixel|Amplification|Mesh}7">;
13061+
"resource ranges %sub{subst_hlsl_format_ranges}0,1,2,3 and %sub{subst_hlsl_format_ranges}4,5,6,7 "
13062+
"overlap within space = %8 and visibility = "
13063+
"%select{All|Vertex|Hull|Domain|Geometry|Pixel|Amplification|Mesh}9">;
1306113064

1306213065
// Layout randomization diagnostics.
1306313066
def err_non_designated_init_used : Error<

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,52 @@ bool SemaHLSL::handleRootSignatureDecl(HLSLRootSignatureDecl *D,
11161116
Info.Space = Descriptor->Space;
11171117
Info.Visibility = Descriptor->Visibility;
11181118
Infos.push_back(Info);
1119+
} else if (const auto *Constants =
1120+
std::get_if<llvm::hlsl::rootsig::RootConstants>(&Elem)) {
1121+
RangeInfo Info;
1122+
Info.LowerBound = Constants->Reg.Number;
1123+
Info.UpperBound = Info.LowerBound; // use inclusive ranges []
1124+
1125+
Info.Class = llvm::dxil::ResourceClass::CBuffer;
1126+
Info.Space = Constants->Space;
1127+
Info.Visibility = Constants->Visibility;
1128+
Infos.push_back(Info);
1129+
} else if (const auto *Sampler =
1130+
std::get_if<llvm::hlsl::rootsig::StaticSampler>(&Elem)) {
1131+
RangeInfo Info;
1132+
Info.LowerBound = Sampler->Reg.Number;
1133+
Info.UpperBound = Info.LowerBound; // use inclusive ranges []
1134+
1135+
Info.Class = llvm::dxil::ResourceClass::Sampler;
1136+
Info.Space = Sampler->Space;
1137+
Info.Visibility = Sampler->Visibility;
1138+
Infos.push_back(Info);
1139+
} else if (const auto *Clause =
1140+
std::get_if<llvm::hlsl::rootsig::DescriptorTableClause>(
1141+
&Elem)) {
1142+
RangeInfo Info;
1143+
Info.LowerBound = Clause->Reg.Number;
1144+
assert(0 < Clause->NumDescriptors && "Verified as part of TODO(#129940)");
1145+
Info.UpperBound = Clause->NumDescriptors == RangeInfo::Unbounded
1146+
? RangeInfo::Unbounded
1147+
: Info.LowerBound + Clause->NumDescriptors -
1148+
1; // use inclusive ranges []
1149+
1150+
Info.Class = Clause->Type;
1151+
Info.Space = Clause->Space;
1152+
// Note: Clause does not hold the visibility this will need to
1153+
Infos.push_back(Info);
1154+
} else if (const auto *Table =
1155+
std::get_if<llvm::hlsl::rootsig::DescriptorTable>(&Elem)) {
1156+
// Table holds the Visibility of all owned Clauses in Table, so iterate
1157+
// owned Clauses and update their corresponding RangeInfo
1158+
assert(Table->NumClauses <= Infos.size() && "RootElement");
1159+
// The last Table->NumClauses elements of Infos are the owned Clauses
1160+
// generated RangeInfo
1161+
auto TableInfos =
1162+
MutableArrayRef<RangeInfo>(Infos).take_back(Table->NumClauses);
1163+
for (RangeInfo &Info : TableInfos)
1164+
Info.Visibility = Table->Visibility;
11191165
}
11201166
}
11211167

@@ -1159,8 +1205,11 @@ bool SemaHLSL::handleRootSignatureDecl(HLSLRootSignatureDecl *D,
11591205
: Info->Visibility;
11601206
this->Diag(Loc, diag::err_hlsl_resource_range_overlap)
11611207
<< llvm::to_underlying(Info->Class) << Info->LowerBound
1208+
<< /*unbounded=*/(Info->UpperBound == RangeInfo::Unbounded)
11621209
<< Info->UpperBound << llvm::to_underlying(OInfo->Class)
1163-
<< OInfo->LowerBound << OInfo->UpperBound << Info->Space << CommonVis;
1210+
<< OInfo->LowerBound
1211+
<< /*unbounded=*/(OInfo->UpperBound == RangeInfo::Unbounded)
1212+
<< OInfo->UpperBound << Info->Space << CommonVis;
11641213
};
11651214

11661215
// 3: Iterate through collected RangeInfos

clang/test/AST/HLSL/RootSignatures-AST.hlsl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ void same_rs_main() {}
9393
"addressU = TEXTURE_ADDRESS_CLAMP, " \
9494
"filter = FILTER_MIN_MAG_MIP_LINEAR )"
9595

96-
9796
// CHECK: -RootSignatureAttr 0x{{.*}} {{.*}} [[SAMPLE_RS_DECL]]
9897
[RootSignature(SampleSameRS)]
9998
void same_rs_string_main() {}

clang/test/SemaHLSL/RootSignature-resource-ranges-err.hlsl

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,46 @@ void bad_root_signature_3() {}
1919
// expected-error@+1 {{resource ranges u[0;0] and u[0;0] overlap within space = 0 and visibility = Pixel}}
2020
[RootSignature("UAV(u0, visibility = SHADER_VISIBILITY_PIXEL), UAV(u0, visibility = SHADER_VISIBILITY_ALL)")]
2121
void bad_root_signature_4() {}
22+
23+
// expected-error@+1 {{resource ranges b[0;0] and b[0;0] overlap within space = 0 and visibility = All}}
24+
[RootSignature("RootConstants(num32BitConstants=4, b0), RootConstants(num32BitConstants=2, b0)")]
25+
void bad_root_signature_5() {}
26+
27+
// expected-error@+1 {{resource ranges s[3;3] and s[3;3] overlap within space = 0 and visibility = All}}
28+
[RootSignature("StaticSampler(s3), StaticSampler(s3)")]
29+
void bad_root_signature_6() {}
30+
31+
// expected-error@+1 {{resource ranges t[2;5] and t[0;3] overlap within space = 0 and visibility = All}}
32+
[RootSignature("DescriptorTable(SRV(t0, numDescriptors=4), SRV(t2, numDescriptors=4))")]
33+
void bad_root_signature_7() {}
34+
35+
// expected-error@+1 {{resource ranges u[2;5] and u[0;unbounded) overlap within space = 0 and visibility = Hull}}
36+
[RootSignature("DescriptorTable(UAV(u0, numDescriptors=unbounded), visibility = SHADER_VISIBILITY_HULL), DescriptorTable(UAV(u2, numDescriptors=4))")]
37+
void bad_root_signature_8() {}
38+
39+
// expected-error@+1 {{resource ranges b[0;2] and b[2;2] overlap within space = 0 and visibility = All}}
40+
[RootSignature("RootConstants(num32BitConstants=4, b2), DescriptorTable(CBV(b0, numDescriptors=3))")]
41+
void bad_root_signature_9() {}
42+
43+
// expected-error@+1 {{resource ranges s[4;unbounded) and s[17;17] overlap within space = 0 and visibility = All}}
44+
[RootSignature("StaticSampler(s17), DescriptorTable(Sampler(s0, numDescriptors=3),Sampler(s4, numDescriptors=unbounded))")]
45+
void bad_root_signature_10() {}
46+
47+
// expected-error@+1 {{resource ranges b[45;45] and b[4;unbounded) overlap within space = 0 and visibility = Geometry}}
48+
[RootSignature("DescriptorTable(CBV(b4, numDescriptors=unbounded)), CBV(b45, visibility = SHADER_VISIBILITY_GEOMETRY)")]
49+
void bad_root_signature_11() {}
50+
51+
#define ReportFirstOverlap \
52+
"DescriptorTable( " \
53+
" CBV(b4, numDescriptors = 4), " \
54+
" CBV(b1, numDescriptors = 2), " \
55+
" CBV(b0, numDescriptors = 8), " \
56+
")"
57+
58+
// expected-error@+1 {{resource ranges b[0;7] and b[1;2] overlap within space = 0 and visibility = All}}
59+
[RootSignature(ReportFirstOverlap)]
60+
void bad_root_signature_12() {}
61+
62+
// expected-error@+1 {{resource ranges s[2;2] and s[2;2] overlap within space = 0 and visibility = Vertex}}
63+
[RootSignature("StaticSampler(s2, visibility=SHADER_VISIBILITY_ALL), DescriptorTable(Sampler(s2), visibility=SHADER_VISIBILITY_VERTEX)")]
64+
void valid_root_signature_13() {}

clang/test/SemaHLSL/RootSignature-resource-ranges.hlsl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,12 @@ void valid_root_signature_2() {}
1313

1414
[RootSignature("CBV(b0), SRV(t0)")]
1515
void valid_root_signature_3() {}
16+
17+
[RootSignature("RootConstants(num32BitConstants=4, b0, space=0), DescriptorTable(CBV(b0, space=1))")]
18+
void valid_root_signature_4() {}
19+
20+
[RootSignature("StaticSampler(s2, visibility=SHADER_VISIBILITY_PIXEL), DescriptorTable(Sampler(s2), visibility=SHADER_VISIBILITY_VERTEX)")]
21+
void valid_root_signature_5() {}
22+
23+
[RootSignature("DescriptorTable(SRV(t5), UAV(u5, numDescriptors=2))")]
24+
void valid_root_signature_6() {}

0 commit comments

Comments
 (0)