clang 23.0.0git
CIRGenCleanup.cpp
Go to the documentation of this file.
1//===--- CIRGenCleanup.cpp - Bookkeeping and code emission for cleanups ---===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains code dealing with the IR generation for cleanups
10// and related information.
11//
12// A "cleanup" is a piece of code which needs to be executed whenever
13// control transfers out of a particular scope. This can be
14// conditionalized to occur only on exceptional control flow, only on
15// normal control flow, or both.
16//
17//===----------------------------------------------------------------------===//
18
19#include "CIRGenCleanup.h"
20#include "CIRGenFunction.h"
21
24
25using namespace clang;
26using namespace clang::CIRGen;
27
28namespace {
29/// Return true if the expression tree contains an AbstractConditionalOperator
30/// (ternary ?:), which is the only construct whose CIR codegen calls
31/// ConditionalEvaluation::beginEvaluation() and thus causes cleanups to be
32/// deferred via pushFullExprCleanup. Logical &&/|| do NOT call
33/// beginEvaluation(); their branch-local cleanups are handled by LexicalScope.
34class ConditionalEvaluationFinder
35 : public RecursiveASTVisitor<ConditionalEvaluationFinder> {
36 bool foundConditional = false;
37
38public:
39 bool found() const { return foundConditional; }
40
41 bool VisitAbstractConditionalOperator(AbstractConditionalOperator *) {
42 foundConditional = true;
43 return false;
44 }
45
46 // Don't cross evaluation-context boundaries.
47 bool TraverseLambdaExpr(LambdaExpr *) { return true; }
48 bool TraverseBlockExpr(BlockExpr *) { return true; }
49 bool TraverseStmtExpr(StmtExpr *) { return true; }
50};
51} // namespace
52
53//===----------------------------------------------------------------------===//
54// CIRGenFunction cleanup related
55//===----------------------------------------------------------------------===//
56
57/// Emits all the code to cause the given temporary to be cleaned up.
59 QualType tempType, Address ptr) {
61}
62
64 assert(isInConditionalBranch());
65 mlir::Location loc = builder.getUnknownLoc();
66
67 // Place the alloca in the function entry block so it dominates everything,
68 // including both regions of any enclosing cir.cleanup.scope. We can't rely
69 // on the default curLexScope path because we may be inside a ternary branch
70 // whose LexicalScope would capture the alloca.
72 builder.getBoolTy(), CharUnits::One(), loc, "cleanup.cond",
73 /*arraySize=*/nullptr,
74 builder.getBestAllocaInsertPoint(getCurFunctionEntryBlock()));
75
76 // Initialize to false before the outermost conditional.
77 {
78 mlir::OpBuilder::InsertionGuard guard(builder);
79 builder.restoreInsertionPoint(outermostConditional->getInsertPoint());
80 builder.createFlagStore(loc, false, active.getPointer());
81 }
82
83 // Set to true at the current location (inside the conditional branch).
84 builder.createFlagStore(loc, true, active.getPointer());
85
86 return active;
87}
88
92
95 assert(!cleanup.hasActiveFlag() && "cleanup already has active flag?");
96 cleanup.setActiveFlag(activeFlag);
97
98 cleanup.setTestFlagInNormalCleanup(cleanup.isNormalCleanup());
99 cleanup.setTestFlagInEHCleanup(cleanup.isEHCleanup());
100}
101
103 const Expr *subExpr)
104 : cgf(cgf), cleanups(cgf), scope(nullptr),
105 deferredCleanupStackSize(cgf.deferredConditionalCleanupStack.size()) {
106
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,
117 /*bodyBuilder=*/
118 [&](mlir::OpBuilder &b, mlir::Location loc) {},
119 /*cleanupBuilder=*/
120 [&](mlir::OpBuilder &b, mlir::Location loc) {});
121 cgf.builder.setInsertionPointToEnd(&scope.getBodyRegion().front());
122 }
123}
124
125/// If the alloca that backs \p addr is currently nested inside the body
126/// region of \p scope, hoist it, and any cast chain leading to it, out of the
127// scope so the alloca dominates the scope's sibling cleanup region.
129 cir::CleanupScopeOp scope) {
130 cir::AllocaOp alloca = addr.getUnderlyingAllocaOp();
131 if (!alloca)
132 return;
133
134 // If the alloca is not contained within the cleanup scope we're currently
135 // proccessing we don't need to hoist it.
136 auto cur = alloca->getParentOfType<cir::CleanupScopeOp>();
137 while (cur && cur != scope)
138 cur = cur->getParentOfType<cir::CleanupScopeOp>();
139 if (cur != scope)
140 return;
141
142 // Place the alloca at the canonical alloca insertion point of the block
143 // containing the cleanup scope op, so it groups with any preceding
144 // allocas / labels and dominates both the body and cleanup regions.
145 mlir::Block *parentBlock = scope->getBlock();
146 mlir::OpBuilder::InsertPoint ip =
148 alloca->moveBefore(parentBlock, ip.getPoint());
149
150 // Move any cast chain that consumes the alloca's result to immediately after
151 // the alloca, so the address used by the deferred cleanup also dominates the
152 // cleanup region. We walk down the chain starting from the alloca's user
153 // that the Address was built from. This is very conservative. In practice,
154 // we should only ever see alloca or address_space(alloca) operations here.
155 mlir::Value ptr = addr.getPointer();
157 for (mlir::Operation *cur = ptr.getDefiningOp(); cur && cur != alloca;) {
158 auto cast = mlir::dyn_cast<cir::CastOp>(cur);
159 if (!cast)
160 break;
161 casts.push_back(cast);
162 cur = cast.getSrc().getDefiningOp();
163 }
164 // Move casts in source order (closest to the alloca first).
165 mlir::Operation *prev = alloca;
166 for (cir::CastOp cast : llvm::reverse(casts)) {
167 cast->moveAfter(prev);
168 prev = cast;
169 }
170}
171
173 ArrayRef<mlir::Value *> valuesToReload) {
174 assert(!exited && "FullExprCleanupScope::exit called twice");
175 exited = true;
176
177 size_t oldSize = deferredCleanupStackSize;
178 bool hasDeferredCleanups =
179 cgf.deferredConditionalCleanupStack.size() > oldSize;
180
181 if (!scope) {
182 cgf.deferredConditionalCleanupStack.truncate(oldSize);
183 cleanups.forceCleanup(valuesToReload);
184 return;
185 }
186
187 // Spill any values that callers need after the scope is closed.
188 SmallVector<Address> tempAllocas;
189 for (mlir::Value *valPtr : valuesToReload) {
190 mlir::Value val = *valPtr;
191 if (!val) {
192 tempAllocas.push_back(Address::invalid());
193 continue;
194 }
195 Address temp = cgf.createDefaultAlignTempAlloca(val.getType(), val.getLoc(),
196 "tmp.exprcleanup");
197 tempAllocas.push_back(temp);
198 cgf.builder.createStore(val.getLoc(), val, temp);
199 }
200
201 // Pop any EH and lifetime-extended cleanups that were pushed during
202 // the expression (e.g. temporary destructors).
203 cleanups.forceCleanup();
204
205 // Make sure the cleanup scope body region has a terminator.
206 {
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());
213 }
214
215 // Each deferred conditional cleanup will reference its addr from the
216 // sibling cleanup region we are about to fill. If the alloca that backs
217 // that addr was created inside this scope's body region, hoist it out so it
218 // dominates the cleanup region.
219 if (hasDeferredCleanups) {
220 for (const PendingCleanupEntry &entry :
221 llvm::make_range(cgf.deferredConditionalCleanupStack.begin() + oldSize,
222 cgf.deferredConditionalCleanupStack.end())) {
223 hoistAllocaOutOfCleanupScope(cgf, entry.addr, scope);
224 }
225 }
226
227 // Emit any deferred cleanups.
228 {
229 mlir::OpBuilder::InsertionGuard guard(cgf.builder);
230 mlir::Block &cleanupBlock = scope.getCleanupRegion().front();
231 cgf.builder.setInsertionPointToEnd(&cleanupBlock);
232
233 if (hasDeferredCleanups) {
234 for (const PendingCleanupEntry &entry : llvm::reverse(llvm::make_range(
235 cgf.deferredConditionalCleanupStack.begin() + oldSize,
236 cgf.deferredConditionalCleanupStack.end()))) {
237 if (entry.activeFlag.isValid()) {
238 // We may have hoisted this alloca out of the cleanup scope. If so,
239 // we will have also hoisted any casts between it and the address that
240 // we stored in the deferredConditionalCleanupStack. While I can't
241 // find a case where this actually happens, there is a theoretical
242 // possibility that we could have a second address that uses an
243 // alloca that has already been hoisted but a different cast chain.
244 // This assert guards against that possibility.
245 assert(entry.addr.getUnderlyingAllocaOp() &&
246 (entry.addr.getUnderlyingAllocaOp()->getBlock() ==
247 entry.addr.getPointer().getDefiningOp()->getBlock()) &&
248 "alloca and cast are in different blocks");
249 mlir::Value flag =
250 cgf.builder.createLoad(scope.getLoc(), entry.activeFlag);
251 cir::IfOp::create(
252 cgf.builder, scope.getLoc(), flag, /*withElseRegion=*/false,
253 [&](mlir::OpBuilder &b, mlir::Location loc) {
254 cgf.emitDestroy(entry.addr, entry.type, entry.destroyer);
255 cgf.builder.createYield(loc);
256 });
257 } else {
258 cgf.emitDestroy(entry.addr, entry.type, entry.destroyer);
259 }
260 }
261 }
262 cgf.builder.createYield(scope.getLoc());
263 }
264
265 cgf.deferredConditionalCleanupStack.truncate(oldSize);
266 cgf.builder.setInsertionPointAfter(scope);
267
268 // Reload spilled values now that the builder is after the closed scope.
269 for (auto [addr, valPtr] : llvm::zip(tempAllocas, valuesToReload)) {
270 if (!addr.isValid())
271 continue;
272 *valPtr = cgf.builder.createLoad(valPtr->getLoc(), addr);
273 }
274}
275
276//===----------------------------------------------------------------------===//
277// EHScopeStack
278//===----------------------------------------------------------------------===//
279
280void EHScopeStack::Cleanup::anchor() {}
281
284 stable_iterator si = getInnermostNormalCleanup();
285 stable_iterator se = stable_end();
286 while (si != se) {
287 EHCleanupScope &cleanup = llvm::cast<EHCleanupScope>(*find(si));
288 if (cleanup.isActive())
289 return si;
290 si = cleanup.getEnclosingNormalCleanup();
291 }
292 return stable_end();
293}
294
295/// Push an entry of the given size onto this protected-scope stack.
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;
307 // We know from the 'else if' condition that requiredCapacity is greater
308 // than currentCapacity.
309 unsigned newCapacity = llvm::PowerOf2Ceil(requiredCapacity);
310
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;
319 }
320
321 assert(startOfBuffer.get() + size <= startOfData);
322 startOfData -= size;
323 return startOfData;
324}
325
326void EHScopeStack::deallocate(size_t size) {
327 startOfData += llvm::alignTo(size, ScopeStackAlignment);
328}
329
330void *EHScopeStack::pushCleanup(CleanupKind kind, size_t size) {
331 char *buffer = allocate(EHCleanupScope::getSizeForCleanupSize(size));
332 bool isNormalCleanup = kind & NormalCleanup;
333 bool isEHCleanup = kind & EHCleanup;
334 bool isLifetimeMarker = kind & LifetimeMarker;
335 bool skipCleanupScope = false;
336
337 cir::CleanupKind cleanupKind = cir::CleanupKind::All;
338 if (isEHCleanup && cgf->getLangOpts().Exceptions) {
339 cleanupKind =
340 isNormalCleanup ? cir::CleanupKind::All : cir::CleanupKind::EH;
341 } else {
342 // Exceptions are disabled (or no EH flag was requested). Drop the EH
343 // flag so the scope entry stays consistent with the op's cleanup kind.
344 isEHCleanup = false;
345 if (isNormalCleanup)
346 cleanupKind = cir::CleanupKind::Normal;
347 else
348 skipCleanupScope = true;
349 }
350
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,
357 /*bodyBuilder=*/
358 [&](mlir::OpBuilder &b, mlir::Location loc) {
359 // Terminations will be handled in popCleanup
360 },
361 /*cleanupBuilder=*/
362 [&](mlir::OpBuilder &b, mlir::Location loc) {
363 // Terminations will be handled after emiting cleanup
364 });
365
366 builder.setInsertionPointToEnd(&cleanupScope.getBodyRegion().back());
367 }
368
369 // Per C++ [except.terminate], it is implementation-defined whether none,
370 // some, or all cleanups are called before std::terminate. Thus, when
371 // terminate is the current EH scope, we may skip adding any EH cleanup
372 // scopes.
373 if (innermostEHScope != stable_end() &&
374 find(innermostEHScope)->getKind() == EHScope::Terminate)
375 isEHCleanup = false;
376
377 EHCleanupScope *scope = new (buffer)
378 EHCleanupScope(isNormalCleanup, isEHCleanup, size, cleanupScope,
379 innermostNormalCleanup, innermostEHScope);
380
381 if (isNormalCleanup)
382 innermostNormalCleanup = stable_begin();
383
384 if (isEHCleanup)
385 innermostEHScope = stable_begin();
386
387 if (isLifetimeMarker)
388 cgf->cgm.errorNYI("push lifetime marker cleanup");
389
390 // With Windows -EHa, Invoke llvm.seh.scope.begin() for EHCleanup
391 if (cgf->getLangOpts().EHAsynch && isEHCleanup && !isLifetimeMarker &&
392 cgf->getTarget().getCXXABI().isMicrosoft())
393 cgf->cgm.errorNYI("push seh cleanup");
394
395 return scope->getCleanupBuffer();
396}
397
399 assert(!empty() && "popping exception stack when not empty");
400
401 assert(isa<EHCleanupScope>(*begin()));
402 EHCleanupScope &cleanup = cast<EHCleanupScope>(*begin());
403 innermostNormalCleanup = cleanup.getEnclosingNormalCleanup();
404 innermostEHScope = cleanup.getEnclosingEHScope();
405 deallocate(cleanup.getAllocatedSize());
406
407 cir::CleanupScopeOp cleanupScope = cleanup.getCleanupScopeOp();
408 if (cleanupScope) {
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());
415 }
416 cgf->getBuilder().setInsertionPointAfter(cleanupScope);
417 }
418
419 // Destroy the cleanup.
420 cleanup.destroy();
421}
422
424 for (stable_iterator si = getInnermostEHScope(); si != stable_end();) {
425 if (auto *cleanup = dyn_cast<EHCleanupScope>(&*find(si))) {
426 if (cleanup->isLifetimeMarker()) {
427 // Skip lifetime markers and continue from the enclosing EH scope
429 continue;
430 }
431 }
432 return true;
433 }
434 return false;
435}
436
437/// The given cleanup block is being deactivated. Configure a cleanup variable
438/// if necessary.
441 mlir::Operation *dominatingIP) {
443
444 assert((scope.isNormalCleanup() || scope.isEHCleanup()) &&
445 "cleanup block is neither normal nor EH?");
446
448 scope.setTestFlagInEHCleanup(scope.isEHCleanup());
449
450 CIRGenBuilderTy &builder = cgf.getBuilder();
451
452 // If the cleanup block doesn't exist yet, create it and set its initial
453 // value to `true`. If we are inside a conditional branch, the value must be
454 // initialized before the conditional branch begins.
455 Address var = scope.getActiveFlag();
456 if (!var.isValid()) {
457 mlir::Location loc = builder.getUnknownLoc();
458
460 loc, "cleanup.isactive");
461 scope.setActiveFlag(var);
462
463 assert(dominatingIP && "no existing variable and no dominating IP!");
464
465 if (cgf.isInConditionalBranch()) {
466 mlir::Value val = builder.getBool(true, loc);
467 cgf.setBeforeOutermostConditional(val, var);
468 } else {
469 mlir::OpBuilder::InsertionGuard guard(builder);
470 builder.setInsertionPoint(dominatingIP);
471 builder.createFlagStore(loc, true, var.getPointer());
472 }
473 }
474
475 // The code above sets the `isActive` flag to `true` as its initial state
476 // at the point where the variable is created. The code below sets it to
477 // `false` at the point where the cleanup is deactivated.
478 mlir::Location loc = builder.getUnknownLoc();
479 builder.createFlagStore(loc, false, var.getPointer());
480}
481
482/// Deactive a cleanup that was created in an active state.
484 mlir::Operation *dominatingIP) {
485 assert(c != ehStack.stable_end() && "deactivating bottom of stack?");
487 assert(scope.isActive() && "double deactivation");
488
489 // If it's the top of the stack, just pop it, but do so only if it belongs
490 // to the current RunCleanupsScope.
491 if (c == ehStack.stable_begin() &&
492 currentCleanupStackDepth.strictlyEncloses(c)) {
493 popCleanupBlock(/*forDeactivation=*/true);
494 return;
495 }
496
497 // Otherwise, follow the general case.
498 setupCleanupBlockDeactivation(*this, c, dominatingIP);
499
500 scope.setActive(false);
501}
502
503static void emitCleanup(CIRGenFunction &cgf, cir::CleanupScopeOp cleanupScope,
504 EHScopeStack::Cleanup *cleanup,
506 Address activeFlag) {
507 CIRGenBuilderTy &builder = cgf.getBuilder();
508 mlir::Block &block = cleanupScope.getCleanupRegion().back();
509
510 mlir::OpBuilder::InsertionGuard guard(builder);
511 builder.setInsertionPointToStart(&block);
512
513 // Ask the cleanup to emit itself.
514 assert(cgf.haveInsertPoint() && "expected insertion point");
515
516 if (activeFlag.isValid()) {
517 mlir::Location loc = cleanupScope.getLoc();
518 mlir::Value isActive = builder.createFlagLoad(loc, activeFlag.getPointer());
519 cir::IfOp::create(builder, loc, isActive,
520 /*withElseRegion=*/false,
521 /*thenBuilder=*/
522 [&](mlir::OpBuilder &, mlir::Location) {
523 cleanup->emit(cgf, flags);
524 assert(cgf.haveInsertPoint() &&
525 "cleanup ended with no insertion point?");
526 builder.createYield(loc);
527 });
528 } else {
529 cleanup->emit(cgf, flags);
530 assert(cgf.haveInsertPoint() && "cleanup ended with no insertion point?");
531 }
532
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);
538 builder.createYield(cleanupScope.getLoc());
539 }
540}
541
542/// Check whether a cleanup scope body contains any non-yield exits that branch
543/// through the cleanup. These exits branch through the cleanup and require
544/// the normal cleanup to be executed even when the cleanup has been
545/// deactivated.
546static bool bodyHasBranchThroughExits(mlir::Region &bodyRegion) {
547 return bodyRegion
548 .walk([&](mlir::Operation *op) {
550 return mlir::WalkResult::interrupt();
551 return mlir::WalkResult::advance();
552 })
553 .wasInterrupted();
554}
555
556/// Pop a cleanup block from the stack.
557///
558/// \param forDeactivation - When true, this indicates that the cleanup block
559/// is being popped because it was deactivated while at the top of the stack.
560void CIRGenFunction::popCleanupBlock(bool forDeactivation) {
561 assert(!ehStack.empty() && "cleanup stack is empty!");
562 assert(isa<EHCleanupScope>(*ehStack.begin()) && "top not a cleanup!");
564
565 // If we pushed an EH-only cleanup but exceptions are disabled, it will leave
566 // an effectively empty cleanup on the EH stack. In that case, there is
567 // nothing to do here except pop the cleanup.
568 cir::CleanupScopeOp cleanupScope = scope.getCleanupScopeOp();
569 if (!cleanupScope) {
570 assert(!scope.isNormalCleanup() && !scope.isEHCleanup() &&
571 "missing cir.cleanup.scope for active cleanup");
572 ehStack.popCleanup();
573 return;
574 }
575
576 bool requiresNormalCleanup = scope.isNormalCleanup();
577 bool requiresEHCleanup = scope.isEHCleanup();
578
579 // When we're popping a cleanup to deactivate it, we need to know if anything
580 // in the cleanup scope body region branches through the cleanup handler
581 // before the entire cleanup scope body has executed. If the cleanup scope
582 // body falls through, we don't want to emit normal cleanup code. However,
583 // if the cleanup body region contains early exits (return or goto), we do
584 // need to execute the normal cleanup when the early exit is taken. To handle
585 // that case, we guard the cleanup with an "active" flag so that it executes
586 // conditionally and set the flag to false when the cleanup body falls
587 // through. Classic codegen tracks this state with "hasBranches" and
588 // "getFixupDepth" on the cleanup scope, but because CIR uses structured
589 // control flow, we need to check for early exits and insert the active
590 // flag handling here. Note that when a cleanup is deactivated while not at
591 // the top of the stack, the active flag gets created in
592 // setupCleanupBlockDeactivation.
593 if (forDeactivation && requiresNormalCleanup) {
594 if (bodyHasBranchThroughExits(cleanupScope.getBodyRegion())) {
595 // The active flag shouldn't exist if the scope was at the top of the
596 // stack when it was deactivated.
597 assert(!scope.getActiveFlag().isValid() && "active flag already set");
598
599 // Create the flag.
600 mlir::Location loc = builder.getUnknownLoc();
602 builder.getBoolTy(), CharUnits::One(), loc, "cleanup.isactive");
603
604 // Initialize the flag to true before the cleanup scope (the point where
605 // the cleanup becomes active).
606 {
607 mlir::OpBuilder::InsertionGuard guard(builder);
608 builder.setInsertionPoint(cleanupScope);
609 builder.createFlagStore(loc, true, activeFlag.getPointer());
610 }
611
612 // Set the flag to false at the end of the cleanup scope body region.
613 assert(builder.getInsertionBlock() ==
614 &cleanupScope.getBodyRegion().back() &&
615 "expected insertion point in cleanup body");
616 builder.createFlagStore(loc, false, activeFlag.getPointer());
617
618 scope.setActiveFlag(activeFlag);
619 scope.setTestFlagInNormalCleanup(true);
620 } else {
621 // If the cleanup was pushed on the stack as normal+eh, downgrade it to
622 // eh-only.
623 if (requiresEHCleanup)
624 cleanupScope.setCleanupKind(cir::CleanupKind::EH);
625 requiresNormalCleanup = false;
626 }
627 }
628
629 Address normalActiveFlag = scope.shouldTestFlagInNormalCleanup()
630 ? scope.getActiveFlag()
632 Address ehActiveFlag = scope.shouldTestFlagInEHCleanup()
633 ? scope.getActiveFlag()
635
636 // If we don't need the cleanup at all, we're done.
637 if (!requiresNormalCleanup && !requiresEHCleanup) {
638 // If we get here, the cleanup scope isn't needed. Rather than try to move
639 // the contents of its body region out of the cleanup and erase it, we just
640 // add a yield to the cleanup region to make it valid but no-op. It will be
641 // erased during canonicalization.
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());
647 }
648 ehStack.popCleanup();
649 return;
650 }
651
652 // Copy the cleanup emission data out. This uses either a stack
653 // array or malloc'd memory, depending on the size, which is
654 // behavior that SmallVector would provide, if we could use it
655 // here. Unfortunately, if you ask for a SmallVector<char>, the
656 // alignment isn't sufficient.
657 auto *cleanupSource = reinterpret_cast<char *>(scope.getCleanupBuffer());
659 cleanupBufferStack[8 * sizeof(void *)];
660 std::unique_ptr<char[]> cleanupBufferHeap;
661 size_t cleanupSize = scope.getCleanupSize();
663
664 // This is necessary because we are going to deallocate the cleanup
665 // (in popCleanup) before we emit it.
666 if (cleanupSize <= sizeof(cleanupBufferStack)) {
667 memcpy(cleanupBufferStack, cleanupSource, cleanupSize);
668 cleanup = reinterpret_cast<EHScopeStack::Cleanup *>(cleanupBufferStack);
669 } else {
670 cleanupBufferHeap.reset(new char[cleanupSize]);
671 memcpy(cleanupBufferHeap.get(), cleanupSource, cleanupSize);
672 cleanup =
673 reinterpret_cast<EHScopeStack::Cleanup *>(cleanupBufferHeap.get());
674 }
675
676 EHScopeStack::Cleanup::Flags cleanupFlags;
677 if (scope.isNormalCleanup())
678 cleanupFlags.setIsNormalCleanupKind();
679 if (scope.isEHCleanup())
680 cleanupFlags.setIsEHCleanupKind();
681
682 // Determine the active flag for the cleanup handler.
683 Address cleanupActiveFlag = normalActiveFlag.isValid() ? normalActiveFlag
684 : ehActiveFlag.isValid() ? ehActiveFlag
686
687 // In CIR, the cleanup code is emitted into the cleanup region of the
688 // cir.cleanup.scope op. There is no CFG threading needed — the FlattenCFG
689 // pass handles lowering the structured cleanup scope.
690 ehStack.popCleanup();
691 scope.markEmitted();
692 emitCleanup(*this, cleanupScope, cleanup, cleanupFlags, cleanupActiveFlag);
693}
694
695/// Pops cleanup blocks until the given savepoint is reached.
697 EHScopeStack::stable_iterator oldCleanupStackDepth,
698 ArrayRef<mlir::Value *> valuesToReload) {
699 // If the current stack depth is the same as the cleanup stack depth,
700 // we won't be exiting any cleanup scopes, so we don't need to reload
701 // any values.
702 bool requiresCleanup = false;
703 for (auto it = ehStack.begin(), ie = ehStack.find(oldCleanupStackDepth);
704 it != ie; ++it) {
705 if (isa<EHCleanupScope>(&*it)) {
706 requiresCleanup = true;
707 break;
708 }
709 }
710
711 // If there are values that we need to keep live, spill them now before
712 // we pop the cleanup blocks. These are passed as pointers to mlir::Value
713 // because we're going to replace them with the reloaded value.
714 SmallVector<Address> tempAllocas;
715 if (requiresCleanup) {
716 for (mlir::Value *valPtr : valuesToReload) {
717 mlir::Value val = *valPtr;
718 if (!val)
719 continue;
720
721 // TODO(cir): Check for static allocas.
722
723 Address temp = createDefaultAlignTempAlloca(val.getType(), val.getLoc(),
724 "tmp.exprcleanup");
725 tempAllocas.push_back(temp);
726 builder.createStore(val.getLoc(), val, temp);
727 }
728 }
729
730 // Pop cleanup blocks until we reach the base stack depth for the
731 // current scope.
732 while (ehStack.stable_begin() != oldCleanupStackDepth)
734
735 // Reload the values that we spilled, if necessary.
736 if (requiresCleanup) {
737 for (auto [addr, valPtr] : llvm::zip(tempAllocas, valuesToReload)) {
738 mlir::Location loc = valPtr->getLoc();
739 *valPtr = builder.createLoad(loc, addr);
740 }
741 }
742}
743
744/// Pops cleanup blocks until the given savepoint is reached, then add the
745/// cleanups from the given savepoint in the lifetime-extended cleanups stack.
747 EHScopeStack::stable_iterator oldCleanupStackDepth,
748 size_t oldLifetimeExtendedSize, ArrayRef<mlir::Value *> valuesToReload) {
749 popCleanupBlocks(oldCleanupStackDepth, valuesToReload);
750
751 // Promote deferred lifetime-extended cleanups onto the EH scope stack.
752 for (const PendingCleanupEntry &cleanup : llvm::make_range(
753 lifetimeExtendedCleanupStack.begin() + oldLifetimeExtendedSize,
756 lifetimeExtendedCleanupStack.truncate(oldLifetimeExtendedSize);
757}
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)
tooling::Replacements cleanup(const FormatStyle &Style, StringRef Code, ArrayRef< tooling::Range > Ranges, StringRef FileName="<stdin>")
Clean up any erroneous/redundant code in the given Ranges in Code.
*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 b
__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
Definition Address.h:98
static Address invalid()
Definition Address.h:76
bool isValid() const
Definition Address.h:77
cir::AllocaOp getUnderlyingAllocaOp() const
Return the underlying alloca for this address, if any.
Definition Address.h:157
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()
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.
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)
cir::CleanupScopeOp getCleanupScopeOp()
static size_t getSizeForCleanupSize(size_t size)
Gets the size required for a lazy cleanup scope with the given cleanup-data requirements.
void setActiveFlag(Address var)
bool shouldTestFlagInNormalCleanup() const
void setActive(bool isActive)
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.
Definition ExprCXX.h:1463
static CharUnits One()
One - Construct a CharUnits quantity of one.
Definition CharUnits.h:58
This represents one expression.
Definition Expr.h:112
A (possibly-)qualified type.
Definition TypeBase.h:937
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)
Definition Address.h:330
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
U cast(CodeGen::Address addr)
Definition Address.h:327
static bool emitLifetimeMarkers()
A cleanup entry that will be promoted onto the EH scope stack at a later point.