[flang-commits] [flang] [flang] Implement non-standard LNBLNK intrinsic (PR #117589)

Tom Eccles via flang-commits flang-commits at lists.llvm.org
Mon Nov 25 10:11:12 PST 2024


https://github.com/tblah created https://github.com/llvm/llvm-project/pull/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

>From 792f3495e7de9b44a5083ce249f27e17a24fc8d0 Mon Sep 17 00:00:00 2001
From: Tom Eccles <tom.eccles at arm.com>
Date: Mon, 25 Nov 2024 18:06:39 +0000
Subject: [PATCH] [flang] Implement non-standard LNBLNK intrinsic

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
---
 flang/docs/Intrinsics.md                      |  3 +++
 flang/lib/Evaluate/intrinsics.cpp             |  1 +
 flang/lib/Optimizer/Builder/IntrinsicCall.cpp |  1 +
 flang/test/Lower/Intrinsics/len_trim.f90      | 19 +++++++++++++++++++
 4 files changed, 24 insertions(+)

diff --git a/flang/docs/Intrinsics.md b/flang/docs/Intrinsics.md
index 0196be10d86556..94bca7a3972b6e 100644
--- a/flang/docs/Intrinsics.md
+++ b/flang/docs/Intrinsics.md
@@ -1052,6 +1052,9 @@ end program rename_proc
 This intrinsic is an alias for `CPU_TIME`: supporting both a subroutine and a
 function form.
 
+### Non-standard Intrinsics: LNBLNK
+This intrinsic is an alias for `LEN_TRIM`, without the optional KIND argument.
+
 #### Usage and Info
 
 - **Standard:** GNU extension
diff --git a/flang/lib/Evaluate/intrinsics.cpp b/flang/lib/Evaluate/intrinsics.cpp
index 1e27c0ae4216c5..ff6ccf154f17a5 100644
--- a/flang/lib/Evaluate/intrinsics.cpp
+++ b/flang/lib/Evaluate/intrinsics.cpp
@@ -616,6 +616,7 @@ static const IntrinsicInterface genericIntrinsicFunction[]{
         DefaultLogical},
     {"llt", {{"string_a", SameCharNoLen}, {"string_b", SameCharNoLen}},
         DefaultLogical},
+    {"lnblnk", {{"string", AnyChar}}, DefaultInt},
     {"loc", {{"x", Addressable, Rank::anyOrAssumedRank}}, SubscriptInt,
         Rank::scalar},
     {"log", {{"x", SameFloating}}, SameFloating},
diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
index a2b327f45c6939..ef9ac1cbb151b2 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -434,6 +434,7 @@ static constexpr IntrinsicHandler handlers[]{
     {"lgt", &I::genCharacterCompare<mlir::arith::CmpIPredicate::sgt>},
     {"lle", &I::genCharacterCompare<mlir::arith::CmpIPredicate::sle>},
     {"llt", &I::genCharacterCompare<mlir::arith::CmpIPredicate::slt>},
+    {"lnblnk", &I::genLenTrim},
     {"loc", &I::genLoc, {{{"x", asBox}}}, /*isElemental=*/false},
     {"malloc", &I::genMalloc},
     {"maskl", &I::genMask<mlir::arith::ShLIOp>},
diff --git a/flang/test/Lower/Intrinsics/len_trim.f90 b/flang/test/Lower/Intrinsics/len_trim.f90
index 8777e5857609f2..e3483331ab5ab3 100644
--- a/flang/test/Lower/Intrinsics/len_trim.f90
+++ b/flang/test/Lower/Intrinsics/len_trim.f90
@@ -18,3 +18,22 @@ integer function len_trim_test(c)
 ! CHECK: %[[len:.*]] = arith.addi %[[iterateResult]]#1, %[[c1]]
 ! CHECK: select %[[iterateResult]]#0, %[[c0]], %[[len]]
 end function
+
+! CHECK-LABEL: lnblnk_test
+integer function lnblnk_test(c)
+character(*) :: c
+ltrim = lnblnk(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



More information about the flang-commits mailing list