[clang] [flang] [llvm] [mlir] [OMPIRBuilder] always leave PARALLEL via the same barrier (PR #164586)
Michael Kruse via llvm-commits
llvm-commits at lists.llvm.org
Thu Nov 20 08:16:19 PST 2025
================
@@ -675,6 +675,60 @@ OpenMPIRBuilder::getOrCreateRuntimeFunction(Module &M, RuntimeFunction FnID) {
return {FnTy, Fn};
}
+Expected<BasicBlock *>
+OpenMPIRBuilder::FinalizationInfo::getFiniBB(IRBuilderBase &Builder) {
+ if (!FiniBB) {
+ Function *ParentFunc = Builder.GetInsertBlock()->getParent();
+ IRBuilderBase::InsertPointGuard Guard(Builder);
+ FiniBB = BasicBlock::Create(Builder.getContext(), ".fini", ParentFunc);
+ Builder.SetInsertPoint(FiniBB);
+ // FiniCB adds the branch to the exit stub.
+ if (Error Err = FiniCB(Builder.saveIP()))
+ return Err;
+ }
+ return FiniBB;
+}
+
+Error OpenMPIRBuilder::FinalizationInfo::mergeFiniBB(IRBuilderBase &Builder,
+ BasicBlock *OtherFiniBB) {
+ // Simple case: FiniBB does not exist yet: re-use OtherFiniBB.
+ if (!FiniBB) {
+ FiniBB = OtherFiniBB;
+ IRBuilderBase::InsertPointGuard Guard(Builder);
+ Builder.SetInsertPoint(FiniBB->getFirstNonPHIIt());
+ if (Error Err = FiniCB(Builder.saveIP()))
+ return Err;
+
+ return Error::success();
+ }
+
+ // Move instructions from FiniBB to the start of OtherFiniBB.
+ auto InsertAfter = OtherFiniBB->getFirstNonPHIIt();
+ for (Instruction &I : make_early_inc_range(*FiniBB)) {
+ if (I.isTerminator()) {
+ I.eraseFromParent();
+ break;
+ }
+ I.insertAfter(InsertAfter);
+ InsertAfter = BasicBlock::iterator(&I);
+ }
+
+ // Replace FiniBB with OtherFiniBB.
+ for (User *U : make_early_inc_range(FiniBB->users())) {
+ if (auto *Instr = dyn_cast<Instruction>(U)) {
----------------
Meinersbur wrote:
```suggestion
auto *Instr = dyn_cast<Instruction>(U);
if (!Instr)
continue;
```
[style] [Use Early Exits and continue to Simplify Code](https://llvm.org/docs/CodingStandards.html#use-early-exits-and-continue-to-simplify-code)
https://github.com/llvm/llvm-project/pull/164586
More information about the llvm-commits
mailing list