Skip to content

[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

Merged
merged 2 commits into from
Dec 16, 2024

Conversation

vzakhari
Copy link
Contributor

@vzakhari vzakhari commented Dec 13, 2024

No description provided.

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.
@vzakhari vzakhari requested review from tblah and jeanPerier December 13, 2024 23:42
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Dec 13, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 13, 2024

@llvm/pr-subscribers-flang-fir-hlfir

Author: Slava Zakharin (vzakhari)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/119936.diff

1 Files Affected:

  • (modified) flang/lib/Optimizer/HLFIR/Transforms/OptimizedBufferization.cpp (+26-6)
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());

@vzakhari
Copy link
Contributor Author

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.

Copy link
Contributor

@jeanPerier jeanPerier left a 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.

Copy link
Contributor

@tblah tblah left a 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) {
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.

@vzakhari
Copy link
Contributor Author

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.

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 realloc attribute early during the lowering, when the option is passed. I will drop the option from this PR, and will only keep the LLVM_DEBUG NFC changes (I found them useful when looking at the benchmarks). I will add a new option in a seprate PR.

@vzakhari vzakhari changed the title [flang][NFC] Engineering option for optimizing allocatable assignments. [flang][NFC] Added debug output to opt-bufferization pass. Dec 16, 2024
@vzakhari vzakhari merged commit 0b442bc into llvm:main Dec 16, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants