Skip to content
Merged
Changes from 1 commit
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
32 changes: 26 additions & 6 deletions flang/lib/Optimizer/HLFIR/Transforms/OptimizedBufferization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ namespace hlfir {

#define DEBUG_TYPE "opt-bufferization"

// An engineering option to allow treating hlfir.assign with realloc
// attribute as never requiring (re)allocation of the LHS.
// Setting it to true may result in incorrect code, so it is present
// just for quicker benchmarking of apps that may benefit
// from multiversioning such hlfir.assign operations under
// the dynamic checks of the type/shape/allocation status.
static llvm::cl::opt<bool> assumeNoLhsReallocation(
"flang-assume-no-lhs-reallocation",
llvm::cl::desc("Assume that hlfir.assign never (re)allocates the LHS, i.e. "
"that the LHS and RHS are always conformant"),
llvm::cl::init(false));

namespace {

/// This transformation should match in place modification of arrays.
Expand Down Expand Up @@ -462,16 +474,20 @@ ElementalAssignBufferization::findMatch(hlfir::ElementalOp elemental) {
// the incoming expression
match.array = match.assign.getLhs();
mlir::Type arrayType = mlir::dyn_cast<fir::SequenceType>(
fir::unwrapPassByRefType(match.array.getType()));
if (!arrayType)
hlfir::getFortranElementOrSequenceType(match.array.getType()));
if (!arrayType) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getFortranElementOrSequenceType will return the element type if the passed type is not an array. I think this should be

Suggested change
if (!arrayType) {
if (!mlir::isa<fir::SequenceType>(arrayType)) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review, Tom! There is a dyn_cast<fir::SequenceType> above, which would set arrayType to null if it was not an array, so I think it should be okay. Anyway, I will move this change into another PR.

LLVM_DEBUG(llvm::dbgs() << "AssignOp's result is not an array\n");
return std::nullopt;
}

// require that the array elements are trivial
// TODO: this is just to make the pass easier to think about. Not an inherent
// limitation
mlir::Type eleTy = hlfir::getFortranElementType(arrayType);
if (!fir::isa_trivial(eleTy))
if (!fir::isa_trivial(eleTy)) {
LLVM_DEBUG(llvm::dbgs() << "AssignOp's data type is not trivial\n");
return std::nullopt;
}

// The array must have the same shape as the elemental.
//
Expand All @@ -485,8 +501,10 @@ ElementalAssignBufferization::findMatch(hlfir::ElementalOp elemental) {
// there is no reallocation of the lhs due to the assignment.
// We can probably try generating multiple versions of the code
// with checking for the shape match, length parameters match, etc.
if (match.assign.getRealloc())
if (match.assign.isAllocatableAssignment() && !assumeNoLhsReallocation) {
LLVM_DEBUG(llvm::dbgs() << "AssignOp may involve (re)allocation of LHS\n");
return std::nullopt;
}

// the transformation wants to apply the elemental in a do-loop at the
// hlfir.assign, check there are no effects which make this unsafe
Expand Down Expand Up @@ -606,6 +624,8 @@ llvm::LogicalResult ElementalAssignBufferization::matchAndRewrite(

// create the loop at the assignment
builder.setInsertionPoint(match->assign);
hlfir::Entity arrayObj = hlfir::derefPointersAndAllocatables(
loc, builder, hlfir::Entity{match->array});

// Generate a loop nest looping around the hlfir.elemental shape and clone
// hlfir.elemental region inside the inner loop
Expand All @@ -619,8 +639,8 @@ llvm::LogicalResult ElementalAssignBufferization::matchAndRewrite(
rewriter.eraseOp(yield);

// Assign the element value to the array element for this iteration.
auto arrayElement = hlfir::getElementAt(
loc, builder, hlfir::Entity{match->array}, loopNest.oneBasedIndices);
auto arrayElement =
hlfir::getElementAt(loc, builder, arrayObj, loopNest.oneBasedIndices);
builder.create<hlfir::AssignOp>(
loc, elementValue, arrayElement, /*realloc=*/false,
/*keep_lhs_length_if_realloc=*/false, match->assign.getTemporaryLhs());
Expand Down
Loading