Generate correctly structured do-while loops.

The loop test is always emitted before the loop body.

For do-while loops, use a phi node to track whether we're
on the first loop iteration, and only check the loop test
on the second and subsequent iterations.

For do-while loops, the loop test branch no longer occurs
at the top of the loop, so it must get its own selection
merge instruction.

A block can't be the target of more than one merge instruction.
So when the loop test executes after the body (as in do-while in GLSL)
we need to introduce a dummy block to be the target of the selection
merge just before the loop test conditional branch.

The other arm of the branch exits the loop and hence is the
"break block" exception in the structured control flow rules.
This commit is contained in:
David Neto
2015-07-15 16:21:26 -04:00
parent 51b31b5785
commit c22f37cfb4
8 changed files with 1486 additions and 1373 deletions

View File

@@ -1147,28 +1147,18 @@ bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIn
// body emission needs to know what the for-loop terminal is when it sees a "continue"
loopTerminal.push(node->getTerminal());
builder.makeNewLoop();
bool bodyOut = false;
if (! node->testFirst()) {
builder.endLoopHeaderWithoutTest();
if (node->getBody()) {
breakForLoop.push(true);
node->getBody()->traverse(this);
breakForLoop.pop();
}
bodyOut = true;
builder.createBranchToLoopTest();
}
builder.makeNewLoop(node->testFirst());
if (node->getTest()) {
node->getTest()->traverse(this);
// the AST only contained the test computation, not the branch, we have to add it
spv::Id condition = builder.accessChainLoad(TranslatePrecisionDecoration(node->getTest()->getType()));
builder.createLoopTestBranch(condition);
} else {
builder.createBranchToBody();
}
if (! bodyOut && node->getBody()) {
if (node->getBody()) {
breakForLoop.push(true);
node->getBody()->traverse(this);
breakForLoop.pop();