[flang-commits] [flang] 85ec449 - [flang] Fix ORDER= argument to RESHAPE

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Fri Nov 12 10:25:06 PST 2021


Author: Peter Klausler
Date: 2021-11-12T10:25:00-08:00
New Revision: 85ec449352fb809e42e3e14dd10152cbdf8b5945

URL: https://github.com/llvm/llvm-project/commit/85ec449352fb809e42e3e14dd10152cbdf8b5945
DIFF: https://github.com/llvm/llvm-project/commit/85ec449352fb809e42e3e14dd10152cbdf8b5945.diff

LOG: [flang] Fix ORDER= argument to RESHAPE

The ORDER= argument to the transformational intrinsic function RESHAPE
was being misinterpreted in an inverted way that could be detected only
with 3-d or higher rank array.  Fix in both folding and the runtime, and
extend tests.

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

Added: 
    

Modified: 
    flang/include/flang/Evaluate/constant.h
    flang/lib/Evaluate/constant.cpp
    flang/runtime/transformational.cpp
    flang/test/Evaluate/folding06.f90

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Evaluate/constant.h b/flang/include/flang/Evaluate/constant.h
index 89b98f389b629..34453d5a2370f 100644
--- a/flang/include/flang/Evaluate/constant.h
+++ b/flang/include/flang/Evaluate/constant.h
@@ -50,7 +50,7 @@ std::size_t TotalElementCount(const ConstantSubscripts &);
 
 // Validate dimension re-ordering like ORDER in RESHAPE.
 // On success, return a vector that can be used as dimOrder in
-// ConstantBound::IncrementSubscripts().
+// ConstantBounds::IncrementSubscripts().
 std::optional<std::vector<int>> ValidateDimensionOrder(
     int rank, const std::vector<int> &order);
 

diff  --git a/flang/lib/Evaluate/constant.cpp b/flang/lib/Evaluate/constant.cpp
index 01f5657360eb4..efc02316d92dc 100644
--- a/flang/lib/Evaluate/constant.cpp
+++ b/flang/lib/Evaluate/constant.cpp
@@ -85,7 +85,7 @@ std::optional<std::vector<int>> ValidateDimensionOrder(
       if (dim < 1 || dim > rank || seenDimensions.test(dim - 1)) {
         return std::nullopt;
       }
-      dimOrder[dim - 1] = j;
+      dimOrder[j] = dim - 1;
       seenDimensions.set(dim - 1);
     }
     return dimOrder;

diff  --git a/flang/runtime/transformational.cpp b/flang/runtime/transformational.cpp
index 4f5291b585f29..42381ee097dcc 100644
--- a/flang/runtime/transformational.cpp
+++ b/flang/runtime/transformational.cpp
@@ -406,7 +406,7 @@ void RTNAME(Reshape)(Descriptor &result, const Descriptor &source,
       RUNTIME_CHECK(
           terminator, k >= 1 && k <= resultRank && !((values >> k) & 1));
       values |= std::uint64_t{1} << k;
-      dimOrder[k - 1] = j;
+      dimOrder[j] = k - 1;
     }
   } else {
     for (int j{0}; j < resultRank; ++j) {

diff  --git a/flang/test/Evaluate/folding06.f90 b/flang/test/Evaluate/folding06.f90
index 4393b3b02f5c5..4dccedd931e50 100644
--- a/flang/test/Evaluate/folding06.f90
+++ b/flang/test/Evaluate/folding06.f90
@@ -63,4 +63,15 @@ module m
       .AND.(derived_result%i.EQ.derived_expected_result%i))
 
   logical, parameter :: test_reshape_derived_2 = all(shape(derived_result).EQ.new_shape)
+
+  ! More complicated ORDER= arguments
+  integer, parameter :: int3d(2,3,4) = reshape([(j,j=1,24)],shape(int3d))
+  logical, parameter :: test_int3d = all([int3d] == [(j,j=1,24)])
+  logical, parameter :: test_reshape_order_1 = all([reshape(int3d, [2,3,4], order=[1,2,3])] == [(j,j=1,24)])
+  logical, parameter :: test_reshape_order_2 = all([reshape(int3d, [2,4,3], order=[1,3,2])] == [1,2,7,8,13,14,19,20,3,4,9,10,15,16,21,22,5,6,11,12,17,18,23,24])
+  logical, parameter :: test_reshape_order_3 = all([reshape(int3d, [3,2,4], order=[2,1,3])] == [1,3,5,2,4,6,7,9,11,8,10,12,13,15,17,14,16,18,19,21,23,20,22,24])
+  logical, parameter :: test_reshape_order_4 = all([reshape(int3d, [3,4,2], order=[2,3,1])] == [1,9,17,2,10,18,3,11,19,4,12,20,5,13,21,6,14,22,7,15,23,8,16,24])
+  logical, parameter :: test_reshape_order_5 = all([reshape(int3d, [4,2,3], order=[3,1,2])] == [1,4,7,10,13,16,19,22,2,5,8,11,14,17,20,23,3,6,9,12,15,18,21,24])
+  logical, parameter :: test_reshape_order_6 = all([reshape(int3d, [4,3,2], order=[3,2,1])] == [1,7,13,19,3,9,15,21,5,11,17,23,2,8,14,20,4,10,16,22,6,12,18,24])
+
 end module


        


More information about the flang-commits mailing list