[flang-commits] [flang] [Flang] Generate inline reduction loops for elemental count intrinsics (PR #75774)

David Green via flang-commits flang-commits at lists.llvm.org
Mon Dec 18 05:48:01 PST 2023


================
@@ -659,6 +659,124 @@ mlir::LogicalResult VariableAssignBufferization::matchAndRewrite(
   return mlir::success();
 }
 
+using GenBodyFn =
+    std::function<mlir::Value(fir::FirOpBuilder &, mlir::Location, mlir::Value,
+                              const llvm::SmallVectorImpl<mlir::Value> &)>;
+static mlir::Value generateReductionLoop(fir::FirOpBuilder &builder,
+                                         mlir::Location loc, mlir::Value init,
+                                         mlir::Value shape, GenBodyFn genBody) {
+  auto extents = hlfir::getIndexExtents(loc, builder, shape);
+  mlir::Value reduction = init;
+  mlir::IndexType idxTy = builder.getIndexType();
+  mlir::Value oneIdx = builder.createIntegerConstant(loc, idxTy, 1);
+
+  // Create a reduction loop nest. We use one-based indices so that they can be
+  // passed to the elemental.
+  llvm::SmallVector<mlir::Value> indices;
+  for (unsigned i = 0; i < extents.size(); ++i) {
----------------
davemgreen wrote:

Yeah I had seen that method, but unfortunately it didn't seem to generate a do loop which would produce a value for the reduction.

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


More information about the flang-commits mailing list