[flang-commits] [flang] f809bf2 - [flang][hlfir] Use complete form of DesignateOp for im/re array designators.

Slava Zakharin via flang-commits flang-commits at lists.llvm.org
Fri May 19 09:02:14 PDT 2023


Author: Slava Zakharin
Date: 2023-05-19T09:02:03-07:00
New Revision: f809bf2b328f1ebfb2d56b804130a67c3ec71985

URL: https://github.com/llvm/llvm-project/commit/f809bf2b328f1ebfb2d56b804130a67c3ec71985
DIFF: https://github.com/llvm/llvm-project/commit/f809bf2b328f1ebfb2d56b804130a67c3ec71985.diff

LOG: [flang][hlfir] Use complete form of DesignateOp for im/re array designators.

With this change, the lowering provides full slice triples for
designators like ...%array_comp%im/re, so that the codegen
does not have to figure it out for the array_comp.
Basically, ...%array_comp%im/re is lowered the same way
as ...%array_comp(:,:,...)%im/re.

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D150842

Added: 
    

Modified: 
    flang/lib/Lower/ConvertExprToHLFIR.cpp
    flang/test/Lower/HLFIR/designators-component-ref.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/ConvertExprToHLFIR.cpp b/flang/lib/Lower/ConvertExprToHLFIR.cpp
index e70a9182ad6c..9522351f214a 100644
--- a/flang/lib/Lower/ConvertExprToHLFIR.cpp
+++ b/flang/lib/Lower/ConvertExprToHLFIR.cpp
@@ -650,6 +650,31 @@ class HlfirDesignatorBuilder {
              "Fortran designators can only have one ranked part");
       return changeElementType(baseType, componentBaseType);
     }
+
+    if (partInfo.complexPart && partInfo.componentShape) {
+      // Treat ...array_comp%im/re as ...array_comp(:,:,...)%im/re
+      // so that the codegen has the full slice triples for the component
+      // readily available.
+      fir::FirOpBuilder &builder = getBuilder();
+      mlir::Type idxTy = builder.getIndexType();
+      mlir::Value one = builder.createIntegerConstant(loc, idxTy, 1);
+
+      llvm::SmallVector<mlir::Value> resultExtents;
+      // Collect <lb, ub> pairs from the component shape.
+      auto bounds = hlfir::genBounds(loc, builder, partInfo.componentShape);
+      for (auto &boundPair : bounds) {
+        // The default subscripts are <lb, ub, 1>:
+        partInfo.subscripts.emplace_back(hlfir::DesignateOp::Triplet{
+            boundPair.first, boundPair.second, one});
+        auto extentValue = builder.genExtentFromTriplet(
+            loc, boundPair.first, boundPair.second, one, idxTy);
+        resultExtents.push_back(extentValue);
+      }
+      // The result shape is: <max((ub - lb + 1) / 1, 0), ...>.
+      partInfo.resultShape = builder.genShape(loc, resultExtents);
+      return componentBaseType;
+    }
+
     // scalar%array_comp or scalar%scalar. In any case the shape of this
     // part-ref is coming from the component.
     partInfo.resultShape = partInfo.componentShape;

diff  --git a/flang/test/Lower/HLFIR/designators-component-ref.f90 b/flang/test/Lower/HLFIR/designators-component-ref.f90
index 5c10d445b399..7cd355d95ac2 100644
--- a/flang/test/Lower/HLFIR/designators-component-ref.f90
+++ b/flang/test/Lower/HLFIR/designators-component-ref.f90
@@ -30,6 +30,10 @@ module comp_ref
   integer :: scalar_i
   character(5) :: array_char_comp(10,20)
 end type
+
+type t_complex
+   complex :: array_comp(2:11,3:22)
+end type
 end module
 
 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@@ -330,3 +334,36 @@ subroutine test_array_ref_chain(a)
 ! CHECK:  %[[VAL_19:.*]] = arith.constant 5 : index
 ! CHECK:  %[[VAL_20:.*]] = hlfir.designate %[[VAL_14]]{"array_comp"} <%[[VAL_17]]> (%[[VAL_18]], %[[VAL_19]])  shape %[[VAL_13]] : (!fir.box<!fir.array<10x!fir.type<_QMcomp_refTt_array{scalar_i:i32,array_comp:!fir.array<10x20xf32>}>>>, !fir.shape<2>, index, index, !fir.shape<1>) -> !fir.box<!fir.array<10xf32>>
 end subroutine
