diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp index 6d0cacffa753..696c6c877dd9 100644 --- a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp @@ -381,8 +381,10 @@ void CIRGenFunction::LexicalScope::cleanup() { if (localScope->Depth != 0) { // end of any local scope != function // Ternary ops have to deal with matching arms for yielding types // and do return a value, it must do its own cir.yield insertion. - if (!localScope->isTernary()) - builder.create(localScope->EndLoc); + if (!localScope->isTernary()) { + !retVal ? builder.create(localScope->EndLoc) + : builder.create(localScope->EndLoc, retVal); + } } else (void)buildReturn(localScope->EndLoc); }; diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.h b/clang/lib/CIR/CodeGen/CIRGenFunction.h index 024ec494bc5b..9e2d1687366f 100644 --- a/clang/lib/CIR/CodeGen/CIRGenFunction.h +++ b/clang/lib/CIR/CodeGen/CIRGenFunction.h @@ -1703,6 +1703,9 @@ class CIRGenFunction : public CIRGenTypeCache { Switch // cir.switch } ScopeKind = Regular; + // Track scope return value. + mlir::Value retVal = nullptr; + public: unsigned Depth = 0; bool HasReturn = false; @@ -1725,6 +1728,8 @@ class CIRGenFunction : public CIRGenTypeCache { assert(EntryBlock && "expected valid block"); } + void setRetVal(mlir::Value v) { retVal = v; } + void cleanup(); void restore() { CGF.currLexScope = ParentScope; }