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);
203 cleanups.forceCleanup();
207 mlir::OpBuilder::InsertionGuard guard(cgf.builder);
208 mlir::Block &lastBodyBlock = scope.getBodyRegion().back();
209 cgf.builder.setInsertionPointToEnd(&lastBodyBlock);
210 if (lastBodyBlock.empty() ||
211 !lastBodyBlock.back().hasTrait<mlir::OpTrait::IsTerminator>())
212 cgf.builder.createYield(scope.getLoc());
219 if (hasDeferredCleanups) {
221 llvm::make_range(cgf.deferredConditionalCleanupStack.begin() + oldSize,
222 cgf.deferredConditionalCleanupStack.end())) {
229 mlir::OpBuilder::InsertionGuard guard(cgf.builder);
230 mlir::Block &cleanupBlock = scope.getCleanupRegion().front();
231 cgf.builder.setInsertionPointToEnd(&cleanupBlock);
233 if (hasDeferredCleanups) {
235 cgf.deferredConditionalCleanupStack.begin() + oldSize,
236 cgf.deferredConditionalCleanupStack.end()))) {
237 if (entry.activeFlag.isValid()) {
245 assert(entry.addr.getUnderlyingAllocaOp() &&
246 (entry.addr.getUnderlyingAllocaOp()->getBlock() ==
247 entry.addr.getPointer().getDefiningOp()->getBlock()) &&
248 "alloca and cast are in different blocks");
250 cgf.builder.createLoad(scope.getLoc(), entry.activeFlag);
252 cgf.builder, scope.getLoc(), flag,
false,
253 [&](mlir::OpBuilder &
b, mlir::Location loc) {
254 cgf.emitDestroy(entry.addr, entry.type, entry.destroyer);
255 cgf.builder.createYield(loc);
258 cgf.emitDestroy(entry.addr, entry.type, entry.destroyer);
262 cgf.builder.createYield(scope.getLoc());
265 cgf.deferredConditionalCleanupStack.truncate(oldSize);
266 cgf.builder.setInsertionPointAfter(scope);
269 for (
auto [addr, valPtr] : llvm::zip(tempAllocas, valuesToReload)) {
272 *valPtr = cgf.builder.createLoad(valPtr->getLoc(), addr);
280void EHScopeStack::Cleanup::anchor() {}
284 stable_iterator si = getInnermostNormalCleanup();
285 stable_iterator se = stable_end();
290 si =
cleanup.getEnclosingNormalCleanup();
296char *EHScopeStack::allocate(
size_t size) {
297 size = llvm::alignTo(size, ScopeStackAlignment);
298 if (!startOfBuffer) {
299 unsigned capacity = llvm::PowerOf2Ceil(std::max<size_t>(size, 1024ul));
300 startOfBuffer = std::make_unique<char[]>(capacity);
301 startOfData = endOfBuffer = startOfBuffer.get() + capacity;
302 }
else if (
static_cast<size_t>(startOfData - startOfBuffer.get()) < size) {
303 unsigned currentCapacity = endOfBuffer - startOfBuffer.get();
304 unsigned usedCapacity =
305 currentCapacity - (startOfData - startOfBuffer.get());
306 unsigned requiredCapacity = usedCapacity + size;
309 unsigned newCapacity = llvm::PowerOf2Ceil(requiredCapacity);
311 std::unique_ptr<char[]> newStartOfBuffer =
312 std::make_unique<char[]>(newCapacity);
313 char *newEndOfBuffer = newStartOfBuffer.get() + newCapacity;
314 char *newStartOfData = newEndOfBuffer - usedCapacity;
315 memcpy(newStartOfData, startOfData, usedCapacity);
316 startOfBuffer.swap(newStartOfBuffer);
317 endOfBuffer = newEndOfBuffer;
318 startOfData = newStartOfData;
321 assert(startOfBuffer.get() + size <= startOfData);
326void EHScopeStack::deallocate(
size_t size) {
327 startOfData += llvm::alignTo(size, ScopeStackAlignment);
335 bool skipCleanupScope =
false;
337 cir::CleanupKind cleanupKind = cir::CleanupKind::All;
338 if (isEHCleanup && cgf->getLangOpts().Exceptions) {
340 isNormalCleanup ? cir::CleanupKind::All : cir::CleanupKind::EH;
346 cleanupKind = cir::CleanupKind::Normal;
348 skipCleanupScope =
true;
351 cir::CleanupScopeOp cleanupScope =
nullptr;
352 if (!skipCleanupScope) {
353 CIRGenBuilderTy &builder = cgf->getBuilder();
354 mlir::Location loc = builder.getUnknownLoc();
355 cleanupScope = cir::CleanupScopeOp::create(
356 builder, loc, cleanupKind,
358 [&](mlir::OpBuilder &
b, mlir::Location loc) {
362 [&](mlir::OpBuilder &
b, mlir::Location loc) {
366 builder.setInsertionPointToEnd(&cleanupScope.getBodyRegion().back());
373 if (innermostEHScope != stable_end() &&
377 EHCleanupScope *scope =
new (buffer)
378 EHCleanupScope(isNormalCleanup, isEHCleanup, size, cleanupScope,
379 innermostNormalCleanup, innermostEHScope);
382 innermostNormalCleanup = stable_begin();
385 innermostEHScope = stable_begin();
387 if (isLifetimeMarker)
388 cgf->cgm.errorNYI(
"push lifetime marker cleanup");
391 if (cgf->getLangOpts().EHAsynch && isEHCleanup && !isLifetimeMarker &&
392 cgf->getTarget().getCXXABI().isMicrosoft())
393 cgf->cgm.errorNYI(
"push seh cleanup");
399 assert(!empty() &&
"popping exception stack when not empty");
403 innermostNormalCleanup =
cleanup.getEnclosingNormalCleanup();
404 innermostEHScope =
cleanup.getEnclosingEHScope();
405 deallocate(
cleanup.getAllocatedSize());
407 cir::CleanupScopeOp cleanupScope =
cleanup.getCleanupScopeOp();
409 auto *block = &cleanupScope.getBodyRegion().back();
410 if (!block->mightHaveTerminator()) {
411 mlir::OpBuilder::InsertionGuard guard(cgf->getBuilder());
412 cgf->getBuilder().setInsertionPointToEnd(block);
413 cir::YieldOp::create(cgf->getBuilder(),
414 cgf->getBuilder().getUnknownLoc());
416 cgf->getBuilder().setInsertionPointAfter(cleanupScope);
424 for (stable_iterator si = getInnermostEHScope(); si != stable_end();) {
425 if (
auto *
cleanup = dyn_cast<EHCleanupScope>(&*find(si))) {
426 if (
cleanup->isLifetimeMarker()) {
441 mlir::Operation *dominatingIP) {
445 "cleanup block is neither normal nor EH?");
456 if (!var.isValid()) {
457 mlir::Location loc = builder.getUnknownLoc();
460 loc,
"cleanup.isactive");
463 assert(dominatingIP &&
"no existing variable and no dominating IP!");
466 mlir::Value val = builder.
getBool(
true, loc);
469 mlir::OpBuilder::InsertionGuard guard(builder);
470 builder.setInsertionPoint(dominatingIP);
478 mlir::Location loc = builder.getUnknownLoc();
484 mlir::Operation *dominatingIP) {
485 assert(
c !=
ehStack.stable_end() &&
"deactivating bottom of stack?");
487 assert(scope.
isActive() &&
"double deactivation");
504 EHScopeStack::Cleanup *
cleanup,
508 mlir::Block &block = cleanupScope.getCleanupRegion().back();
510 mlir::OpBuilder::InsertionGuard guard(builder);
511 builder.setInsertionPointToStart(&block);
517 mlir::Location loc = cleanupScope.getLoc();
519 cir::IfOp::create(builder, loc, isActive,
522 [&](mlir::OpBuilder &, mlir::Location) {
525 "cleanup ended with no insertion point?");
530 assert(cgf.
haveInsertPoint() &&
"cleanup ended with no insertion point?");
533 mlir::Block &cleanupRegionLastBlock = cleanupScope.getCleanupRegion().back();
534 if (cleanupRegionLastBlock.empty() ||
535 !cleanupRegionLastBlock.back().hasTrait<mlir::OpTrait::IsTerminator>()) {
536 mlir::OpBuilder::InsertionGuard guardCase(builder);
537 builder.setInsertionPointToEnd(&cleanupRegionLastBlock);
548 .walk([&](mlir::Operation *op) {
550 return mlir::WalkResult::interrupt();
551 return mlir::WalkResult::advance();
561 assert(!
ehStack.empty() &&
"cleanup stack is empty!");
571 "missing cir.cleanup.scope for active cleanup");
593 if (forDeactivation && requiresNormalCleanup) {
600 mlir::Location loc = builder.getUnknownLoc();
607 mlir::OpBuilder::InsertionGuard guard(builder);
608 builder.setInsertionPoint(cleanupScope);
609 builder.createFlagStore(loc,
true, activeFlag.
getPointer());
613 assert(builder.getInsertionBlock() ==
614 &cleanupScope.getBodyRegion().back() &&
615 "expected insertion point in cleanup body");
616 builder.createFlagStore(loc,
false, activeFlag.
getPointer());
623 if (requiresEHCleanup)
624 cleanupScope.setCleanupKind(cir::CleanupKind::EH);
625 requiresNormalCleanup =
false;
637 if (!requiresNormalCleanup && !requiresEHCleanup) {
642 mlir::Block &cleanupBlock = cleanupScope.getCleanupRegion().back();
643 if (!cleanupBlock.mightHaveTerminator()) {
644 mlir::OpBuilder::InsertionGuard guard(builder);
645 builder.setInsertionPointToEnd(&cleanupBlock);
646 cir::YieldOp::create(builder, builder.getUnknownLoc());
659 cleanupBufferStack[8 *
sizeof(
void *)];
660 std::unique_ptr<char[]> cleanupBufferHeap;
666 if (cleanupSize <=
sizeof(cleanupBufferStack)) {
667 memcpy(cleanupBufferStack, cleanupSource, cleanupSize);
670 cleanupBufferHeap.reset(
new char[cleanupSize]);
671 memcpy(cleanupBufferHeap.get(), cleanupSource, cleanupSize);
683 Address cleanupActiveFlag = normalActiveFlag.
isValid() ? normalActiveFlag
684 : ehActiveFlag.
isValid() ? ehActiveFlag
702 bool requiresCleanup =
false;
703 for (
auto it =
ehStack.begin(), ie =
ehStack.find(oldCleanupStackDepth);
706 requiresCleanup =
true;
715 if (requiresCleanup) {
716 for (mlir::Value *valPtr : valuesToReload) {
717 mlir::Value val = *valPtr;
725 tempAllocas.push_back(temp);
726 builder.createStore(val.getLoc(), val, temp);
732 while (
ehStack.stable_begin() != oldCleanupStackDepth)
736 if (requiresCleanup) {
737 for (
auto [addr, valPtr] : llvm::zip(tempAllocas, valuesToReload)) {
738 mlir::Location loc = valPtr->getLoc();
739 *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)
__device__ __2f16 float 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.