Skip to content

Commit f809bf2

Browse files
committed
[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
1 parent 212af2c commit f809bf2

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

flang/lib/Lower/ConvertExprToHLFIR.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,31 @@ class HlfirDesignatorBuilder {
650650
"Fortran designators can only have one ranked part");
651651
return changeElementType(baseType, componentBaseType);
652652
}
653+
654+
if (partInfo.complexPart && partInfo.componentShape) {
655+
// Treat ...array_comp%im/re as ...array_comp(:,:,...)%im/re
656+
// so that the codegen has the full slice triples for the component
657+
// readily available.
658+
fir::FirOpBuilder &builder = getBuilder();
659+
mlir::Type idxTy = builder.getIndexType();
660+
mlir::Value one = builder.createIntegerConstant(loc, idxTy, 1);
661+
662+
llvm::SmallVector<mlir::Value> resultExtents;
663+
// Collect <lb, ub> pairs from the component shape.
664+
auto bounds = hlfir::genBounds(loc, builder, partInfo.componentShape);
665+
for (auto &boundPair : bounds) {
666+
// The default subscripts are <lb, ub, 1>:
667+
partInfo.subscripts.emplace_back(hlfir::DesignateOp::Triplet{
668+
boundPair.first, boundPair.second, one});
669+
auto extentValue = builder.genExtentFromTriplet(
670+
loc, boundPair.first, boundPair.second, one, idxTy);
671+
resultExtents.push_back(extentValue);
672+
}
673+
// The result shape is: <max((ub - lb + 1) / 1, 0), ...>.
674+
partInfo.resultShape = builder.genShape(loc, resultExtents);
675+
return componentBaseType;
676+
}
677+
653678
// scalar%array_comp or scalar%scalar. In any case the shape of this
654679
// part-ref is coming from the component.
655680
partInfo.resultShape = partInfo.componentShape;

flang/test/Lower/HLFIR/designators-component-ref.f90

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ module comp_ref
3030
integer :: scalar_i
3131
character(5) :: array_char_comp(10,20)
3232
end type
33+
34+
type t_complex
35+
complex :: array_comp(2:11,3:22)
36+
end type
3337
end module
3438

3539
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@@ -330,3 +334,36 @@ subroutine test_array_ref_chain(a)
330334
! CHECK: %[[VAL_19:.*]] = arith.constant 5 : index
331335
! 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>>
332336
end subroutine
337+
338+
subroutine test_scalar_array_complex_chain(a)
339+
use comp_ref
340+
type(t_complex) :: a
341+
print *, a%array_comp%im
342+
! CHECK-LABEL: func.func @_QPtest_scalar_array_complex_chain(
343+
! 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>>}>>)
344+
! CHECK: %[[VAL_7:.*]] = arith.constant 10 : index
345+
! CHECK: %[[VAL_8:.*]] = arith.constant 20 : index
346+
! CHECK: %[[VAL_9:.*]] = arith.constant 2 : index
347+
! CHECK: %[[VAL_10:.*]] = arith.constant 3 : index
348+
! CHECK: %[[VAL_11:.*]] = fir.shape_shift %[[VAL_9]], %[[VAL_7]], %[[VAL_10]], %[[VAL_8]] : (index, index, index, index) -> !fir.shapeshift<2>
349+
! CHECK: %[[VAL_12:.*]] = arith.constant 1 : index
350+
! CHECK: %[[VAL_13:.*]] = arith.constant 1 : index
351+
! CHECK: %[[VAL_14:.*]] = arith.addi %[[VAL_9]], %[[VAL_7]] : index
352+
! CHECK: %[[VAL_15:.*]] = arith.subi %[[VAL_14]], %[[VAL_13]] : index
353+
! CHECK: %[[VAL_16:.*]] = arith.addi %[[VAL_10]], %[[VAL_8]] : index
354+
! CHECK: %[[VAL_17:.*]] = arith.subi %[[VAL_16]], %[[VAL_13]] : index
355+
! CHECK: %[[VAL_18:.*]] = arith.constant 0 : index
356+
! CHECK: %[[VAL_19:.*]] = arith.subi %[[VAL_15]], %[[VAL_9]] : index
357+
! CHECK: %[[VAL_20:.*]] = arith.addi %[[VAL_19]], %[[VAL_12]] : index
358+
! CHECK: %[[VAL_21:.*]] = arith.divsi %[[VAL_20]], %[[VAL_12]] : index
359+
! CHECK: %[[VAL_22:.*]] = arith.cmpi sgt, %[[VAL_21]], %[[VAL_18]] : index
360+
! CHECK: %[[VAL_23:.*]] = arith.select %[[VAL_22]], %[[VAL_21]], %[[VAL_18]] : index
361+
! CHECK: %[[VAL_24:.*]] = arith.constant 0 : index
362+
! CHECK: %[[VAL_25:.*]] = arith.subi %[[VAL_17]], %[[VAL_10]] : index
363+
! CHECK: %[[VAL_26:.*]] = arith.addi %[[VAL_25]], %[[VAL_12]] : index
364+
! CHECK: %[[VAL_27:.*]] = arith.divsi %[[VAL_26]], %[[VAL_12]] : index
365+
! CHECK: %[[VAL_28:.*]] = arith.cmpi sgt, %[[VAL_27]], %[[VAL_24]] : index
366+
! CHECK: %[[VAL_29:.*]] = arith.select %[[VAL_28]], %[[VAL_27]], %[[VAL_24]] : index
367+
! CHECK: %[[VAL_30:.*]] = fir.shape %[[VAL_23]], %[[VAL_29]] : (index, index) -> !fir.shape<2>
368+
! 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>>
369+
end subroutine

0 commit comments

Comments
 (0)