Skip to content

Commit b163e52

Browse files
authored
[flang][openacc][hlfir] Add declare op in reduction recipe (#67484)
Same change than #67368 but for the reduction recipe.
1 parent 329c64c commit b163e52

File tree

2 files changed

+104
-55
lines changed

2 files changed

+104
-55
lines changed

flang/lib/Lower/OpenACC.cpp

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ static constexpr std::int64_t starCst = -1;
3535
static unsigned routineCounter = 0;
3636
static constexpr llvm::StringRef accRoutinePrefix = "acc_routine_";
3737
static constexpr llvm::StringRef accPrivateInitName = "acc.private.init";
38+
static constexpr llvm::StringRef accReductionInitName = "acc.reduction.init";
3839

3940
static mlir::Location
4041
genOperandLocation(Fortran::lower::AbstractConverter &converter,
@@ -344,6 +345,16 @@ static void genDataExitOperations(fir::FirOpBuilder &builder,
344345
}
345346
}
346347

348+
fir::ShapeOp genShapeOp(mlir::OpBuilder &builder, fir::SequenceType seqTy,
349+
mlir::Location loc) {
350+
llvm::SmallVector<mlir::Value> extents;
351+
mlir::Type idxTy = builder.getIndexType();
352+
for (auto extent : seqTy.getShape())
353+
extents.push_back(builder.create<mlir::arith::ConstantOp>(
354+
loc, idxTy, builder.getIntegerAttr(idxTy, extent)));
355+
return builder.create<fir::ShapeOp>(loc, extents);
356+
}
357+
347358
template <typename RecipeOp>
348359
static void genPrivateLikeInitRegion(mlir::OpBuilder &builder, RecipeOp recipe,
349360
mlir::Type ty, mlir::Location loc) {
@@ -361,12 +372,7 @@ static void genPrivateLikeInitRegion(mlir::OpBuilder &builder, RecipeOp recipe,
361372
TODO(loc, "private recipe of array with dynamic extents");
362373
if (fir::isa_trivial(seqTy.getEleTy())) {
363374
auto alloca = builder.create<fir::AllocaOp>(loc, seqTy);
364-
llvm::SmallVector<mlir::Value> extents;
365-
mlir::Type idxTy = builder.getIndexType();
366-
for (auto extent : seqTy.getShape())
367-
extents.push_back(builder.create<mlir::arith::ConstantOp>(
368-
loc, idxTy, builder.getIntegerAttr(idxTy, extent)));
369-
auto shapeOp = builder.create<fir::ShapeOp>(loc, extents);
375+
auto shapeOp = genShapeOp(builder, seqTy, loc);
370376
auto declareOp = builder.create<hlfir::DeclareOp>(
371377
loc, alloca, accPrivateInitName, shapeOp,
372378
llvm::ArrayRef<mlir::Value>{}, fir::FortranVariableFlagsAttr{});
@@ -708,14 +714,21 @@ static mlir::Value genReductionInitRegion(fir::FirOpBuilder &builder,
708714
mlir::Value initValue = getReductionInitValue(builder, loc, ty, op);
709715
if (fir::isa_trivial(ty)) {
710716
mlir::Value alloca = builder.create<fir::AllocaOp>(loc, ty);
717+
auto declareOp = builder.create<hlfir::DeclareOp>(
718+
loc, alloca, accReductionInitName, /*shape=*/nullptr,
719+
llvm::ArrayRef<mlir::Value>{}, fir::FortranVariableFlagsAttr{});
711720
builder.create<fir::StoreOp>(loc, builder.createConvert(loc, ty, initValue),
712-
alloca);
713-
return alloca;
721+
declareOp.getBase());
722+
return declareOp.getBase();
714723
} else if (auto seqTy = mlir::dyn_cast_or_null<fir::SequenceType>(ty)) {
715724
if (seqTy.hasDynamicExtents())
716725
TODO(loc, "private recipe of array with dynamic extents");
717726
if (fir::isa_trivial(seqTy.getEleTy())) {
718727
mlir::Value alloca = builder.create<fir::AllocaOp>(loc, seqTy);
728+
auto shapeOp = genShapeOp(builder, seqTy, loc);
729+
auto declareOp = builder.create<hlfir::DeclareOp>(
730+
loc, alloca, accReductionInitName, shapeOp,
731+
llvm::ArrayRef<mlir::Value>{}, fir::FortranVariableFlagsAttr{});
719732
mlir::Type idxTy = builder.getIndexType();
720733
mlir::Type refTy = fir::ReferenceType::get(seqTy.getEleTy());
721734
llvm::SmallVector<fir::DoLoopOp> loops;
@@ -730,10 +743,11 @@ static mlir::Value genReductionInitRegion(fir::FirOpBuilder &builder,
730743
loops.push_back(loop);
731744
ivs.push_back(loop.getInductionVar());
732745
}
733-
auto coord = builder.create<fir::CoordinateOp>(loc, refTy, alloca, ivs);
746+
auto coord = builder.create<fir::CoordinateOp>(loc, refTy,
747+
declareOp.getBase(), ivs);
734748
builder.create<fir::StoreOp>(loc, initValue, coord);
735749
builder.setInsertionPointAfter(loops[0]);
736-
return alloca;
750+
return declareOp.getBase();
737751
}
738752
}
739753
llvm::report_fatal_error("Unsupported OpenACC reduction type");

0 commit comments

Comments
 (0)