[SPIRV] Fix crash when returning void (#4576)

* [SPIRV] Fix crash when returning void

This fixes a bug where invalid SPIR-V would be emitted when returning a
void value from a void function. According to the spec, the `Value` of
`OpReturnValue` must not have type `OpTypeVoid`; therefore, `OpReturn`
will now be emitted instead.

Fixes #3596

* assert instead of branch
This commit is contained in:
Cassandra Beckley 2022-08-08 05:38:46 -07:00 коммит произвёл GitHub
Родитель 7a45cbbf8b
Коммит 9ce4a5317c
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 14 добавлений и 1 удалений

Просмотреть файл

@ -2273,7 +2273,10 @@ void SpirvEmitter::doIfStmt(const IfStmt *ifStmt,
}
void SpirvEmitter::doReturnStmt(const ReturnStmt *stmt) {
if (const auto *retVal = stmt->getRetValue()) {
const auto *retVal = stmt->getRetValue();
bool returnsVoid = curFunction->getReturnType().getTypePtr()->isVoidType();
if (!returnsVoid) {
assert(retVal);
// Update counter variable associated with function returns
tryToAssignCounterVar(curFunction, retVal);

Просмотреть файл

@ -0,0 +1,9 @@
// RUN: %dxc -T ps_6_0 -E main
void A() {
}
void main() {
// CHECK: OpReturn
return A();
}

Просмотреть файл

@ -582,6 +582,7 @@ TEST_F(FileTest, ReturnFromDifferentStorageClass) {
TEST_F(FileTest, ReturnFromDifferentMemoryLayout) {
runFileTest("cf.return.memory-layout.hlsl");
}
TEST_F(FileTest, VoidReturn) { runFileTest("cf.return.void.hlsl"); }
// For control flows
TEST_F(FileTest, ControlFlowNestedIfForStmt) { runFileTest("cf.if.for.hlsl"); }