@@ -158,9 +158,9 @@ void CIRGenFunction::PopCleanupBlock(bool FallthroughIsBranchThrough) {
158158
159159 // Check whether we need an EH cleanup. This is only true if we've
160160 // 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 );
164164 EHScopeStack::stable_iterator EHParent = Scope.getEnclosingEHScope ();
165165
166166 // Check the three conditions which might require a normal cleanup:
@@ -300,8 +300,8 @@ void CIRGenFunction::PopCleanupBlock(bool FallthroughIsBranchThrough) {
300300 }
301301
302302 assert (tryOp && " expected available cir.try" );
303- auto *NextAction = getEHDispatchBlock (EHParent, tryOp);
304- (void )NextAction ;
303+ auto *nextAction = getEHDispatchBlock (EHParent, tryOp);
304+ (void )nextAction ;
305305
306306 // Push a terminate scope or cleanupendpad scope around the potentially
307307 // throwing cleanups. For funclet EH personalities, the cleanupendpad models
@@ -328,23 +328,34 @@ void CIRGenFunction::PopCleanupBlock(bool FallthroughIsBranchThrough) {
328328 if (EHActiveFlag.isValid () || IsActive) {
329329 cleanupFlags.setIsForEHCleanup ();
330330 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- }
338331
339- auto yield = cast<YieldOp>(cleanup ->getTerminator ());
332+ auto yield = cast<YieldOp>(ehEntry ->getTerminator ());
340333 builder.setInsertionPoint (yield);
341334 buildCleanup (*this , Fn, cleanupFlags, EHActiveFlag);
342335 }
343336
344- // In LLVM traditional codegen, here's where it branches off to
345- // NextAction.
346337 if (CPI)
347338 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+ }
348359
349360 // Leave the terminate scope.
350361 if (PushedTerminate)
0 commit comments