[flang-commits] [flang] [flang][hlfir] address char_convert issues as mentioned in #64315 (PR #67570)
Anthony Cabrera via flang-commits
flang-commits at lists.llvm.org
Wed Oct 4 09:04:22 PDT 2023
https://github.com/cabreraam updated https://github.com/llvm/llvm-project/pull/67570
>From 71876d06b150586e0660516a89e73f84ac635b79 Mon Sep 17 00:00:00 2001
From: cabreraam <cabreraam33 at gmail.com>
Date: Tue, 26 Sep 2023 18:02:00 -0400
Subject: [PATCH 1/5] [flang][hlfir] address char_convert issues as mentioned
in #64315
---
flang/lib/Lower/ConvertExprToHLFIR.cpp | 28 ++++++++++--
flang/test/Lower/HLFIR/charconvert.f90 | 60 ++++++++++++++++++++++++++
2 files changed, 85 insertions(+), 3 deletions(-)
create mode 100644 flang/test/Lower/HLFIR/charconvert.f90
diff --git a/flang/lib/Lower/ConvertExprToHLFIR.cpp b/flang/lib/Lower/ConvertExprToHLFIR.cpp
index bc98fdd917d41d0..7fed9dc5c08868e 100644
--- a/flang/lib/Lower/ConvertExprToHLFIR.cpp
+++ b/flang/lib/Lower/ConvertExprToHLFIR.cpp
@@ -1403,6 +1403,22 @@ struct UnaryOp<Fortran::evaluate::Parentheses<T>> {
}
};
+static fir::CharBoxValue genUnboxChar(mlir::Location loc,
+ fir::FirOpBuilder &builder,
+ mlir::Value boxChar) {
+ if (auto emboxChar = boxChar.getDefiningOp<fir::EmboxCharOp>())
+ return {emboxChar.getMemref(), emboxChar.getLen()};
+ mlir::Type refType = fir::ReferenceType::get(
+ boxChar.getType().cast<fir::BoxCharType>().getEleTy());
+ auto unboxed = builder.create<fir::UnboxCharOp>(
+ loc, refType, builder.getIndexType(), boxChar);
+ mlir::Value addr = unboxed.getResult(0);
+ mlir::Value len = unboxed.getResult(1);
+ if (auto varIface = boxChar.getDefiningOp<fir::FortranVariableOpInterface>())
+ if (mlir::Value explicitlen = varIface.getExplicitCharLen())
+ len = explicitlen;
+ return {addr, len};
+}
template <Fortran::common::TypeCategory TC1, int KIND,
Fortran::common::TypeCategory TC2>
struct UnaryOp<
@@ -1414,7 +1430,6 @@ struct UnaryOp<
hlfir::Entity lhs) {
if constexpr (TC1 == Fortran::common::TypeCategory::Character &&
TC2 == TC1) {
- // TODO(loc, "character conversion in HLFIR");
auto kindMap = builder.getKindMap();
mlir::Type fromTy = lhs.getFortranElementType();
mlir::Value origBufferSize = genCharLength(loc, builder, lhs);
@@ -1435,8 +1450,15 @@ struct UnaryOp<
// 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);
+ auto src = lhs.getFirBase();
+ if (lhs.getType().isa<hlfir::ExprType>()) {
+ if (auto asExpr = lhs.getDefiningOp<hlfir::AsExprOp>()) {
+ src = asExpr.getVar();
+ }
+ } else if (lhs.getType().isa<fir::BoxCharType>())
+ if (!lhs.getDefiningOp<hlfir::DeclareOp>())
+ src = genUnboxChar(loc, builder, lhs).getAddr();
+ builder.create<fir::CharConvertOp>(loc, src, origBufferSize, dest);
return hlfir::EntityWithAttributes{builder.create<hlfir::DeclareOp>(
loc, dest, "ctor.temp", /*shape=*/nullptr,
diff --git a/flang/test/Lower/HLFIR/charconvert.f90 b/flang/test/Lower/HLFIR/charconvert.f90
new file mode 100644
index 000000000000000..9779e79e4034d90
--- /dev/null
+++ b/flang/test/Lower/HLFIR/charconvert.f90
@@ -0,0 +1,60 @@
+! Test lowering of character concatenation to HLFIR
+! RUN: bbc -emit-hlfir -o - %s 2>&1 | FileCheck %s
+
+subroutine charconvert1(c,n)
+ character(*,4),intent(in) :: c(:)
+ integer,intent(in) :: n
+ interface
+ subroutine callee(c)
+ character(*),intent(in) :: c(:)
+ end subroutine callee
+ end interface
+ call show([character(n)::c])
+end subroutine charconvert1
+
+! CHECK-LABEL: func.func @_QPcharconvert1
+! CHECK: %[[VAL_2:.*]]:2 = hlfir.declare %{{.*}} {fortran_attrs = #fir.var_attrs<intent_in>, uniq_name = "_QFcharconvert1Ec"} : (!fir.box<!fir.array<?x!fir.char<4,?>>>) -> (!fir.box<!fir.array<?x!fir.char<4,?>>>, !fir.box<!fir.array<?x!fir.char<4,?>>>)
+! CHECK: ^bb0(%[[ARG2:.*]]: index):
+! CHECK: %[[VAL_37:.*]] = fir.box_elesize %[[VAL_2]]#1 : (!fir.box<!fir.array<?x!fir.char<4,?>>>) -> index
+! CHECK: %[[C4_4:.*]] = arith.constant 4 : index
+! CHECK: %[[VAL_38:.*]] = arith.divsi %[[VAL_37]], %[[C4_4]] : index
+! CHECK: %[[VAL_39:.*]] = hlfir.designate %[[VAL_2]]#0 (%[[ARG2]]) typeparams %[[VAL_38]] : (!fir.box<!fir.array<?x!fir.char<4,?>>>, index, index) -> !fir.boxchar<4>
+! CHECK: %[[C4_5:.*]] = arith.constant 4 : index
+! CHECK: %[[VAL_40:.*]] = arith.muli %[[VAL_38]], %[[C4_5]] : index
+! CHECK: %[[VAL_41:.*]] = fir.alloca !fir.char<1,?>(%[[VAL_40]] : index)
+! CHECK: %[[VAL_42:.*]]:2 = fir.unboxchar %[[VAL_39]] : (!fir.boxchar<4>) -> (!fir.ref<!fir.char<4,?>>, index)
+! CHECK: fir.char_convert %[[VAL_42]]#0 for %[[VAL_38:.*]] to %[[VAL_41]] : !fir.ref<!fir.char<4,?>>, index, !fir.ref<!fir.char<1,?>>
+! CHECK: %[[VAL_43:.*]]:2 = hlfir.declare %[[VAL_41]] typeparams %[[VAL_38]] {uniq_name = "ctor.temp"} : (!fir.ref<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
+! CHECK: hlfir.yield_element %[[VAL_43]]#0 : !fir.boxchar<1>
+! CHECK: }
+
+subroutine test2(x)
+ integer,intent(in) :: x
+ character(kind=4) :: cx
+ cx = achar(x)
+end subroutine test2
+! CHECK-LABEL: func.func @_QPtest2
+! CHECK-SAME: %[[ARG0:.*]]: !fir.ref<i32>
+! CHECK: %[[VAL_0:.*]] = fir.alloca !fir.char<1>
+! CHECK: %[[C1:.*]] = arith.constant 1 : index
+! CHECK: %[[VAL_1:.*]] = fir.alloca !fir.char<4> {bindc_name = "cx", uniq_name = "_QFtest2Ecx"}
+! CHECK: %[[VAL_2:.*]]:2 = hlfir.declare %[[VAL_1]] typeparams %[[C1]] {uniq_name = "_QFtest2Ecx"} : (!fir.ref<!fir.char<4>>, index) -> (!fir.ref<!fir.char<4>>, !fir.ref<!fir.char<4>>)
+! CHECK: %[[VAL_3:.*]]:2 = hlfir.declare %[[ARG0]] {fortran_attrs = #fir.var_attrs<intent_in>, uniq_name = "_QFtest2Ex"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
+! CHECK: %[[VAL_4:.*]] = fir.load %[[VAL_3]]#0 : !fir.ref<i32>
+! CHECK: %[[VAL_5:.*]] = fir.convert %[[VAL_4]] : (i32) -> i64
+! CHECK: %[[VAL_6:.*]] = fir.convert %[[VAL_5]] : (i64) -> i8
+! CHECK: %[[VAL_7:.*]] = fir.undefined !fir.char<1>
+! CHECK: %[[VAL_8:.*]] = fir.insert_value %[[VAL_7]], %[[VAL_6]], [0 : index] : (!fir.char<1>, i8) -> !fir.char<1>
+! CHECK: fir.store %[[VAL_8:.*]] to %[[VAL_0]] : !fir.ref<!fir.char<1>>
+! CHECK: %[[FALSE:.*]] = arith.constant false
+! CHECK: %[[VAL_9:.*]] = hlfir.as_expr %[[VAL_0]] move %[[FALSE]] : (!fir.ref<!fir.char<1>>, i1) -> !hlfir.expr<!fir.char<1>>
+! CHECK: %[[C1_0:.*]] = arith.constant 1 : index
+! CHECK: %[[VAL_10:.*]] = fir.alloca !fir.char<4,?>(%[[C1_0]] : index)
+! CHECK: fir.char_convert %[[VAL_0:.*]] for %[[C1_0]] to %[[VAL_10]] : !fir.ref<!fir.char<1>>, index, !fir.ref<!fir.char<4,?>>
+! CHECK: %[[VAL_11:.*]]:2 = hlfir.declare %[[VAL_10]] typeparams %[[C1_0]] {uniq_name = "ctor.temp"} : (!fir.ref<!fir.char<4,?>>, index) -> (!fir.boxchar<4>, !fir.ref<!fir.char<4,?>>)
+! CHECK: %[[C1_I64:.*]] = arith.constant 1 : i64
+! CHECK: %[[VAL_12:.*]] = hlfir.set_length %[[VAL_11]]#0 len %[[C1_I64]] : (!fir.boxchar<4>, i64) -> !hlfir.expr<!fir.char<4>>
+! CHECK: hlfir.assign %[[VAL_12:.*]] to %[[VAL_2]]#0 : !hlfir.expr<!fir.char<4>>, !fir.ref<!fir.char<4>>
+! CHECK: hlfir.destroy %[[VAL_9:.*]] : !hlfir.expr<!fir.char<1>>
+! CHECK: return
+! CHECK: }
>From 852488e73aef469b02f09502bfed817b15c05297 Mon Sep 17 00:00:00 2001
From: cabreraam <cabreraam33 at gmail.com>
Date: Mon, 2 Oct 2023 15:34:33 -0400
Subject: [PATCH 2/5] address Jean's comment
---
flang/lib/Lower/ConvertExprToHLFIR.cpp | 13 ++++++++-----
flang/test/Lower/HLFIR/charconvert.f90 | 12 ++++++------
2 files changed, 14 insertions(+), 11 deletions(-)
diff --git a/flang/lib/Lower/ConvertExprToHLFIR.cpp b/flang/lib/Lower/ConvertExprToHLFIR.cpp
index 7fed9dc5c08868e..4161fc0bcc84279 100644
--- a/flang/lib/Lower/ConvertExprToHLFIR.cpp
+++ b/flang/lib/Lower/ConvertExprToHLFIR.cpp
@@ -1403,7 +1403,7 @@ struct UnaryOp<Fortran::evaluate::Parentheses<T>> {
}
};
-static fir::CharBoxValue genUnboxChar(mlir::Location loc,
+/*static fir::CharBoxValue genUnboxChar(mlir::Location loc,
fir::FirOpBuilder &builder,
mlir::Value boxChar) {
if (auto emboxChar = boxChar.getDefiningOp<fir::EmboxCharOp>())
@@ -1418,7 +1418,7 @@ static fir::CharBoxValue genUnboxChar(mlir::Location loc,
if (mlir::Value explicitlen = varIface.getExplicitCharLen())
len = explicitlen;
return {addr, len};
-}
+}*/
template <Fortran::common::TypeCategory TC1, int KIND,
Fortran::common::TypeCategory TC2>
struct UnaryOp<
@@ -1450,15 +1450,18 @@ struct UnaryOp<
// allocate space on the stack for toBuffer
auto dest = builder.create<fir::AllocaOp>(loc, toTy,
mlir::ValueRange{bufferSize});
- auto src = lhs.getFirBase();
+ /*auto src = lhs.getFirBase();
if (lhs.getType().isa<hlfir::ExprType>()) {
if (auto asExpr = lhs.getDefiningOp<hlfir::AsExprOp>()) {
src = asExpr.getVar();
}
} else if (lhs.getType().isa<fir::BoxCharType>())
if (!lhs.getDefiningOp<hlfir::DeclareOp>())
- src = genUnboxChar(loc, builder, lhs).getAddr();
- builder.create<fir::CharConvertOp>(loc, src, origBufferSize, dest);
+ src = genUnboxChar(loc, builder, lhs).getAddr();*/
+ auto src = hlfir::convertToAddress(loc, builder, lhs,
+ lhs.getFortranElementType());
+ builder.create<fir::CharConvertOp>(loc, src.first.getCharBox()->getAddr(),
+ origBufferSize, dest);
return hlfir::EntityWithAttributes{builder.create<hlfir::DeclareOp>(
loc, dest, "ctor.temp", /*shape=*/nullptr,
diff --git a/flang/test/Lower/HLFIR/charconvert.f90 b/flang/test/Lower/HLFIR/charconvert.f90
index 9779e79e4034d90..b76eea665234ea1 100644
--- a/flang/test/Lower/HLFIR/charconvert.f90
+++ b/flang/test/Lower/HLFIR/charconvert.f90
@@ -28,18 +28,18 @@ end subroutine charconvert1
! CHECK: hlfir.yield_element %[[VAL_43]]#0 : !fir.boxchar<1>
! CHECK: }
-subroutine test2(x)
+subroutine charconvert2(x)
integer,intent(in) :: x
character(kind=4) :: cx
cx = achar(x)
-end subroutine test2
-! CHECK-LABEL: func.func @_QPtest2
+end subroutine charconvert2
+! CHECK-LABEL: func.func @_QPcharconvert2
! CHECK-SAME: %[[ARG0:.*]]: !fir.ref<i32>
! CHECK: %[[VAL_0:.*]] = fir.alloca !fir.char<1>
! CHECK: %[[C1:.*]] = arith.constant 1 : index
-! CHECK: %[[VAL_1:.*]] = fir.alloca !fir.char<4> {bindc_name = "cx", uniq_name = "_QFtest2Ecx"}
-! CHECK: %[[VAL_2:.*]]:2 = hlfir.declare %[[VAL_1]] typeparams %[[C1]] {uniq_name = "_QFtest2Ecx"} : (!fir.ref<!fir.char<4>>, index) -> (!fir.ref<!fir.char<4>>, !fir.ref<!fir.char<4>>)
-! CHECK: %[[VAL_3:.*]]:2 = hlfir.declare %[[ARG0]] {fortran_attrs = #fir.var_attrs<intent_in>, uniq_name = "_QFtest2Ex"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
+! CHECK: %[[VAL_1:.*]] = fir.alloca !fir.char<4> {bindc_name = "cx", uniq_name = "_QFcharconvert2Ecx"}
+! CHECK: %[[VAL_2:.*]]:2 = hlfir.declare %[[VAL_1]] typeparams %[[C1]] {uniq_name = "_QFcharconvert2Ecx"} : (!fir.ref<!fir.char<4>>, index) -> (!fir.ref<!fir.char<4>>, !fir.ref<!fir.char<4>>)
+! CHECK: %[[VAL_3:.*]]:2 = hlfir.declare %[[ARG0]] {fortran_attrs = #fir.var_attrs<intent_in>, uniq_name = "_QFcharconvert2Ex"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
! CHECK: %[[VAL_4:.*]] = fir.load %[[VAL_3]]#0 : !fir.ref<i32>
! CHECK: %[[VAL_5:.*]] = fir.convert %[[VAL_4]] : (i32) -> i64
! CHECK: %[[VAL_6:.*]] = fir.convert %[[VAL_5]] : (i64) -> i8
>From d95b4736317ad588f7dab5e88e792faab32457f1 Mon Sep 17 00:00:00 2001
From: cabreraam <cabreraam33 at gmail.com>
Date: Mon, 2 Oct 2023 15:47:19 -0400
Subject: [PATCH 3/5] additional cleanup
---
flang/lib/Lower/ConvertExprToHLFIR.cpp | 24 ------------------------
1 file changed, 24 deletions(-)
diff --git a/flang/lib/Lower/ConvertExprToHLFIR.cpp b/flang/lib/Lower/ConvertExprToHLFIR.cpp
index 4161fc0bcc84279..e00a56e024b3f9a 100644
--- a/flang/lib/Lower/ConvertExprToHLFIR.cpp
+++ b/flang/lib/Lower/ConvertExprToHLFIR.cpp
@@ -1403,22 +1403,6 @@ struct UnaryOp<Fortran::evaluate::Parentheses<T>> {
}
};
-/*static fir::CharBoxValue genUnboxChar(mlir::Location loc,
- fir::FirOpBuilder &builder,
- mlir::Value boxChar) {
- if (auto emboxChar = boxChar.getDefiningOp<fir::EmboxCharOp>())
- return {emboxChar.getMemref(), emboxChar.getLen()};
- mlir::Type refType = fir::ReferenceType::get(
- boxChar.getType().cast<fir::BoxCharType>().getEleTy());
- auto unboxed = builder.create<fir::UnboxCharOp>(
- loc, refType, builder.getIndexType(), boxChar);
- mlir::Value addr = unboxed.getResult(0);
- mlir::Value len = unboxed.getResult(1);
- if (auto varIface = boxChar.getDefiningOp<fir::FortranVariableOpInterface>())
- if (mlir::Value explicitlen = varIface.getExplicitCharLen())
- len = explicitlen;
- return {addr, len};
-}*/
template <Fortran::common::TypeCategory TC1, int KIND,
Fortran::common::TypeCategory TC2>
struct UnaryOp<
@@ -1450,14 +1434,6 @@ struct UnaryOp<
// allocate space on the stack for toBuffer
auto dest = builder.create<fir::AllocaOp>(loc, toTy,
mlir::ValueRange{bufferSize});
- /*auto src = lhs.getFirBase();
- if (lhs.getType().isa<hlfir::ExprType>()) {
- if (auto asExpr = lhs.getDefiningOp<hlfir::AsExprOp>()) {
- src = asExpr.getVar();
- }
- } else if (lhs.getType().isa<fir::BoxCharType>())
- if (!lhs.getDefiningOp<hlfir::DeclareOp>())
- src = genUnboxChar(loc, builder, lhs).getAddr();*/
auto src = hlfir::convertToAddress(loc, builder, lhs,
lhs.getFortranElementType());
builder.create<fir::CharConvertOp>(loc, src.first.getCharBox()->getAddr(),
>From f002f084eb6f8c7a6f5b9ac2cda4bfffa949cbbf Mon Sep 17 00:00:00 2001
From: cabreraam <cabreraam33 at gmail.com>
Date: Tue, 3 Oct 2023 12:14:13 -0400
Subject: [PATCH 4/5] add call to cleanup
---
flang/lib/Lower/ConvertExprToHLFIR.cpp | 2 ++
flang/test/Lower/HLFIR/charconvert.f90 | 12 +-----------
flang/test/Lower/charconvert.f90 | 9 +--------
3 files changed, 4 insertions(+), 19 deletions(-)
diff --git a/flang/lib/Lower/ConvertExprToHLFIR.cpp b/flang/lib/Lower/ConvertExprToHLFIR.cpp
index e00a56e024b3f9a..62a31ee7858a6cc 100644
--- a/flang/lib/Lower/ConvertExprToHLFIR.cpp
+++ b/flang/lib/Lower/ConvertExprToHLFIR.cpp
@@ -1438,6 +1438,8 @@ struct UnaryOp<
lhs.getFortranElementType());
builder.create<fir::CharConvertOp>(loc, src.first.getCharBox()->getAddr(),
origBufferSize, dest);
+ if (src.second.has_value())
+ src.second.value();
return hlfir::EntityWithAttributes{builder.create<hlfir::DeclareOp>(
loc, dest, "ctor.temp", /*shape=*/nullptr,
diff --git a/flang/test/Lower/HLFIR/charconvert.f90 b/flang/test/Lower/HLFIR/charconvert.f90
index b76eea665234ea1..779a8ec9db822e7 100644
--- a/flang/test/Lower/HLFIR/charconvert.f90
+++ b/flang/test/Lower/HLFIR/charconvert.f90
@@ -24,9 +24,6 @@ end subroutine charconvert1
! CHECK: %[[VAL_41:.*]] = fir.alloca !fir.char<1,?>(%[[VAL_40]] : index)
! CHECK: %[[VAL_42:.*]]:2 = fir.unboxchar %[[VAL_39]] : (!fir.boxchar<4>) -> (!fir.ref<!fir.char<4,?>>, index)
! CHECK: fir.char_convert %[[VAL_42]]#0 for %[[VAL_38:.*]] to %[[VAL_41]] : !fir.ref<!fir.char<4,?>>, index, !fir.ref<!fir.char<1,?>>
-! CHECK: %[[VAL_43:.*]]:2 = hlfir.declare %[[VAL_41]] typeparams %[[VAL_38]] {uniq_name = "ctor.temp"} : (!fir.ref<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
-! CHECK: hlfir.yield_element %[[VAL_43]]#0 : !fir.boxchar<1>
-! CHECK: }
subroutine charconvert2(x)
integer,intent(in) :: x
@@ -50,11 +47,4 @@ end subroutine charconvert2
! CHECK: %[[VAL_9:.*]] = hlfir.as_expr %[[VAL_0]] move %[[FALSE]] : (!fir.ref<!fir.char<1>>, i1) -> !hlfir.expr<!fir.char<1>>
! CHECK: %[[C1_0:.*]] = arith.constant 1 : index
! CHECK: %[[VAL_10:.*]] = fir.alloca !fir.char<4,?>(%[[C1_0]] : index)
-! CHECK: fir.char_convert %[[VAL_0:.*]] for %[[C1_0]] to %[[VAL_10]] : !fir.ref<!fir.char<1>>, index, !fir.ref<!fir.char<4,?>>
-! CHECK: %[[VAL_11:.*]]:2 = hlfir.declare %[[VAL_10]] typeparams %[[C1_0]] {uniq_name = "ctor.temp"} : (!fir.ref<!fir.char<4,?>>, index) -> (!fir.boxchar<4>, !fir.ref<!fir.char<4,?>>)
-! CHECK: %[[C1_I64:.*]] = arith.constant 1 : i64
-! CHECK: %[[VAL_12:.*]] = hlfir.set_length %[[VAL_11]]#0 len %[[C1_I64]] : (!fir.boxchar<4>, i64) -> !hlfir.expr<!fir.char<4>>
-! CHECK: hlfir.assign %[[VAL_12:.*]] to %[[VAL_2]]#0 : !hlfir.expr<!fir.char<4>>, !fir.ref<!fir.char<4>>
-! CHECK: hlfir.destroy %[[VAL_9:.*]] : !hlfir.expr<!fir.char<1>>
-! CHECK: return
-! CHECK: }
+! CHECK: fir.char_convert %[[VAL_0:.*]] for %[[C1_0]] to %[[VAL_10]] : !fir.ref<!fir.char<1>>, index, !fir.ref<!fir.char<4,?>>
\ No newline at end of file
diff --git a/flang/test/Lower/charconvert.f90 b/flang/test/Lower/charconvert.f90
index 427b5b1b2627124..693d5bf60378864 100644
--- a/flang/test/Lower/charconvert.f90
+++ b/flang/test/Lower/charconvert.f90
@@ -19,10 +19,6 @@ subroutine test_c4_to_c1(c4, c1)
! 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)
@@ -33,7 +29,4 @@ subroutine test_c4_to_c1(c4, c1)
! 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
+! 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,?>>)
\ No newline at end of file
>From cb09b8099e3a3dbf86382818346471798acc76a0 Mon Sep 17 00:00:00 2001
From: cabreraam <cabreraam33 at gmail.com>
Date: Wed, 4 Oct 2023 11:58:58 -0400
Subject: [PATCH 5/5] call hlfir::cleanupfunction correctly and add test
---
flang/lib/Lower/ConvertExprToHLFIR.cpp | 2 +-
.../char_extremum-bufferization-done.fir | 67 +++++++++++++++++++
flang/test/Lower/HLFIR/charconvert.f90 | 23 ++++++-
3 files changed, 90 insertions(+), 2 deletions(-)
create mode 100644 flang/test/HLFIR/char_extremum-bufferization-done.fir
diff --git a/flang/lib/Lower/ConvertExprToHLFIR.cpp b/flang/lib/Lower/ConvertExprToHLFIR.cpp
index 62a31ee7858a6cc..44c9eb1e9123b63 100644
--- a/flang/lib/Lower/ConvertExprToHLFIR.cpp
+++ b/flang/lib/Lower/ConvertExprToHLFIR.cpp
@@ -1439,7 +1439,7 @@ struct UnaryOp<
builder.create<fir::CharConvertOp>(loc, src.first.getCharBox()->getAddr(),
origBufferSize, dest);
if (src.second.has_value())
- src.second.value();
+ src.second.value()();
return hlfir::EntityWithAttributes{builder.create<hlfir::DeclareOp>(
loc, dest, "ctor.temp", /*shape=*/nullptr,
diff --git a/flang/test/HLFIR/char_extremum-bufferization-done.fir b/flang/test/HLFIR/char_extremum-bufferization-done.fir
new file mode 100644
index 000000000000000..3ed501afb0044b9
--- /dev/null
+++ b/flang/test/HLFIR/char_extremum-bufferization-done.fir
@@ -0,0 +1,67 @@
+// Test hlfir.concat operation lowering to operations operating on memory.
+
+// RUN: fir-opt %s -bufferize-hlfir | FileCheck %s
+// XFAIL: *
+
+// Along with the TODOs, we'll still need to go and change all of the RHS stuff
+
+func.func @_QPmax1(%arg0: !fir.boxchar<1> {fir.bindc_name = "c1"}, %arg1: !fir.boxchar<1> {fir.bindc_name = "c2"}, %arg2: !fir.boxchar<1> {fir.bindc_name = "c3"}) {
+ %0:2 = fir.unboxchar %arg0 : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
+ %1:2 = hlfir.declare %0#0 typeparams %0#1 {uniq_name = "_QFmax1Ec1"} : (!fir.ref<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
+ %2:2 = fir.unboxchar %arg1 : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
+ %3:2 = hlfir.declare %2#0 typeparams %2#1 {uniq_name = "_QFmax1Ec2"} : (!fir.ref<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
+ %4:2 = fir.unboxchar %arg2 : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
+ %5:2 = hlfir.declare %4#0 typeparams %4#1 {uniq_name = "_QFmax1Ec3"} : (!fir.ref<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
+ %6 = hlfir.char_extremum max, %3#0, %5#0 : (!fir.boxchar<1>, !fir.boxchar<1>) -> !hlfir.expr<!fir.char<1,?>>
+ hlfir.assign %6 to %1#0 : !hlfir.expr<!fir.char<1,?>>, !fir.boxchar<1>
+ hlfir.destroy %6 : !hlfir.expr<!fir.char<1,?>>
+ return
+}
+
+// CHECK-LABEL: func.func @_QPmax1(
+// CHECK-SAME: %[[ARG0:[^:]*]]: !fir.boxchar<1> {{[^,]*}},
+// CHECK-SAME: %[[ARG1:[^:]*]]: !fir.boxchar<1> {{[^,].*}},
+// CHECK-SAME: %[[ARG2:[^:]*]]: !fir.boxchar<1> {{[^)].*}}) {
+// CHECK: %[[ARG0_UNBOXED:.*]]:2 = fir.unboxchar %[[ARG0]] : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
+// CHECK: %[[ARG0_VAL:[^:]*]]:2 = hlfir.declare %[[ARG0_UNBOXED]]#0 typeparams %[[ARG0_UNBOXED]]#1 {uniq_name = "_QFmax1Ec1"} : (!fir.ref<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
+// CHECK: %[[ARG1_UNBOXED:[^:]*]]:2 = fir.unboxchar %[[ARG1]] : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
+// CHECK: %[[ARG1_VAL:[^:]*]]:2 = hlfir.declare %[[ARG1_UNBOXED]]#0 typeparams %[[ARG1_UNBOXED]]#1 {uniq_name = "_QFmax1Ec2"} : (!fir.ref<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
+// CHECK: %[[ARG2_UNBOXED:[^:]*]]:2 = fir.unboxchar %[[ARG2]] : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
+// CHECK: %[[ARG2_VAL:[^:]*]]:2 = hlfir.declare %[[ARG2_UNBOXED]]#0 typeparams %[[ARG2_UNBOXED]]#1 {uniq_name = "_QFmax1Ec3"} : (!fir.ref<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
+// CHECK: %[[BIGGEST_LEN_CMP:[^:]*]] = arith.cmpi uge, %[[ARG1_UNBOXED]]#1, %[[ARG2_UNBOXED]]#1 : index
+// CHECK: %[[BIGGEST_LEN:[^:]*]] = arith.select %[[BIGGEST_LEN_CMP]], %[[ARG1_UNBOXED]]#1, %[[ARG2_UNBOXED]]#1 : index
+// CHECK: %[[CAST_ARG2_BUF_TO_UNKNOWN:[^:]*]] = fir.convert %[[ARG2_VAL]]#1 : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<i8>
+// CHECK: %[[CAST_ARG1_BUF_TO_UNKNOWN:[^:]*]] = fir.convert %[[ARG1_VAL]]#1 : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<i8>
+// CHECK: %[[CAST_ARG2_LEN_TO_LENTYPE:[^:]*]] = fir.convert %[[ARG2_UNBOXED]]#1 : (index) -> i64
+// CHECK: %[[CAST_ARG1_LEN_TO_LENTYPE:[^:]*]] = fir.convert %[[ARG1_UNBOXED]]#1 : (index) -> i64
+// CHECK: %[[LEX_CMP_RT:[^:]*]] = fir.call @_FortranACharacterCompareScalar1(%[[CAST_ARG2_BUF_TO_UNKNOWN]], %[[CAST_ARG1_BUF_TO_UNKNOWN]], %[[CAST_ARG2_LEN_TO_LENTYPE]], %[[CAST_ARG1_LEN_TO_LENTYPE]]) : (!fir.ref<i8>, !fir.ref<i8>, i64, i64) -> i32
+// CHECK: %[[C0_I32:[^:]*]] = arith.constant 0 : i32
+// CHECK: %[[LEX_CMP:[^:]*]] = arith.cmpi sgt, %[[LEX_CMP_RT]], %[[C0_I32]] : i32
+// CHECK: %[[LEX_CMP_RES_BUF:[^:]*]] = arith.select %[[LEX_CMP]], %[[ARG2_VAL]]#1, %[[ARG1_VAL]]#1 : !fir.ref<!fir.char<1,?>>
+// CHECK: %[[LEX_CMP_RES_LEN:[^:]*]] = arith.select %[[LEX_CMP]], %[[ARG2_UNBOXED]]#1, %[[ARG1_UNBOXED]]#1 : index
+// CHECK: %[[TMP_BUF:[^:]*]] = fir.alloca !fir.char<1,?>(%[[BIGGEST_LEN]] : index) {bindc_name = ".chrtmp"}
+// CHECK: %[[LHS_SMALLER_CMP:[^:]*]] = arith.cmpi slt, %[[BIGGEST_LEN]], %[[LEX_CMP_RES_LEN]] : index
+// CHECK: %[[LHS_SMALLER_RES:[^:]*]] = arith.select %[[LHS_SMALLER_CMP]], %[[BIGGEST_LEN]], %[[LEX_CMP_RES_LEN]] : index
+// CHECK: %[[C1_I64:[^:]*]] = arith.constant 1 : i64
+// CHECK: %[[CAST_CONSTANT_1:[^:]*]] = fir.convert %[[LHS_SMALLER_RES]] : (index) -> i64
+// CHECK: %[[NUM_BYTES_TO_MOVE:[^:]*]] = arith.muli %[[C1_I64]], %[[CAST_CONSTANT_1]] : i64
+// CHECK: %[[FALSE:[^:]*]] = arith.constant false
+// CHECK: %[[DST_BUF:[^:]*]] = fir.convert %[[TMP_BUF]] : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<i8>
+// CHECK: %[[SRC_BUF:[^:]*]] = fir.convert %[[LEX_CMP_RES_BUF]] : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<i8>
+// CHECK: fir.call @llvm.memmove.p0.p0.i64(%[[DST_BUF]], %[[SRC_BUF]], %[[NUM_BYTES_TO_MOVE]], %[[FALSE]]) : (!fir.ref<i8>, !fir.ref<i8>, i64, i1) -> ()
+// CHECK: %[[C1:[^:]*]] = arith.constant 1 : index
+// CHECK: %[[PAD_LENGTH:[^:]*]] = arith.subi %[[BIGGEST_LEN]], %[[C1]] : index
+// CHECK: %[[C32_I8:[^:]*]] = arith.constant 32 : i8
+// CHECK: %[[BLANK_CONSTANT:[^:]*]] = fir.undefined !fir.char<1>
+// CHECK: %[[PAD_CHAR:[^:]*]] = fir.insert_value %[[BLANK_CONSTANT]], %[[C32_I8]], [0 : index] : (!fir.char<1>, i8) -> !fir.char<1>
+// CHECK: %[[C1_0:[^:]*]] = arith.constant 1 : index
+// CHECK: fir.do_loop %arg3 = %[[LHS_SMALLER_RES]] to %[[PAD_LENGTH]] step %[[C1_0]] {
+// CHECK: %[[FINAL_DST:[^:]*]] = fir.convert %[[TMP_BUF]] : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<!fir.array<?x!fir.char<1>>>
+// CHECK: %[[FINAL_DST_POS:[^:]*]] = fir.coordinate_of %[[FINAL_DST]], %arg3 : (!fir.ref<!fir.array<?x!fir.char<1>>>, index) -> !fir.ref<!fir.char<1>>
+// CHECK: fir.store %[[PAD_CHAR]] to %[[FINAL_DST_POS]] : !fir.ref<!fir.char<1>>
+// CHECK: %[[FINAL_DEST_SSA:[^:]*]]:2 = hlfir.declare %[[TMP_BUF]] typeparams %[[BIGGEST_LEN]] {uniq_name = "tmp"} : (!fir.ref<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
+// CHECK: %[[FALSE_1:[^:]*]] = arith.constant false
+// CHECK: %[[FINAL_CLEANUP_0:[^:]*]] = fir.undefined tuple<!fir.boxchar<1>, i1>
+// CHECK: %[[FINAL_CLEANUP_1:[^:]*]] = fir.insert_value %[[FINAL_CLEANUP_0]], %[[FALSE_1]], [1 : index] : (tuple<!fir.boxchar<1>, i1>, i1) -> tuple<!fir.boxchar<1>, i1>
+// CHECK: %[[FINAL_CLEANUP_2:[^:]*]] = fir.insert_value %[[FINAL_CLEANUP_1]], %[[FINAL_DEST_SSA]]#0, [0 : index] : (tuple<!fir.boxchar<1>, i1>, !fir.boxchar<1>) -> tuple<!fir.boxchar<1>, i1>
+// CHECK: hlfir.assign %[[FINAL_DEST_SSA]]#0 to %[[ARG0_VAL]]#0 : !fir.boxchar<1>, !fir.boxchar<1>
diff --git a/flang/test/Lower/HLFIR/charconvert.f90 b/flang/test/Lower/HLFIR/charconvert.f90
index 779a8ec9db822e7..9b9c8670077ddd5 100644
--- a/flang/test/Lower/HLFIR/charconvert.f90
+++ b/flang/test/Lower/HLFIR/charconvert.f90
@@ -47,4 +47,25 @@ end subroutine charconvert2
! CHECK: %[[VAL_9:.*]] = hlfir.as_expr %[[VAL_0]] move %[[FALSE]] : (!fir.ref<!fir.char<1>>, i1) -> !hlfir.expr<!fir.char<1>>
! CHECK: %[[C1_0:.*]] = arith.constant 1 : index
! CHECK: %[[VAL_10:.*]] = fir.alloca !fir.char<4,?>(%[[C1_0]] : index)
-! CHECK: fir.char_convert %[[VAL_0:.*]] for %[[C1_0]] to %[[VAL_10]] : !fir.ref<!fir.char<1>>, index, !fir.ref<!fir.char<4,?>>
\ No newline at end of file
+! CHECK: fir.char_convert %[[VAL_0:.*]] for %[[C1_0]] to %[[VAL_10]] : !fir.ref<!fir.char<1>>, index, !fir.ref<!fir.char<4,?>>
+
+subroutine charconvert3(c, c4)
+ character(kind=1, len=*) :: c
+ character(kind=4, len=*) :: c4
+ c4 = c // c
+end subroutine
+
+! CHECK-LABEL: func.func @_QPcharconvert3
+! CHECK-SAME: %[[ARG0:.*]]: !fir.boxchar<1> {{.*}}, %[[ARG1:.*]]: !fir.boxchar<4>
+! CHECK: %[[VAL_0:.*]]:2 = fir.unboxchar %[[ARG0]] : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
+! CHECK: %[[VAL_1:.*]]:2 = hlfir.declare %[[VAL_0]]#0 typeparams %[[VAL_0]]#1 {uniq_name = "_QFcharconvert3Ec"} : (!fir.ref<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
+! CHECK: %[[VAL_2:.*]]:2 = fir.unboxchar %[[ARG1]] : (!fir.boxchar<4>) -> (!fir.ref<!fir.char<4,?>>, index)
+! CHECK: %[[VAL_3:.*]]:2 = hlfir.declare %[[VAL_2]]#0 typeparams %[[VAL_2]]#1 {uniq_name = "_QFcharconvert3Ec4"} : (!fir.ref<!fir.char<4,?>>, index) -> (!fir.boxchar<4>, !fir.ref<!fir.char<4,?>>)
+! CHECK: %[[VAL_4:.*]] = arith.addi %[[VAL_0]]#1, %[[VAL_0]]#1 : index
+! CHECK: %[[VAL_5:.*]] = hlfir.concat %[[VAL_1]]#0, %[[VAL_1]]#0 len %[[VAL_4]] : (!fir.boxchar<1>, !fir.boxchar<1>, index) -> !hlfir.expr<!fir.char<1,?>>
+! CHECK: %[[VAL_6:.*]] = fir.alloca !fir.char<4,?>(%[[VAL_4]] : index)
+! CHECK: %[[VAL_7:.*]]:3 = hlfir.associate %[[VAL_5]] typeparams %[[VAL_4]] {uniq_name = "adapt.valuebyref"} : (!hlfir.expr<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>, i1)
+! CHECK: fir.char_convert %[[VAL_7]]#1 for %[[VAL_4:.*]] to %[[VAL_6]] : !fir.ref<!fir.char<1,?>>, index, !fir.ref<!fir.char<4,?>>
+! CHECK: hlfir.end_associate %[[VAL_7]]#1, %[[VAL_7]]#2 : !fir.ref<!fir.char<1,?>>, i1
+! CHECK: %[[VAL_8:.*]]:2 = hlfir.declare %[[VAL_6]] typeparams %[[VAL_4]] {uniq_name = "ctor.temp"} : (!fir.ref<!fir.char<4,?>>, index) -> (!fir.boxchar<4>, !fir.ref<!fir.char<4,?>>)
+! CHECK: hlfir.assign %[[VAL_8]]#0 to %[[VAL_3]]#0 : !fir.boxchar<4>, !fir.boxchar<4>
\ No newline at end of file
More information about the flang-commits
mailing list