[flang-commits] [flang] 2696901 - [flang] Lower character related intrinsic

Valentin Clement via flang-commits flang-commits at lists.llvm.org
Tue Mar 15 14:17:44 PDT 2022


Author: Valentin Clement
Date: 2022-03-15T22:16:47+01:00
New Revision: 269690116283f5dc192513e8ff18d24aad6918be

URL: https://github.com/llvm/llvm-project/commit/269690116283f5dc192513e8ff18d24aad6918be
DIFF: https://github.com/llvm/llvm-project/commit/269690116283f5dc192513e8ff18d24aad6918be.diff

LOG: [flang] Lower character related intrinsic

This patch adds lowering for the following character related intrinsics:
- `len`
- `len_trim`
- `lge`, `lgt`, `lle` and `llt`

This patch is part of the upstreaming effort from fir-dev branch.

Reviewed By: PeteSteinfeld

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

Co-authored-by: V Donaldson <vdonaldson at nvidia.com>
Co-authored-by: Jean Perier <jperier at nvidia.com>
Co-authored-by: Eric Schweitz <eschweitz at nvidia.com>

Added: 
    flang/test/Lower/Intrinsics/len.f90
    flang/test/Lower/Intrinsics/len_trim.f90
    flang/test/Lower/Intrinsics/lge_lgt_lle_llt.f90

Modified: 
    flang/lib/Lower/IntrinsicCall.cpp
    flang/lib/Optimizer/Builder/FIRBuilder.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/IntrinsicCall.cpp b/flang/lib/Lower/IntrinsicCall.cpp
index 6d034627fb29a..531cab96675a1 100644
--- a/flang/lib/Lower/IntrinsicCall.cpp
+++ b/flang/lib/Lower/IntrinsicCall.cpp
@@ -24,6 +24,7 @@
 #include "flang/Optimizer/Builder/Complex.h"
 #include "flang/Optimizer/Builder/FIRBuilder.h"
 #include "flang/Optimizer/Builder/MutableBox.h"
+#include "flang/Optimizer/Builder/Runtime/Character.h"
 #include "flang/Optimizer/Builder/Runtime/Inquiry.h"
 #include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
 #include "flang/Optimizer/Builder/Runtime/Reduction.h"
