Skip to content

[flang] Implement non-standard LNBLNK intrinsic #117589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 26, 2024

Conversation

tblah
Copy link
Contributor

@tblah tblah commented Nov 25, 2024

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

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir flang:semantics labels Nov 25, 2024
@llvmbot
Copy link
Member

llvmbot commented Nov 25, 2024

@llvm/pr-subscribers-flang-fir-hlfir

Author: Tom Eccles (tblah)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/117589.diff

4 Files Affected:

  • (modified) flang/docs/Intrinsics.md (+3)
  • (modified) flang/lib/Evaluate/intrinsics.cpp (+1)
  • (modified) flang/lib/Optimizer/Builder/IntrinsicCall.cpp (+1)
  • (modified) flang/test/Lower/Intrinsics/len_trim.f90 (+19)
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

@llvmbot
Copy link
Member

llvmbot commented Nov 25, 2024

@llvm/pr-subscribers-flang-semantics

Author: Tom Eccles (tblah)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/117589.diff

4 Files Affected:

  • (modified) flang/docs/Intrinsics.md (+3)
  • (modified) flang/lib/Evaluate/intrinsics.cpp (+1)
  • (modified) flang/lib/Optimizer/Builder/IntrinsicCall.cpp (+1)
  • (modified) flang/test/Lower/Intrinsics/len_trim.f90 (+19)
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

Copy link
Contributor

@clementval clementval left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@mjklemm mjklemm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@jeanPerier jeanPerier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tblah tblah merged commit ad7bb65 into llvm:main Nov 26, 2024
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants