Skip to content

Commit

Permalink
[flang][hlfir] fix regression in inline elementals
Browse files Browse the repository at this point in the history
InlineElementals created a regression when inlining elemental
expressions where the type of the result of the hlfir.apply does not
match the hlfir.yield.

This patch ensures the pass doesn't match in these cases, fixing the
regression.

It isn't clear to me what the /right/ solution is:
 - Is it actually valid for the hlfir.apply to have a different type
   (even just different array bounds?). Should this be enforced in the
   verifier?
 - Inserting a convert if these types don't match doesn't work because
   fir.convert doesn't know how to convert a hlfir.expr. Should this be
   added?

Test case is from @vzakhari

Differential Revision: https://reviews.llvm.org/D151202
  • Loading branch information
tblah committed May 25, 2023
1 parent 0123deb commit 5b65dbd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
9 changes: 9 additions & 0 deletions flang/lib/Optimizer/HLFIR/Transforms/InlineElementals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ getTwoUses(hlfir::ElementalOp elemental) {

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<hlfir::YieldElementOp>(
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};
}

Expand Down
15 changes: 15 additions & 0 deletions flang/test/HLFIR/inline-elemental.f90
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5b65dbd

Please sign in to comment.