[flang-commits] [flang] 7db4c58 - [flang] Fix crash in shape analysis of PACK()

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Mon Jun 5 19:32:51 PDT 2023


Author: Peter Klausler
Date: 2023-06-05T19:32:37-07:00
New Revision: 7db4c583db634ca4f16cd7aadb967dcb209bad5d

URL: https://github.com/llvm/llvm-project/commit/7db4c583db634ca4f16cd7aadb967dcb209bad5d
DIFF: https://github.com/llvm/llvm-project/commit/7db4c583db634ca4f16cd7aadb967dcb209bad5d.diff

LOG: [flang] Fix crash in shape analysis of PACK()

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.

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/flang/lib/Evaluate/shape.cpp b/flang/lib/Evaluate/shape.cpp
index c7dcb1c672979..9ddd7e585daeb 100644
--- a/flang/lib/Evaluate/shape.cpp
+++ b/flang/lib/Evaluate/shape.cpp
@@ -902,18 +902,18 @@ auto GetShapeHelper::operator()(const ProcedureRef &call) const -> Result {
           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)]

diff  --git a/flang/test/Evaluate/errors01.f90 b/flang/test/Evaluate/errors01.f90
index 0b86ccb199ccd..1ae6c62a4e3ee 100644
--- a/flang/test/Evaluate/errors01.f90
+++ b/flang/test/Evaluate/errors01.f90
@@ -45,6 +45,7 @@ subroutine s4
     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))


        


More information about the flang-commits mailing list