diff --git a/flang/lib/Optimizer/HLFIR/Transforms/InlineElementals.cpp b/flang/lib/Optimizer/HLFIR/Transforms/InlineElementals.cpp --- a/flang/lib/Optimizer/HLFIR/Transforms/InlineElementals.cpp +++ b/flang/lib/Optimizer/HLFIR/Transforms/InlineElementals.cpp @@ -51,6 +51,15 @@ if (!apply || !destroy) return std::nullopt; + + // we can't inline if the return type of the yield doesn't match the return + // type of the apply + auto yield = mlir::dyn_cast_or_null( + elemental.getRegion().back().back()); + assert(yield && "hlfir.elemental should always end with a yield"); + if (apply.getResult().getType() != yield.getElementValue().getType()) + return std::nullopt; + return std::pair{apply, destroy}; } diff --git a/flang/test/HLFIR/inline-elemental.f90 b/flang/test/HLFIR/inline-elemental.f90 new file mode 100644 --- /dev/null +++ b/flang/test/HLFIR/inline-elemental.f90 @@ -0,0 +1,15 @@ +! RUN: %flang_fc1 -emit-obj -flang-experimental-hlfir -o /dev/null %s + +! Regression test: ensure we can compile this without crashing +! this results in a hlfir.elemental with mismatched types in the hlfir.apply +! and hlfir.yield +subroutine test + interface + function func(i,j,k) + character(5),allocatable :: func(:,:,:) + end function func + end interface + character(13),allocatable :: a(:,:,:) + print *, (func(2,5,3)//reshape([(char(ichar('a')+n),n=1,2*5*3)], & + & [2,5,3])) +end subroutine test