34class ConditionalEvaluationFinder
36 bool foundConditional =
false;
39 bool found()
const {
return foundConditional; }
41 bool VisitAbstractConditionalOperator(AbstractConditionalOperator *) {
42 foundConditional =
true;
47 bool TraverseLambdaExpr(
LambdaExpr *) {
return true; }
48 bool TraverseBlockExpr(BlockExpr *) {
return true; }
49 bool TraverseStmtExpr(StmtExpr *) {
return true; }
65 mlir::Location loc = builder.getUnknownLoc();
78 mlir::OpBuilder::InsertionGuard guard(builder);
80 builder.createFlagStore(loc,
false, active.
getPointer());
84 builder.createFlagStore(loc,
true, active.
getPointer());
95 assert(!
cleanup.hasActiveFlag() &&
"cleanup already has active flag?");
96 cleanup.setActiveFlag(activeFlag);
104 : cgf(cgf), cleanups(cgf), scope(
nullptr),
107 assert(subExpr &&
"ExprWithCleanups always has a sub-expression");
108 ConditionalEvaluationFinder finder;
109 finder.TraverseStmt(
const_cast<Expr *
>(subExpr));
110 if (finder.found()) {
111 mlir::Location loc = cgf.builder.getUnknownLoc();
112 cir::CleanupKind cleanupKind = cgf.getLangOpts().Exceptions
113 ? cir::CleanupKind::All
114 : cir::CleanupKind::Normal;
115 scope = cir::CleanupScopeOp::create(
116 cgf.builder, loc, cleanupKind,
118 [&](mlir::OpBuilder &b, mlir::Location loc) {},
120 [&](mlir::OpBuilder &b, mlir::Location loc) {});
121 cgf.builder.setInsertionPointToEnd(&scope.getBodyRegion().front());
129 cir::CleanupScopeOp scope) {
136 auto cur = alloca->getParentOfType<cir::CleanupScopeOp>();
137 while (cur && cur != scope)
138 cur = cur->getParentOfType<cir::CleanupScopeOp>();
145 mlir::Block *parentBlock = scope->getBlock();
146 mlir::OpBuilder::InsertPoint ip =
148 alloca->moveBefore(parentBlock, ip.getPoint());
157 for (mlir::Operation *cur = ptr.getDefiningOp(); cur && cur != alloca;) {
158 auto cast = mlir::dyn_cast<cir::CastOp>(cur);
161 casts.push_back(
cast);
162 cur =
cast.getSrc().getDefiningOp();
165 mlir::Operation *prev = alloca;
166 for (cir::CastOp
cast : llvm::reverse(casts)) {
167 cast->moveAfter(prev);
174 assert(!exited &&
"FullExprCleanupScope::exit called twice");
177 size_t oldSize = deferredCleanupStackSize;
178 bool hasDeferredCleanups =
179 cgf.deferredConditionalCleanupStack.size() > oldSize;
182 cgf.deferredConditionalCleanupStack.truncate(oldSize);
183 cleanups.forceCleanup(valuesToReload);
189 for (mlir::Value *valPtr : valuesToReload) {
190 mlir::Value val = *valPtr;
195 Address temp = cgf.createDefaultAlignTempAlloca(val.getType(), val.getLoc(),
197 tempAllocas.push_back(temp);
198 cgf.builder.createStore(val.getLoc(), val, temp);
204 cleanups.forceCleanupExceptLifetimeExtended();
208 mlir::OpBuilder::InsertionGuard guard(cgf.builder);
209 mlir::Block &lastBodyBlock = scope.getBodyRegion().back();
210 cgf.builder.setInsertionPointToEnd(&lastBodyBlock);
211 if (lastBodyBlock.empty() ||
212 !lastBodyBlock.back().hasTrait<mlir::OpTrait::IsTerminator>())
213 cgf.builder.createYield(scope.getLoc());
220 if (hasDeferredCleanups) {
222 llvm::make_range(cgf.deferredConditionalCleanupStack.begin() + oldSize,
223 cgf.deferredConditionalCleanupStack.end())) {
230 mlir::OpBuilder::InsertionGuard guard(cgf.builder);
231 mlir::Block &cleanupBlock = scope.getCleanupRegion().front();
232 cgf.builder.setInsertionPointToEnd(&cleanupBlock);
234 if (hasDeferredCleanups) {
236 cgf.deferredConditionalCleanupStack.begin() + oldSize,
237 cgf.deferredConditionalCleanupStack.end()))) {
238 if (entry.activeFlag.isValid()) {
246 assert(entry.addr.getUnderlyingAllocaOp() &&
247 (entry.addr.getUnderlyingAllocaOp()->getBlock() ==
248 entry.addr.getPointer().getDefiningOp()->getBlock()) &&
249 "alloca and cast are in different blocks");
251 cgf.builder.createLoad(scope.getLoc(), entry.activeFlag);
253 cgf.builder, scope.getLoc(), flag,
false,
254 [&](mlir::OpBuilder &b, mlir::Location loc) {
255 cgf.emitDestroy(entry.addr, entry.type, entry.destroyer);
256 cgf.builder.createYield(loc);
259 cgf.emitDestroy(entry.addr, entry.type, entry.destroyer);
263 cgf.builder.createYield(scope.getLoc());
266 cgf.deferredConditionalCleanupStack.truncate(oldSize);
267 cgf.builder.setInsertionPointAfter(scope);
273 cleanups.forceLifetimeExtendedCleanups();
276 for (
auto [addr, valPtr] : llvm::zip(tempAllocas, valuesToReload)) {
279 *valPtr = cgf.builder.createLoad(valPtr->getLoc(), addr);
287void EHScopeStack::Cleanup::anchor() {}
291 stable_iterator si = getInnermostNormalCleanup();
292 stable_iterator se = stable_end();
297 si =
cleanup.getEnclosingNormalCleanup();
303char *EHScopeStack::allocate(
size_t size) {
304 size = llvm::alignTo(size, ScopeStackAlignment);
305 if (!startOfBuffer) {
306 unsigned capacity = llvm::PowerOf2Ceil(std::max<size_t>(size, 1024ul));
307 startOfBuffer = std::make_unique<char[]>(capacity);
308 startOfData = endOfBuffer = startOfBuffer.get() + capacity;
309 }
else if (
static_cast<size_t>(startOfData - startOfBuffer.get()) < size) {
310 unsigned currentCapacity = endOfBuffer - startOfBuffer.get();
311 unsigned usedCapacity =
312 currentCapacity - (startOfData - startOfBuffer.get());
313 unsigned requiredCapacity = usedCapacity + size;
316 unsigned newCapacity = llvm::PowerOf2Ceil(requiredCapacity);
318 std::unique_ptr<char[]> newStartOfBuffer =
319 std::make_unique<char[]>(newCapacity);
320 char *newEndOfBuffer = newStartOfBuffer.get() + newCapacity;
321 char *newStartOfData = newEndOfBuffer - usedCapacity;
322 memcpy(newStartOfData, startOfData, usedCapacity);
323 startOfBuffer.swap(newStartOfBuffer);
324 endOfBuffer = newEndOfBuffer;
325 startOfData = newStartOfData;
328 assert(startOfBuffer.get() + size <= startOfData);
333void EHScopeStack::deallocate(
size_t size) {
334 startOfData += llvm::alignTo(size, ScopeStackAlignment);
342 bool skipCleanupScope =
false;
344 cir::CleanupKind cleanupKind = cir::CleanupKind::All;
345 if (isEHCleanup && cgf->getLangOpts().Exceptions) {
347 isNormalCleanup ? cir::CleanupKind::All : cir::CleanupKind::EH;
353 cleanupKind = cir::CleanupKind::Normal;
355 skipCleanupScope =
true;
358 cir::CleanupScopeOp cleanupScope =
nullptr;
359 if (!skipCleanupScope) {
360 CIRGenBuilderTy &builder = cgf->getBuilder();
361 mlir::Location loc = builder.getUnknownLoc();
362 cleanupScope = cir::CleanupScopeOp::create(
363 builder, loc, cleanupKind,
365 [&](mlir::OpBuilder &b, mlir::Location loc) {
369 [&](mlir::OpBuilder &b, mlir::Location loc) {
373 builder.setInsertionPointToEnd(&cleanupScope.getBodyRegion().back());
380 if (innermostEHScope != stable_end() &&
384 EHCleanupScope *scope =
new (buffer)
385 EHCleanupScope(isNormalCleanup, isEHCleanup, size, cleanupScope,
386 innermostNormalCleanup, innermostEHScope);
389 innermostNormalCleanup = stable_begin();
392 innermostEHScope = stable_begin();
394 if (isLifetimeMarker)
395 cgf->cgm.errorNYI(
"push lifetime marker cleanup");
398 if (cgf->getLangOpts().EHAsynch && isEHCleanup && !isLifetimeMarker &&
399 cgf->getTarget().getCXXABI().isMicrosoft())
400 cgf->cgm.errorNYI(
"push seh cleanup");
406 assert(!empty() &&
"popping exception stack when not empty");
410 innermostNormalCleanup =
cleanup.getEnclosingNormalCleanup();
411 innermostEHScope =
cleanup.getEnclosingEHScope();
412 deallocate(
cleanup.getAllocatedSize());
414 cir::CleanupScopeOp cleanupScope =
cleanup.getCleanupScopeOp();
416 auto *block = &cleanupScope.getBodyRegion().back();
417 if (!block->mightHaveTerminator()) {
418 mlir::OpBuilder::InsertionGuard guard(cgf->getBuilder());
419 cgf->getBuilder().setInsertionPointToEnd(block);
420 cir::YieldOp::create(cgf->getBuilder(),
421 cgf->getBuilder().getUnknownLoc());
425 mlir::Block *insertBlock = cgf->getBuilder().getInsertionBlock();
427 cleanupScope.getBodyRegion().findAncestorBlockInRegion(*insertBlock))
428 cgf->getBuilder().setInsertionPointAfter(cleanupScope);
436 for (stable_iterator si = getInnermostEHScope(); si != stable_end();) {
437 if (
auto *
cleanup = dyn_cast<EHCleanupScope>(&*find(si))) {
438 if (
cleanup->isLifetimeMarker()) {
453 mlir::Operation *dominatingIP) {
457 "cleanup block is neither normal nor EH?");
468 if (!var.isValid()) {
469 mlir::Location loc = builder.getUnknownLoc();
472 loc,
"cleanup.isactive");
475 assert(dominatingIP &&
"no existing variable and no dominating IP!");
478 mlir::Value val = builder.
getBool(
true, loc);
481 mlir::OpBuilder::InsertionGuard guard(builder);
482 builder.setInsertionPoint(dominatingIP);
490 mlir::Location loc = builder.getUnknownLoc();
496 mlir::Operation *dominatingIP) {
497 assert(c !=
ehStack.stable_end() &&
"deactivating bottom of stack?");
499 assert(scope.
isActive() &&
"double deactivation");
503 if (c ==
ehStack.stable_begin() &&
516 EHScopeStack::Cleanup *
cleanup,
520 mlir::Block &block = cleanupScope.getCleanupRegion().back();
522 mlir::OpBuilder::InsertionGuard guard(builder);
523 builder.setInsertionPointToStart(&block);
529 mlir::Location loc = cleanupScope.getLoc();
531 cir::IfOp::create(builder, loc, isActive,
534 [&](mlir::OpBuilder &, mlir::Location) {
537 "cleanup ended with no insertion point?");
542 assert(cgf.
haveInsertPoint() &&
"cleanup ended with no insertion point?");
545 mlir::Block &cleanupRegionLastBlock = cleanupScope.getCleanupRegion().back();
546 if (cleanupRegionLastBlock.empty() ||
547 !cleanupRegionLastBlock.back().hasTrait<mlir::OpTrait::IsTerminator>()) {
548 mlir::OpBuilder::InsertionGuard guardCase(builder);
549 builder.setInsertionPointToEnd(&cleanupRegionLastBlock);
560 .walk([&](mlir::Operation *op) {
562 return mlir::WalkResult::interrupt();
563 return mlir::WalkResult::advance();
573 assert(!
ehStack.empty() &&
"cleanup stack is empty!");
583 "missing cir.cleanup.scope for active cleanup");
605 if (forDeactivation && requiresNormalCleanup) {
612 mlir::Location loc = builder.getUnknownLoc();
619 mlir::OpBuilder::InsertionGuard guard(builder);
620 builder.setInsertionPoint(cleanupScope);
621 builder.createFlagStore(loc,
true, activeFlag.
getPointer());
625 assert(builder.getInsertionBlock() ==
626 &cleanupScope.getBodyRegion().back() &&
627 "expected insertion point in cleanup body");
628 builder.createFlagStore(loc,
false, activeFlag.
getPointer());
635 if (requiresEHCleanup)
636 cleanupScope.setCleanupKind(cir::CleanupKind::EH);
637 requiresNormalCleanup =
false;
649 if (!requiresNormalCleanup && !requiresEHCleanup) {
654 mlir::Block &cleanupBlock = cleanupScope.getCleanupRegion().back();
655 if (!cleanupBlock.mightHaveTerminator()) {
656 mlir::OpBuilder::InsertionGuard guard(builder);
657 builder.setInsertionPointToEnd(&cleanupBlock);
658 cir::YieldOp::create(builder, builder.getUnknownLoc());
671 cleanupBufferStack[8 *
sizeof(
void *)];
672 std::unique_ptr<char[]> cleanupBufferHeap;
678 if (cleanupSize <=
sizeof(cleanupBufferStack)) {
679 memcpy(cleanupBufferStack, cleanupSource, cleanupSize);
682 cleanupBufferHeap.reset(
new char[cleanupSize]);
683 memcpy(cleanupBufferHeap.get(), cleanupSource, cleanupSize);
695 Address cleanupActiveFlag = normalActiveFlag.
isValid() ? normalActiveFlag
696 : ehActiveFlag.
isValid() ? ehActiveFlag
714 bool requiresCleanup =
false;
715 for (
auto it =
ehStack.begin(), ie =
ehStack.find(oldCleanupStackDepth);
718 requiresCleanup =
true;
727 if (requiresCleanup) {
728 for (mlir::Value *valPtr : valuesToReload) {
729 mlir::Value val = *valPtr;
737 tempAllocas.push_back(temp);
738 builder.createStore(val.getLoc(), val, temp);
744 while (
ehStack.stable_begin() != oldCleanupStackDepth)
748 if (requiresCleanup) {
749 for (
auto [addr, valPtr] : llvm::zip(tempAllocas, valuesToReload)) {
750 mlir::Location loc = valPtr->getLoc();
751 *valPtr = builder.createLoad(loc, addr);
static void setupCleanupBlockDeactivation(CIRGenFunction &cgf, EHScopeStack::stable_iterator c, mlir::Operation *dominatingIP)
The given cleanup block is being deactivated.
static bool bodyHasBranchThroughExits(mlir::Region &bodyRegion)
Check whether a cleanup scope body contains any non-yield exits that branch through the cleanup.
static void hoistAllocaOutOfCleanupScope(CIRGenFunction &cgf, Address addr, cir::CleanupScopeOp scope)
If the alloca that backs addr is currently nested inside the body region of scope,...
static void emitCleanup(CIRGenFunction &cgf, cir::CleanupScopeOp cleanupScope, EHScopeStack::Cleanup *cleanup, EHScopeStack::Cleanup::Flags flags, Address activeFlag)
static Decl::Kind getKind(const Decl *D)
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
__DEVICE__ void * memcpy(void *__a, const void *__b, size_t __c)
cir::ConstantOp getBool(bool state, mlir::Location loc)
cir::StoreOp createFlagStore(mlir::Location loc, bool val, mlir::Value dst)
static OpBuilder::InsertPoint getBestAllocaInsertPoint(mlir::Block *block)
cir::YieldOp createYield(mlir::Location loc, mlir::ValueRange value={})
Create a yield operation.
cir::LoadOp createFlagLoad(mlir::Location loc, mlir::Value addr)
Emit a load from an boolean flag variable.
cir::BoolType getBoolTy()
mlir::Value getPointer() const
cir::AllocaOp getUnderlyingAllocaOp() const
Return the underlying alloca for this address, if any.
FullExprCleanupScope(CIRGenFunction &cgf, const Expr *subExpr)
void exit(ArrayRef< mlir::Value * > valuesToReload={})
llvm::SmallVector< PendingCleanupEntry > lifetimeExtendedCleanupStack
void initFullExprCleanup()
Set up the last cleanup that was pushed as a conditional full-expression cleanup.
mlir::Block * getCurFunctionEntryBlock()
bool isInConditionalBranch() const
void setBeforeOutermostConditional(mlir::Value value, Address addr)
ConditionalEvaluation * outermostConditional
EHScopeStack ehStack
Tracks function scope overall cleanup handling.
llvm::SmallVector< PendingCleanupEntry > deferredConditionalCleanupStack
void pushDestroy(QualType::DestructionKind dtorKind, Address addr, QualType type)
Push the standard destructor for the given type as at least a normal cleanup.
static Destroyer destroyCXXObject
void initFullExprCleanupWithFlag(Address activeFlag)
Address createCleanupActiveFlag()
Create an active flag variable for use with conditional cleanups.
void deactivateCleanupBlock(EHScopeStack::stable_iterator cleanup, mlir::Operation *dominatingIP)
Deactivates the given cleanup block.
bool haveInsertPoint() const
True if an insertion point is defined.
void emitCXXTemporary(const CXXTemporary *temporary, QualType tempType, Address ptr)
Emits all the code to cause the given temporary to be cleaned up.
void popCleanupBlocks(EHScopeStack::stable_iterator oldCleanupStackDepth, ArrayRef< mlir::Value * > valuesToReload={})
Takes the old cleanup stack size and emits the cleanup blocks that have been added.
CIRGenBuilderTy & getBuilder()
void pushPendingCleanupToEHStack(const PendingCleanupEntry &entry)
Promote a single pending cleanup entry onto the EH scope stack.
void popCleanupBlock(bool forDeactivation=false)
Pop a cleanup block from the stack.
EHScopeStack::stable_iterator currentCleanupStackDepth
CIRGenFunction(CIRGenModule &cgm, CIRGenBuilderTy &builder, bool suppressNewContext=false)
Address createTempAllocaWithoutCast(mlir::Type ty, CharUnits align, mlir::Location loc, const Twine &name="tmp", mlir::Value arraySize=nullptr, mlir::OpBuilder::InsertPoint ip={})
This creates a alloca and inserts it into the entry block of the current region.
Address createDefaultAlignTempAlloca(mlir::Type ty, mlir::Location loc, const Twine &name)
CreateDefaultAlignTempAlloca - This creates an alloca with the default alignment of the corresponding...
A cleanup scope which generates the cleanup blocks lazily.
void setTestFlagInEHCleanup(bool value)
void setTestFlagInNormalCleanup(bool value)
Address getActiveFlag() const
size_t getCleanupSize() const
cir::CleanupScopeOp getCleanupScopeOp()
bool shouldTestFlagInEHCleanup() const
static size_t getSizeForCleanupSize(size_t size)
Gets the size required for a lazy cleanup scope with the given cleanup-data requirements.
bool isNormalCleanup() const
void setActiveFlag(Address var)
void * getCleanupBuffer()
bool shouldTestFlagInNormalCleanup() const
void setActive(bool isActive)
void setIsEHCleanupKind()
void setIsNormalCleanupKind()
Information for lazily generating a cleanup.
A saved depth on the scope stack.
void popCleanup()
Pops a cleanup scope off the stack. This is private to CIRGenCleanup.cpp.
iterator find(stable_iterator savePoint) const
Turn a stable reference to a scope depth into a unstable pointer to the EH stack.
bool requiresCatchOrCleanup() const
stable_iterator getInnermostActiveNormalCleanup() const
Represents a C++ temporary.
static CharUnits One()
One - Construct a CharUnits quantity of one.
This represents one expression.
A (possibly-)qualified type.
A class that does preorder or postorder depth-first traversal on the entire Clang AST and visits each...
@ EHCleanup
Denotes a cleanup that should run when a scope is exited using exceptional control flow (a throw stat...
@ NormalCleanup
Denotes a cleanup that should run when a scope is exited using normal control flow (falling off the e...
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
U cast(CodeGen::Address addr)
static bool emitLifetimeMarkers()
A cleanup entry that will be promoted onto the EH scope stack at a later point.