[flang-commits] [flang] 345f869 - [flang][HLFIR] allow hlfir.get_length with hlfir.associate

Tom Eccles via flang-commits flang-commits at lists.llvm.org
Wed Jul 12 10:08:31 PDT 2023


Author: Tom Eccles
Date: 2023-07-12T17:02:58Z
New Revision: 345f8699c7ce8e492899147a7f0519769af7ec35

URL: https://github.com/llvm/llvm-project/commit/345f8699c7ce8e492899147a7f0519769af7ec35
DIFF: https://github.com/llvm/llvm-project/commit/345f8699c7ce8e492899147a7f0519769af7ec35.diff

LOG: [flang][HLFIR] allow hlfir.get_length with hlfir.associate

hlfir.get_length will not modify the buffer and so it is safe for a
hlfir.associate using the same expression buffer not to make its own
copy.

Differential Revision: https://reviews.llvm.org/D154942

Added: 
    

Modified: 
    flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
    flang/test/HLFIR/associate-codegen.fir

Removed: 
    


################################################################################
diff  --git a/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp b/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
index ce86ae9a9c775e..53117e0d654afe 100644
--- a/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
+++ b/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
@@ -351,10 +351,12 @@ static bool allOtherUsesAreSafeForAssociate(mlir::Value value,
                                             mlir::Operation *endAssociate) {
   for (mlir::Operation *useOp : value.getUsers())
     if (!mlir::isa<hlfir::DestroyOp>(useOp) && useOp != currentUse) {
-      // hlfir.shape_of will not disrupt cleanup so it is safe for
-      // hlfir.associate. hlfir.shape_of might read the box dimensions and so it
-      // needs to come before the hflir.end_associate (which may deallocate).
-      if (mlir::isa<hlfir::ShapeOfOp>(useOp)) {
+      // hlfir.shape_of and hlfir.get_length will not disrupt cleanup so it is
+      // safe for hlfir.associate. These operations might read from the box and
+      // so they need to come before the hflir.end_associate (which may
+      // deallocate).
+      if (mlir::isa<hlfir::ShapeOfOp>(useOp) ||
+          mlir::isa<hlfir::GetLengthOp>(useOp)) {
         if (!endAssociate)
           continue;
         // not known to occur in practice:

diff  --git a/flang/test/HLFIR/associate-codegen.fir b/flang/test/HLFIR/associate-codegen.fir
index 2f9e6f1485c927..4bd67bd0d5cb85 100644
--- a/flang/test/HLFIR/associate-codegen.fir
+++ b/flang/test/HLFIR/associate-codegen.fir
@@ -338,6 +338,30 @@ func.func @test_multiple_associations(%arg0: !hlfir.expr<1x2xi32>) {
 // CHECK:           return
 // CHECK:         }
 
+// test that we support a hlfir.associate operation where the expr is also used in a hlfir.get_length op
+func.func @test_get_length(%arg0: !fir.ref<!fir.char<1,2>>) {
+  %0 = hlfir.as_expr %arg0 : (!fir.ref<!fir.char<1,2>>) -> !hlfir.expr<!fir.char<1,2>>
+  %1 = hlfir.get_length %0 : (!hlfir.expr<!fir.char<1,2>>)  -> index
+  %c2 = arith.constant 2 : index
+  %2:3 = hlfir.associate %0 typeparams %c2 {uniq_name = "adapt.valuebyref"} : (!hlfir.expr<!fir.char<1,2>>, index) -> (!fir.ref<!fir.char<1,2>>, !fir.ref<!fir.char<1,2>>, i1)
+  // ...
+  hlfir.end_associate %2#1, %2#2 : !fir.ref<!fir.char<1,2>>, i1
+  return
+}
+// CHECK-LABEL:   func.func @test_get_length(
+// CHECK-SAME:                               %[[VAL_0:.*]]: !fir.ref<!fir.char<1,2>>) {
+// CHECK:           %[[VAL_1:.*]] = fir.alloca !fir.char<1,2> {bindc_name = ".tmp"}
+// CHECK:           %[[VAL_2:.*]] = arith.constant 2 : index
+// CHECK:           %[[VAL_3:.*]] = arith.constant false
+// CHECK:           %[[VAL_4:.*]]:2 = hlfir.declare %[[VAL_1]] typeparams %[[VAL_2]] {uniq_name = ".tmp"} : (!fir.ref<!fir.char<1,2>>, index) -> (!fir.ref<!fir.char<1,2>>, !fir.ref<!fir.char<1,2>>)
+// CHECK:           hlfir.assign %[[VAL_0]] to %[[VAL_4]]#0 temporary_lhs : !fir.ref<!fir.char<1,2>>, !fir.ref<!fir.char<1,2>>
+// CHECK:           %[[VAL_5:.*]] = fir.undefined tuple<!fir.ref<!fir.char<1,2>>, i1>
+// CHECK:           %[[VAL_6:.*]] = fir.insert_value %[[VAL_5]], %[[VAL_3]], [1 : index] : (tuple<!fir.ref<!fir.char<1,2>>, i1>, i1) -> tuple<!fir.ref<!fir.char<1,2>>, i1>
+// CHECK:           %[[VAL_7:.*]] = fir.insert_value %[[VAL_6]], %[[VAL_4]]#0, [0 : index] : (tuple<!fir.ref<!fir.char<1,2>>, i1>, !fir.ref<!fir.char<1,2>>) -> tuple<!fir.ref<!fir.char<1,2>>, i1>
+// CHECK:           %[[VAL_8:.*]] = arith.constant 2 : index
+// CHECK:           return
+// CHECK:         }
+
 func.func private @take_i4(!fir.ref<i32>)
 func.func private @take_r4(!fir.ref<f32>)
 func.func private @take_l4(!fir.ref<!fir.logical<4>>)


        


More information about the flang-commits mailing list