[llvm] [mlir] [Utils][mlir] Fix interaction between CodeExtractor and OpenMPIRBuilder (PR #145051)

Kajetan Puchalski via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 23 08:30:35 PDT 2025


================
@@ -1868,8 +1866,14 @@ CallInst *CodeExtractor::emitReplacerCall(
   // This takes place of the original loop
   BasicBlock *codeReplacer =
       BasicBlock::Create(Context, "codeRepl", oldFunction, ReplIP);
+  // In cases with multiple levels of outlining, e.g. with OpenMP,
+  // AllocationBlock may end up in a function different than oldFunction. We
+  // need to make sure we do not use it in those cases, otherwise the alloca
+  // will end up in a different function from its users and break the module.
   BasicBlock *AllocaBlock =
-      AllocationBlock ? AllocationBlock : &oldFunction->getEntryBlock();
+      (AllocationBlock && oldFunction == AllocationBlock->getParent())
+          ? AllocationBlock
+          : &oldFunction->getEntryBlock();
----------------
mrkajetanp wrote:

Whenever things are saved on that stack, the thing being saved is what was previously returned by findAllocaInsertPoint. E.g.:
https://github.com/llvm/llvm-project/blob/fccab5d757778204666d70e2f1592952fc8b336d/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp#L2265

The specific call which finds the block in a different function during the stack walk is inside convertOmpTarget, here:
https://github.com/llvm/llvm-project/blob/fccab5d757778204666d70e2f1592952fc8b336d/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp#L5555.

findAllocaInsertPoint defines its own insert point if it can't already find one, hence adding that extra check fixes the problem at hand. In this case the outer op - task - does add the alloca insertion point to the stack like it should, but that insertion point is inside of main as opposed to the outlined function for task.

https://github.com/llvm/llvm-project/pull/145051


More information about the llvm-commits mailing list