diff --git a/flang/lib/Lower/IntrinsicCall.cpp b/flang/lib/Lower/IntrinsicCall.cpp index fd8bd6ab419bc..4575db5fd3cc6 100644 --- a/flang/lib/Lower/IntrinsicCall.cpp +++ b/flang/lib/Lower/IntrinsicCall.cpp @@ -467,6 +467,7 @@ struct IntrinsicLibrary { mlir::Value genIbset(mlir::Type, llvm::ArrayRef); fir::ExtendedValue genIchar(mlir::Type, llvm::ArrayRef); mlir::Value genIeor(mlir::Type, llvm::ArrayRef); + fir::ExtendedValue genIndex(mlir::Type, llvm::ArrayRef); mlir::Value genIshft(mlir::Type, llvm::ArrayRef); mlir::Value genIshftc(mlir::Type, llvm::ArrayRef); fir::ExtendedValue genLbound(mlir::Type, llvm::ArrayRef); @@ -661,6 +662,12 @@ static constexpr IntrinsicHandler handlers[]{ {"ibset", &I::genIbset}, {"ichar", &I::genIchar}, {"ieor", &I::genIeor}, + {"index", + &I::genIndex, + {{{"string", asAddr}, + {"substring", asAddr}, + {"back", asValue, handleDynamicOptional}, + {"kind", asValue}}}}, {"ishft", &I::genIshft}, {"ishftc", &I::genIshftc}, {"len", @@ -2113,6 +2120,60 @@ mlir::Value IntrinsicLibrary::genIeor(mlir::Type resultType, return builder.create(loc, args[0], args[1]); } +// INDEX +fir::ExtendedValue +IntrinsicLibrary::genIndex(mlir::Type resultType, + llvm::ArrayRef args) { + assert(args.size() >= 2 && args.size() <= 4); + + mlir::Value stringBase = fir::getBase(args[0]); + fir::KindTy kind = + fir::factory::CharacterExprHelper{builder, loc}.getCharacterKind( + stringBase.getType()); + mlir::Value stringLen = fir::getLen(args[0]); + mlir::Value substringBase = fir::getBase(args[1]); + mlir::Value substringLen = fir::getLen(args[1]); + mlir::Value back = + isAbsent(args, 2) + ? builder.createIntegerConstant(loc, builder.getI1Type(), 0) + : fir::getBase(args[2]); + if (isAbsent(args, 3)) + return builder.createConvert( + loc, resultType, + fir::runtime::genIndex(builder, loc, kind, stringBase, stringLen, + substringBase, substringLen, back)); + + // Call the descriptor-based Index implementation + mlir::Value string = builder.createBox(loc, args[0]); + mlir::Value substring = builder.createBox(loc, args[1]); + auto makeRefThenEmbox = [&](mlir::Value b) { + fir::LogicalType logTy = fir::LogicalType::get( + builder.getContext(), builder.getKindMap().defaultLogicalKind()); + mlir::Value temp = builder.createTemporary(loc, logTy); + mlir::Value castb = builder.createConvert(loc, logTy, b); + builder.create(loc, castb, temp); + return builder.createBox(loc, temp); + }; + mlir::Value backOpt = isAbsent(args, 2) + ? builder.create( + loc, fir::BoxType::get(builder.getI1Type())) + : makeRefThenEmbox(fir::getBase(args[2])); + mlir::Value kindVal = isAbsent(args, 3) + ? builder.createIntegerConstant( + loc, builder.getIndexType(), + builder.getKindMap().defaultIntegerKind()) + : fir::getBase(args[3]); + // Create mutable fir.box to be passed to the runtime for the result. + fir::MutableBoxValue mutBox = + fir::factory::createTempMutableBox(builder, loc, resultType); + mlir::Value resBox = fir::factory::getMutableIRBox(builder, loc, mutBox); + // Call runtime. The runtime is allocating the result. + fir::runtime::genIndexDescriptor(builder, loc, resBox, string, substring, + backOpt, kindVal); + // Read back the result from the mutable box. + return readAndAddCleanUp(mutBox, resultType, "INDEX"); +} + // ISHFT mlir::Value IntrinsicLibrary::genIshft(mlir::Type resultType, llvm::ArrayRef args) { diff --git a/flang/test/Lower/Intrinsics/index.f90 b/flang/test/Lower/Intrinsics/index.f90 new file mode 100644 index 0000000000000..cccfc050e0f53 --- /dev/null +++ b/flang/test/Lower/Intrinsics/index.f90 @@ -0,0 +1,81 @@ +! RUN: bbc -emit-fir %s -o - | FileCheck %s +! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck %s + +! CHECK-LABEL: func @_QPindex_test( +! CHECK-SAME: %[[s:[^:]+]]: !fir.boxchar<1>{{.*}}, %[[ss:[^:]+]]: !fir.boxchar<1>{{.*}}) -> i32 +integer function index_test(s1, s2) + character(*) :: s1, s2 + ! CHECK: %[[st:[^:]*]]:2 = fir.unboxchar %[[s]] : (!fir.boxchar<1>) -> (!fir.ref>, index) + ! CHECK: %[[sst:[^:]*]]:2 = fir.unboxchar %[[ss]] : (!fir.boxchar<1>) -> (!fir.ref>, index) + ! CHECK: %[[a1:.*]] = fir.convert %[[st]]#0 : (!fir.ref>) -> !fir.ref + ! CHECK: %[[a2:.*]] = fir.convert %[[st]]#1 : (index) -> i64 + ! CHECK: %[[a3:.*]] = fir.convert %[[sst]]#0 : (!fir.ref>) -> !fir.ref + ! CHECK: %[[a4:.*]] = fir.convert %[[sst]]#1 : (index) -> i64 + ! CHECK: = fir.call @_FortranAIndex1(%[[a1]], %[[a2]], %[[a3]], %[[a4]], %{{.*}}) : (!fir.ref, i64, !fir.ref, i64, i1) -> i64 + index_test = index(s1, s2) +end function index_test + +! CHECK-LABEL: func @_QPindex_test2( +! CHECK-SAME: %[[s:[^:]+]]: !fir.boxchar<1>{{.*}}, %[[ss:[^:]+]]: !fir.boxchar<1>{{.*}}) -> i32 +integer function index_test2(s1, s2) + character(*) :: s1, s2 + ! CHECK: %[[mut:.*]] = fir.alloca !fir.box> + ! CHECK: %[[st:[^:]*]]:2 = fir.unboxchar %[[s]] : (!fir.boxchar<1>) -> (!fir.ref>, index) + ! CHECK: %[[sst:[^:]*]]:2 = fir.unboxchar %[[ss]] : (!fir.boxchar<1>) -> (!fir.ref>, index) + ! CHECK: %[[sb:.*]] = fir.embox %[[st]]#0 typeparams %[[st]]#1 : (!fir.ref>, index) -> !fir.box> + ! CHECK: %[[ssb:.*]] = fir.embox %[[sst]]#0 typeparams %[[sst]]#1 : (!fir.ref>, index) -> !fir.box> + ! CHECK: %[[back:.*]] = fir.embox %{{.*}} : (!fir.ref>) -> !fir.box> + ! CHECK: %[[hb:.*]] = fir.embox %{{.*}} : (!fir.heap) -> !fir.box> + ! CHECK: %[[a0:.*]] = fir.convert %[[mut]] : (!fir.ref>>) -> !fir.ref> + ! CHECK: %[[a1:.*]] = fir.convert %[[sb]] : (!fir.box>) -> !fir.box + ! CHECK: %[[a2:.*]] = fir.convert %[[ssb]] : (!fir.box>) -> !fir.box + ! CHECK: %[[a3:.*]] = fir.convert %[[back]] : (!fir.box>) -> !fir.box + ! CHECK: %[[a5:.*]] = fir.convert %{{.*}} : (!fir.ref>) -> !fir.ref + ! CHECK: fir.call @_FortranAIndex(%[[a0]], %[[a1]], %[[a2]], %[[a3]], %{{.*}}, %[[a5]], %{{.*}}) : (!fir.ref>, !fir.box, !fir.box, !fir.box, i32, !fir.ref, i32) -> none + index_test2 = index(s1, s2, .true., 4) + ! CHECK: %[[ld1:.*]] = fir.load %[[mut]] : !fir.ref>> + ! CHECK: %[[ad1:.*]] = fir.box_addr %[[ld1]] : (!fir.box>) -> !fir.heap + ! CHECK: %[[ld2:.*]] = fir.load %[[ad1]] : !fir.heap + ! CHECK: fir.freemem %[[ad1]] +end function index_test2 + +! CHECK-LABEL: func @_QPindex_test3 +integer function index_test3(s, i) + character(*) :: s + integer :: i + ! CHECK: %[[tmpChar:.*]] = fir.alloca !fir.char<1> + ! CHECK: fir.store %{{.*}} to %[[tmpChar]] : !fir.ref> + ! CHECK: %[[tmpCast:.*]] = fir.convert %[[tmpChar]] : (!fir.ref>) -> !fir.ref + ! CHECK: fir.call @_FortranAIndex1(%{{.*}}, %{{.*}}, %[[tmpCast]], %{{.*}}, %{{.*}}) + index_test3 = index(s, char(i)) +end function + +! CHECK-LABEL: func @_QPtest_optional( +! CHECK-SAME: %[[VAL_0:.*]]: !fir.box>> +! CHECK-SAME: %[[VAL_1:.*]]: !fir.boxchar<1> +! CHECK-SAME: %[[VAL_2:.*]]: !fir.box>> +subroutine test_optional(string, substring, back) + character (*) :: string(:), substring + logical, optional :: back(:) + print *, index(string, substring, back) +! CHECK: %[[VAL_11:.*]] = fir.is_present %[[VAL_2]] : (!fir.box>>) -> i1 +! CHECK: %[[VAL_12:.*]] = fir.zero_bits !fir.ref>> +! CHECK: %[[VAL_13:.*]] = arith.constant 0 : index +! CHECK: %[[VAL_14:.*]] = fir.shape %[[VAL_13]] : (index) -> !fir.shape<1> +! CHECK: %[[VAL_15:.*]] = fir.embox %[[VAL_12]](%[[VAL_14]]) : (!fir.ref>>, !fir.shape<1>) -> !fir.box>> +! CHECK: %[[VAL_16:.*]] = arith.select %[[VAL_11]], %[[VAL_2]], %[[VAL_15]] : !fir.box>> +! CHECK: %[[VAL_17:.*]] = fir.array_load %[[VAL_16]] {fir.optional} : (!fir.box>>) -> !fir.array> +! CHECK: %[[VAL_24:.*]] = fir.do_loop %[[VAL_25:.*]] = %{{.*}} to %{{.*}} step %{{.*}} unordered iter_args(%[[VAL_26:.*]] = %{{.*}}) -> (!fir.array) { + ! CHECK: %[[VAL_31:.*]] = fir.if %[[VAL_11]] -> (!fir.logical<4>) { + ! CHECK: %[[VAL_32:.*]] = fir.array_fetch %[[VAL_17]], %[[VAL_25]] : (!fir.array>, index) -> !fir.logical<4> + ! CHECK: fir.result %[[VAL_32]] : !fir.logical<4> + ! CHECK: } else { + ! CHECK: %[[VAL_33:.*]] = arith.constant false + ! CHECK: %[[VAL_34:.*]] = fir.convert %[[VAL_33]] : (i1) -> !fir.logical<4> + ! CHECK: fir.result %[[VAL_34]] : !fir.logical<4> + ! CHECK: } + ! CHECK: %[[VAL_39:.*]] = fir.convert %[[VAL_31]] : (!fir.logical<4>) -> i1 + ! CHECK: fir.call @_FortranAIndex1(%{{.*}}, %{{.*}}, %{{.*}}, %{{.*}}, %[[VAL_39]]) : (!fir.ref, i64, !fir.ref, i64, i1) -> i64 +! CHECK: } +! CHECK: fir.array_merge_store +end subroutine