Skip to content

Commit 5c09841

Browse files
committed
Add a diagnostic check to warn when special types leak from param groups
As part of type legalization, certain special types may be removed from parameter group types. For example, in the following structure: [vk::push_constants] cbuffer PushConstants { int i; RaytracingAccelerationStructure rtas; } `rtas` cannot be included in this structure, and will leak out to a different binding. Warn the user when this happens. Fixes #8818
1 parent 562fa67 commit 5c09841

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

source/slang/slang-diagnostic-defs.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,17 @@ DIAGNOSTIC(
11781178
imageFormatUnsupportedByBackend,
11791179
"Image format '$0' is not explicitly supported by the $1 backend, using supported format '$2' "
11801180
"instead.")
1181+
DIAGNOSTIC(
1182+
31106,
1183+
Warning,
1184+
specialTypeLeaksFromParameterGroup,
1185+
"Parameter group type includes some members with types which cannot be included in the same "
1186+
"binding. These types will be moved into another parameter binding slot.")
1187+
DIAGNOSTIC(
1188+
31107,
1189+
Warning,
1190+
specialTypeLeaksFromParameterGroupElement,
1191+
"Structure member will leak into separate binding.")
11811192

11821193
DIAGNOSTIC(31120, Error, invalidAttributeTarget, "invalid syntax target for user defined attribute")
11831194
DIAGNOSTIC(

source/slang/slang-legalize-types.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,25 @@ LegalType legalizeTypeImpl(TypeLegalizationContext* context, IRType* type)
12251225
else
12261226
{
12271227
legalElementType = legalizeType(context, originalElementType);
1228+
1229+
// When special types leak out of a parameter group, they need to
1230+
// be bound differently. Warn the user when this happens.
1231+
if (legalElementType.flavor == LegalType::Flavor::pair)
1232+
{
1233+
context->m_sink->diagnose(
1234+
findFirstUseLoc(type),
1235+
Diagnostics::specialTypeLeaksFromParameterGroup);
1236+
1237+
// indicate which elements cannot be part of the parameter group
1238+
for (auto specialElement :
1239+
legalElementType.getPair()->specialType.getTuple()->elements)
1240+
{
1241+
context->m_sink->diagnose(
1242+
specialElement.key,
1243+
Diagnostics::specialTypeLeaksFromParameterGroupElement);
1244+
}
1245+
}
1246+
12281247
// As a bit of a corner case, if the user requested something
12291248
// like `ConstantBuffer<Texture2D>` the element type would
12301249
// legalize to a "simple" type, and that would be interpreted
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//DIAGNOSTIC_TEST:SIMPLE: -entry computeMain -stage compute -target spirv -preserve-params
2+
3+
// RTAS can't be a part of this binding, and will leak to a different binding.
4+
[vk::push_constant]
5+
cbuffer PushConstants {
6+
int i;
7+
RaytracingAccelerationStructure rtas;
8+
};
9+
10+
[numthreads(1,1,1)]
11+
void computeMain()
12+
{
13+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
result code = 0
2+
standard error = {
3+
shader.slang(5): warning 31106: Parameter group type includes some members with types which cannot be included in the same binding. These types will be moved into another parameter binding slot.
4+
cbuffer PushConstants {
5+
^~~~~~~~~~~~~
6+
shader.slang(7): warning 31107: Structure member will leak into separate binding.
7+
RaytracingAccelerationStructure rtas;
8+
^~~~
9+
}
10+
standard output = {
11+
; SPIR-V
12+
; Version: 1.5
13+
; Generator: Khronos Slang Compiler; 0
14+
; Bound: 12
15+
; Schema: 0
16+
OpCapability RayTracingKHR
17+
OpCapability Shader
18+
OpExtension "SPV_KHR_ray_tracing"
19+
OpMemoryModel Logical GLSL450
20+
OpEntryPoint GLCompute %computeMain "main"
21+
OpExecutionMode %computeMain LocalSize 1 1 1
22+
23+
; Debug Information
24+
OpSource Slang 1
25+
OpName %computeMain "computeMain" ; id %9
26+
27+
; Types, variables and constants
28+
%void = OpTypeVoid
29+
%10 = OpTypeFunction %void
30+
31+
; Function computeMain
32+
%computeMain = OpFunction %void None %10
33+
%11 = OpLabel
34+
OpReturn
35+
OpFunctionEnd
36+
}

0 commit comments

Comments
 (0)