[flang-commits] [flang] 734ad03 - [flang] Handle boxed characters that are values when doing a conversion

Valentin Clement via flang-commits flang-commits at lists.llvm.org
Thu Jun 23 09:05:32 PDT 2022


Author: Valentin Clement
Date: 2022-06-23T18:05:24+02:00
New Revision: 734ad031f16653f1b650d2ea8b7d83e8910925b3

URL: https://github.com/llvm/llvm-project/commit/734ad031f16653f1b650d2ea8b7d83e8910925b3
DIFF: https://github.com/llvm/llvm-project/commit/734ad031f16653f1b650d2ea8b7d83e8910925b3.diff

LOG: [flang] Handle boxed characters that are values when doing a conversion

Character conversion requires memory storage as it operates on a
sequence of code points.

This patch is part of the upstreaming effort from fir-dev branch.

Reviewed By: PeteSteinfeld

Differential Revision: https://reviews.llvm.org/D128438

Co-authored-by: Eric Schweitz <eschweitz at nvidia.com>

Added: 
    flang/test/Fir/achar.f90

Modified: 
    flang/lib/Lower/ConvertExpr.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/ConvertExpr.cpp b/flang/lib/Lower/ConvertExpr.cpp
index 92d379d3e317a..4fdcc04662c40 100644
--- a/flang/lib/Lower/ConvertExpr.cpp
+++ b/flang/lib/Lower/ConvertExpr.cpp
@@ -1301,10 +1301,20 @@ class ScalarExprLowering {
             // to "0xE2 0x82 0xAC" : UTF-8.
             mlir::Value bufferSize = boxchar.getLen();
             auto kindMap = builder.getKindMap();
-            auto fromBits = kindMap.getCharacterBitsize(
-                fir::unwrapRefType(boxchar.getAddr().getType())
-                    .cast<fir::CharacterType>()
-                    .getFKind());
+            mlir::Value boxCharAddr = boxchar.getAddr();
+            auto fromTy = boxCharAddr.getType();
+            if (auto charTy = fromTy.dyn_cast<fir::CharacterType>()) {
+              // boxchar is a value, not a variable. Turn it into a temporary.
+              // As a value, it ought to have a constant LEN value.
+              assert(charTy.hasConstantLen() && "must have constant length");
+              mlir::Value tmp = builder.createTemporary(loc, charTy);
+              builder.create<fir::StoreOp>(loc, boxCharAddr, tmp);
+              boxCharAddr = tmp;
+            }
+            auto fromBits =
+                kindMap.getCharacterBitsize(fir::unwrapRefType(fromTy)
+                                                .cast<fir::CharacterType>()
+                                                .getFKind());
             auto toBits = kindMap.getCharacterBitsize(
                 ty.cast<fir::CharacterType>().getFKind());
             if (toBits < fromBits) {
@@ -1316,7 +1326,7 @@ class ScalarExprLowering {
             }
             auto dest = builder.create<fir::AllocaOp>(
                 loc, ty, mlir::ValueRange{bufferSize});
-            builder.create<fir::CharConvertOp>(loc, boxchar.getAddr(),
+            builder.create<fir::CharConvertOp>(loc, boxCharAddr,
                                                boxchar.getLen(), dest);
             return fir::CharBoxValue{dest, boxchar.getLen()};
           } else {

diff  --git a/flang/test/Fir/achar.f90 b/flang/test/Fir/achar.f90
new file mode 100644
index 0000000000000..691f4ae811a9d
--- /dev/null
+++ b/flang/test/Fir/achar.f90
@@ -0,0 +1,24 @@
+! RUN: bbc -emit-fir %s -o - | FileCheck %s
+
+! Tests ACHAR lowering (converting an INTEGER to a CHARACTER (singleton, LEN=1)
+! along with conversion of CHARACTER to another KIND.
+subroutine achar_test1(a)
+  integer, parameter :: ckind = 2
+  integer, intent(in) :: a
+  character(kind=ckind, len=1) :: ch
+
+  ch = achar(a)
+  call achar_test1_foo(ch)
+end subroutine achar_test1
+
+! CHECK-LABEL: func @_QPachar_test1(
+! CHECK-SAME: %[[arg:.*]]: !fir.ref<i32> {fir.bindc_name = "a"}) {
+! CHECK: %[[VAL_0:.*]] = fir.alloca !fir.char<1>
+! CHECK: %[[VAL_1:.*]] = fir.alloca !fir.char<2> {bindc_name = "ch", uniq_name = "_QFachar_test1Ech"}
+! CHECK: %[[VAL_2:.*]] = fir.load %[[arg]] : !fir.ref<i32>
+! CHECK: %[[VAL_5:.*]] = fir.undefined !fir.char<1>
+! CHECK: %[[VAL_6:.*]] = fir.insert_value %[[VAL_5]], %{{.*}}, [0 : index] : (!fir.char<1>, i8) -> !fir.char<1>
+! CHECK: fir.store %[[VAL_6]] to %[[VAL_0]] : !fir.ref<!fir.char<1>>
+! CHECK: %[[VAL_7:.*]] = fir.alloca !fir.char<2,?>(%{{.*}} : index)
+! CHECK: fir.char_convert %[[VAL_0]] for %{{.*}} to %[[VAL_7]] : !fir.ref<!fir.char<1>>, index, !fir.ref<!fir.char<2,?>>
+! CHECK: fir.call @_QPachar_test1_foo(%{{.*}}) : (!fir.boxchar<2>) -> ()


        


More information about the flang-commits mailing list