[clang] [CIR] Add implicit return zero handling (PR #194490)
Andy Kaylor via cfe-commits
cfe-commits at lists.llvm.org
Mon Apr 27 17:03:48 PDT 2026
================
@@ -514,6 +514,21 @@ void CIRGenFunction::startFunction(GlobalDecl gd, QualType returnType,
}
emitAndUpdateRetAlloca(returnType, getLoc(bodyEndLoc),
getContext().getTypeAlignInChars(returnType));
+
+ // If this is an implicit-return-zero function, initialize the return
+ // value. This mirrors the implicit-return-zero handling in classic
+ // codegen's EmitFunctionProlog (CGCall.cpp). It is done here, after
+ // emitAndUpdateRetAlloca, because in CIR the return slot is created
+ // after the prolog (the opposite of classic codegen, where ReturnValue
+ // is set up before EmitFunctionProlog runs).
+ // TODO(cir): Align prolog handling with classic codegen.
+ if (fd && fd->hasImplicitReturnZero()) {
+ mlir::Type cirRetTy = convertType(returnType.getUnqualifiedType());
+ mlir::Location bodyBeginMLIRLoc = getLoc(bodyBeginLoc);
----------------
andykaylor wrote:
This location more-or-less follows classic codegen, which emits this store in `EmitFunctionProlog` (see the comment above for why I didn't put it there in CIR -- basically it would have broken about 40 tests and it seemed like such refactoring probably deserved deeper thought).
There's code elsewhere (`emitImplicitReturn->emitReturn`) that loads from the `fnRetAlloca` when we fall off end of the function. This is just initializing that location in case nothing else has written to it. Here's an example showing this in classic codegen with a non-empty body: https://godbolt.org/z/eKrzoj9h1
I'll add a test case here showing the non-empty body handling.
https://github.com/llvm/llvm-project/pull/194490
More information about the cfe-commits
mailing list