Skip to content

Failure in EliminateEmptyTensors pass fails  #11273

Open

Description

@MaheshRavishankar

With this input

module {
  func.func @_pad_1D_test_dispatch_0() {
    %c7 = arith.constant 7 : index
    %c0 = arith.constant 0 : index
    %c0_i32 = arith.constant 0 : i32
    %0 = hal.interface.binding.subspan set(0) binding(0) type(storage_buffer) offset(%c0) alignment(64) : !flow.dispatch.tensor<readonly:tensor<2xi32>>
    %1 = hal.interface.binding.subspan set(0) binding(1) type(storage_buffer) offset(%c0) alignment(64) : !flow.dispatch.tensor<writeonly:tensor<7xi32>>
    %workgroup_id_x = hal.interface.workgroup.id[0] : index
    %workgroup_count_x = hal.interface.workgroup.count[0] : index
    scf.for %arg0 = %workgroup_id_x to %c7 step %workgroup_count_x {
      %2 = affine.max affine_map<(d0) -> (-d0 + 3, 0)>(%arg0)
      %3 = affine.max affine_map<(d0) -> (0, d0 - 3)>(%arg0)
      %4 = affine.min affine_map<(d0) -> (2, d0)>(%3)
      %5 = affine.max affine_map<(d0) -> (0, d0 - 2)>(%arg0)
      %6 = affine.min affine_map<(d0) -> (2, d0)>(%5)
      %7 = affine.apply affine_map<(d0, d1) -> (d0 - d1)>(%6, %4)
      %8 = arith.cmpi eq, %7, %c0 : index
      %9 = affine.apply affine_map<(d0, d1, d2) -> (-d0 - d1 + d2 + 1)>(%2, %6, %4)
      %10 = scf.if %8 -> (tensor<1xi32>) {
        %generated = tensor.generate  {
        ^bb0(%arg1: index):
          tensor.yield %c0_i32 : i32
        } : tensor<1xi32>
        scf.yield %generated : tensor<1xi32>
      } else {
        %11 = flow.dispatch.tensor.load %0, offsets = [%4], sizes = [%7], strides = [1] : !flow.dispatch.tensor<readonly:tensor<2xi32>> -> tensor<?xi32>
        %padded = tensor.pad %11 low[%2] high[%9] {
        ^bb0(%arg1: index):
          tensor.yield %c0_i32 : i32
        } : tensor<?xi32> to tensor<1xi32>
        scf.yield %padded : tensor<1xi32>
      }
      flow.dispatch.tensor.store %10, %1, offsets = [%arg0], sizes = [1], strides = [1] : tensor<1xi32> -> !flow.dispatch.tensor<writeonly:tensor<7xi32>>
    }
    return
  }
}
iree-opt --iree-eliminate-empty-tensors repro.mlir

fails with the following error

$ ~/iree/build_debug/tools/iree-opt --iree-eliminate-empty-tensors elimiate_empty_tensors.repro.mlir 
elimiate_empty_tensors.repro.mlir:24:9: error: operand #0 of ReturnLike op does not satisfy destination passing style
        scf.yield %generated : tensor<1xi32>
        ^
elimiate_empty_tensors.repro.mlir:24:9: note: see current operation: "scf.yield"(%16) : (tensor<1xi32>) -> ()
elimiate_empty_tensors.repro.mlir:31:9: error: operand #0 of ReturnLike op does not satisfy destination passing style
        scf.yield %padded : tensor<1xi32>
        ^
elimiate_empty_tensors.repro.mlir:31:9: note: see current operation: "scf.yield"(%17) : (tensor<1xi32>) -> ()

