[flang-commits] [flang] d7697c1 - [flang] Avoid segfault when defining op is not a fir::Convert

Valentin Clement via flang-commits flang-commits at lists.llvm.org
Mon Jul 4 07:03:09 PDT 2022


Author: Valentin Clement
Date: 2022-07-04T16:03:02+02:00
New Revision: d7697c175db96601ad5726111e3ac9057af56756

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

LOG: [flang] Avoid segfault when defining op is not a fir::Convert

The previous code made the assumption that the defining
operation is a fir::ConvertOp without checking. This results in
segmentation fault in code like the added test.

Reviewed By: jeanPerier

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

Added: 
    flang/test/Lower/extent_triplets.f90

Modified: 
    flang/lib/Optimizer/Builder/FIRBuilder.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Optimizer/Builder/FIRBuilder.cpp b/flang/lib/Optimizer/Builder/FIRBuilder.cpp
index 5e0668e5b79e5..8d53694c8f8ea 100644
--- a/flang/lib/Optimizer/Builder/FIRBuilder.cpp
+++ b/flang/lib/Optimizer/Builder/FIRBuilder.cpp
@@ -1234,8 +1234,11 @@ fir::factory::getExtentFromTriplet(mlir::Value lb, mlir::Value ub,
       [&](mlir::Value value) -> llvm::Optional<std::int64_t> {
     if (auto valInt = fir::factory::getIntIfConstant(value))
       return valInt;
-    else if (auto valOp = mlir::dyn_cast<fir::ConvertOp>(value.getDefiningOp()))
+    auto *definingOp = value.getDefiningOp();
+    if (mlir::isa_and_nonnull<fir::ConvertOp>(definingOp)) {
+      auto valOp = mlir::dyn_cast<fir::ConvertOp>(definingOp);
       return getConstantValue(valOp.getValue());
+    }
     return {};
   };
   if (auto lbInt = getConstantValue(lb)) {

diff  --git a/flang/test/Lower/extent_triplets.f90 b/flang/test/Lower/extent_triplets.f90
new file mode 100644
index 0000000000000..b29d48e910e4e
--- /dev/null
+++ b/flang/test/Lower/extent_triplets.f90
@@ -0,0 +1,11 @@
+! RUN:  bbc -emit-fir %s -o - | FileCheck %s
+
+program test_extent_from_triplet
+  implicit none
+  integer, parameter:: n = 3
+  INTEGER a(n), b(n), i
+  a = (/ 1, 2, 3 /)
+  b = (/ (sum(a(1:i)), i=1, n) /)
+end program
+
+! CHECK: %{{.*}} = fir.embox %{{.*}}(%{{.*}}) [%{{.*}}] : (!fir.ref<!fir.array<3xi32>>, !fir.shape<1>, !fir.slice<1>) -> !fir.box<!fir.array<?xi32>>


        


More information about the flang-commits mailing list