From 726786083f1bdf8c073ea6c75b321d26719981ab Mon Sep 17 00:00:00 2001 From: Mats Petersson Date: Tue, 9 Aug 2022 16:51:00 +0100 Subject: [PATCH] [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 --- .../Optimizer/Transforms/SimplifyIntrinsics.cpp | 24 +++++++++++---- flang/test/Transforms/simplifyintrinsics.fir | 35 ++++++++++++++++++---- 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp b/flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp index e0bc108..223140d 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(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( 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(defOp)) + if (auto op = expectConvertOp(val)) { + assert(op->getOperands().size() != 0); + if (auto box = + mlir::dyn_cast(op->getOperand(0).getDefiningOp())) return box.getShape(); } return {}; diff --git a/flang/test/Transforms/simplifyintrinsics.fir b/flang/test/Transforms/simplifyintrinsics.fir index c2315fc..3580da7 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>> + %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.shape<2>, !fir.slice<2>) -> !fir.box> + %box_none = fir.convert %arr : (!fir.ref>>>) -> !fir.ref> + %box_none2 = fir.convert %box_array : (!fir.box>) -> !fir.box + %absent = fir.absent !fir.box + %file = fir.address_of(@filename) : !fir.ref> + %file_ref = fir.convert %file : (!fir.ref>) -> !fir.ref + %absent_none = fir.convert %absent : (!fir.box) -> !fir.box + %res = fir.call @_FortranASumDim(%box_none, %box_none2, %c1_i32, %file_ref, %lineno, %absent_none) : (!fir.ref>, !fir.box, i32, !fir.ref, i32, !fir.box) -> none + func.return + } +} + +// Just check that SOMETHING is being output. +// CHECK-LABEL @sum_dim() { +// CHECK: return + +// ----- + func.func @dot_f32(%arg0: !fir.box> {fir.bindc_name = "a"}, %arg1: !fir.box> {fir.bindc_name = "b"}) -> f32 { %0 = fir.alloca f32 {bindc_name = "dot", uniq_name = "_QFdotEdot"} %1 = fir.address_of(@_QQcl.2E2F646F742E66393000) : !fir.ref> @@ -332,12 +361,6 @@ func.func @dot_f32(%arg0: !fir.box> {fir.bindc_name = "a"}, %a %6 = fir.load %0 : !fir.ref return %6 : f32 } -func.func private @_FortranADotProductReal4(!fir.box, !fir.box, !fir.ref, 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.bindc_name = "a"}, // CHECK-SAME: %[[B:.*]]: !fir.box> {fir.bindc_name = "b"}) -> f32 { -- 2.7.4