[flang-commits] [flang] 5b65dbd - [flang][hlfir] fix regression in inline elementals

Tom Eccles via flang-commits flang-commits at lists.llvm.org
Thu May 25 04:03:08 PDT 2023


Author: Tom Eccles
Date: 2023-05-25T11:02:42Z
New Revision: 5b65dbd19756fc6522167d4c884d138907026254

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

LOG: [flang][hlfir] fix regression in inline elementals

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

Added: 
    flang/test/HLFIR/inline-elemental.f90

Modified: 
    flang/lib/Optimizer/HLFIR/Transforms/InlineElementals.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Optimizer/HLFIR/Transforms/InlineElementals.cpp b/flang/lib/Optimizer/HLFIR/Transforms/InlineElementals.cpp
index f0acd2210268..136921b944e2 100644
--- a/flang/lib/Optimizer/HLFIR/Transforms/InlineElementals.cpp
+++ b/flang/lib/Optimizer/HLFIR/Transforms/InlineElementals.cpp
@@ -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};
 }
 

diff  --git a/flang/test/HLFIR/inline-elemental.f90 b/flang/test/HLFIR/inline-elemental.f90
new file mode 100644
index 000000000000..4e91cc70ede2
--- /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


        


More information about the flang-commits mailing list