Skip to content

Commit 5c21aad

Browse files
sitio-coutolanza
authored andcommitted
[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: 9305d2b Pull Request resolved: #312
1 parent 0c74e35 commit 5c21aad

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

clang/lib/CIR/CodeGen/CIRGenFunction.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,10 @@ void CIRGenFunction::LexicalScope::cleanup() {
381381
if (localScope->Depth != 0) { // end of any local scope != function
382382
// Ternary ops have to deal with matching arms for yielding types
383383
// and do return a value, it must do its own cir.yield insertion.
384-
if (!localScope->isTernary())
385-
builder.create<YieldOp>(localScope->EndLoc);
384+
if (!localScope->isTernary()) {
385+
!retVal ? builder.create<YieldOp>(localScope->EndLoc)
386+
: builder.create<YieldOp>(localScope->EndLoc, retVal);
387+
}
386388
} else
387389
(void)buildReturn(localScope->EndLoc);
388390
};

clang/lib/CIR/CodeGen/CIRGenFunction.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,6 +1703,9 @@ class CIRGenFunction : public CIRGenTypeCache {
17031703
Switch // cir.switch
17041704
} ScopeKind = Regular;
17051705

1706+
// Track scope return value.
1707+
mlir::Value retVal = nullptr;
1708+
17061709
public:
17071710
unsigned Depth = 0;
17081711
bool HasReturn = false;
@@ -1725,6 +1728,8 @@ class CIRGenFunction : public CIRGenTypeCache {
17251728
assert(EntryBlock && "expected valid block");
17261729
}
17271730

1731+
void setRetVal(mlir::Value v) { retVal = v; }
1732+
17281733
void cleanup();
17291734
void restore() { CGF.currLexScope = ParentScope; }
17301735

0 commit comments

Comments
 (0)