Skip to content

Commit

Permalink
[CIR][CIRGen][NFC] Support yielding values in LexicalScope
Browse files Browse the repository at this point in the history
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: #312
  • Loading branch information
sitio-couto authored and lanza committed Nov 2, 2024
1 parent 0c74e35 commit 5c21aad
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 4 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<YieldOp>(localScope->EndLoc);
if (!localScope->isTernary()) {
!retVal ? builder.create<YieldOp>(localScope->EndLoc)
: builder.create<YieldOp>(localScope->EndLoc, retVal);
}
} else
(void)buildReturn(localScope->EndLoc);
};
Expand Down
5 changes: 5 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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; }

Expand Down

0 comments on commit 5c21aad

Please sign in to comment.