Skip to content

Commit 32311a6

Browse files
committed
Reland [flang] Generalized simplification of HLFIR reduction ops. (llvm#136071)
This change generalizes SumAsElemental inlining in SimplifyHLFIRIntrinsics pass so that it can be applied to ALL, ANY, COUNT, MAXLOC, MAXVAL, MINLOC, MINVAL, SUM. This change makes the special handling of the reduction operations in OptimizedBufferization redundant: once HLFIR operations are inlined, the hlfir.elemental inlining should do the rest of the job.
1 parent 69ade7c commit 32311a6

18 files changed

+2286
-1999
lines changed

flang/include/flang/Optimizer/Builder/HLFIRTools.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,11 @@ mlir::Value genExtent(mlir::Location loc, fir::FirOpBuilder &builder,
301301
mlir::Value genLBound(mlir::Location loc, fir::FirOpBuilder &builder,
302302
hlfir::Entity entity, unsigned dim);
303303

304+
/// Compute the lower bounds of \p entity, which is an array of known rank.
305+
llvm::SmallVector<mlir::Value> genLBounds(mlir::Location loc,
306+
fir::FirOpBuilder &builder,
307+
hlfir::Entity entity);
308+
304309
/// Generate a vector of extents with index type from a fir.shape
305310
/// of fir.shape_shift value.
306311
llvm::SmallVector<mlir::Value> getIndexExtents(mlir::Location loc,

flang/lib/Optimizer/Builder/HLFIRTools.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,33 @@ mlir::Value hlfir::genLBound(mlir::Location loc, fir::FirOpBuilder &builder,
659659
return dimInfo.getLowerBound();
660660
}
661661

662+
llvm::SmallVector<mlir::Value> hlfir::genLBounds(mlir::Location loc,
663+
fir::FirOpBuilder &builder,
664+
hlfir::Entity entity) {
665+
assert(!entity.isAssumedRank() &&
666+
"cannot compute all lower bounds for assumed rank");
667+
assert(!entity.isScalar() && "expected an array entity");
668+
int rank = entity.getRank();
669+
mlir::Type idxTy = builder.getIndexType();
670+
if (!entity.mayHaveNonDefaultLowerBounds())
671+
return {static_cast<std::size_t>(rank),
672+
builder.createIntegerConstant(loc, idxTy, 1)};
673+
674+
if (auto shape = tryRetrievingShapeOrShift(entity)) {
675+
auto lbounds = getExplicitLboundsFromShape(shape);
676+
if (!lbounds.empty())
677+
return lbounds;
678+
}
679+
680+
if (entity.isMutableBox())
681+
entity = hlfir::derefPointersAndAllocatables(loc, builder, entity);
682+
683+
llvm::SmallVector<mlir::Value> lbounds;
684+
fir::factory::genDimInfoFromBox(builder, loc, entity, &lbounds,
685+
/*extents=*/nullptr, /*strides=*/nullptr);
686+
return lbounds;
687+
}
688+
662689
void hlfir::genLengthParameters(mlir::Location loc, fir::FirOpBuilder &builder,
663690
Entity entity,
664691
llvm::SmallVectorImpl<mlir::Value> &result) {

flang/lib/Optimizer/HLFIR/Transforms/OptimizedBufferization.cpp

Lines changed: 0 additions & 465 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)