[flang-commits] [PATCH] D152212: [flang] Fix crash in shape analysis of PACK()

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Mon Jun 5 17:00:51 PDT 2023


klausler created this revision.
klausler added a reviewer: PeteSteinfeld.
klausler added a project: Flang.
Herald added subscribers: sunshaoce, jdoerfert.
Herald added a project: All.
klausler requested review of this revision.

A CHECK() was firing when a call to the PACK intrinsic does not have a
VECTOR= argument and at least one dimension of the shape of the ARRAY=
argument could not be determined.  The CHECK was inappropriate, since
this can of course happen, such as when that argument is the result
of the SPREAD() intrinsic with non-constant DIM= or NCOPIES= arguments.
Replace with an if() statement.


https://reviews.llvm.org/D152212

Files:
  flang/lib/Evaluate/shape.cpp
  flang/test/Evaluate/errors01.f90


Index: flang/test/Evaluate/errors01.f90
===================================================================
--- flang/test/Evaluate/errors01.f90
+++ flang/test/Evaluate/errors01.f90
@@ -45,6 +45,7 @@
     integer :: x(3)
     !CHECK: error: Invalid 'vector=' argument in PACK: the 'mask=' argument has 3 true elements, but the vector has only 2 elements
     x = pack(array, mask, [0,0])
+    x = pack(spread(array, x(1), x(2)), .true.) ! regression check, once crashed
   end subroutine
   subroutine s5
     logical, parameter :: mask(2,3) = reshape([.false., .true., .true., .false., .false., .true.], shape(mask))
Index: flang/lib/Evaluate/shape.cpp
===================================================================
--- flang/lib/Evaluate/shape.cpp
+++ flang/lib/Evaluate/shape.cpp
@@ -902,18 +902,18 @@
           if (maskShape->size() == 0) {
             // Scalar MASK= -> [MERGE(SIZE(ARRAY=), 0, mask)]
             if (auto arrayShape{(*this)(call.arguments().at(0))}) {
-              auto arraySize{GetSize(std::move(*arrayShape))};
-              CHECK(arraySize);
-              ActualArguments toMerge{
-                  ActualArgument{AsGenericExpr(std::move(*arraySize))},
-                  ActualArgument{AsGenericExpr(ExtentExpr{0})},
-                  common::Clone(call.arguments().at(1))};
-              auto specific{context_->intrinsics().Probe(
-                  CallCharacteristics{"merge"}, toMerge, *context_)};
-              CHECK(specific);
-              return Shape{ExtentExpr{FunctionRef<ExtentType>{
-                  ProcedureDesignator{std::move(specific->specificIntrinsic)},
-                  std::move(specific->arguments)}}};
+              if (auto arraySize{GetSize(std::move(*arrayShape))}) {
+                ActualArguments toMerge{
+                    ActualArgument{AsGenericExpr(std::move(*arraySize))},
+                    ActualArgument{AsGenericExpr(ExtentExpr{0})},
+                    common::Clone(call.arguments().at(1))};
+                auto specific{context_->intrinsics().Probe(
+                    CallCharacteristics{"merge"}, toMerge, *context_)};
+                CHECK(specific);
+                return Shape{ExtentExpr{FunctionRef<ExtentType>{
+                    ProcedureDesignator{std::move(specific->specificIntrinsic)},
+                    std::move(specific->arguments)}}};
+              }
             }
           } else {
             // Non-scalar MASK= -> [COUNT(mask)]


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D152212.528635.patch
Type: text/x-patch
Size: 2466 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20230606/b7dfa2ce/attachment-0001.bin>


More information about the flang-commits mailing list