[flang-commits] [flang] [flang] Assume matching shapes in elemental assignment with non-realloc lhs. (PR #118552)

via flang-commits flang-commits at lists.llvm.org
Wed Dec 4 00:50:10 PST 2024


================
@@ -338,32 +338,33 @@ ElementalAssignBufferization::findMatch(hlfir::ElementalOp elemental) {
   if (!fir::isa_trivial(eleTy))
     return std::nullopt;
 
-  // the array must have the same shape as the elemental. CSE should have
+  // The array must have the same shape as the elemental. CSE should have
   // deduplicated the fir.shape operations where they are provably the same
-  // so we just have to check for the same ssa value
-  // TODO: add more ways of getting the shape of the array
-  mlir::Value arrayShape;
-  if (match.array.getDefiningOp())
-    arrayShape =
-        mlir::TypeSwitch<mlir::Operation *, mlir::Value>(
-            match.array.getDefiningOp())
-            .Case([](hlfir::DesignateOp designate) {
-              return designate.getShape();
-            })
-            .Case([](hlfir::DeclareOp declare) { return declare.getShape(); })
-            .Default([](mlir::Operation *) { return mlir::Value{}; });
-  if (!arrayShape) {
-    LLVM_DEBUG(llvm::dbgs() << "Can't get shape of " << match.array << " at "
-                            << elemental->getLoc() << "\n");
-    return std::nullopt;
-  }
-  if (arrayShape != elemental.getShape()) {
-    // f2018 10.2.1.2 (3) requires the lhs and rhs of an assignment to be
-    // conformable unless the lhs is an allocatable array. In HLFIR we can
-    // see this from the presence or absence of the realloc attribute on
-    // hlfir.assign. If it is not a realloc assignment, we can trust that
-    // the shapes do conform
-    if (match.assign.getRealloc())
+  // so we just have to check for the same ssa value.
+  //
+  // f2018 10.2.1.2 (3) requires the lhs and rhs of an assignment to be
+  // conformable unless the lhs is an allocatable array. In HLFIR we can
+  // see this from the presence or absence of the realloc attribute on
+  // hlfir.assign. If it is not a realloc assignment, we can trust that
+  // the shapes do conform.
+  if (match.assign.getRealloc()) {
----------------
jeanPerier wrote:

Are there actually any cases were the check below will succeed?
Whole allocatable assignments are made to fir.ref<fir.box> LHS, and lower never generates fir.shape operands for the related declare/designate because the shape is dynamic.

We will need to optimize whole allocatable assignment, but this is its own can of worms (need to prove the LHS is allocated too, that it has the same length parameters for characters...).

Better to just return nullopt here I think and a TODO comment to optimize this case (maybe using code specialization?).

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


More information about the flang-commits mailing list