[flang-commits] [flang] 6544d9a - [flang] Fix vector cshift runtime with non zero lower bounds

Jean Perier via flang-commits flang-commits at lists.llvm.org
Fri Nov 12 00:44:49 PST 2021


Author: Jean Perier
Date: 2021-11-12T09:26:08+01:00
New Revision: 6544d9a4a098c24ebafe2856259f927af8f211ec

URL: https://github.com/llvm/llvm-project/commit/6544d9a4a098c24ebafe2856259f927af8f211ec
DIFF: https://github.com/llvm/llvm-project/commit/6544d9a4a098c24ebafe2856259f927af8f211ec.diff

LOG: [flang] Fix vector cshift runtime with non zero lower bounds

The source index should not be compared to zero after applying the
shift with the modulo, it must be compared to the lower bound.
Otherwise, the extent is not added in case it should and the computed
source index may be less than the lower bound, causing invalid results.

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

Added: 
    

Modified: 
    flang/runtime/transformational.cpp
    flang/unittests/Runtime/Transformational.cpp

Removed: 
    


################################################################################
diff  --git a/flang/runtime/transformational.cpp b/flang/runtime/transformational.cpp
index 20d6100fab047..4f5291b585f29 100644
--- a/flang/runtime/transformational.cpp
+++ b/flang/runtime/transformational.cpp
@@ -184,7 +184,7 @@ void RTNAME(CshiftVector)(Descriptor &result, const Descriptor &source,
   for (SubscriptValue j{0}; j < extent; ++j) {
     SubscriptValue resultAt{1 + j};
     SubscriptValue sourceAt{lb + (j + shift) % extent};
-    if (sourceAt < 0) {
+    if (sourceAt < lb) {
       sourceAt += extent;
     }
     CopyElement(result, &resultAt, source, &sourceAt, terminator);

diff  --git a/flang/unittests/Runtime/Transformational.cpp b/flang/unittests/Runtime/Transformational.cpp
index ac4d9dd008c9c..2656a4124e2f7 100644
--- a/flang/unittests/Runtime/Transformational.cpp
+++ b/flang/unittests/Runtime/Transformational.cpp
@@ -93,6 +93,26 @@ TEST(Transformational, Shifts) {
   }
   vectorResult.Destroy();
 
+  // VECTOR  1 3 5 2 4 6 WITH non zero lower bound in a negative cshift.
+  auto vectorWithLowerBounds{MakeArray<TypeCategory::Integer, 4>(
+      std::vector<int>{6}, std::vector<std::int32_t>{1, 2, 3, 4, 5, 6})};
+  vector->GetDimension(0).SetLowerBound(2);
+  StaticDescriptor<1, true> vectorDesc2;
+
+  RTNAME(CshiftVector)
+  (vectorResult, *vectorWithLowerBounds, -2, __FILE__, __LINE__);
+  EXPECT_EQ(vectorResult.type(), array->type());
+  EXPECT_EQ(vectorResult.rank(), 1);
+  EXPECT_EQ(vectorResult.GetDimension(0).LowerBound(), 1);
+  EXPECT_EQ(vectorResult.GetDimension(0).Extent(), 6);
+  EXPECT_EQ(vectorResult.type(), (TypeCode{TypeCategory::Integer, 4}));
+  static std::int32_t cshiftExpect5[6]{5, 6, 1, 2, 3, 4};
+  for (int j{0}; j < 6; ++j) {
+    EXPECT_EQ(*vectorResult.ZeroBasedIndexedElement<std::int32_t>(j),
+        cshiftExpect5[j]);
+  }
+  vectorResult.Destroy();
+
   auto boundary{MakeArray<TypeCategory::Integer, 4>(
       std::vector<int>{3}, std::vector<std::int32_t>{-1, -2, -3})};
   boundary->GetDimension(0).SetLowerBound(9); // shouldn't matter
@@ -130,6 +150,22 @@ TEST(Transformational, Shifts) {
         eoshiftVectorExpect[j]);
   }
   vectorResult.Destroy();
+
+  // VECTOR EOSHIFT on input with non zero lower bounds
+  RTNAME(EoshiftVector)
+  (vectorResult, *vectorWithLowerBounds, -2, &vectorBoundary, __FILE__,
+      __LINE__);
+  EXPECT_EQ(vectorResult.type(), array->type());
+  EXPECT_EQ(vectorResult.rank(), 1);
+  EXPECT_EQ(vectorResult.GetDimension(0).LowerBound(), 1);
+  EXPECT_EQ(vectorResult.GetDimension(0).Extent(), 6);
+  EXPECT_EQ(vectorResult.type(), (TypeCode{TypeCategory::Integer, 4}));
+  static std::int32_t eoshiftVectorExpect2[6]{343, 343, 1, 2, 3, 4};
+  for (int j{0}; j < 6; ++j) {
+    EXPECT_EQ(*vectorResult.ZeroBasedIndexedElement<std::int32_t>(j),
+        eoshiftVectorExpect2[j]);
+  }
+  vectorResult.Destroy();
 }
 
 TEST(Transformational, Pack) {


        


More information about the flang-commits mailing list