@@ -275,6 +276,9 @@ struct IntrinsicLibrary {
   mlir::Value genDim(mlir::Type, llvm::ArrayRef<mlir::Value>);
   fir::ExtendedValue genDotProduct(mlir::Type,
                                    llvm::ArrayRef<fir::ExtendedValue>);
+  template <mlir::arith::CmpIPredicate pred>
+  fir::ExtendedValue genCharacterCompare(mlir::Type,
+                                         llvm::ArrayRef<fir::ExtendedValue>);
   template <Extremum, ExtremumBehavior>
   mlir::Value genExtremum(mlir::Type, llvm::ArrayRef<mlir::Value>);
   /// Lowering for the IAND intrinsic. The IAND intrinsic expects two arguments
@@ -283,6 +287,8 @@ struct IntrinsicLibrary {
   mlir::Value genIbits(mlir::Type, llvm::ArrayRef<mlir::Value>);
   fir::ExtendedValue genLbound(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
   fir::ExtendedValue genNull(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
+  fir::ExtendedValue genLen(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
+  fir::ExtendedValue genLenTrim(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
   fir::ExtendedValue genSize(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
   fir::ExtendedValue genSum(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
   fir::ExtendedValue genUbound(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
@@ -352,6 +358,9 @@ struct IntrinsicHandler {
   // The following may be omitted in the table below.
   Fortran::lower::IntrinsicArgumentLoweringRules argLoweringRules = {};
   bool isElemental = true;
+  /// Code heavy intrinsic can be outlined to make FIR
+  /// more readable.
+  bool outline = false;
 };
 
 constexpr auto asValue = Fortran::lower::LowerIntrinsicArgAs::Value;
@@ -399,6 +408,15 @@ static constexpr IntrinsicHandler handlers[]{
      /*isElemental=*/false},
     {"iand", &I::genIand},
     {"ibits", &I::genIbits},
+    {"len",
+     &I::genLen,
+     {{{"string", asInquired}, {"kind", asValue}}},
+     /*isElemental=*/false},
+    {"len_trim", &I::genLenTrim},
+    {"lge", &I::genCharacterCompare<mlir::arith::CmpIPredicate::sge>},
+    {"lgt", &I::genCharacterCompare<mlir::arith::CmpIPredicate::sgt>},
+    {"lle", &I::genCharacterCompare<mlir::arith::CmpIPredicate::sle>},
+    {"llt", &I::genCharacterCompare<mlir::arith::CmpIPredicate::slt>},
     {"min", &I::genExtremum<Extremum::Min, ExtremumBehavior::MinMaxss>},
     {"null", &I::genNull, {{{"mold", asInquired}}}, /*isElemental=*/false},
     {"sum",
@@ -423,6 +441,14 @@ static const IntrinsicHandler *findIntrinsicHandler(llvm::StringRef name) {
                                                               : nullptr;
 }
 
+/// To make fir output more readable for debug, one can outline all intrinsic
+/// implementation in wrappers (overrides the IntrinsicHandler::outline flag).
+static llvm::cl::opt<bool> outlineAllIntrinsics(
+    "outline-intrinsics",
+    llvm::cl::desc(
+        "Lower all intrinsic procedure implementation in their own functions"),
+    llvm::cl::init(false));
+
 //===----------------------------------------------------------------------===//
 // Math runtime description and matching utility
 //===----------------------------------------------------------------------===//
@@ -861,7 +887,7 @@ IntrinsicLibrary::genIntrinsicCall(llvm::StringRef name,
                                    llvm::Optional<mlir::Type> resultType,
                                    llvm::ArrayRef<fir::ExtendedValue> args) {
   if (const IntrinsicHandler *handler = findIntrinsicHandler(name)) {
-    bool outline = false;
+    bool outline = handler->outline || outlineAllIntrinsics;
     return std::visit(
         [&](auto &generator) -> fir::ExtendedValue {
           return invokeHandler(generator, *handler, resultType, args, outline,
@@ -1350,6 +1376,43 @@ mlir::Value IntrinsicLibrary::genIbits(mlir::Type resultType,
   return builder.create<mlir::arith::SelectOp>(loc, lenIsZero, zero, res2);
 }
 
+// LEN
+// Note that this is only used for an unrestricted intrinsic LEN call.
+// Other uses of LEN are rewritten as descriptor inquiries by the front-end.
+fir::ExtendedValue
+IntrinsicLibrary::genLen(mlir::Type resultType,
+                         llvm::ArrayRef<fir::ExtendedValue> args) {
+  // Optional KIND argument reflected in result type and otherwise ignored.
+  assert(args.size() == 1 || args.size() == 2);
+  mlir::Value len = fir::factory::readCharLen(builder, loc, args[0]);
+  return builder.createConvert(loc, resultType, len);
+}
+
+// LEN_TRIM
+fir::ExtendedValue
+IntrinsicLibrary::genLenTrim(mlir::Type resultType,
+                             llvm::ArrayRef<fir::ExtendedValue> args) {
+  // Optional KIND argument reflected in result type and otherwise ignored.
+  assert(args.size() == 1 || args.size() == 2);
+  const fir::CharBoxValue *charBox = args[0].getCharBox();
+  if (!charBox)
+    TODO(loc, "character array len_trim");
+  auto len =
+      fir::factory::CharacterExprHelper(builder, loc).createLenTrim(*charBox);
+  return builder.createConvert(loc, resultType, len);
+}
+
+// LGE, LGT, LLE, LLT
+template <mlir::arith::CmpIPredicate pred>
+fir::ExtendedValue
+IntrinsicLibrary::genCharacterCompare(mlir::Type type,
+                                      llvm::ArrayRef<fir::ExtendedValue> args) {
+  assert(args.size() == 2);
+  return fir::runtime::genCharCompare(
+      builder, loc, pred, fir::getBase(args[0]), fir::getLen(args[0]),
+      fir::getBase(args[1]), fir::getLen(args[1]));
+}
+
 // Compare two FIR values and return boolean result as i1.
 template <Extremum extremum, ExtremumBehavior behavior>
 static mlir::Value createExtremumCompare(mlir::Location loc,

diff  --git a/flang/lib/Optimizer/Builder/FIRBuilder.cpp b/flang/lib/Optimizer/Builder/FIRBuilder.cpp
index 2db10305f26cc..64694aa56ca76 100644
--- a/flang/lib/Optimizer/Builder/FIRBuilder.cpp
+++ b/flang/lib/Optimizer/Builder/FIRBuilder.cpp
@@ -543,10 +543,9 @@ mlir::Value fir::factory::readCharLen(fir::FirOpBuilder &builder,
         return fir::factory::CharacterExprHelper{builder, loc}
             .readLengthFromBox(x.getAddr());
       },
-      [&](const fir::MutableBoxValue &) -> mlir::Value {
-        // MutableBoxValue must be read into another category to work with them
-        // outside of allocation/assignment contexts.
-        fir::emitFatalError(loc, "readCharLen on MutableBoxValue");
+      [&](const fir::MutableBoxValue &x) -> mlir::Value {
+        return readCharLen(builder, loc,
+                           fir::factory::genMutableBoxRead(builder, loc, x));
       },
       [&](const auto &) -> mlir::Value {
         fir::emitFatalError(

diff  --git a/flang/test/Lower/Intrinsics/len.f90 b/flang/test/Lower/Intrinsics/len.f90
new file mode 100644
index 0000000000000..b14046fc0f319
--- /dev/null
+++ b/flang/test/Lower/Intrinsics/len.f90
@@ -0,0 +1,76 @@
+! RUN: bbc -emit-fir %s -o - | FileCheck %s
+
+! CHECK-LABEL: len_test
+subroutine len_test(i, c)
+    integer :: i
+    character(*) :: c
+    ! CHECK: %[[c:.*]]:2 = fir.unboxchar %arg1
+    ! CHECK: %[[xx:.*]] = fir.convert %[[c]]#1 : (index) -> i32
+    ! CHECK: fir.store %[[xx]] to %arg0
+    i = len(c)
+  end subroutine
+  
+  ! CHECK-LABEL: len_test_array
+  ! CHECK-SAME: %[[arg0:.*]]: !fir.ref<i32> {fir.bindc_name = "i"}, %[[arg1:.*]]: !fir.boxchar<1> {fir.bindc_name = "c"}
+  subroutine len_test_array(i, c)
+    integer :: i
+    character(*) :: c(100)
+    ! CHECK: %[[c:.*]]:2 = fir.unboxchar %[[arg1]]
+    ! CHECK: %[[xx:.*]] = fir.convert %[[c]]#1 : (index) -> i32
+    ! CHECK: fir.store %[[xx]] to %[[arg0]]
+    i = len(c)
+  end subroutine
+  
+  ! CHECK-LABEL: func @_QPlen_test_assumed_shape_array(
+  ! CHECK-SAME:  %[[VAL_0:.*]]: !fir.ref<i32> {fir.bindc_name = "i"},
+  ! CHECK-SAME:  %[[VAL_1:.*]]: !fir.box<!fir.array<?x!fir.char<1,?>>> {fir.bindc_name = "c"}) {
+  subroutine len_test_assumed_shape_array(i, c)
+    integer :: i
+    character(*) :: c(:)
+  ! CHECK:  %[[VAL_2:.*]] = fir.box_elesize %[[VAL_1]] : (!fir.box<!fir.array<?x!fir.char<1,?>>>) -> index
+  ! CHECK:  %[[VAL_3:.*]] = fir.convert %[[VAL_2]] : (index) -> i32
+  ! CHECK:  fir.store %[[VAL_3]] to %[[VAL_0]] : !fir.ref<i32>
+    i = len(c)
+  end subroutine
+  
+  ! CHECK-LABEL: func @_QPlen_test_array_alloc(
+  ! CHECK-SAME:  %[[VAL_0:.*]]: !fir.ref<i32> {fir.bindc_name = "i"},
+  ! CHECK-SAME:  %[[VAL_1:.*]]: !fir.ref<!fir.box<!fir.heap<!fir.array<?x!fir.char<1,?>>>>> {fir.bindc_name = "c"}) {
+  subroutine len_test_array_alloc(i, c)
+    integer :: i
+    character(:), allocatable :: c(:)
+  ! CHECK:  %[[VAL_2:.*]] = fir.load %[[VAL_1]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?x!fir.char<1,?>>>>>
+  ! CHECK:  %[[VAL_3:.*]] = fir.box_elesize %[[VAL_2]] : (!fir.box<!fir.heap<!fir.array<?x!fir.char<1,?>>>>) -> index
+  ! CHECK:  %[[VAL_4:.*]] = fir.convert %[[VAL_3]] : (index) -> i32
+  ! CHECK:  fir.store %[[VAL_4]] to %[[VAL_0]] : !fir.ref<i32>
+    i = len(c)
+  end subroutine
+  
+  ! CHECK-LABEL: func @_QPlen_test_array_local_alloc(
+  ! CHECK-SAME:  %[[VAL_0:.*]]: !fir.ref<i32> {fir.bindc_name = "i"})
+  subroutine len_test_array_local_alloc(i)
+    integer :: i
+    character(:), allocatable :: c(:)
+  ! CHECK:  %[[VAL_5:.*]] = fir.alloca index {uniq_name = "_QFlen_test_array_local_allocEc.len"}
+  ! CHECK:  %[[VAL_7:.*]] = arith.constant 10 : i32
+  ! CHECK:  %[[VAL_10:.*]] = fir.convert %[[VAL_7]] : (i32) -> index
+  ! CHECK:  fir.store %[[VAL_10]] to %[[VAL_5]] : !fir.ref<index>
+    allocate(character(10):: c(100))
+  ! CHECK:  %[[VAL_13:.*]] = fir.load %[[VAL_5]] : !fir.ref<index>
+  ! CHECK:  %[[VAL_14:.*]] = fir.convert %[[VAL_13]] : (index) -> i32
+  ! CHECK:  fir.store %[[VAL_14]] to %[[VAL_0]] : !fir.ref<i32>
+    i = len(c)
+  end subroutine
+  
+  ! CHECK-LABEL: func @_QPlen_test_alloc_explicit_len(
+  ! CHECK-SAME:  %[[VAL_0:.*]]: !fir.ref<i32> {fir.bindc_name = "i"},
+  ! CHECK-SAME:  %[[VAL_1:.*]]: !fir.ref<i32> {fir.bindc_name = "n"},
+  ! CHECK-SAME:  %[[VAL_2:.*]]: !fir.ref<!fir.box<!fir.heap<!fir.array<?x!fir.char<1,?>>>>> {fir.bindc_name = "c"}) {
+  subroutine len_test_alloc_explicit_len(i, n, c)
+    integer :: i
+    integer :: n
+    character(n), allocatable :: c(:)
+  ! CHECK:  %[[VAL_3:.*]] = fir.load %[[VAL_1]] : !fir.ref<i32>
+  ! CHECK:  fir.store %[[VAL_3]] to %[[VAL_0]] : !fir.ref<i32>
+    i = len(c)
+  end subroutine

diff  --git a/flang/test/Lower/Intrinsics/len_trim.f90 b/flang/test/Lower/Intrinsics/len_trim.f90
new file mode 100644
index 0000000000000..8777e5857609f
--- /dev/null
+++ b/flang/test/Lower/Intrinsics/len_trim.f90
@@ -0,0 +1,20 @@
+! RUN: bbc -emit-fir %s -o - | FileCheck %s
+
+! CHECK-LABEL: len_trim_test
+integer function len_trim_test(c)
+character(*) :: c
+ltrim = len_trim(c)
+! CHECK-DAG: %[[c0:.*]] = arith.constant 0 : index
+! CHECK-DAG: %[[c1:.*]] = arith.constant 1 : index
+! CHECK-DAG: %[[cm1:.*]] = arith.constant -1 : index
+! CHECK-DAG: %[[lastChar:.*]] = arith.subi {{.*}}, %[[c1]]
+! CHECK: %[[iterateResult:.*]]:2 = fir.iterate_while (%[[index:.*]] = %[[lastChar]] to %[[c0]] step %[[cm1]]) and ({{.*}}) iter_args({{.*}}) {
+  ! CHECK: %[[addr:.*]] = fir.coordinate_of {{.*}}, %[[index]]
+  ! CHECK: %[[codeAddr:.*]] = fir.convert %[[addr]]
+  ! CHECK: %[[code:.*]] = fir.load %[[codeAddr]]
+  ! CHECK: %[[bool:.*]] = arith.cmpi eq
+  ! CHECK: fir.result %[[bool]], %[[index]]
+! CHECK: }
+! CHECK: %[[len:.*]] = arith.addi %[[iterateResult]]#1, %[[c1]]
+! CHECK: select %[[iterateResult]]#0, %[[c0]], %[[len]]
+end function

diff  --git a/flang/test/Lower/Intrinsics/lge_lgt_lle_llt.f90 b/flang/test/Lower/Intrinsics/lge_lgt_lle_llt.f90
new file mode 100644
index 0000000000000..c49d193f0c098
--- /dev/null
+++ b/flang/test/Lower/Intrinsics/lge_lgt_lle_llt.f90
@@ -0,0 +1,33 @@
+! RUN: bbc -emit-fir %s -o - | FileCheck %s
+
+subroutine lge_test
+    character*3 :: c1(3)
+    character*7 :: c2(3)
+    ! c1(1) = 'a'; c1(2) = 'B'; c1(3) = 'c';
+    ! c2(1) = 'A'; c2(2) = 'b'; c2(3) = 'c';
+    ! CHECK: BeginExternalListOutput
+    ! CHECK: fir.do_loop
+    ! CHECK: CharacterCompareScalar1
+    ! CHECK: OutputDescriptor
+    ! CHECK: EndIoStatement
+    print*, lge(c1, c2)
+    ! CHECK: BeginExternalListOutput
+    ! CHECK: fir.do_loop
+    ! CHECK: CharacterCompareScalar1
+    ! CHECK: OutputDescriptor
+    ! CHECK: EndIoStatement
+    print*, lgt(c1, c2)
+    ! CHECK: BeginExternalListOutput
+    ! CHECK: fir.do_loop
+    ! CHECK: CharacterCompareScalar1
+    ! CHECK: OutputDescriptor
+    ! CHECK: EndIoStatement
+    print*, lle(c1, c2)
+    ! CHECK: BeginExternalListOutput
+    ! CHECK: fir.do_loop
+    ! CHECK: CharacterCompareScalar1
+    ! CHECK: OutputDescriptor
+    ! CHECK: EndIoStatement
+    print*, llt(c1, c2)
+  end
+  
\ No newline at end of file


        


More information about the flang-commits mailing list