Skip to content

Commit ad7bb65

Browse files
authored
[flang] Implement non-standard LNBLNK intrinsic (#117589)
This is defined here https://gcc.gnu.org/onlinedocs/gfortran/LNBLNK.html. It is just an alias to LEN_TRIM. This was requested by a user: https://discourse.llvm.org/t/unresolved-externals-with-appendend-underscore/83305
1 parent cf602b9 commit ad7bb65

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed

flang/docs/Intrinsics.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,9 @@ end program rename_proc
10521052
This intrinsic is an alias for `CPU_TIME`: supporting both a subroutine and a
10531053
function form.
10541054

1055+
### Non-standard Intrinsics: LNBLNK
1056+
This intrinsic is an alias for `LEN_TRIM`, without the optional KIND argument.
1057+
10551058
#### Usage and Info
10561059

10571060
- **Standard:** GNU extension

flang/lib/Evaluate/intrinsics.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@ static const IntrinsicInterface genericIntrinsicFunction[]{
616616
DefaultLogical},
617617
{"llt", {{"string_a", SameCharNoLen}, {"string_b", SameCharNoLen}},
618618
DefaultLogical},
619+
{"lnblnk", {{"string", AnyChar}}, DefaultInt},
619620
{"loc", {{"x", Addressable, Rank::anyOrAssumedRank}}, SubscriptInt,
620621
Rank::scalar},
621622
{"log", {{"x", SameFloating}}, SameFloating},

flang/lib/Optimizer/Builder/IntrinsicCall.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ static constexpr IntrinsicHandler handlers[]{
434434
{"lgt", &I::genCharacterCompare<mlir::arith::CmpIPredicate::sgt>},
435435
{"lle", &I::genCharacterCompare<mlir::arith::CmpIPredicate::sle>},
436436
{"llt", &I::genCharacterCompare<mlir::arith::CmpIPredicate::slt>},
437+
{"lnblnk", &I::genLenTrim},
437438
{"loc", &I::genLoc, {{{"x", asBox}}}, /*isElemental=*/false},
438439
{"malloc", &I::genMalloc},
439440
{"maskl", &I::genMask<mlir::arith::ShLIOp>},

flang/test/Lower/Intrinsics/len_trim.f90

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,22 @@ integer function len_trim_test(c)
1818
! CHECK: %[[len:.*]] = arith.addi %[[iterateResult]]#1, %[[c1]]
1919
! CHECK: select %[[iterateResult]]#0, %[[c0]], %[[len]]
2020
end function
21+
22+
! CHECK-LABEL: lnblnk_test
23+
integer function lnblnk_test(c)
24+
character(*) :: c
25+
ltrim = lnblnk(c)
26+
! CHECK-DAG: %[[c0:.*]] = arith.constant 0 : index
27+
! CHECK-DAG: %[[c1:.*]] = arith.constant 1 : index
28+
! CHECK-DAG: %[[cm1:.*]] = arith.constant -1 : index
29+
! CHECK-DAG: %[[lastChar:.*]] = arith.subi {{.*}}, %[[c1]]
30+
! CHECK: %[[iterateResult:.*]]:2 = fir.iterate_while (%[[index:.*]] = %[[lastChar]] to %[[c0]] step %[[cm1]]) and ({{.*}}) iter_args({{.*}}) {
31+
! CHECK: %[[addr:.*]] = fir.coordinate_of {{.*}}, %[[index]]
32+
! CHECK: %[[codeAddr:.*]] = fir.convert %[[addr]]
33+
! CHECK: %[[code:.*]] = fir.load %[[codeAddr]]
34+
! CHECK: %[[bool:.*]] = arith.cmpi eq
35+
! CHECK: fir.result %[[bool]], %[[index]]
36+
! CHECK: }
37+
! CHECK: %[[len:.*]] = arith.addi %[[iterateResult]]#1, %[[c1]]
38+
! CHECK: select %[[iterateResult]]#0, %[[c0]], %[[len]]
39+
end function

0 commit comments

Comments
 (0)