[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
This commit is contained in:
Valentin Clement 2022-07-04 16:02:42 +02:00
parent 4e6c30c835
commit d7697c175d
No known key found for this signature in database
GPG Key ID: 086D54783C928776
2 changed files with 15 additions and 1 deletions

View File

@ -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)) {

View File

@ -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>>