@@ -633,13 +633,25 @@ void ReductionProcessor::processReductionArguments(
633633 }
634634 }
635635
636- fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder ();
637-
638636 // Reduction variable processing common to both intrinsic operators and
639637 // procedure designators
640638 fir::FirOpBuilder &builder = converter.getFirOpBuilder ();
639+ mlir::OpBuilder::InsertPoint dcIP;
640+ constexpr bool isDoConcurrent =
641+ std::is_same_v<OpType, fir::DeclareReductionOp>;
642+
643+ if (isDoConcurrent) {
644+ dcIP = builder.saveInsertionPoint ();
645+ builder.setInsertionPoint (
646+ builder.getRegion ().getParentOfType <fir::DoConcurrentOp>());
647+ }
648+
641649 for (const semantics::Symbol *symbol : reductionSymbols) {
642650 mlir::Value symVal = converter.getSymbolAddress (*symbol);
651+
652+ if (auto declOp = symVal.getDefiningOp <hlfir::DeclareOp>())
653+ symVal = declOp.getBase ();
654+
643655 mlir::Type eleType;
644656 auto refType = mlir::dyn_cast_or_null<fir::ReferenceType>(symVal.getType ());
645657 if (refType)
@@ -667,13 +679,13 @@ void ReductionProcessor::processReductionArguments(
667679 // boxed arrays are passed as values not by reference. Unfortunately,
668680 // we can't pass a box by value to omp.redution_declare, so turn it
669681 // into a reference
670-
682+ auto oldIP = builder.saveInsertionPoint ();
683+ builder.setInsertionPointToStart (builder.getAllocaBlock ());
671684 auto alloca =
672685 builder.create <fir::AllocaOp>(currentLocation, symVal.getType ());
686+ builder.restoreInsertionPoint (oldIP);
673687 builder.create <fir::StoreOp>(currentLocation, symVal, alloca);
674688 symVal = alloca;
675- } else if (auto declOp = symVal.getDefiningOp <hlfir::DeclareOp>()) {
676- symVal = declOp.getBase ();
677689 }
678690
679691 // this isn't the same as the by-val and by-ref passing later in the
@@ -693,7 +705,7 @@ void ReductionProcessor::processReductionArguments(
693705 unsigned idx = 0 ;
694706 for (auto [symVal, isByRef] : llvm::zip (reductionVars, reduceVarByRef)) {
695707 auto redType = mlir::cast<fir::ReferenceType>(symVal.getType ());
696- const auto &kindMap = firOpBuilder .getKindMap ();
708+ const auto &kindMap = builder .getKindMap ();
697709 std::string reductionName;
698710 ReductionIdentifier redId;
699711
@@ -745,9 +757,12 @@ void ReductionProcessor::processReductionArguments(
745757 OpType decl = createDeclareReduction<OpType>(
746758 converter, reductionName, redId, redType, currentLocation, isByRef);
747759 reductionDeclSymbols.push_back (
748- mlir::SymbolRefAttr::get (firOpBuilder .getContext (), decl.getSymName ()));
760+ mlir::SymbolRefAttr::get (builder .getContext (), decl.getSymName ()));
749761 ++idx;
750762 }
763+
764+ if (isDoConcurrent)
765+ builder.restoreInsertionPoint (dcIP);
751766}
752767
753768const semantics::SourceName
0 commit comments