[flang-commits] [PATCH] D113659: [flang] Fix vector cshift runtime with non zero lower bounds

Jean Perier via Phabricator via flang-commits flang-commits at lists.llvm.org
Thu Nov 11 02:56:27 PST 2021


jeanPerier created this revision.
jeanPerier added a reviewer: klausler.
jeanPerier added a project: Flang.
Herald added a subscriber: jdoerfert.
jeanPerier requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D113659

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


Index: flang/unittests/Runtime/Transformational.cpp
===================================================================
--- flang/unittests/Runtime/Transformational.cpp
+++ flang/unittests/Runtime/Transformational.cpp
@@ -93,6 +93,27 @@
   }
   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;
+  Descriptor &vectorResult2{vectorDesc2.descriptor()};
+
+  RTNAME(CshiftVector)
+  (vectorResult2, *vectorWithLowerBounds, -2, __FILE__, __LINE__);
+  EXPECT_EQ(vectorResult2.type(), array->type());
+  EXPECT_EQ(vectorResult2.rank(), 1);
+  EXPECT_EQ(vectorResult2.GetDimension(0).LowerBound(), 1);
+  EXPECT_EQ(vectorResult2.GetDimension(0).Extent(), 6);
+  EXPECT_EQ(vectorResult2.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(*vectorResult2.ZeroBasedIndexedElement<std::int32_t>(j),
+        cshiftExpect5[j]);
+  }
+  vectorResult2.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
Index: flang/runtime/transformational.cpp
===================================================================
--- flang/runtime/transformational.cpp
+++ flang/runtime/transformational.cpp
@@ -184,7 +184,7 @@
   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);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D113659.386464.patch
Type: text/x-patch
Size: 1925 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20211111/45e32162/attachment-0001.bin>


More information about the flang-commits mailing list