Skip to content

[flang][Lower][nfc] vector subscript lhs first element to helper #137456

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 28, 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
9 changes: 9 additions & 0 deletions flang/include/flang/Lower/ConvertExprToHLFIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ hlfir::ElementalAddrOp convertVectorSubscriptedExprToElementalAddr(
const Fortran::lower::SomeExpr &, Fortran::lower::SymMap &,
Fortran::lower::StatementContext &);

/// Lower a designator containing vector subscripts, creating a hlfir::Entity
/// representing the first element in the vector subscripted array. This is a
/// helper which calls convertVectorSubscriptedExprToElementalAddr and lowers
/// the hlfir::ElementalAddrOp.
hlfir::Entity genVectorSubscriptedDesignatorFirstElementAddress(
mlir::Location loc, Fortran::lower::AbstractConverter &converter,
const Fortran::lower::SomeExpr &expr, Fortran::lower::SymMap &symMap,
Fortran::lower::StatementContext &stmtCtx);

} // namespace Fortran::lower

#endif // FORTRAN_LOWER_CONVERTEXPRTOHLFIR_H
46 changes: 46 additions & 0 deletions flang/lib/Lower/ConvertExprToHLFIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "flang/Optimizer/Builder/Runtime/Pointer.h"
#include "flang/Optimizer/Builder/Todo.h"
#include "flang/Optimizer/HLFIR/HLFIROps.h"
#include "mlir/IR/IRMapping.h"
#include "llvm/ADT/TypeSwitch.h"
#include <optional>

Expand Down Expand Up @@ -2120,3 +2121,48 @@ Fortran::lower::convertVectorSubscriptedExprToElementalAddr(
return HlfirDesignatorBuilder(loc, converter, symMap, stmtCtx)
.convertVectorSubscriptedExprToElementalAddr(designatorExpr);
}

hlfir::Entity Fortran::lower::genVectorSubscriptedDesignatorFirstElementAddress(
mlir::Location loc, Fortran::lower::AbstractConverter &converter,
const Fortran::lower::SomeExpr &expr, Fortran::lower::SymMap &symMap,
Fortran::lower::StatementContext &stmtCtx) {
fir::FirOpBuilder &builder = converter.getFirOpBuilder();

// 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 operation is never seen by the
// verifier because it is immediately inlined.
hlfir::ElementalAddrOp addrOp = convertVectorSubscriptedExprToElementalAddr(
loc, converter, expr, symMap, stmtCtx);
if (!addrOp.getCleanup().empty())
TODO(converter.getCurrentLocation(),
"Vector subscript 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 requring element cleanup");

hlfir::Entity result{yield.getEntity()};
yield->erase();
return result;
}
46 changes: 2 additions & 44 deletions flang/lib/Lower/OpenMP/ClauseProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -895,50 +895,8 @@ bool ClauseProcessor::processDepend(lower::SymMap &symMap,
// 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();
dependVar = genVectorSubscriptedDesignatorFirstElementAddress(
converter.getCurrentLocation(), converter, expr, symMap, stmtCtx);
} else {
// Ordinary array section e.g. A(1:512:2)
hlfir::EntityWithAttributes entity = convertExprToHLFIR(
Expand Down
Loading