@@ -317,10 +317,27 @@ void CIRGenFunction::emitIgnoredExpr(const Expr *e) {
317317}
318318
319319mlir::Value CIRGenFunction::emitAlloca (StringRef name, mlir::Type ty,
320- mlir::Location loc,
321- CharUnits alignment) {
322- mlir::Block *entryBlock = getCurFunctionEntryBlock ();
320+ mlir::Location loc, CharUnits alignment,
321+ bool insertIntoFnEntryBlock,
322+ mlir::Value arraySize) {
323+ mlir::Block *entryBlock = insertIntoFnEntryBlock
324+ ? getCurFunctionEntryBlock ()
325+ : curLexScope->getEntryBlock ();
326+
327+ // If this is an alloca in the entry basic block of a cir.try and there's
328+ // a surrounding cir.scope, make sure the alloca ends up in the surrounding
329+ // scope instead. This is necessary in order to guarantee all SSA values are
330+ // reachable during cleanups.
331+ assert (!cir::MissingFeatures::tryOp ());
332+
333+ return emitAlloca (name, ty, loc, alignment,
334+ builder.getBestAllocaInsertPoint (entryBlock), arraySize);
335+ }
323336
337+ mlir::Value CIRGenFunction::emitAlloca (StringRef name, mlir::Type ty,
338+ mlir::Location loc, CharUnits alignment,
339+ mlir::OpBuilder::InsertPoint ip,
340+ mlir::Value arraySize) {
324341 // CIR uses its own alloca address space rather than follow the target data
325342 // layout like original CodeGen. The data layout awareness should be done in
326343 // the lowering pass instead.
@@ -331,7 +348,7 @@ mlir::Value CIRGenFunction::emitAlloca(StringRef name, mlir::Type ty,
331348 mlir::Value addr;
332349 {
333350 mlir::OpBuilder::InsertionGuard guard (builder);
334- builder.restoreInsertionPoint (builder. getBestAllocaInsertPoint (entryBlock) );
351+ builder.restoreInsertionPoint (ip );
335352 addr = builder.createAlloca (loc, /* addr type*/ localVarPtrTy,
336353 /* var type*/ ty, name, alignIntAttr);
337354 assert (!cir::MissingFeatures::astVarDeclInterface ());
@@ -346,11 +363,13 @@ mlir::Value CIRGenFunction::createDummyValue(mlir::Location loc,
346363 return builder.createDummyValue (loc, t, alignment);
347364}
348365
349- // / This creates an alloca and inserts it at the current insertion point of the
350- // / builder.
366+ // / This creates an alloca and inserts it into the entry block if
367+ // / \p insertIntoFnEntryBlock is true, otherwise it inserts it at the current
368+ // / insertion point of the builder.
351369Address CIRGenFunction::createTempAlloca (mlir::Type ty, CharUnits align,
352- mlir::Location loc,
353- const Twine &name) {
354- mlir::Value alloca = emitAlloca (name.str (), ty, loc, align);
370+ mlir::Location loc, const Twine &name,
371+ bool insertIntoFnEntryBlock) {
372+ mlir::Value alloca =
373+ emitAlloca (name.str (), ty, loc, align, insertIntoFnEntryBlock);
355374 return Address (alloca, ty, align);
356375}
0 commit comments