[flang-commits] [flang] 4642198 - [flang][hlfir] Codegen for hlfir.get_length.

Slava Zakharin via flang-commits flang-commits at lists.llvm.org
Thu Jul 6 13:21:59 PDT 2023


Author: Slava Zakharin
Date: 2023-07-06T13:21:45-07:00
New Revision: 4642198b654064929ec7ae5de02de67e24158f81

URL: https://github.com/llvm/llvm-project/commit/4642198b654064929ec7ae5de02de67e24158f81
DIFF: https://github.com/llvm/llvm-project/commit/4642198b654064929ec7ae5de02de67e24158f81.diff

LOG: [flang][hlfir] Codegen for hlfir.get_length.

Lower hlfir.get_length into the char length inquiry of the bufferized
entity. In some cases the codegen will fail with
`hlfir.associate of hlfir.expr with more than one use` - this will be fixed
separately (after D154521).

Depends on D154560

Reviewed By: tblah, jeanPerier

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

Added: 
    flang/test/HLFIR/get_length_codegen.fir

Modified: 
    flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp b/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
index bb6d495ae3d37f..9b894027a9f203 100644
--- a/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
+++ b/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
@@ -320,6 +320,27 @@ struct SetLengthOpConversion
   }
 };
 
+struct GetLengthOpConversion
+    : public mlir::OpConversionPattern<hlfir::GetLengthOp> {
+  using mlir::OpConversionPattern<hlfir::GetLengthOp>::OpConversionPattern;
+  explicit GetLengthOpConversion(mlir::MLIRContext *ctx)
+      : mlir::OpConversionPattern<hlfir::GetLengthOp>{ctx} {}
+  mlir::LogicalResult
+  matchAndRewrite(hlfir::GetLengthOp getLength, OpAdaptor adaptor,
+                  mlir::ConversionPatternRewriter &rewriter) const override {
+    mlir::Location loc = getLength->getLoc();
+    auto module = getLength->getParentOfType<mlir::ModuleOp>();
+    fir::FirOpBuilder builder(rewriter, fir::getKindMapping(module));
+    hlfir::Entity bufferizedExpr = getBufferizedExprStorage(adaptor.getExpr());
+    mlir::Value length = hlfir::genCharLength(loc, builder, bufferizedExpr);
+    if (!length)
+      return rewriter.notifyMatchFailure(
+          getLength, "could not deduce length from GetLengthOp operand");
+    rewriter.replaceOp(getLength, length);
+    return mlir::success();
+  }
+};
+
 static bool allOtherUsesAreDestroys(mlir::Value value,
                                     mlir::Operation *currentUse) {
   for (mlir::Operation *useOp : value.getUsers())
@@ -662,12 +683,12 @@ class BufferizeHLFIR : public hlfir::impl::BufferizeHLFIRBase<BufferizeHLFIR> {
     auto module = this->getOperation();
     auto *context = &getContext();
     mlir::RewritePatternSet patterns(context);
-    patterns
-        .insert<ApplyOpConversion, AsExprOpConversion, AssignOpConversion,
-                AssociateOpConversion, CharExtremumOpConversion,
-                ConcatOpConversion, DestroyOpConversion, ElementalOpConversion,
-                EndAssociateOpConversion, NoReassocOpConversion,
-                SetLengthOpConversion, ShapeOfOpConversion>(context);
+    patterns.insert<ApplyOpConversion, AsExprOpConversion, AssignOpConversion,
+                    AssociateOpConversion, CharExtremumOpConversion,
+                    ConcatOpConversion, DestroyOpConversion,
+                    ElementalOpConversion, EndAssociateOpConversion,
+                    NoReassocOpConversion, SetLengthOpConversion,
+                    ShapeOfOpConversion, GetLengthOpConversion>(context);
     mlir::ConversionTarget target(*context);
     // Note that YieldElementOp is not marked as an illegal operation.
     // It must be erased by its parent converter and there is no explicit

diff  --git a/flang/test/HLFIR/get_length_codegen.fir b/flang/test/HLFIR/get_length_codegen.fir
new file mode 100644
index 00000000000000..3b3785c82667ce
--- /dev/null
+++ b/flang/test/HLFIR/get_length_codegen.fir
@@ -0,0 +1,32 @@
+// Test hlfir.get_length code generation
+// RUN: fir-opt %s --bufferize-hlfir | FileCheck %s
+
+func.func @_QPtest_char_get_length(%arg0: !fir.boxchar<1> {fir.bindc_name = "ch"}) -> index {
+  %c3 = arith.constant 3 : index
+  %0:2 = fir.unboxchar %arg0 : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
+  %1:2 = hlfir.declare %0#0 typeparams %0#1 {uniq_name = "_QFtest_char_get_lengthEch"} : (!fir.ref<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
+  %2 = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFtest_char_get_lengthEx"}
+  %3:2 = hlfir.declare %2 {uniq_name = "_QFtest_char_get_lengthEx"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
+  %4 = fir.address_of(@_QQcl.616263) : !fir.ref<!fir.char<1,3>>
+  %5:2 = hlfir.declare %4 typeparams %c3 {fortran_attrs = #fir.var_attrs<parameter>, uniq_name = "_QQcl.616263"} : (!fir.ref<!fir.char<1,3>>, index) -> (!fir.ref<!fir.char<1,3>>, !fir.ref<!fir.char<1,3>>)
+  %6 = arith.addi %0#1, %c3 : index
+  %7 = hlfir.concat %5#0, %1#0 len %6 : (!fir.ref<!fir.char<1,3>>, !fir.boxchar<1>, index) -> !hlfir.expr<!fir.char<1,?>>
+  %8:3 = hlfir.associate %7 typeparams %6 {uniq_name = ".tmp.assign"} : (!hlfir.expr<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>, i1)
+  %9 = hlfir.as_expr %8#0 : (!fir.boxchar<1>) -> !hlfir.expr<!fir.char<1,?>>
+  %10 = hlfir.get_length %9 : (!hlfir.expr<!fir.char<1,?>>) -> index
+  hlfir.destroy %9 : !hlfir.expr<!fir.char<1,?>>
+  hlfir.end_associate %8#1, %8#2 : !fir.ref<!fir.char<1,?>>, i1
+  return %10 : index
+}
+fir.global linkonce @_QQcl.616263 constant : !fir.char<1,3> {
+  %0 = fir.string_lit "abc"(3) : !fir.char<1,3>
+  fir.has_value %0 : !fir.char<1,3>
+}
+// CHECK-LABEL:   func.func @_QPtest_char_get_length(
+// CHECK-SAME:      %[[VAL_0:.*]]: !fir.boxchar<1> {fir.bindc_name = "ch"}) -> index {
+// CHECK:           %[[VAL_1:.*]] = arith.constant 3 : index
+// CHECK:           %[[VAL_2:.*]]:2 = fir.unboxchar %[[VAL_0]] : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
+// CHECK:           %[[VAL_9:.*]] = arith.addi %[[VAL_1]], %[[VAL_2]]#1 : index
+// CHECK:           %[[VAL_33:.*]]:2 = hlfir.declare %[[VAL_31:.*]] typeparams %[[VAL_9]] {uniq_name = ".tmp"} : (!fir.ref<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
+// CHECK:           return %[[VAL_9]] : index
+// CHECK:         }


        


More information about the flang-commits mailing list