@@ -261,10 +261,27 @@ void CIRGenFunction::emitIgnoredExpr(const Expr *e) {
261261}
262262
263263mlir::Value CIRGenFunction::emitAlloca (StringRef name, mlir::Type ty,
264- mlir::Location loc,
265- CharUnits alignment) {
266- mlir::Block *entryBlock = getCurFunctionEntryBlock ();
264+ mlir::Location loc, CharUnits alignment,
265+ bool insertIntoFnEntryBlock,
266+ mlir::Value arraySize) {
267+ mlir::Block *entryBlock = insertIntoFnEntryBlock
268+ ? getCurFunctionEntryBlock ()
269+ : curLexScope->getEntryBlock ();
270+
271+ // If this is an alloca in the entry basic block of a cir.try and there's
272+ // a surrounding cir.scope, make sure the alloca ends up in the surrounding
273+ // scope instead. This is necessary in order to guarantee all SSA values are
274+ // reachable during cleanups.
275+ assert (!cir::MissingFeatures::tryOp ());
276+
277+ return emitAlloca (name, ty, loc, alignment,
278+ builder.getBestAllocaInsertPoint (entryBlock), arraySize);
279+ }
267280
281+ mlir::Value CIRGenFunction::emitAlloca (StringRef name, mlir::Type ty,
282+ mlir::Location loc, CharUnits alignment,
283+ mlir::OpBuilder::InsertPoint ip,
284+ mlir::Value arraySize) {
268285 // CIR uses its own alloca address space rather than follow the target data
269286 // layout like original CodeGen. The data layout awareness should be done in
270287 // the lowering pass instead.
@@ -275,7 +292,7 @@ mlir::Value CIRGenFunction::emitAlloca(StringRef name, mlir::Type ty,
275292 mlir::Value addr;
276293 {
277294 mlir::OpBuilder::InsertionGuard guard (builder);
278- builder.restoreInsertionPoint (builder. getBestAllocaInsertPoint (entryBlock) );
295+ builder.restoreInsertionPoint (ip );
279296 addr = builder.createAlloca (loc, /* addr type*/ localVarPtrTy,
280297 /* var type*/ ty, name, alignIntAttr);
281298 assert (!cir::MissingFeatures::astVarDeclInterface ());
@@ -290,11 +307,13 @@ mlir::Value CIRGenFunction::createDummyValue(mlir::Location loc,
290307 return builder.createDummyValue (loc, t, alignment);
291308}
292309
293- // / This creates an alloca and inserts it at the current insertion point of the
294- // / builder.
310+ // / This creates an alloca and inserts it into the entry block if
311+ // / \p insertIntoFnEntryBlock is true, otherwise it inserts it at the current
312+ // / insertion point of the builder.
295313Address CIRGenFunction::createTempAlloca (mlir::Type ty, CharUnits align,
296- mlir::Location loc,
297- const Twine &name) {
298- mlir::Value alloca = emitAlloca (name.str (), ty, loc, align);
314+ mlir::Location loc, const Twine &name,
315+ bool insertIntoFnEntryBlock) {
316+ mlir::Value alloca =
317+ emitAlloca (name.str (), ty, loc, align, insertIntoFnEntryBlock);
299318 return Address (alloca, ty, align);
300319}
0 commit comments