[flang] Minor fix in folding of reshape intrinsic (flang-compiler/f18#842)

The standard permits source to have more elements than the
result (as specified by the SHAPE arg). While copying, ensure
that we do not copy more than the number of elements of the
result.

Original-commit: flang-compiler/f18@6b8284f4f2
Reviewed-on: https://github.com/flang-compiler/f18/pull/842
This commit is contained in:
kiranchandramohan 2019-11-26 21:34:03 +00:00 committed by Steve Scalpone
parent 477bd3c67e
commit 201119217f
2 changed files with 4 additions and 2 deletions

View file

@ -386,8 +386,8 @@ Expr<T> Reshape(FoldingContext &context, FunctionRef<T> &&funcRef) {
? source->Reshape(std::move(shape.value()))
: pad->Reshape(std::move(shape.value()))};
ConstantSubscripts subscripts{result.lbounds()};
auto copied{
result.CopyFrom(*source, source->size(), subscripts, dimOrderPtr)};
auto copied{result.CopyFrom(*source,
std::min(source->size(), resultElements), subscripts, dimOrderPtr)};
if (copied < resultElements) {
CHECK(pad);
copied += result.CopyFrom(

View file

@ -31,8 +31,10 @@ module m
integer(4), parameter :: int_pad(2) = [7, 8]
integer(4), parameter :: int_expected_result(*, *) = reshape([1, 5, 2, 6, 3, 7, 4, 8], new_shape)
integer(4), parameter :: int_result(*, *) = reshape(int_source, new_shape, int_pad, order)
integer(4), parameter :: int_result_long_source(*, *) = reshape([1, 5, 2, 6, 3, 7, 4, 8, 9], new_shape)
logical, parameter :: test_reshape_integer_1 = all(int_expected_result == int_result)
logical, parameter :: test_reshape_integer_2 = all(shape(int_result, 8).EQ.new_shape)
logical, parameter :: test_reshape_integer_3 = all(int_expected_result == int_result_long_source)
! Testing characters