[flang-commits] [flang] 7267860 - [flang]Avoid asking for operands when there are none

Mats Petersson via flang-commits flang-commits at lists.llvm.org
Wed Aug 17 04:32:56 PDT 2022


Author: Mats Petersson
Date: 2022-08-17T12:31:42+01:00
New Revision: 726786083f1bdf8c073ea6c75b321d26719981ab

URL: https://github.com/llvm/llvm-project/commit/726786083f1bdf8c073ea6c75b321d26719981ab
DIFF: https://github.com/llvm/llvm-project/commit/726786083f1bdf8c073ea6c75b321d26719981ab.diff

LOG: [flang]Avoid asking for operands when there are none

Fix one encountered (issue #57072) and two potential scenarios where the
code would ask for an operand that isn't there.

Add test for the encountered case.

Reviewed By: vzakhari

Differential Revision: https://reviews.llvm.org/D131671

Added: 
    

Modified: 
    flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp
    flang/test/Transforms/simplifyintrinsics.fir

Removed: 
    


################################################################################
diff  --git a/flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp b/flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp
index e0bc108634d68..223140d01415e 100644
--- a/flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp
+++ b/flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp
@@ -302,25 +302,37 @@ mlir::func::FuncOp SimplifyIntrinsicsPass::getOrCreateFunction(
   return newFunc;
 }
 
+fir::ConvertOp expectConvertOp(mlir::Value val) {
+  if (fir::ConvertOp op =
+          mlir::dyn_cast_or_null<fir::ConvertOp>(val.getDefiningOp()))
+    return op;
+  LLVM_DEBUG(llvm::dbgs() << "Didn't find expected fir::ConvertOp\n");
+  return nullptr;
+}
+
 static bool isOperandAbsent(mlir::Value val) {
-  if (mlir::Operation *op = val.getDefiningOp())
+  if (auto op = expectConvertOp(val)) {
+    assert(op->getOperands().size() != 0);
     return mlir::isa_and_nonnull<fir::AbsentOp>(
         op->getOperand(0).getDefiningOp());
+  }
   return false;
 }
 
 static bool isZero(mlir::Value val) {
-  if (mlir::Operation *op = val.getDefiningOp())
+  if (auto op = expectConvertOp(val)) {
+    assert(op->getOperands().size() != 0);
     if (mlir::Operation *defOp = op->getOperand(0).getDefiningOp())
       return mlir::matchPattern(defOp, mlir::m_Zero());
+  }
   return false;
 }
 
 static mlir::Value findShape(mlir::Value val) {
-  mlir::Operation *defOp = val.getDefiningOp();
-  while (defOp) {
-    defOp = defOp->getOperand(0).getDefiningOp();
-    if (fir::EmboxOp box = mlir::dyn_cast_or_null<fir::EmboxOp>(defOp))
+  if (auto op = expectConvertOp(val)) {
+    assert(op->getOperands().size() != 0);
+    if (auto box =
+            mlir::dyn_cast<fir::EmboxOp>(op->getOperand(0).getDefiningOp()))
       return box.getShape();
   }
   return {};

diff  --git a/flang/test/Transforms/simplifyintrinsics.fir b/flang/test/Transforms/simplifyintrinsics.fir
index c2315fca02d10..3580da70d1ef0 100644
--- a/flang/test/Transforms/simplifyintrinsics.fir
+++ b/flang/test/Transforms/simplifyintrinsics.fir
@@ -320,6 +320,35 @@ module attributes {fir.defaultkind = "a1c4d8i4l4r4", fir.kindmap = "", llvm.targ
 
 // -----
 
+// Check that the compiler accepts unknown size arrays.
+module attributes {fir.defaultkind = "a1c4d8i4l4r4", fir.kindmap = "", llvm.target_triple = "native"} {
+  func.func @sum_dim() {
+    %arr = fir.alloca !fir.box<!fir.heap<!fir.array<?xi32>>>
+    %var = fir.alloca !fir.array<8x8xi32>  
+    %size = arith.constant 8 : index
+    %c1 = arith.constant 1 : index
+    %c1_i32 = arith.constant 1 : i32
+    %lineno = arith.constant 12 : i32
+    %shape = fir.shape %size, %size : (index, index) -> !fir.shape<2>
+    %slice = fir.slice %c1, %size, %c1, %c1, %size, %c1 : (index, index, index, index, index, index) -> !fir.slice<2>
+    %box_array = fir.embox %var(%shape) [%slice] : (!fir.ref<!fir.array<8x8xi32>>, !fir.shape<2>, !fir.slice<2>) -> !fir.box<!fir.array<?x?xi32>>
+    %box_none = fir.convert %arr : (!fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>) -> !fir.ref<!fir.box<none>>
+    %box_none2 = fir.convert %box_array : (!fir.box<!fir.array<?x?xi32>>) -> !fir.box<none>
+    %absent = fir.absent !fir.box<i1>
+    %file = fir.address_of(@filename) : !fir.ref<!fir.char<1,16>>
+    %file_ref = fir.convert %file : (!fir.ref<!fir.char<1,16>>) -> !fir.ref<i8>
+    %absent_none = fir.convert %absent : (!fir.box<i1>) -> !fir.box<none>
+    %res = fir.call @_FortranASumDim(%box_none, %box_none2, %c1_i32, %file_ref, %lineno, %absent_none) : (!fir.ref<!fir.box<none>>, !fir.box<none>, i32, !fir.ref<i8>, i32, !fir.box<none>) -> none
+    func.return
+  }
+}
+
+// Just check that SOMETHING is being output.
+// CHECK-LABEL @sum_dim() {
+// CHECK: return
+
+// -----
+
 func.func @dot_f32(%arg0: !fir.box<!fir.array<?xf32>> {fir.bindc_name = "a"}, %arg1: !fir.box<!fir.array<?xf32>> {fir.bindc_name = "b"}) -> f32 {
   %0 = fir.alloca f32 {bindc_name = "dot", uniq_name = "_QFdotEdot"}
   %1 = fir.address_of(@_QQcl.2E2F646F742E66393000) : !fir.ref<!fir.char<1,10>>
@@ -332,12 +361,6 @@ func.func @dot_f32(%arg0: !fir.box<!fir.array<?xf32>> {fir.bindc_name = "a"}, %a
   %6 = fir.load %0 : !fir.ref<f32>
   return %6 : f32
 }
-func.func private @_FortranADotProductReal4(!fir.box<none>, !fir.box<none>, !fir.ref<i8>, i32) -> f32 attributes {fir.runtime}
-fir.global linkonce @_QQcl.2E2F646F742E66393000 constant : !fir.char<1,10> {
-  %0 = fir.string_lit "./dot.f90\00"(10) : !fir.char<1,10>
-  fir.has_value %0 : !fir.char<1,10>
-}
-
 // CHECK-LABEL:   func.func @dot_f32(
 // CHECK-SAME:                      %[[A:.*]]: !fir.box<!fir.array<?xf32>> {fir.bindc_name = "a"},
 // CHECK-SAME:                      %[[B:.*]]: !fir.box<!fir.array<?xf32>> {fir.bindc_name = "b"}) -> f32 {


        


More information about the flang-commits mailing list