+
+subroutine test_scalar_array_complex_chain(a)
+  use comp_ref
+  type(t_complex) :: a
+  print *, a%array_comp%im
+! CHECK-LABEL:   func.func @_QPtest_scalar_array_complex_chain(
+! CHECK:           %[[VAL_1:.*]]:2 = hlfir.declare %[[VAL_0]] {uniq_name = "_QFtest_scalar_array_complex_chainEa"} : (!fir.ref<!fir.type<_QMcomp_refTt_complex{array_comp:!fir.array<10x20x!fir.complex<4>>}>>) -> (!fir.ref<!fir.type<_QMcomp_refTt_complex{array_comp:!fir.array<10x20x!fir.complex<4>>}>>, !fir.ref<!fir.type<_QMcomp_refTt_complex{array_comp:!fir.array<10x20x!fir.complex<4>>}>>)
+! CHECK:           %[[VAL_7:.*]] = arith.constant 10 : index
+! CHECK:           %[[VAL_8:.*]] = arith.constant 20 : index
+! CHECK:           %[[VAL_9:.*]] = arith.constant 2 : index
+! CHECK:           %[[VAL_10:.*]] = arith.constant 3 : index
+! CHECK:           %[[VAL_11:.*]] = fir.shape_shift %[[VAL_9]], %[[VAL_7]], %[[VAL_10]], %[[VAL_8]] : (index, index, index, index) -> !fir.shapeshift<2>
+! CHECK:           %[[VAL_12:.*]] = arith.constant 1 : index
+! CHECK:           %[[VAL_13:.*]] = arith.constant 1 : index
+! CHECK:           %[[VAL_14:.*]] = arith.addi %[[VAL_9]], %[[VAL_7]] : index
+! CHECK:           %[[VAL_15:.*]] = arith.subi %[[VAL_14]], %[[VAL_13]] : index
+! CHECK:           %[[VAL_16:.*]] = arith.addi %[[VAL_10]], %[[VAL_8]] : index
+! CHECK:           %[[VAL_17:.*]] = arith.subi %[[VAL_16]], %[[VAL_13]] : index
+! CHECK:           %[[VAL_18:.*]] = arith.constant 0 : index
+! CHECK:           %[[VAL_19:.*]] = arith.subi %[[VAL_15]], %[[VAL_9]] : index
+! CHECK:           %[[VAL_20:.*]] = arith.addi %[[VAL_19]], %[[VAL_12]] : index
+! CHECK:           %[[VAL_21:.*]] = arith.divsi %[[VAL_20]], %[[VAL_12]] : index
+! CHECK:           %[[VAL_22:.*]] = arith.cmpi sgt, %[[VAL_21]], %[[VAL_18]] : index
+! CHECK:           %[[VAL_23:.*]] = arith.select %[[VAL_22]], %[[VAL_21]], %[[VAL_18]] : index
+! CHECK:           %[[VAL_24:.*]] = arith.constant 0 : index
+! CHECK:           %[[VAL_25:.*]] = arith.subi %[[VAL_17]], %[[VAL_10]] : index
+! CHECK:           %[[VAL_26:.*]] = arith.addi %[[VAL_25]], %[[VAL_12]] : index
+! CHECK:           %[[VAL_27:.*]] = arith.divsi %[[VAL_26]], %[[VAL_12]] : index
+! CHECK:           %[[VAL_28:.*]] = arith.cmpi sgt, %[[VAL_27]], %[[VAL_24]] : index
+! CHECK:           %[[VAL_29:.*]] = arith.select %[[VAL_28]], %[[VAL_27]], %[[VAL_24]] : index
+! CHECK:           %[[VAL_30:.*]] = fir.shape %[[VAL_23]], %[[VAL_29]] : (index, index) -> !fir.shape<2>
+! CHECK:           %[[VAL_31:.*]] = hlfir.designate %[[VAL_1]]#0{"array_comp"} <%[[VAL_11]]> (%[[VAL_9]]:%[[VAL_15]]:%[[VAL_12]], %[[VAL_10]]:%[[VAL_17]]:%[[VAL_12]]) imag shape %[[VAL_30]] : (!fir.ref<!fir.type<_QMcomp_refTt_complex{array_comp:!fir.array<10x20x!fir.complex<4>>}>>, !fir.shapeshift<2>, index, index, index, index, index, index, !fir.shape<2>) -> !fir.box<!fir.array<10x20xf32>>
+end subroutine


        


More information about the flang-commits mailing list