SPV return from main: Simplify a legacy design such returns are not jumps to exit block.
Structured control-flow rules allow leaving the middle of a construct through
a return, but not through a jump to a block that does a return.
Addresses issue #58.
diff --git a/SPIRV/GlslangToSpv.cpp b/SPIRV/GlslangToSpv.cpp
index 0240dea..37ee52f 100755
--- a/SPIRV/GlslangToSpv.cpp
+++ b/SPIRV/GlslangToSpv.cpp
@@ -454,7 +454,7 @@
if (! mainTerminated) {
spv::Block* lastMainBlock = shaderEntry->getLastBlock();
builder.setBuildPoint(lastMainBlock);
- builder.leaveFunction(true);
+ builder.leaveFunction();
}
}
@@ -854,7 +854,7 @@
} else {
if (inMain)
mainTerminated = true;
- builder.leaveFunction(inMain);
+ builder.leaveFunction();
inMain = false;
}
@@ -1276,12 +1276,10 @@
builder.createLoopContinue();
break;
case glslang::EOpReturn:
- if (inMain)
- builder.makeMainReturn();
- else if (node->getExpression())
+ if (node->getExpression())
builder.makeReturn(false, builder.accessChainLoad(convertGlslangToSpvType(node->getExpression()->getType())));
else
- builder.makeReturn();
+ builder.makeReturn(false);
builder.clearAccessChain();
break;
diff --git a/SPIRV/SpvBuilder.cpp b/SPIRV/SpvBuilder.cpp
index 9913b0b..ba03314 100755
--- a/SPIRV/SpvBuilder.cpp
+++ b/SPIRV/SpvBuilder.cpp
@@ -65,8 +65,7 @@
builderNumber(userNumber << 16 | SpvBuilderMagic),
buildPoint(0),
uniqueId(0),
- mainFunction(0),
- stageExit(0)
+ mainFunction(0)
{
clearAccessChain();
}
@@ -723,20 +722,11 @@
std::vector<Id> params;
mainFunction = makeFunctionEntry(makeVoidType(), "main", params, &entry);
- stageExit = new Block(getUniqueId(), *mainFunction);
return mainFunction;
}
// Comments in header
-void Builder::closeMain()
-{
- setBuildPoint(stageExit);
- stageExit->addInstruction(new Instruction(NoResult, NoType, OpReturn));
- mainFunction->addBlock(stageExit);
-}
-
-// Comments in header
Function* Builder::makeFunctionEntry(Id returnType, const char* name, std::vector<Id>& paramTypes, Block **entry)
{
Id typeId = makeFunctionType(returnType, paramTypes);
@@ -756,14 +746,9 @@
}
// Comments in header
-void Builder::makeReturn(bool implicit, Id retVal, bool isMain)
+void Builder::makeReturn(bool implicit, Id retVal)
{
- if (isMain && retVal)
- MissingFunctionality("return value from main()");
-
- if (isMain)
- createBranch(stageExit);
- else if (retVal) {
+ if (retVal) {
Instruction* inst = new Instruction(NoResult, NoType, OpReturnValue);
inst->addIdOperand(retVal);
buildPoint->addInstruction(inst);
@@ -775,7 +760,7 @@
}
// Comments in header
-void Builder::leaveFunction(bool main)
+void Builder::leaveFunction()
{
Block* block = buildPoint;
Function& function = buildPoint->getParent();
@@ -791,10 +776,8 @@
// Given that this block is at the end of a function, it must be right after an
// explicit return, just remove it.
function.popBlock(block);
- } else if (main)
- makeMainReturn(true);
- else {
- // We're get a return instruction at the end of the current block,
+ } else {
+ // We'll add a return instruction at the end of the current block,
// which for a non-void function is really error recovery (?), as the source
// being translated should have had an explicit return, which would have been
// followed by an unreachable block, which was handled above.
@@ -805,9 +788,6 @@
}
}
}
-
- if (main)
- closeMain();
}
// Comments in header
diff --git a/SPIRV/SpvBuilder.h b/SPIRV/SpvBuilder.h
index f81f9ed..e483570 100755
--- a/SPIRV/SpvBuilder.h
+++ b/SPIRV/SpvBuilder.h
@@ -195,23 +195,16 @@
// Make the main function.
Function* makeMain();
- // Return from main. Implicit denotes a return at the very end of main.
- void makeMainReturn(bool implicit = false) { makeReturn(implicit, 0, true); }
-
- // Close the main function.
- void closeMain();
-
// Make a shader-style function, and create its entry block if entry is non-zero.
// Return the function, pass back the entry.
Function* makeFunctionEntry(Id returnType, const char* name, std::vector<Id>& paramTypes, Block **entry = 0);
- // Create a return. Pass whether it is a return form main, and the return
- // value (if applicable). In the case of an implicit return, no post-return
- // block is inserted.
- void makeReturn(bool implicit = false, Id retVal = 0, bool isMain = false);
+ // Create a return. An 'implicit' return is one not appearing in the source
+ // code. In the case of an implicit return, no post-return block is inserted.
+ void makeReturn(bool implicit, Id retVal = 0);
// Generate all the code needed to finish up a function.
- void leaveFunction(bool main);
+ void leaveFunction();
// Create a discard.
void makeDiscard();
@@ -516,7 +509,6 @@
Block* buildPoint;
Id uniqueId;
Function* mainFunction;
- Block* stageExit;
AccessChain accessChain;
// special blocks of instructions for output