Skip to content

Commit

Permalink
Return insertion for void functions without explicit return
Browse files Browse the repository at this point in the history
  • Loading branch information
wpmed92 committed Aug 23, 2024
1 parent a994000 commit fb6401f
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 9 deletions.
1 change: 0 additions & 1 deletion example/passthrough_fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ layout(location = 0) out vec4 outColor;

void main() {
outColor = vec4(1.0, 0.0, 0.0, 1.0);
return;
}
13 changes: 13 additions & 0 deletions lib/CodeGen/MLIRCodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,19 @@ void MLIRCodeGen::visit(FunctionDeclaration *funcDecl) {

funcDecl->getBody()->accept(this);

// Return insertion for void functions
if (funcDecl->getReturnType()->getKind() == TypeKind::Void) {
if (auto stmts = dynamic_cast<StatementList*>(funcDecl->getBody())) {
auto &lastStmt = stmts->getStatements().back();

if (!dynamic_cast<ReturnStatement*>(lastStmt.get())) {
builder.create<spirv::ReturnOp>(builder.getUnknownLoc());
}
} else if (dynamic_cast<Statement*>(funcDecl->getBody()) && !dynamic_cast<ReturnStatement*>(funcDecl->getBody())) {
builder.create<spirv::ReturnOp>(builder.getUnknownLoc());
}
}

inGlobalScope = true;

functionMap.insert({funcDecl->getName(), funcOp});
Expand Down
2 changes: 0 additions & 2 deletions test/Analysis/vardecl.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,4 @@ int myFunc() {

// error: boolean expression expected
e = d ? 1 : 2;

return;
}
2 changes: 0 additions & 2 deletions test/CodeGen/arrays.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,4 @@ void main() {
// CHECK-NEXT: %cst_f32_16 = spirv.Constant 1.000000e+00 : f32
// CHECK-NEXT: spirv.Store "Function" %22, %cst_f32_16 : f32
multiArr[0][1] = 1.0;

return;
}
2 changes: 0 additions & 2 deletions test/CodeGen/binary_expressions.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@ void main() {

// CHECK: %6 = spirv.FAdd %cst_f64, %cst_f64_1 : f64
double d = 1.0lf + 2.0lf;

return;
}
2 changes: 0 additions & 2 deletions test/CodeGen/structs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,4 @@ void main() {
// CHECK-NEXT: %13 = spirv.Variable : !spirv.ptr<i1, Function>
// CHECK-NEXT: spirv.Store "Function" %13, %12 : i1
bool d = myStruct.d;

return;
}

0 comments on commit fb6401f

Please sign in to comment.