[flang-commits] [flang] [FLANG] Solving issue with adjustr intrinsic in where construct (PR #146851)

via flang-commits flang-commits at lists.llvm.org
Thu Jul 3 03:39:35 PDT 2025


https://github.com/EbinJose2002 created https://github.com/llvm/llvm-project/pull/146851

Solves #122823
An unnecessary convert instruction was generated in the case of reference to boxchar conversion. So excluded them in the condition. The output is generating correctly.

>From 6476107702e746e1fcd6a95a0e88fb8df6c4b97f Mon Sep 17 00:00:00 2001
From: EbinJose2002 <ebin.jose at multicorewareinc.com>
Date: Tue, 1 Jul 2025 18:11:05 +0530
Subject: [PATCH 1/3] Removed unnecessary handling of reference to boxchar
 conversion in BufferizeHLFIR.cpp

---
 flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp b/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
index 58f2b57712974..240b79138b0e1 100644
--- a/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
+++ b/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
@@ -446,7 +446,8 @@ struct AssociateOpConversion
             ((mlir::isa<fir::BoxCharType>(sourceVar.getType()) &&
               !mlir::isa<fir::BoxCharType>(assocType)))) {
           sourceVar = builder.create<fir::BoxAddrOp>(loc, assocType, sourceVar);
-        } else {
+        } else if (!mlir::isa<fir::ReferenceType>(sourceVar.getType()) ||
+                   !mlir::isa<fir::BoxCharType>(assocType)) {
           sourceVar = builder.createConvert(loc, assocType, sourceVar);
         }
         return sourceVar;

>From ee425fb1685f8d783d0e50e76b15df59c921e3b2 Mon Sep 17 00:00:00 2001
From: EbinJose2002 <ebin.jose at multicorewareinc.com>
Date: Thu, 3 Jul 2025 15:58:59 +0530
Subject: [PATCH 2/3] Added test file for the issue

---
 flang/test/Lower/adjustr.f90 | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
 create mode 100644 flang/test/Lower/adjustr.f90

diff --git a/flang/test/Lower/adjustr.f90 b/flang/test/Lower/adjustr.f90
new file mode 100644
index 0000000000000..6add2f5456bbc
--- /dev/null
+++ b/flang/test/Lower/adjustr.f90
@@ -0,0 +1,14 @@
+! RUN: bbc -emit-fir -o - %s | FileCheck %s
+
+! Checking no reference to boxchar conversion is in the emitted fir.
+! CHECK-NOT: (!fir.ref<!fir.char<1,4>>) -> !fir.boxchar<1>
+
+program main
+  logical         ,dimension(1):: mask = .true.
+  character(len=2),dimension(1):: d1 = "a "
+  character(len=4),dimension(1):: d4
+  where (mask)
+    d4 = adjustr(d1 // d1)
+  end where
+  write(6,*) "d4     =", d4
+end
\ No newline at end of file

>From 3928151da22c344c403585c6b4420a3097991ad0 Mon Sep 17 00:00:00 2001
From: Ebin-McW <ebin.jose at multicorewareinc.com>
Date: Thu, 3 Jul 2025 16:04:36 +0530
Subject: [PATCH 3/3] Update adjustr.f90

---
 flang/test/Lower/adjustr.f90 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/flang/test/Lower/adjustr.f90 b/flang/test/Lower/adjustr.f90
index 6add2f5456bbc..4813834614bd6 100644
--- a/flang/test/Lower/adjustr.f90
+++ b/flang/test/Lower/adjustr.f90
@@ -11,4 +11,4 @@ program main
     d4 = adjustr(d1 // d1)
   end where
   write(6,*) "d4     =", d4
-end
\ No newline at end of file
+end



More information about the flang-commits mailing list