Skip to content

Commit 884a9bc

Browse files
authored
Handling of switch with empty body (shader-slang#1284)
* Added handling for empty switch body. Added test for empty switch. * Fix testing for case in switch.
1 parent cc4c5b8 commit 884a9bc

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

source/slang/slang-lower-to-ir.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -3592,6 +3592,41 @@ struct StmtLoweringVisitor : StmtVisitor<StmtLoweringVisitor>
35923592
return newCaseLabel;
35933593
}
35943594

3595+
bool hasSwitchCases(Stmt* inStmt)
3596+
{
3597+
Stmt* stmt = inStmt;
3598+
// Unwrap any surrounding `{ ... }` so we can look
3599+
// at the statement inside.
3600+
while (auto blockStmt = as<BlockStmt>(stmt))
3601+
{
3602+
stmt = blockStmt->body;
3603+
continue;
3604+
}
3605+
3606+
if (auto seqStmt = as<SeqStmt>(stmt))
3607+
{
3608+
// Walk through the children looking for cases
3609+
for (auto childStmt : seqStmt->stmts)
3610+
{
3611+
if (hasSwitchCases(childStmt))
3612+
{
3613+
return true;
3614+
}
3615+
}
3616+
}
3617+
else if (auto caseStmt = as<CaseStmt>(stmt))
3618+
{
3619+
return true;
3620+
}
3621+
else if (auto defaultStmt = as<DefaultStmt>(stmt))
3622+
{
3623+
// A 'default:' is a kind of case.
3624+
return true;
3625+
}
3626+
3627+
return false;
3628+
}
3629+
35953630
// Given a statement that appears as (or in) the body
35963631
// of a `switch` statement
35973632
void lowerSwitchCases(Stmt* inStmt, SwitchStmtInfo* info)
@@ -3734,6 +3769,14 @@ struct StmtLoweringVisitor : StmtVisitor<StmtLoweringVisitor>
37343769
// First emit code to compute the condition:
37353770
auto conditionVal = getSimpleVal(context, lowerRValueExpr(context, stmt->condition));
37363771

3772+
// Check for any cases or default.
3773+
if (!hasSwitchCases(stmt->body))
3774+
{
3775+
// If we don't have any case/default then nothing inside switch can be executed (other than condition)
3776+
// so we are done.
3777+
return;
3778+
}
3779+
37373780
// Remember the initial block so that we can add to it
37383781
// after we've collected all the `case`s
37393782
auto initialBlock = builder->getBlock();

tests/bugs/empty-switch.slang

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute
2+
//TEST(compute,vulkan):COMPARE_COMPUTE_EX:-vk -slang -compute
3+
//TEST(compute):COMPARE_COMPUTE_EX:-cpu -slang -compute
4+
//TEST(compute):COMPARE_COMPUTE_EX:-cuda -slang -compute
5+
6+
//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out, name outputBuffer
7+
RWStructuredBuffer<int> outputBuffer;
8+
9+
[numthreads(4, 1, 1)]
10+
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
11+
{
12+
int index = int(dispatchThreadID.x);
13+
14+
int a = index;
15+
16+
// This is kind of silly - but it is a valid construct.
17+
// We want to check condition expression is executed though
18+
switch (++a)
19+
{
20+
}
21+
22+
switch (index)
23+
{
24+
// This should not be executed
25+
a += 10;
26+
}
27+
28+
outputBuffer[index] = a;
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1
2+
2
3+
3
4+
4

0 commit comments

Comments
 (0)