[PATCH] D84352: [flang] Fix an assert when RESHAPE() is called on empty strings

Pete Steinfeld via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 22 12:07:27 PDT 2020


PeteSteinfeld updated this revision to Diff 279907.
PeteSteinfeld added a comment.

Removed an unnecessary `if` condition.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D84352/new/

https://reviews.llvm.org/D84352

Files:
  flang/lib/Evaluate/constant.cpp
  flang/test/Semantics/modfile25.f90


Index: flang/test/Semantics/modfile25.f90
===================================================================
--- flang/test/Semantics/modfile25.f90
+++ flang/test/Semantics/modfile25.f90
@@ -22,6 +22,9 @@
     real, intent(in) :: x(:,:)
     integer, intent(in) :: n1(3), n2(:)
     real, allocatable :: a(:,:,:)
+    ! the following fail if we don't handle empty strings
+    Character(0) :: ch1(1,2,3) = Reshape([('',n=1,1*2*3)],[1,2,3])
+    Character(0) :: ch2(3) = reshape(['','',''], [3])
     a = reshape(x,n1)
     a = reshape(x,n2(10:30:9)) ! fails if we can't figure out triplet shape
   end subroutine
Index: flang/lib/Evaluate/constant.cpp
===================================================================
--- flang/lib/Evaluate/constant.cpp
+++ flang/lib/Evaluate/constant.cpp
@@ -244,19 +244,26 @@
     std::size_t count, ConstantSubscripts &resultSubscripts,
     const std::vector<int> *dimOrder) {
   CHECK(length_ == source.length_);
-  std::size_t copied{0};
-  std::size_t elementBytes{length_ * sizeof(decltype(values_[0]))};
-  ConstantSubscripts sourceSubscripts{source.lbounds()};
-  while (copied < count) {
-    auto *dest{&values_.at(SubscriptsToOffset(resultSubscripts) * length_)};
-    const auto *src{&source.values_.at(
-        source.SubscriptsToOffset(sourceSubscripts) * length_)};
-    std::memcpy(dest, src, elementBytes);
-    copied++;
-    source.IncrementSubscripts(sourceSubscripts);
-    IncrementSubscripts(resultSubscripts, dimOrder);
+  if (length_ == 0) {
+    // It's possible that the array of strings consists of all empty strings.
+    // If so, constant folding will result in a string that's completely empty
+    // and the length_ will be zero, and there's nothing to do.
+    return count;
+  } else {
+    std::size_t copied{0};
+    std::size_t elementBytes{length_ * sizeof(decltype(values_[0]))};
+    ConstantSubscripts sourceSubscripts{source.lbounds()};
+    while (copied < count) {
+      auto *dest{&values_.at(SubscriptsToOffset(resultSubscripts) * length_)};
+      const auto *src{&source.values_.at(
+          source.SubscriptsToOffset(sourceSubscripts) * length_)};
+      std::memcpy(dest, src, elementBytes);
+      copied++;
+      source.IncrementSubscripts(sourceSubscripts);
+      IncrementSubscripts(resultSubscripts, dimOrder);
+    }
+    return copied;
   }
-  return copied;
 }
 
 // Constant<SomeDerived> specialization


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D84352.279907.patch
Type: text/x-patch
Size: 2409 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200722/aee529d3/attachment.bin>


More information about the llvm-commits mailing list