[flang-commits] [flang] [flang] Reset all extents to zero for empty hlfir.elemental loops. (PR #124867)

Slava Zakharin via flang-commits flang-commits at lists.llvm.org
Wed Jan 29 08:43:16 PST 2025


================
@@ -1759,3 +1759,38 @@ fir::factory::deduceOptimalExtents(mlir::ValueRange extents1,
   }
   return extents;
 }
+
+llvm::SmallVector<mlir::Value> fir::factory::updateRuntimeExtentsForEmptyArrays(
+    fir::FirOpBuilder &builder, mlir::Location loc, mlir::ValueRange extents) {
+  if (extents.size() <= 1)
+    return extents;
+
+  // Try to reduce the number of new zero constant operations.
+  // This just makes MLIR matching easier, if CSE is not run
+  // after this.
+  llvm::DenseMap<mlir::Type, mlir::Value> zeroesMap;
+  mlir::Type i1Type = builder.getI1Type();
+  mlir::Value isEmpty = createZeroValue(builder, loc, i1Type);
+  zeroesMap.try_emplace(i1Type, isEmpty);
+
+  llvm::SmallVector<mlir::Value, Fortran::common::maxRank> zeroes;
+  for (mlir::Value extent : extents) {
+    mlir::Type type = extent.getType();
+    mlir::Value zero = zeroesMap.lookup(type);
+    if (!zero) {
+      zero = createZeroValue(builder, loc, type);
+      zeroesMap.try_emplace(type, zero);
+    }
+    zeroes.push_back(zero);
+    mlir::Value isZero = builder.create<mlir::arith::CmpIOp>(
+        loc, mlir::arith::CmpIPredicate::eq, extent, zero);
----------------
vzakhari wrote:

Yes, there are folders in place to fold the compares and selects.  They are not applied as part of the bufferization pass, but the canonicalization will clean it up.

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


More information about the flang-commits mailing list