diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2020-03-20 16:59:15 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-20 16:59:15 -0400 |
| commit | 884a9bcafc5fb9ae47245fa3ea9a6e64cb65a482 (patch) | |
| tree | 6cd0de692b9e90bbdaef645ac2349a977740952f | |
| parent | cc4c5b8dbbac648a7e914ff46f79293c1e122570 (diff) | |
Handling of switch with empty body (#1284)
* Added handling for empty switch body.
Added test for empty switch.
* Fix testing for case in switch.
| -rw-r--r-- | source/slang/slang-lower-to-ir.cpp | 43 | ||||
| -rw-r--r-- | tests/bugs/empty-switch.slang | 29 | ||||
| -rw-r--r-- | tests/bugs/empty-switch.slang.expected.txt | 4 |
3 files changed, 76 insertions, 0 deletions
diff --git a/source/slang/slang-lower-to-ir.cpp b/source/slang/slang-lower-to-ir.cpp index 029fe23f6..16dc14819 100644 --- a/source/slang/slang-lower-to-ir.cpp +++ b/source/slang/slang-lower-to-ir.cpp @@ -3592,6 +3592,41 @@ struct StmtLoweringVisitor : StmtVisitor<StmtLoweringVisitor> return newCaseLabel; } + bool hasSwitchCases(Stmt* inStmt) + { + Stmt* stmt = inStmt; + // Unwrap any surrounding `{ ... }` so we can look + // at the statement inside. + while (auto blockStmt = as<BlockStmt>(stmt)) + { + stmt = blockStmt->body; + continue; + } + + if (auto seqStmt = as<SeqStmt>(stmt)) + { + // Walk through the children looking for cases + for (auto childStmt : seqStmt->stmts) + { + if (hasSwitchCases(childStmt)) + { + return true; + } + } + } + else if (auto caseStmt = as<CaseStmt>(stmt)) + { + return true; + } + else if (auto defaultStmt = as<DefaultStmt>(stmt)) + { + // A 'default:' is a kind of case. + return true; + } + + return false; + } + // Given a statement that appears as (or in) the body // of a `switch` statement void lowerSwitchCases(Stmt* inStmt, SwitchStmtInfo* info) @@ -3734,6 +3769,14 @@ struct StmtLoweringVisitor : StmtVisitor<StmtLoweringVisitor> // First emit code to compute the condition: auto conditionVal = getSimpleVal(context, lowerRValueExpr(context, stmt->condition)); + // Check for any cases or default. + if (!hasSwitchCases(stmt->body)) + { + // If we don't have any case/default then nothing inside switch can be executed (other than condition) + // so we are done. + return; + } + // Remember the initial block so that we can add to it // after we've collected all the `case`s auto initialBlock = builder->getBlock(); diff --git a/tests/bugs/empty-switch.slang b/tests/bugs/empty-switch.slang new file mode 100644 index 000000000..104c63367 --- /dev/null +++ b/tests/bugs/empty-switch.slang @@ -0,0 +1,29 @@ +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute +//TEST(compute,vulkan):COMPARE_COMPUTE_EX:-vk -slang -compute +//TEST(compute):COMPARE_COMPUTE_EX:-cpu -slang -compute +//TEST(compute):COMPARE_COMPUTE_EX:-cuda -slang -compute + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out, name outputBuffer +RWStructuredBuffer<int> outputBuffer; + +[numthreads(4, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + int index = int(dispatchThreadID.x); + + int a = index; + + // This is kind of silly - but it is a valid construct. + // We want to check condition expression is executed though + switch (++a) + { + } + + switch (index) + { + // This should not be executed + a += 10; + } + + outputBuffer[index] = a; +}
\ No newline at end of file diff --git a/tests/bugs/empty-switch.slang.expected.txt b/tests/bugs/empty-switch.slang.expected.txt new file mode 100644 index 000000000..94ebaf900 --- /dev/null +++ b/tests/bugs/empty-switch.slang.expected.txt @@ -0,0 +1,4 @@ +1 +2 +3 +4 |
