Skip to content

Commit 5cce160

Browse files
committed
[Flang][OpenMP] Prevent allocas from being inserted into loop wrappers
This patch updates the `FirOpBuilder::getAllocaBlock()` method to avoid returning blocks which are part of a region owned by a loop wrapper operation. This avoids introducing `fir.alloca` operations inside of loop wrappers, which would cause verifier errors due to strict restrictions on their allowed contents. These allocations will be created in the entry block of the first non-loop wrapper parent region.
1 parent c77d8ee commit 5cce160

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

flang/lib/Optimizer/Builder/FIRBuilder.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,29 @@ mlir::Block *fir::FirOpBuilder::getAllocaBlock() {
259259
return recipeIface.getAllocaBlock(getRegion());
260260
}
261261

262+
// All allocations associated with an OpenMP loop wrapper must happen outside
263+
// of all wrappers.
264+
mlir::Operation *currentOp = getRegion().getParentOp();
265+
auto wrapperIface =
266+
llvm::isa<mlir::omp::LoopNestOp>(currentOp)
267+
? llvm::cast<mlir::omp::LoopWrapperInterface>(
268+
currentOp->getParentOp())
269+
: llvm::dyn_cast<mlir::omp::LoopWrapperInterface>(currentOp);
270+
if (wrapperIface) {
271+
// Cannot use LoopWrapperInterface methods here because the whole nest may
272+
// not have been created at this point. Manually traverse parents instead.
273+
mlir::omp::LoopWrapperInterface lastWrapperOp = wrapperIface;
274+
while (true) {
275+
if (auto nextWrapper =
276+
llvm::dyn_cast_if_present<mlir::omp::LoopWrapperInterface>(
277+
lastWrapperOp->getParentOp()))
278+
lastWrapperOp = nextWrapper;
279+
else
280+
break;
281+
}
282+
return &lastWrapperOp->getParentRegion()->front();
283+
}
284+
262285
return getEntryBlock();
263286
}
264287

0 commit comments

Comments
 (0)