[flang] Correct folding of TRANSFER(integer, character array)

The code that copies data from a constant source array into a character
array constant result was failing to copy its last element if it was
only partially defined due to misalignment.

Differential Revision: https://reviews.llvm.org/D130376
This commit is contained in:
Peter Klausler 2022-07-15 12:15:07 -07:00
parent 80a4e6fd31
commit 60b1fcb1a5
2 changed files with 25 additions and 7 deletions

View File

@ -135,12 +135,22 @@ public:
for (std::size_t j{0}; j < elements; ++j) { for (std::size_t j{0}; j < elements; ++j) {
using Char = typename Scalar::value_type; using Char = typename Scalar::value_type;
auto at{static_cast<std::size_t>(offset_ + j * stride)}; auto at{static_cast<std::size_t>(offset_ + j * stride)};
if (at + length > image_.data_.size()) { auto chunk{length};
if (at + chunk > image_.data_.size()) {
CHECK(padWithZero_); CHECK(padWithZero_);
break; if (at >= image_.data_.size()) {
chunk = 0;
} else {
chunk = image_.data_.size() - at;
}
}
if (chunk > 0) {
const Char *data{reinterpret_cast<const Char *>(&image_.data_[at])};
typedValue[j].assign(data, chunk);
}
if (chunk < length && padWithZero_) {
typedValue[j].append(length - chunk, Char{});
} }
const Char *data{reinterpret_cast<const Char *>(&image_.data_[at])};
typedValue[j].assign(data, length);
} }
return AsGenericExpr( return AsGenericExpr(
Const{length, std::move(typedValue), std::move(extents_)}); Const{length, std::move(typedValue), std::move(extents_)});
@ -153,12 +163,15 @@ public:
if (at + chunk > image_.data_.size()) { if (at + chunk > image_.data_.size()) {
CHECK(padWithZero_); CHECK(padWithZero_);
if (at >= image_.data_.size()) { if (at >= image_.data_.size()) {
break; chunk = 0;
} else {
chunk = image_.data_.size() - at;
} }
chunk = image_.data_.size() - at;
} }
// TODO endianness // TODO endianness
std::memcpy(&typedValue[j], &image_.data_[at], chunk); if (chunk > 0) {
std::memcpy(&typedValue[j], &image_.data_[at], chunk);
}
} }
return AsGenericExpr(Const{std::move(typedValue), std::move(extents_)}); return AsGenericExpr(Const{std::move(typedValue), std::move(extents_)});
} }

View File

@ -34,4 +34,9 @@ module m
logical, parameter :: test_c2i_v_2 = all(jc3 == [int(z'61626364'), int(z'65666768')]) .or. all(jc3 == [int(z'64636261'), int(z'68676665')]) logical, parameter :: test_c2i_v_2 = all(jc3 == [int(z'61626364'), int(z'65666768')]) .or. all(jc3 == [int(z'64636261'), int(z'68676665')])
integer, parameter :: jc4(*) = transfer(["abcd", "efgh"], 0, 1) integer, parameter :: jc4(*) = transfer(["abcd", "efgh"], 0, 1)
logical, parameter :: test_c2i_vs_1 = all(jc4 == [int(z'61626364')]) .or. all(jc4 == [int(z'64636261')]) logical, parameter :: test_c2i_vs_1 = all(jc4 == [int(z'61626364')]) .or. all(jc4 == [int(z'64636261')])
integer, parameter :: le1 = int(z'64636261', 4), be1 = int(z'65666768', 4)
character*5, parameter :: le1c(*) = transfer(le1, [character(5)::])
character*5, parameter :: be1c(*) = transfer(be1, [character(5)::])
logical, parameter :: test_i2c_s = all(le1c == ["abcd"//char(0)]) .or. all(be1c == ["efgh"//char(0)])
end module end module