@@ -158,9 +158,9 @@ void CIRGenFunction::PopCleanupBlock(bool FallthroughIsBranchThrough) {
158
158
159
159
// Check whether we need an EH cleanup. This is only true if we've
160
160
// generated a lazy EH cleanup block.
161
- auto *EHEntry = Scope.getCachedEHDispatchBlock ();
162
- assert (Scope.hasEHBranches () == (EHEntry != nullptr ));
163
- bool RequiresEHCleanup = (EHEntry != nullptr );
161
+ auto *ehEntry = Scope.getCachedEHDispatchBlock ();
162
+ assert (Scope.hasEHBranches () == (ehEntry != nullptr ));
163
+ bool RequiresEHCleanup = (ehEntry != nullptr );
164
164
EHScopeStack::stable_iterator EHParent = Scope.getEnclosingEHScope ();
165
165
166
166
// Check the three conditions which might require a normal cleanup:
@@ -300,8 +300,8 @@ void CIRGenFunction::PopCleanupBlock(bool FallthroughIsBranchThrough) {
300
300
}
301
301
302
302
assert (tryOp && " expected available cir.try" );
303
- auto *NextAction = getEHDispatchBlock (EHParent, tryOp);
304
- (void )NextAction ;
303
+ auto *nextAction = getEHDispatchBlock (EHParent, tryOp);
304
+ (void )nextAction ;
305
305
306
306
// Push a terminate scope or cleanupendpad scope around the potentially
307
307
// throwing cleanups. For funclet EH personalities, the cleanupendpad models
@@ -328,23 +328,34 @@ void CIRGenFunction::PopCleanupBlock(bool FallthroughIsBranchThrough) {
328
328
if (EHActiveFlag.isValid () || IsActive) {
329
329
cleanupFlags.setIsForEHCleanup ();
330
330
mlir::OpBuilder::InsertionGuard guard (builder);
331
- if (!tryOp.isCleanupActive ())
332
- builder.createBlock (&tryOp.getCleanupRegion ());
333
- mlir::Block *cleanup = &tryOp.getCleanupRegion ().back ();
334
- if (cleanup->empty ()) {
335
- builder.setInsertionPointToEnd (cleanup);
336
- builder.createYield (tryOp.getLoc ());
337
- }
338
331
339
- auto yield = cast<YieldOp>(cleanup ->getTerminator ());
332
+ auto yield = cast<YieldOp>(ehEntry ->getTerminator ());
340
333
builder.setInsertionPoint (yield);
341
334
buildCleanup (*this , Fn, cleanupFlags, EHActiveFlag);
342
335
}
343
336
344
- // In LLVM traditional codegen, here's where it branches off to
345
- // NextAction.
346
337
if (CPI)
347
338
llvm_unreachable (" NYI" );
339
+ else {
340
+ // In LLVM traditional codegen, here's where it branches off to
341
+ // nextAction. CIR does not have a flat layout at this point, so
342
+ // instead patch all the landing pads that need to run this cleanup
343
+ // as well.
344
+ mlir::Block *currBlock = ehEntry;
345
+ while (currBlock && cleanupsToPatch.contains (currBlock)) {
346
+ mlir::OpBuilder::InsertionGuard guard (builder);
347
+ mlir::Block *blockToPatch = cleanupsToPatch[currBlock];
348
+ auto currYield = cast<YieldOp>(blockToPatch->getTerminator ());
349
+ builder.setInsertionPoint (currYield);
350
+ buildCleanup (*this , Fn, cleanupFlags, EHActiveFlag);
351
+ currBlock = blockToPatch;
352
+ }
353
+
354
+ // The nextAction is yet to be populated, register that this
355
+ // cleanup should also incorporate any cleanup from nextAction
356
+ // when available.
357
+ cleanupsToPatch[nextAction] = ehEntry;
358
+ }
348
359
349
360
// Leave the terminate scope.
350
361
if (PushedTerminate)
0 commit comments