Skip to content

[flang][OpenMP][HLFIR] Support vector subscripted array sections for DEPEND #133892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 55 additions & 6 deletions flang/lib/Lower/OpenMP/ClauseProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -889,13 +889,62 @@ bool ClauseProcessor::processDepend(lower::SymMap &symMap,
} else if (evaluate::IsArrayElement(*object.ref())) {
// Array Section
SomeExpr expr = *object.ref();
if (isVectorSubscript(expr))
TODO(converter.getCurrentLocation(),
"Vector subscripted array section for task dependency");

hlfir::EntityWithAttributes entity = convertExprToHLFIR(
converter.getCurrentLocation(), converter, expr, symMap, stmtCtx);
dependVar = entity.getBase();
if (isVectorSubscript(expr)) {
// OpenMP needs the address of the first indexed element (required by
// the standard to be the lowest index) to identify the dependency. We
// don't need an accurate length for the array section because the
// OpenMP standard forbids overlapping array sections.

// Get a hlfir.elemental_addr op describing the address of the value
// indexed from the original array.
// Note: the hlfir.elemental_addr op verifier requires it to be inside
// of a hlfir.region_assign op. This is because the only place in base
// Fortran where you need the address of a vector subscript would be
// in an assignment operation. We are not doing an assignment here
// but we do want the address (without having to duplicate all of
// Fortran designation lowering!). This operation is never seen by the
// verifier because it is immediately inlined.
hlfir::ElementalAddrOp addrOp =
convertVectorSubscriptedExprToElementalAddr(
converter.getCurrentLocation(), converter, expr, symMap,
stmtCtx);
if (!addrOp.getCleanup().empty())
TODO(converter.getCurrentLocation(),
"Vector subscript in DEPEND clause requring a cleanup region");

// hlfir.elemental_addr doesn't have a normal lowering because it
// can't return a value. Instead we need to inline it here using
// values for the first element. Similar to hlfir::inlineElementalOp.

mlir::Value one = builder.createIntegerConstant(
converter.getCurrentLocation(), builder.getIndexType(), 1);
mlir::SmallVector<mlir::Value> oneBasedIndices;
oneBasedIndices.resize(addrOp.getIndices().size(), one);

mlir::IRMapping mapper;
mapper.map(addrOp.getIndices(), oneBasedIndices);
assert(addrOp.getElementalRegion().hasOneBlock());
mlir::Operation *newOp;
for (mlir::Operation &op :
addrOp.getElementalRegion().back().getOperations())
newOp = builder.clone(op, mapper);
auto yield = mlir::cast<hlfir::YieldOp>(newOp);

addrOp->erase();

if (!yield.getCleanup().empty())
TODO(converter.getCurrentLocation(),
"Vector subscript in DEPEND clause requring element cleanup");

dependVar = yield.getEntity();
yield->erase();
} else {
// Ordinary array section e.g. A(1:512:2)
hlfir::EntityWithAttributes entity = convertExprToHLFIR(
converter.getCurrentLocation(), converter, expr, symMap, stmtCtx);
dependVar = entity.getBase();
}
} else {
semantics::Symbol *sym = object.sym();
dependVar = converter.getSymbolAddress(*sym);
Expand Down

This file was deleted.

33 changes: 33 additions & 0 deletions flang/test/Lower/OpenMP/task-depend-array-section.f90
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,36 @@ subroutine assumedShape(array)
! CHECK: }
! CHECK: return
! CHECK: }

subroutine vectorSubscriptArraySection(array, indices)
integer :: array(:)
integer :: indices(:)

!$omp task depend (in: array(indices))
!$omp end task
end subroutine
! CHECK-LABEL: func.func @_QPvectorsubscriptarraysection(
! CHECK-SAME: %[[VAL_0:[0-9]+|[a-zA-Z$._-][a-zA-Z0-9$._-]*]]: !fir.box<!fir.array<?xi32>> {fir.bindc_name = "array"},
! CHECK-SAME: %[[VAL_1:[0-9]+|[a-zA-Z$._-][a-zA-Z0-9$._-]*]]: !fir.box<!fir.array<?xi32>> {fir.bindc_name = "indices"}) {
! CHECK: %[[VAL_2:.*]] = fir.dummy_scope : !fir.dscope
! 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>>)
! 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>>)
! CHECK: %[[VAL_5:.*]] = arith.constant 0 : index
! CHECK: %[[VAL_6:.*]]:3 = fir.box_dims %[[VAL_4]]#0, %[[VAL_5]] : (!fir.box<!fir.array<?xi32>>, index) -> (index, index, index)
! CHECK: %[[VAL_7:.*]] = fir.shape %[[VAL_6]]#1 : (index) -> !fir.shape<1>
! CHECK: %[[VAL_8:.*]] = hlfir.elemental %[[VAL_7]] unordered : (!fir.shape<1>) -> !hlfir.expr<?xi64> {
! CHECK: ^bb0(%[[VAL_9:.*]]: index):
! CHECK: %[[VAL_10:.*]] = hlfir.designate %[[VAL_4]]#0 (%[[VAL_9]]) : (!fir.box<!fir.array<?xi32>>, index) -> !fir.ref<i32>
! CHECK: %[[VAL_11:.*]] = fir.load %[[VAL_10]] : !fir.ref<i32>
! CHECK: %[[VAL_12:.*]] = fir.convert %[[VAL_11]] : (i32) -> i64
! CHECK: hlfir.yield_element %[[VAL_12]] : i64
! CHECK: }
! CHECK: %[[VAL_13:.*]] = arith.constant 1 : index
! CHECK: %[[VAL_14:.*]] = hlfir.apply %[[VAL_8]], %[[VAL_13]] : (!hlfir.expr<?xi64>, index) -> i64
! CHECK: %[[VAL_15:.*]] = hlfir.designate %[[VAL_3]]#0 (%[[VAL_14]]) : (!fir.box<!fir.array<?xi32>>, i64) -> !fir.ref<i32>
! CHECK: omp.task depend(taskdependin -> %[[VAL_15]] : !fir.ref<i32>) {
! CHECK: omp.terminator
! CHECK: }
! CHECK: hlfir.destroy %[[VAL_8]] : !hlfir.expr<?xi64>
! CHECK: return
! CHECK: }
Loading