Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flang] SimplifyIntrinsics pass asserts in findShape #57072

Closed
vzakhari opened this issue Aug 11, 2022 · 4 comments
Closed

[flang] SimplifyIntrinsics pass asserts in findShape #57072

vzakhari opened this issue Aug 11, 2022 · 4 comments
Assignees
Labels

Comments

@vzakhari
Copy link
Contributor

vzakhari commented Aug 11, 2022

FIR produced from this Fortran code:

subroutine foo(a, v)
  real, allocatable :: a(:, :)
  REAL :: v(:)
  v = SUM(a,DIM=1)
end subroutine foo

FIR to reproduce the issue:

module attributes {fir.defaultkind = "a1c4d8i4l4r4", fir.kindmap = "", llvm.target_triple = "x86_64-unknown-linux-gnu"} {
  func.func @_QPfoo(%arg0: !fir.ref<!fir.box<!fir.heap<!fir.array<?x?xf32>>>> {fir.bindc_name = "a"}, %arg1: !fir.box<!fir.array<?xf32>> {fir.bindc_name = "v"}) {
    %0 = fir.alloca !fir.box<!fir.heap<!fir.array<?xf32>>>
    %c0 = arith.constant 0 : index
    %1:3 = fir.box_dims %arg1, %c0 : (!fir.box<!fir.array<?xf32>>, index) -> (index, index, index)
    %2 = fir.array_load %arg1 : (!fir.box<!fir.array<?xf32>>) -> !fir.array<?xf32>
    %3 = fir.load %arg0 : !fir.ref<!fir.box<!fir.heap<!fir.array<?x?xf32>>>>
    %c0_0 = arith.constant 0 : index
    %4:3 = fir.box_dims %3, %c0_0 : (!fir.box<!fir.heap<!fir.array<?x?xf32>>>, index) -> (index, index, index)
    %c1 = arith.constant 1 : index
    %5:3 = fir.box_dims %3, %c1 : (!fir.box<!fir.heap<!fir.array<?x?xf32>>>, index) -> (index, index, index)
    %6 = fir.box_addr %3 : (!fir.box<!fir.heap<!fir.array<?x?xf32>>>) -> !fir.heap<!fir.array<?x?xf32>>
    %7 = fir.shape_shift %4#0, %4#1, %5#0, %5#1 : (index, index, index, index) -> !fir.shapeshift<2>
    %8 = fir.embox %6(%7) : (!fir.heap<!fir.array<?x?xf32>>, !fir.shapeshift<2>) -> !fir.box<!fir.array<?x?xf32>>
    %c1_i32 = arith.constant 1 : i32
    %9 = fir.absent !fir.box<i1>
    %10 = fir.zero_bits !fir.heap<!fir.array<?xf32>>
    %c0_1 = arith.constant 0 : index
    %11 = fir.shape %c0_1 : (index) -> !fir.shape<1>
    %12 = fir.embox %10(%11) : (!fir.heap<!fir.array<?xf32>>, !fir.shape<1>) -> !fir.box<!fir.heap<!fir.array<?xf32>>>
    fir.store %12 to %0 : !fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>>
    %13 = fir.address_of(@_QQcl.2E2F7474742E66393000) : !fir.ref<!fir.char<1,10>>
    %c4_i32 = arith.constant 4 : i32
    %14 = fir.convert %0 : (!fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>>) -> !fir.ref<!fir.box<none>>
    %15 = fir.convert %8 : (!fir.box<!fir.array<?x?xf32>>) -> !fir.box<none>
    %16 = fir.convert %13 : (!fir.ref<!fir.char<1,10>>) -> !fir.ref<i8>
    %17 = fir.convert %9 : (!fir.box<i1>) -> !fir.box<none>
    %18 = fir.call @_FortranASumDim(%14, %15, %c1_i32, %16, %c4_i32, %17) : (!fir.ref<!fir.box<none>>, !fir.box<none>, i32, !fir.ref<i8>, i32, !fir.box<none>) -> none
    %19 = fir.load %0 : !fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>>
    %c0_2 = arith.constant 0 : index
    %20:3 = fir.box_dims %19, %c0_2 : (!fir.box<!fir.heap<!fir.array<?xf32>>>, index) -> (index, index, index)
    %21 = fir.box_addr %19 : (!fir.box<!fir.heap<!fir.array<?xf32>>>) -> !fir.heap<!fir.array<?xf32>>
    %22 = fir.shape_shift %20#0, %20#1 : (index, index) -> !fir.shapeshift<1>
    %23 = fir.array_load %21(%22) : (!fir.heap<!fir.array<?xf32>>, !fir.shapeshift<1>) -> !fir.array<?xf32>
    %c1_3 = arith.constant 1 : index
    %c0_4 = arith.constant 0 : index
    %24 = arith.subi %1#1, %c1_3 : index
    %25 = fir.do_loop %arg2 = %c0_4 to %24 step %c1_3 unordered iter_args(%arg3 = %2) -> (!fir.array<?xf32>) {
      %26 = fir.array_fetch %23, %arg2 : (!fir.array<?xf32>, index) -> f32
      %27 = fir.array_update %arg3, %26, %arg2 : (!fir.array<?xf32>, f32, index) -> !fir.array<?xf32>
      fir.result %27 : !fir.array<?xf32>
    }
    fir.array_merge_store %2, %25 to %arg1 : !fir.array<?xf32>, !fir.array<?xf32>, !fir.box<!fir.array<?xf32>>
    fir.freemem %21 : !fir.heap<!fir.array<?xf32>>
    return
  }
  func.func private @_FortranASumDim(!fir.ref<!fir.box<none>>, !fir.box<none>, i32, !fir.ref<i8>, i32, !fir.box<none>) -> none attributes {fir.runtime}
  fir.global linkonce @_QQcl.2E2F7474742E66393000 constant : !fir.char<1,10> {
    %0 = fir.string_lit "./ttt.f90\00"(10) : !fir.char<1,10>
    fir.has_value %0 : !fir.char<1,10>
  }
}

fir-opt --simplify-intrinsics ttt.fir:

 #9 0x0000000001539f0d llvm::MutableArrayRef<mlir::OpOperand>::operator[](unsigned long) const /local/home/szakharin/llvm-project/llvm/include/llvm/ADT/ArrayRef.h:0:7
#10 0x0000000001539da6 mlir::Operation::getOpOperand(unsigned int) /local/home/szakharin/llvm-project/llvm/../mlir/include/mlir/IR/Operation.h:306:5
#11 0x0000000001539d2b mlir::Operation::getOperand(unsigned int) /local/home/szakharin/llvm-project/llvm/../mlir/include/mlir/IR/Operation.h:267:61
#12 0x000000000174654f findShape(mlir::Value) /local/home/szakharin/llvm-project/flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp:305:20

It fails to getOperand(0) on %0 = fir.alloca !fir.box<!fir.heap<!fir.array<?xf32>>>.

@vzakhari
Copy link
Contributor Author

@Leporacanthicus FYI

@llvmbot
Copy link
Collaborator

llvmbot commented Aug 11, 2022

@llvm/issue-subscribers-flang-ir

@Leporacanthicus Leporacanthicus self-assigned this Aug 11, 2022
@Leporacanthicus
Copy link
Contributor

Fix for review here:
https://reviews.llvm.org/D131671

Leporacanthicus added a commit that referenced this issue Aug 17, 2022
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
@Leporacanthicus
Copy link
Contributor

I believe this is fixed as of 7267860

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants