Skip to content

Commit 734ad03

Browse files
[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 <[email protected]>
1 parent 124338d commit 734ad03

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

flang/lib/Lower/ConvertExpr.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,10 +1301,20 @@ class ScalarExprLowering {
13011301
// to "0xE2 0x82 0xAC" : UTF-8.
13021302
mlir::Value bufferSize = boxchar.getLen();
13031303
auto kindMap = builder.getKindMap();
1304-
auto fromBits = kindMap.getCharacterBitsize(
1305-
fir::unwrapRefType(boxchar.getAddr().getType())
1306-
.cast<fir::CharacterType>()
1307-
.getFKind());
1304+
mlir::Value boxCharAddr = boxchar.getAddr();
1305+
auto fromTy = boxCharAddr.getType();
1306+
if (auto charTy = fromTy.dyn_cast<fir::CharacterType>()) {
1307+
// boxchar is a value, not a variable. Turn it into a temporary.
1308+
// As a value, it ought to have a constant LEN value.
1309+
assert(charTy.hasConstantLen() && "must have constant length");
1310+
mlir::Value tmp = builder.createTemporary(loc, charTy);
1311+
builder.create<fir::StoreOp>(loc, boxCharAddr, tmp);
1312+
boxCharAddr = tmp;
1313+
}
1314+
auto fromBits =
1315+
kindMap.getCharacterBitsize(fir::unwrapRefType(fromTy)
1316+
.cast<fir::CharacterType>()
1317+
.getFKind());
13081318
auto toBits = kindMap.getCharacterBitsize(
13091319
ty.cast<fir::CharacterType>().getFKind());
13101320
if (toBits < fromBits) {
@@ -1316,7 +1326,7 @@ class ScalarExprLowering {
13161326
}
13171327
auto dest = builder.create<fir::AllocaOp>(
13181328
loc, ty, mlir::ValueRange{bufferSize});
1319-
builder.create<fir::CharConvertOp>(loc, boxchar.getAddr(),
1329+
builder.create<fir::CharConvertOp>(loc, boxCharAddr,
13201330
boxchar.getLen(), dest);
13211331
return fir::CharBoxValue{dest, boxchar.getLen()};
13221332
} else {

flang/test/Fir/achar.f90

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
! RUN: bbc -emit-fir %s -o - | FileCheck %s
2+
3+
! Tests ACHAR lowering (converting an INTEGER to a CHARACTER (singleton, LEN=1)
4+
! along with conversion of CHARACTER to another KIND.
5+
subroutine achar_test1(a)
6+
integer, parameter :: ckind = 2
7+
integer, intent(in) :: a
8+
character(kind=ckind, len=1) :: ch
9+
10+
ch = achar(a)
11+
call achar_test1_foo(ch)
12+
end subroutine achar_test1
13+
14+
! CHECK-LABEL: func @_QPachar_test1(
15+
! CHECK-SAME: %[[arg:.*]]: !fir.ref<i32> {fir.bindc_name = "a"}) {
16+
! CHECK: %[[VAL_0:.*]] = fir.alloca !fir.char<1>
17+
! CHECK: %[[VAL_1:.*]] = fir.alloca !fir.char<2> {bindc_name = "ch", uniq_name = "_QFachar_test1Ech"}
18+
! CHECK: %[[VAL_2:.*]] = fir.load %[[arg]] : !fir.ref<i32>
19+
! CHECK: %[[VAL_5:.*]] = fir.undefined !fir.char<1>
20+
! CHECK: %[[VAL_6:.*]] = fir.insert_value %[[VAL_5]], %{{.*}}, [0 : index] : (!fir.char<1>, i8) -> !fir.char<1>
21+
! CHECK: fir.store %[[VAL_6]] to %[[VAL_0]] : !fir.ref<!fir.char<1>>
22+
! CHECK: %[[VAL_7:.*]] = fir.alloca !fir.char<2,?>(%{{.*}} : index)
23+
! CHECK: fir.char_convert %[[VAL_0]] for %{{.*}} to %[[VAL_7]] : !fir.ref<!fir.char<1>>, index, !fir.ref<!fir.char<2,?>>
24+
! CHECK: fir.call @_QPachar_test1_foo(%{{.*}}) : (!fir.boxchar<2>) -> ()

0 commit comments

Comments
 (0)