diff --git a/flang/lib/Optimizer/Builder/FIRBuilder.cpp b/flang/lib/Optimizer/Builder/FIRBuilder.cpp index 5e0668e5b79e..8d53694c8f8e 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 { if (auto valInt = fir::factory::getIntIfConstant(value)) return valInt; - else if (auto valOp = mlir::dyn_cast(value.getDefiningOp())) + auto *definingOp = value.getDefiningOp(); + if (mlir::isa_and_nonnull(definingOp)) { + auto valOp = mlir::dyn_cast(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 000000000000..b29d48e910e4 --- /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.shape<1>, !fir.slice<1>) -> !fir.box>