#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"
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
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>);
// 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;
/*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",
: 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
//===----------------------------------------------------------------------===//
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,
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,
--- /dev/null
+! 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