From 5c21aad4ee8d0b1a7b087a245cc7bc0a2f6766bd Mon Sep 17 00:00:00 2001 From: Vinicius Couto Espindola Date: Wed, 10 Jan 2024 20:08:12 -0300 Subject: [PATCH] [CIR][CIRGen][NFC] Support yielding values in LexicalScope Once the LexicalScope goes out of scope, its cleanup process will also check if a return was set to be yielded, and, if so, generate the yield with the respective value. ghstack-source-id: 9305d2ba5631840937721755358a774dc9e08b90 Pull Request resolved: https://github.com/llvm/clangir/pull/312 --- clang/lib/CIR/CodeGen/CIRGenFunction.cpp | 6 ++++-- clang/lib/CIR/CodeGen/CIRGenFunction.h | 5 +++++ 2 files changed, 9 insertions(+), 2 deletions(-) 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; }