-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[flang][NFC] Added debug output to opt-bufferization pass. #119936
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
Conversation
The options is added just for quick experiments with different apps. If any headroom is found, we will implement proper multiversioning of hlfir.assign with allocatable LHS.
@llvm/pr-subscribers-flang-fir-hlfir Author: Slava Zakharin (vzakhari) ChangesThe options is added just for quick experiments with different apps. Full diff: https://github.com/llvm/llvm-project/pull/119936.diff 1 Files Affected:
diff --git a/flang/lib/Optimizer/HLFIR/Transforms/OptimizedBufferization.cpp b/flang/lib/Optimizer/HLFIR/Transforms/OptimizedBufferization.cpp
index 8342458e00763c..9ade904dcda835 100644
--- a/flang/lib/Optimizer/HLFIR/Transforms/OptimizedBufferization.cpp
+++ b/flang/lib/Optimizer/HLFIR/Transforms/OptimizedBufferization.cpp
@@ -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.
@@ -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) {
+ 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.
//
@@ -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
@@ -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
@@ -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());
|
I ran some SPEC and Polyhedron benchmarks on x86, and did not find any noticeable improvements from enabling this option. Of course, the ability to optimize an assignment with allocatable LHS depends on the alias analysis. So we may not see any benefits now, but maybe we will see some later. I would like to keep this option, so that we can quickly run an experiment for an app where we think this might be profitable. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
FYI, some compilers actually have user flags to do that since that was the F90 semantics and not everybody was happy with the F2003 semantic change here.
For instance, gfortran has -fno-realloc-lhs
, ifx has -assume norealloc_lhs
, and nvfortran has -Mallocatable=95
.
So we could also make that a -fno-realloc-lhs
user option.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with one nit
fir::unwrapPassByRefType(match.array.getType())); | ||
if (!arrayType) | ||
hlfir::getFortranElementOrSequenceType(match.array.getType())); | ||
if (!arrayType) { |
There was a problem hiding this comment.
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
if (!arrayType) { | |
if (!mlir::isa<fir::SequenceType>(arrayType)) { |
There was a problem hiding this comment.
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.
Thank you, Jean! I did not know about this option in other compilers. Then, I guess, the more appropriate way is to not set the |
No description provided.