[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@nvidia.com>
This commit is contained in:
Valentin Clement 2022-06-23 18:04:50 +02:00
parent 124338dd80
commit 734ad031f1
No known key found for this signature in database
GPG Key ID: 086D54783C928776
2 changed files with 39 additions and 5 deletions

View File

@ -1301,10 +1301,20 @@ public:
// 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 @@ public:
}
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 {

24
flang/test/Fir/achar.f90 Normal file
View File

@ -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>) -> ()