[flang] Fix bugs relating to support for characters of different kinds

Fix bugs relating to support for characters of different kinds. Lowering
was creating bad FIR and MLIR that crashed in conversion to LLVM IR.

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

Reviewed By: jeanPerier

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

Co-authored-by: Eric Schweitz <eschweitz@nvidia.com>
This commit is contained in:
Valentin Clement 2022-06-28 15:28:25 +02:00
parent 7faf75bb3e
commit ae35635f34
No known key found for this signature in database
GPG Key ID: 086D54783C928776
3 changed files with 48 additions and 8 deletions

View File

@ -3122,11 +3122,13 @@ mlir::LogicalResult fir::StoreOp::verify() {
// StringLitOp
//===----------------------------------------------------------------------===//
bool fir::StringLitOp::isWideValue() {
auto eleTy = getType().cast<fir::SequenceType>().getEleTy();
return eleTy.cast<fir::CharacterType>().getFKind() != 1;
inline fir::CharacterType::KindTy stringLitOpGetKind(fir::StringLitOp op) {
auto eleTy = op.getType().cast<fir::SequenceType>().getEleTy();
return eleTy.cast<fir::CharacterType>().getFKind();
}
bool fir::StringLitOp::isWideValue() { return stringLitOpGetKind(*this) != 1; }
static mlir::NamedAttribute
mkNamedIntegerAttr(mlir::OpBuilder &builder, llvm::StringRef name, int64_t v) {
assert(v > 0);
@ -3205,6 +3207,9 @@ mlir::ParseResult fir::StringLitOp::parse(mlir::OpAsmParser &parser,
if (auto v = val.dyn_cast<mlir::StringAttr>())
result.attributes.push_back(
builder.getNamedAttr(fir::StringLitOp::value(), v));
else if (auto v = val.dyn_cast<mlir::DenseElementsAttr>())
result.attributes.push_back(
builder.getNamedAttr(fir::StringLitOp::xlist(), v));
else if (auto v = val.dyn_cast<mlir::ArrayAttr>())
result.attributes.push_back(
builder.getNamedAttr(fir::StringLitOp::xlist(), v));
@ -3238,10 +3243,15 @@ mlir::LogicalResult fir::StringLitOp::verify() {
if (getSize().cast<mlir::IntegerAttr>().getValue().isNegative())
return emitOpError("size must be non-negative");
if (auto xl = getOperation()->getAttr(fir::StringLitOp::xlist())) {
auto xList = xl.cast<mlir::ArrayAttr>();
for (auto a : xList)
if (!a.isa<mlir::IntegerAttr>())
return emitOpError("values in list must be integers");
if (auto xList = xl.dyn_cast<mlir::ArrayAttr>()) {
for (auto a : xList)
if (!a.isa<mlir::IntegerAttr>())
return emitOpError("values in initializer must be integers");
} else if (xl.isa<mlir::DenseElementsAttr>()) {
// do nothing
} else {
return emitOpError("has unexpected attribute");
}
}
return mlir::success();
}

View File

@ -12,7 +12,7 @@
// -----
// expected-error@+1{{'fir.string_lit' op values in list must be integers}}
// expected-error@+1{{'fir.string_lit' op values in initializer must be integers}}
%2 = fir.string_lit [158, 2.0](2) : !fir.char<2>
// -----

View File

@ -0,0 +1,30 @@
! RUN: bbc %s -o - | tco | FileCheck %s
character(LEN=128, KIND=4), PARAMETER :: conarr(3) = &
[ character(128,4) :: "now is the time", "for all good men to come", &
"to the aid of the country" ]
character(LEN=10, KIND=4) :: arr(3) = &
[ character(10,4) :: "good buddy", "best buddy", " " ]
call action_on_char4(conarr)
call action_on_char4(arr)
end program
subroutine sub1
integer, parameter :: k = 4
character(63,k), parameter :: wiggle = k_"wiggle"
call sub2(wiggle)
end subroutine sub1
! CHECK-LABEL: @_QFEarr = internal global [3 x [10 x i32]] [
! CHECK-SAME: [10 x i32] [i32 103, i32 111, i32 111, i32 100, i32 32, i32 98, i32 117, i32 100, i32 100, i32 121],
! CHECK-SAME: [10 x i32] [i32 98, i32 101, i32 115, i32 116, i32 32, i32 98, i32 117, i32 100, i32 100, i32 121],
! CHECK-SAME: [10 x i32] [i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32, i32 32]]
! CHECK-LABEL: @_QFsub1ECwiggle = internal constant [63 x i32] [i32 119,
! CHECK-SAME: i32 105, i32 103, i32 103, i32 108, i32 101, i32 32, i32 32,
! CHECK: @_QQcl[[inline:.*]] = linkonce constant [63 x i32] [i32 119, i32 105, i32 103, i32 103, i32 108, i32 101, i32 32,
! CHECK-LABEL: define void @_QQmain()
! CHECK: call void @_QPaction_on_char4(ptr @_QFEarr, i64 10)
! CHECK-LABEL: define void @_QPsub1(
! CHECK: call void @_QPsub2(ptr @_QQcl.77, i64 63)