Activity

  1. matthias-springer commented on Nov 29, 2022

    @matthias-springer
    Contributor

    The error message is confusing, here is a cleanup to make it more descriptive: https://reviews.llvm.org/D138902
    It will now print as:

    elimiate_empty_tensors.repro.mlir:24:9: error: operand #0 may return/yield a new buffer allocation
            scf.yield %generated : tensor<1xi32>
    

    This a limitation that we currently have in One-Shot Bufferize: When you yield a new buffer allocation (both tensor.pad and tensor.generate bufferize to a new allocation), One-Shot Bufferize cannot deallocate the buffer. We could support this case by running the bufferization with allowReturnAllocs = true and createDealloc = false, followed by the MLIR BufferDeallocationPass. However, the generated code can be quite inefficient and contain buffer copies, so I wouldn't recommend doing that. (Although here it's just a tensor<1xi32>, so it wouldn't matter too much.) Also the buffer deallocation has known bugs that need to be fixed.

    Can we write a pass to move the flow.dispatch.tensor.store into the scf.if (and duplicate it)? That would solve the problem here. As a followup, we can then rewrite the IR to operate directly on the loaded buffer.

    I.e., after moving the flow.dispatch.tensor.store inside the scf.if (only showing the "then" branch here):

    %generated = tensor.generate  {
    ^bb0(%arg1: index):
      tensor.yield %c0_i32 : i32
    } : tensor<1xi32>
    flow.dispatch.tensor.store %generated, %1, offsets = [%arg0], sizes = [1], strides = [1] : tensor<1xi32> -> !flow.dispatch.tensor<writeonly:tensor<7xi32>>

    At this point, we still have an alloc inside of the scf.if, but at least it can be deallocated at the end of the "then" block, so the error is gone.

    Then the follow-up rewrite. This is essentially what EliminateEmptyTensors does, but it could be extended to other ops that allocate. So it would turn into something like EliminateAllocs.

    %loaded = flow.dispatch.tensor.load %1, offsets = [%arg0], sizes = [1], strides = [1] : tensor<1xi32> -> !flow.dispatch.tensor<writeonly:tensor<7xi32>>
    %generated = linalg.generic outs(%loaded : tensor<1xi32>) {
    ^bb0(%out: f32):
      %arg1 = linalg.index()
      tensor.yield %c0_i32 : i32
    }
    flow.dispatch.tensor.store %generated, %1, offsets = [%arg0], sizes = [1], strides = [1] : tensor<1xi32> -> !flow.dispatch.tensor<writeonly:tensor<7xi32>>

    Now the alloc is completely gone. We basically turned the tensor.generate into a destination-passing style op.

    We have to do the same for the "else" branch.

    Note: You are currently this error message during EliminateEmptyTensors. That's because that passes reuses the bufferization analysis under the hood. If you were to the remove that pass from your pipeline, it would still fail with the same error message when One-Shot Bufferize runs.

  2. MaheshRavishankar commented on Dec 2, 2022

    @MaheshRavishankar
    CollaboratorAuthor

    Actually I just realized that I am not using pad vectorization, so Ill circle back to this if it is still an issue then. Sorry for the noise.

  3. MaheshRavishankar commented on Jan 6, 2023

    @MaheshRavishankar
    CollaboratorAuthor

    @matthias-springer thanks for the response earlier. I finally got around to parsing what you had. Couple of things

    1. Moving the flow.dispatch.tensor.store around this way is frought.... It opens up a can of worms that is hard to reason about in general. Just looking at the scf.if
          %10 = scf.if %8 -> (tensor<1xi32>) {
            %generated = tensor.generate  {
            ^bb0(%arg1: index):
              tensor.yield %c0_i32 : i32
            } : tensor<1xi32>
            scf.yield %generated : tensor<1xi32>
          } else {
            %11 = flow.dispatch.tensor.load %0, offsets = [%4], sizes = [%7], strides = [1] : !flow.dispatch.tensor<readonly:tensor<2xi32>> -> tensor<?xi32>
            %padded = tensor.pad %11 low[%2] high[%9] {
            ^bb0(%arg1: index):
              tensor.yield %c0_i32 : i32
            } : tensor<?xi32> to tensor<1xi32>
    	   scf.yield %padded : tensor<1xi32>
          }
    

    I would expect that %10, %generated and %padded to be marked as "aliased" (or whatever term usptream bufferization uses). By semantics of scf.if these tensors are all effectively the same buffer. With that, then you can convert the tensor.generate to just be a linalg.fill of 0's into the result buffer. We already know %10 writes into the result buffer, so that should avoid the temporary buffer?

    1. Soon, I am going to begin the work of dropping the use of sliced flow.dispatch.tensor.load/store. I can do this by basically using scf.foreach_thread for the first level tile and distribute in IREE. Would that make this easier?
  4. matthias-springer commented on Jan 7, 2023

    @matthias-springer
    Contributor

    That could work. There's three things that we have to do:

    Step 1: Decompose tensor.generate into tensor.empty + linalg.generic to fill the tensor. Decompose tensor.pad into tensor.empty + linalg.fill + tensor.insert_slice.

          %10 = scf.if %8 -> (tensor<1xi32>) {
            %empty1 = tensor.empty : tensor<1xi32>
            %generated = linalg.generic outs(%empty1) ....
            scf.yield %generated : tensor<1xi32>
          } else {
            %11 = flow.dispatch.tensor.load %0, offsets = [%4], sizes = [%7], strides = [1] : !flow.dispatch.tensor<readonly:tensor<2xi32>> -> tensor<?xi32>
            %empty2 = tensor.empty : tensor<1xi32>
            %filled = linalg.fill %empty2
            %padded = tensor.insert_slice %11 into %filled
            scf.yield %padded : tensor<1xi32>
          }
          flow.dispatch.tensor.store %10, %1, offsets = [%arg0], sizes = [1], strides = [1] : tensor<1xi32> -> !flow.dispatch.tensor<writeonly:tensor<7xi32>>
    

    Step 2: Minor update to --iree-eliminate-empty-tensors so that it handles cases where a value may be originating from one of multiple tensor.empty. Then after running empty tensor elimination:

          %10 = scf.if %8 -> (tensor<1xi32>) {
            %empty1 = flow.dispatch.tensor.load %1 ...
            %generated = linalg.generic outs(%empty1) ....
            scf.yield %generated : tensor<1xi32>
          } else {
            %11 = flow.dispatch.tensor.load %0, offsets = [%4], sizes = [%7], strides = [1] : !flow.dispatch.tensor<readonly:tensor<2xi32>> -> tensor<?xi32>
            %empty2 = flow.dispatch.tensor.load %1 ...
            %filled = linalg.fill %empty2
            %padded = tensor.insert_slice %11 into %filled
            scf.yield %padded : tensor<1xi32>
          }
          flow.dispatch.tensor.store %10, %1, offsets = [%arg0], sizes = [1], strides = [1] : tensor<1xi32> -> !flow.dispatch.tensor<writeonly:tensor<7xi32>>
    

    Now we bufferize.

    %10 = scf.if ... -> (memref<1x32>) {
      %empty1 = memref.subview %1
      ...
      scf.yield %empty1
    } else {
      %empty2 = memref.subview %1
      ...
      scf.yield %empty2
    }
    %sv = memref.subview %1 ...
    memref.copy %10, %sv
    

    Step 3: Add an scf.if canonicalization that hoists the memref.subviews, CSE's them and fold away the scf.if result. This may already be implemented (at least the folding of scf.if results). Then we get this IR:

    %sv = memref.subview %1
    %10 = scf.if {
      ...
    } else {
      ...
    }
    memref.copy %sv, sv
    

    Now the memref.copy folds away. I can give it a try next week.

    RE scf.foreach_thread: I think that won't make a difference here. But it would allow us to remove --iree-eliminate-empty-tensors and instead use the pass in MLIR core.

  5. MaheshRavishankar commented on Jan 18, 2023

    @MaheshRavishankar
    CollaboratorAuthor

    @matthias-springer any updates on this. If this is going to take longer, I can add an IREE specific WAR for now

  6. matthias-springer commented on Jan 18, 2023

    @matthias-springer
    Contributor

    I'm working on this but there's one detail that I overlooked. We need to extend BufferizableOpInterface for keeping track of equivalent OpOperands properly. This is a pretty big change. I think I should be able to finish this by the end of this week. If you have free cycles, we also still need lowering patterns for tensor.generate and tensor.pad that could be written in parallel. (I would put it somewhere in Dialect/Tensor/Transforms in MLIR.)

  7. MaheshRavishankar commented on Jan 18, 2023

    @MaheshRavishankar
    CollaboratorAuthor

    I'm working on this but there's one detail that I overlooked. We need to extend BufferizableOpInterface for keeping track of equivalent OpOperands properly. This is a pretty big change. I think I should be able to finish this by the end of this week. If you have free cycles, we also still need lowering patterns for tensor.generate and tensor.pad that could be written in parallel. (I would put it somewhere in Dialect/Tensor/Transforms in MLIR.)

    How about changing the lowering itself to not use tensor.generate ? Maybe pad needs to be generalized. @vmurali FYI in case you have cycles before I get to it.

  8. matthias-springer commented on Jan 19, 2023

    @matthias-springer
    Contributor

    I finished the implementation for Step 2: https://reviews.llvm.org/D142130. It turned out to be a larger change and the entire stack is needed. Working on Step 1 now.

  9. matthias-springer commented on Jan 19, 2023

    @matthias-springer
    Contributor

    How about changing the lowering itself to not use tensor.generate?

    Which lowering are you referring to? Do you mean not producing the tensor.generate in IREE in the first place?

  10. MaheshRavishankar commented on Jan 19, 2023

    @MaheshRavishankar
    CollaboratorAuthor

    How about changing the lowering itself to not use tensor.generate?

    Which lowering are you referring to? Do you mean not producing the tensor.generate in IREE in the first place?

    The tensor.generate is produced by the upstream implementation of TilingInterface for tensor.pad.

  11. added a commit that references this issue on Jan 19, 2023
  12. matthias-springer commented on Jan 20, 2023

    @matthias-springer
    Contributor

    Step 1 is also done: stack of https://reviews.llvm.org/D142207.

    Now it should bufferize but there is still a self-copy.

    E.g.,

      func.func @eleminate_multiple_ops(%arg0: memref<?xf32>, %arg1: index, %arg2: i1) -> memref<?xf32> {
        %cst = arith.constant 1.000000e+00 : f32
        %cst_0 = arith.constant 0.000000e+00 : f32
        %0 = scf.if %arg2 -> (memref<?xf32, strided<[1], offset: 42>>) {
          %subview_1 = memref.subview %arg0[42] [%arg1] [1] : memref<?xf32> to memref<?xf32, strided<[1], offset: 42>>
          linalg.fill ins(%cst_0 : f32) outs(%subview_1 : memref<?xf32, strided<[1], offset: 42>>)
          scf.yield %subview_1 : memref<?xf32, strided<[1], offset: 42>>
        } else {
          %subview_1 = memref.subview %arg0[42] [%arg1] [1] : memref<?xf32> to memref<?xf32, strided<[1], offset: 42>>
          linalg.fill ins(%cst : f32) outs(%subview_1 : memref<?xf32, strided<[1], offset: 42>>)
          scf.yield %subview_1 : memref<?xf32, strided<[1], offset: 42>>
        }
        %subview = memref.subview %arg0[42] [%arg1] [1] : memref<?xf32> to memref<?xf32, strided<[1], offset: 42>>
        memref.copy %0, %subview : memref<?xf32, strided<[1], offset: 42>> to memref<?xf32, strided<[1], offset: 42>>
        return %arg0 : memref<?xf32>
      }
    

    Unfortunately, the memref.subview does not CSE with -cse. Actually I'm not quite sure how to simplify this. In general, hoisting the memref.subview from an scf.if may not be safe.

    I could write a hoisting transformation that looks for identical ops in an scf.if on the then and the else branch. Does that sound reasonable?

  13. MaheshRavishankar commented on Jan 20, 2023

    @MaheshRavishankar
    CollaboratorAuthor

    Step 1 is also done: stack of reviews.llvm.org/D142207.

    Are you waiting for me to review? Maybe someone with more context can. I can take a look if you want me to.

    Now it should bufferize but there is still a self-copy.

    E.g.,

      func.func @eleminate_multiple_ops(%arg0: memref<?xf32>, %arg1: index, %arg2: i1) -> memref<?xf32> {
        %cst = arith.constant 1.000000e+00 : f32
        %cst_0 = arith.constant 0.000000e+00 : f32
        %0 = scf.if %arg2 -> (memref<?xf32, strided<[1], offset: 42>>) {
          %subview_1 = memref.subview %arg0[42] [%arg1] [1] : memref<?xf32> to memref<?xf32, strided<[1], offset: 42>>
          linalg.fill ins(%cst_0 : f32) outs(%subview_1 : memref<?xf32, strided<[1], offset: 42>>)
          scf.yield %subview_1 : memref<?xf32, strided<[1], offset: 42>>
        } else {
          %subview_1 = memref.subview %arg0[42] [%arg1] [1] : memref<?xf32> to memref<?xf32, strided<[1], offset: 42>>
          linalg.fill ins(%cst : f32) outs(%subview_1 : memref<?xf32, strided<[1], offset: 42>>)
          scf.yield %subview_1 : memref<?xf32, strided<[1], offset: 42>>
        }
        %subview = memref.subview %arg0[42] [%arg1] [1] : memref<?xf32> to memref<?xf32, strided<[1], offset: 42>>
        memref.copy %0, %subview : memref<?xf32, strided<[1], offset: 42>> to memref<?xf32, strided<[1], offset: 42>>
        return %arg0 : memref<?xf32>
      }
    

    Unfortunately, the memref.subview does not CSE with -cse. Actually I'm not quite sure how to simplify this. In general, hoisting the memref.subview from an scf.if may not be safe.

    I could write a hoisting transformation that looks for identical ops in an scf.if on the then and the else branch. Does that sound reasonable?

    Easier might be to just write a hoisting of ops from either the if or the else branch. Then CSE should be able to take care of the rest and get you to the form you want.

  14. 9 remaining items

  15. moved this from Not Started to Inbox in (Deprecated) IREEon Apr 5, 2023
  16. moved this from Inbox to In Progress in (Deprecated) IREEon Apr 11, 2023
  17. MaheshRavishankar commented on Apr 11, 2023

    @MaheshRavishankar
    CollaboratorAuthor

    Moved to P2 for now.

  18. moved this from In Progress to Not Started in (Deprecated) IREEon May 8, 2023
  19. moved this from Not Started to Needs Scheduling in (Deprecated) IREEon May 8, 2023
  20. moved this from Needs Scheduling to Backlog in (Deprecated) IREEon Aug 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions