[flang-commits] [flang] a033bf2 - [flang][hlfir] work towards handling char_convert in hlfir
via flang-commits
flang-commits at lists.llvm.org
Mon Jul 31 07:45:19 PDT 2023
Author: cabreraam
Date: 2023-07-31T10:45:10-04:00
New Revision: a033bf242f7dccd5546292bf6e554cf2de3ccd58
URL: https://github.com/llvm/llvm-project/commit/a033bf242f7dccd5546292bf6e554cf2de3ccd58
DIFF: https://github.com/llvm/llvm-project/commit/a033bf242f7dccd5546292bf6e554cf2de3ccd58.diff
LOG: [flang][hlfir] work towards handling char_convert in hlfir
This patch aims to address the TODO for handling character conversion in HLFIR found [here](https://github.com/llvm/llvm-project/blob/1defa781243f9d0bc66719465e4de33e9fb7a243/flang/lib/Lower/ConvertExprToHLFIR.cpp#L1388) using [this similar operation but for FIR as inspiration](https://github.com/llvm/llvm-project/blob/3ea673a97b0583affc22345b9d62e863ba36b3d8/flang/lib/Lower/ConvertExpr.cpp#L1212-L1271).
Reviewed By: vzakhari, tblah
Differential Revision: https://reviews.llvm.org/D155650
Added:
flang/test/Lower/charconvert.f90
Modified:
flang/lib/Lower/ConvertExprToHLFIR.cpp
Removed:
################################################################################
diff --git a/flang/lib/Lower/ConvertExprToHLFIR.cpp b/flang/lib/Lower/ConvertExprToHLFIR.cpp
index b17aa11be35e0c..698f3422bda6f5 100644
--- a/flang/lib/Lower/ConvertExprToHLFIR.cpp
+++ b/flang/lib/Lower/ConvertExprToHLFIR.cpp
@@ -1385,7 +1385,34 @@ struct UnaryOp<
hlfir::Entity lhs) {
if constexpr (TC1 == Fortran::common::TypeCategory::Character &&
TC2 == TC1) {
- TODO(loc, "character conversion in HLFIR");
+ // TODO(loc, "character conversion in HLFIR");
+ auto kindMap = builder.getKindMap();
+ mlir::Type fromTy = lhs.getFortranElementType();
+ mlir::Value origBufferSize = genCharLength(loc, builder, lhs);
+ mlir::Value bufferSize{origBufferSize};
+ auto fromBits = kindMap.getCharacterBitsize(
+ fir::unwrapRefType(fromTy).cast<fir::CharacterType>().getFKind());
+ mlir::Type toTy = Fortran::lower::getFIRType(
+ builder.getContext(), TC1, KIND, /*params=*/std::nullopt);
+ auto toBits = kindMap.getCharacterBitsize(
+ toTy.cast<fir::CharacterType>().getFKind());
+ if (toBits < fromBits) {
+ // Scale by relative ratio to give a buffer of the same length.
+ auto ratio = builder.createIntegerConstant(loc, bufferSize.getType(),
+ fromBits / toBits);
+ bufferSize =
+ builder.create<mlir::arith::MulIOp>(loc, bufferSize, ratio);
+ }
+ // allocate space on the stack for toBuffer
+ auto dest = builder.create<fir::AllocaOp>(loc, toTy,
+ mlir::ValueRange{bufferSize});
+ builder.create<fir::CharConvertOp>(loc, lhs.getFirBase(), origBufferSize,
+ dest);
+
+ return hlfir::EntityWithAttributes{builder.create<hlfir::DeclareOp>(
+ loc, dest, "ctor.temp", /*shape=*/nullptr,
+ /*typeparams=*/mlir::ValueRange{origBufferSize},
+ fir::FortranVariableFlagsAttr{})};
}
mlir::Type type = Fortran::lower::getFIRType(builder.getContext(), TC1,
KIND, /*params=*/std::nullopt);
diff --git a/flang/test/Lower/charconvert.f90 b/flang/test/Lower/charconvert.f90
new file mode 100644
index 00000000000000..427b5b1b262712
--- /dev/null
+++ b/flang/test/Lower/charconvert.f90
@@ -0,0 +1,39 @@
+! RUN: bbc -emit-hlfir %s -o - | FileCheck %s
+
+subroutine test_c1_to_c4(c4, c1)
+ character(len=*, kind=4) :: c4
+ character(len=*, kind=1) :: c1
+ c4 = c1
+end subroutine
+
+subroutine test_c4_to_c1(c4, c1)
+ character(len=*, kind=4) :: c4
+ character(len=*, kind=1) :: c1
+ c1 = c4
+end subroutine
+
+! CHECK: func.func @_QPtest_c1_to_c4(%[[ARG0:.*]]: !fir.boxchar<4> {fir.bindc_name = "c4"}, %[[ARG1:.*]]: !fir.boxchar<1> {fir.bindc_name = "c1"}) {
+! CHECK: %[[VAL_0:.*]]:2 = fir.unboxchar %[[ARG1]] : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
+! CHECK: %[[VAL_1:.*]]:2 = hlfir.declare %[[VAL_0]]#0 typeparams %[[VAL_0]]#1 {uniq_name = "_QFtest_c1_to_c4Ec1"} : (!fir.ref<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
+! CHECK: %[[VAL_2:.*]]:2 = fir.unboxchar %[[ARG0]] : (!fir.boxchar<4>) -> (!fir.ref<!fir.char<4,?>>, index)
+! CHECK: %[[VAL_3:.*]]:2 = hlfir.declare %[[VAL_2]]#0 typeparams %[[VAL_2]]#1 {uniq_name = "_QFtest_c1_to_c4Ec4"} : (!fir.ref<!fir.char<4,?>>, index) -> (!fir.boxchar<4>, !fir.ref<!fir.char<4,?>>)
+! CHECK: %[[VAL_4:.*]] = fir.alloca !fir.char<4,?>(%[[VAL_0]]#1 : index)
+! CHECK: fir.char_convert %[[VAL_1]]#1 for %[[VAL_0]]#1 to %[[VAL_4:.*]] : !fir.ref<!fir.char<1,?>>, index, !fir.ref<!fir.char<4,?>>
+! CHECK: %[[VAL_5:.*]]:2 = hlfir.declare %[[VAL_4]] typeparams %[[VAL_0]]#1 {uniq_name = "ctor.temp"} : (!fir.ref<!fir.char<4,?>>, index) -> (!fir.boxchar<4>, !fir.ref<!fir.char<4,?>>)
+! CHECK: hlfir.assign %[[VAL_5]]#0 to %[[VAL_3]]#0 : !fir.boxchar<4>, !fir.boxchar<4>
+! CHECK: return
+! CHECK: }
+
+! CHECK: func.func @_QPtest_c4_to_c1(%[[ARG0:.*]]: !fir.boxchar<4> {fir.bindc_name = "c4"}, %[[ARG1:.*]]: !fir.boxchar<1> {fir.bindc_name = "c1"}) {
+! CHECK: %[[VAL_0:.*]]:2 = fir.unboxchar %[[ARG1]] : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
+! CHECK: %[[VAL_1:.*]]:2 = hlfir.declare %[[VAL_0]]#0 typeparams %[[VAL_0]]#1 {uniq_name = "_QFtest_c4_to_c1Ec1"} : (!fir.ref<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
+! CHECK: %[[VAL_2:.*]]:2 = fir.unboxchar %[[ARG0]] : (!fir.boxchar<4>) -> (!fir.ref<!fir.char<4,?>>, index)
+! CHECK: %[[VAL_3:.*]]:2 = hlfir.declare %[[VAL_2]]#0 typeparams %[[VAL_2]]#1 {uniq_name = "_QFtest_c4_to_c1Ec4"} : (!fir.ref<!fir.char<4,?>>, index) -> (!fir.boxchar<4>, !fir.ref<!fir.char<4,?>>)
+! CHECK: %[[C4:.*]] = arith.constant 4 : index
+! CHECK: %[[VAL_4:.*]] = arith.muli %[[VAL_2]]#1, %[[C4]] : index
+! CHECK: %[[VAL_5:.*]] = fir.alloca !fir.char<1,?>(%[[VAL_4]] : index)
+! CHECK: fir.char_convert %[[VAL_3]]#1 for %[[VAL_2]]#1 to %[[VAL_5:.*]] : !fir.ref<!fir.char<4,?>>, index, !fir.ref<!fir.char<1,?>>
+! CHECK: %[[VAL_6:.*]]:2 = hlfir.declare %[[VAL_5]] typeparams %[[VAL_2]]#1 {uniq_name = "ctor.temp"} : (!fir.ref<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
+! CHECK: hlfir.assign %[[VAL_6]]#0 to %[[VAL_1]]#0 : !fir.boxchar<1>, !fir.boxchar<1>
+! CHECK: return
+! CHECK: }
\ No newline at end of file
More information about the flang-commits
mailing list