Skip to content

Commit 4983aec

Browse files
authored
[flang][OpenMP][HLFIR] Support vector subscripted array sections for DEPEND (#133892)
The OpenMP runtime needs the base address of the array section to identify the dependency. If we just put the vector subscript through the usual HLFIR expression lowering, that would generate a new contiguous array representing the values of the elements in the array which was sectioned. We cannot use addresses from this array because these addresses would not match dependencies on the original array. For example ``` integer :: array(1024) integer :: indices(2) indices(1) = 1 indices(2) = 100 !$omp task depend(out: array(1:512)) !$omp end task !$omp task depend(in: array(indices)) !$omp end task ``` This requires taking the lowering path previously only used for ordered assignments to get the address of the elements in the original array which were indexed. This is done using `hlfir.elemental_addr`. e.g. ``` array(indices) = 2 ``` `hlfir.elemental_addr` is awkward to use because it (by design) doesn't return something like `!hlfir.expr<>` (like `hlfir.elemental`) and so it can't have a generic lowering: each place it is used has to carefully inline the contents of the operation and extract the needed address. For this reason, `hlfir.elemental_addr` is not allowed outside of these ordered assignments. In this commit I ignore this restriction so that I can use `hlfir.elemental_addr` to lower the OpenMP DEPEND clause (this works because the operation is inlined and removed before the verifier runs). One alternative solution would have been to provide my own more limited re-implementation of `HlfirDesignatorBuilder` which skipped `hlfir::elemental_addr`, instead inlining its body directly at the current insertion point applying indices only for the first element. This would have been difficult to maintain because designation in Fortran is complex.
1 parent fef5b6f commit 4983aec

File tree

3 files changed

+88
-17
lines changed

3 files changed

+88
-17
lines changed

flang/lib/Lower/OpenMP/ClauseProcessor.cpp

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -889,13 +889,62 @@ bool ClauseProcessor::processDepend(lower::SymMap &symMap,
889889
} else if (evaluate::IsArrayElement(*object.ref())) {
890890
// Array Section
891891
SomeExpr expr = *object.ref();
892-
if (isVectorSubscript(expr))
893-
TODO(converter.getCurrentLocation(),
894-
"Vector subscripted array section for task dependency");
895892

896-
hlfir::EntityWithAttributes entity = convertExprToHLFIR(
897-
converter.getCurrentLocation(), converter, expr, symMap, stmtCtx);
898-
dependVar = entity.getBase();
893+
if (isVectorSubscript(expr)) {
894+
// OpenMP needs the address of the first indexed element (required by
895+
// the standard to be the lowest index) to identify the dependency. We
896+
// don't need an accurate length for the array section because the
897+
// OpenMP standard forbids overlapping array sections.
898+
899+
// Get a hlfir.elemental_addr op describing the address of the value
900+
// indexed from the original array.
901+
// Note: the hlfir.elemental_addr op verifier requires it to be inside
902+
// of a hlfir.region_assign op. This is because the only place in base
903+
// Fortran where you need the address of a vector subscript would be
904+
// in an assignment operation. We are not doing an assignment here
905+
// but we do want the address (without having to duplicate all of
906+
// Fortran designation lowering!). This operation is never seen by the
907+
// verifier because it is immediately inlined.
908+
hlfir::ElementalAddrOp addrOp =
909+
convertVectorSubscriptedExprToElementalAddr(
910+
converter.getCurrentLocation(), converter, expr, symMap,
911+
stmtCtx);
912+
if (!addrOp.getCleanup().empty())
913+
TODO(converter.getCurrentLocation(),
914+
"Vector subscript in DEPEND clause requring a cleanup region");
915+
916+
// hlfir.elemental_addr doesn't have a normal lowering because it
917+
// can't return a value. Instead we need to inline it here using
918+
// values for the first element. Similar to hlfir::inlineElementalOp.
919+
920+
mlir::Value one = builder.createIntegerConstant(
921+
converter.getCurrentLocation(), builder.getIndexType(), 1);
922+
mlir::SmallVector<mlir::Value> oneBasedIndices;
923+
oneBasedIndices.resize(addrOp.getIndices().size(), one);
924+
925+
mlir::IRMapping mapper;
926+
mapper.map(addrOp.getIndices(), oneBasedIndices);
927+
assert(addrOp.getElementalRegion().hasOneBlock());
928+
mlir::Operation *newOp;
929+
for (mlir::Operation &op :
930+
addrOp.getElementalRegion().back().getOperations())
931+
newOp = builder.clone(op, mapper);
932+
auto yield = mlir::cast<hlfir::YieldOp>(newOp);
933+
934+
addrOp->erase();
935+
936+
if (!yield.getCleanup().empty())
937+
TODO(converter.getCurrentLocation(),
938+
"Vector subscript in DEPEND clause requring element cleanup");
939+
940+
dependVar = yield.getEntity();
941+
yield->erase();
942+
} else {
943+
// Ordinary array section e.g. A(1:512:2)
944+
hlfir::EntityWithAttributes entity = convertExprToHLFIR(
945+
converter.getCurrentLocation(), converter, expr, symMap, stmtCtx);
946+
dependVar = entity.getBase();
947+
}
899948
} else {
900949
semantics::Symbol *sym = object.sym();
901950
dependVar = converter.getSymbolAddress(*sym);

flang/test/Lower/OpenMP/Todo/depend-clause-vector-subscript-array-section.f90

Lines changed: 0 additions & 11 deletions
This file was deleted.

flang/test/Lower/OpenMP/task-depend-array-section.f90

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,36 @@ subroutine assumedShape(array)
4949
! CHECK: }
5050
! CHECK: return
5151
! CHECK: }
52+
53+
subroutine vectorSubscriptArraySection(array, indices)
54+
integer :: array(:)
55+
integer :: indices(:)
56+
57+
!$omp task depend (in: array(indices))
58+
!$omp end task
59+
end subroutine
60+
! CHECK-LABEL: func.func @_QPvectorsubscriptarraysection(
61+
! CHECK-SAME: %[[VAL_0:[0-9]+|[a-zA-Z$._-][a-zA-Z0-9$._-]*]]: !fir.box<!fir.array<?xi32>> {fir.bindc_name = "array"},
62+
! CHECK-SAME: %[[VAL_1:[0-9]+|[a-zA-Z$._-][a-zA-Z0-9$._-]*]]: !fir.box<!fir.array<?xi32>> {fir.bindc_name = "indices"}) {
63+
! CHECK: %[[VAL_2:.*]] = fir.dummy_scope : !fir.dscope
64+
! CHECK: %[[VAL_3:.*]]:2 = hlfir.declare %[[VAL_0]] dummy_scope %[[VAL_2]] {uniq_name = "_QFvectorsubscriptarraysectionEarray"} : (!fir.box<!fir.array<?xi32>>, !fir.dscope) -> (!fir.box<!fir.array<?xi32>>, !fir.box<!fir.array<?xi32>>)
65+
! CHECK: %[[VAL_4:.*]]:2 = hlfir.declare %[[VAL_1]] dummy_scope %[[VAL_2]] {uniq_name = "_QFvectorsubscriptarraysectionEindices"} : (!fir.box<!fir.array<?xi32>>, !fir.dscope) -> (!fir.box<!fir.array<?xi32>>, !fir.box<!fir.array<?xi32>>)
66+
! CHECK: %[[VAL_5:.*]] = arith.constant 0 : index
67+
! CHECK: %[[VAL_6:.*]]:3 = fir.box_dims %[[VAL_4]]#0, %[[VAL_5]] : (!fir.box<!fir.array<?xi32>>, index) -> (index, index, index)
68+
! CHECK: %[[VAL_7:.*]] = fir.shape %[[VAL_6]]#1 : (index) -> !fir.shape<1>
69+
! CHECK: %[[VAL_8:.*]] = hlfir.elemental %[[VAL_7]] unordered : (!fir.shape<1>) -> !hlfir.expr<?xi64> {
70+
! CHECK: ^bb0(%[[VAL_9:.*]]: index):
71+
! CHECK: %[[VAL_10:.*]] = hlfir.designate %[[VAL_4]]#0 (%[[VAL_9]]) : (!fir.box<!fir.array<?xi32>>, index) -> !fir.ref<i32>
72+
! CHECK: %[[VAL_11:.*]] = fir.load %[[VAL_10]] : !fir.ref<i32>
73+
! CHECK: %[[VAL_12:.*]] = fir.convert %[[VAL_11]] : (i32) -> i64
74+
! CHECK: hlfir.yield_element %[[VAL_12]] : i64
75+
! CHECK: }
76+
! CHECK: %[[VAL_13:.*]] = arith.constant 1 : index
77+
! CHECK: %[[VAL_14:.*]] = hlfir.apply %[[VAL_8]], %[[VAL_13]] : (!hlfir.expr<?xi64>, index) -> i64
78+
! CHECK: %[[VAL_15:.*]] = hlfir.designate %[[VAL_3]]#0 (%[[VAL_14]]) : (!fir.box<!fir.array<?xi32>>, i64) -> !fir.ref<i32>
79+
! CHECK: omp.task depend(taskdependin -> %[[VAL_15]] : !fir.ref<i32>) {
80+
! CHECK: omp.terminator
81+
! CHECK: }
82+
! CHECK: hlfir.destroy %[[VAL_8]] : !hlfir.expr<?xi64>
83+
! CHECK: return
84+
! CHECK: }

0 commit comments

Comments
 (0)