Repository navigation
Open
Description
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>) -> ()
Metadata
Metadata
Assignees
Labels
No labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
matthias-springer commentedon Nov 29, 2022
The error message is confusing, here is a cleanup to make it more descriptive: https://reviews.llvm.org/D138902
It will now print as:
This a limitation that we currently have in One-Shot Bufferize: When you yield a new buffer allocation (both
tensor.padandtensor.generatebufferize to a new allocation), One-Shot Bufferize cannot deallocate the buffer. We could support this case by running the bufferization withallowReturnAllocs = trueandcreateDealloc = 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.storeinto thescf.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.storeinside thescf.if(only showing the "then" branch here):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
EliminateEmptyTensorsdoes, but it could be extended to other ops that allocate. So it would turn into something likeEliminateAllocs.Now the alloc is completely gone. We basically turned the
tensor.generateinto 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.MaheshRavishankar commentedon Dec 2, 2022
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.
MaheshRavishankar commentedon Jan 6, 2023
@matthias-springer thanks for the response earlier. I finally got around to parsing what you had. Couple of things
flow.dispatch.tensor.storearound this way is frought.... It opens up a can of worms that is hard to reason about in general. Just looking at thescf.ifI would expect that
%10,%generatedand%paddedto be marked as "aliased" (or whatever term usptream bufferization uses). By semantics ofscf.ifthese tensors are all effectively the same buffer. With that, then you can convert thetensor.generateto just be alinalg.fillof 0's into the result buffer. We already know%10writes into the result buffer, so that should avoid the temporary buffer?flow.dispatch.tensor.load/store. I can do this by basically usingscf.foreach_threadfor the first level tile and distribute in IREE. Would that make this easier?matthias-springer commentedon Jan 7, 2023
That could work. There's three things that we have to do:
Step 1: Decompose
tensor.generateintotensor.empty+linalg.genericto fill the tensor. Decomposetensor.padintotensor.empty+linalg.fill+tensor.insert_slice.Step 2: Minor update to
--iree-eliminate-empty-tensorsso that it handles cases where a value may be originating from one of multipletensor.empty. Then after running empty tensor elimination:Now we bufferize.
Step 3: Add an
scf.ifcanonicalization that hoists thememref.subviews, CSE's them and fold away thescf.ifresult. This may already be implemented (at least the folding ofscf.ifresults). Then we get this IR:Now the
memref.copyfolds 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-tensorsand instead use the pass in MLIR core.MaheshRavishankar commentedon Jan 18, 2023
@matthias-springer any updates on this. If this is going to take longer, I can add an IREE specific WAR for now
matthias-springer commentedon Jan 18, 2023
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.generateandtensor.padthat could be written in parallel. (I would put it somewhere inDialect/Tensor/Transformsin MLIR.)MaheshRavishankar commentedon Jan 18, 2023
How about changing the lowering itself to not use
tensor.generate? Maybepadneeds to be generalized. @vmurali FYI in case you have cycles before I get to it.matthias-springer commentedon Jan 19, 2023
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.
matthias-springer commentedon Jan 19, 2023
Which lowering are you referring to? Do you mean not producing the
tensor.generatein IREE in the first place?MaheshRavishankar commentedon Jan 19, 2023
The
tensor.generateis produced by the upstream implementation ofTilingInterfacefortensor.pad.[WIP] Data Tiling for GPU
matthias-springer commentedon Jan 20, 2023
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.,
Unfortunately, the
memref.subviewdoes not CSE with-cse. Actually I'm not quite sure how to simplify this. In general, hoisting thememref.subviewfrom anscf.ifmay not be safe.I could write a hoisting transformation that looks for identical ops in an
scf.ifon thethenand theelsebranch. Does that sound reasonable?MaheshRavishankar commentedon Jan 20, 2023
Are you waiting for me to review? Maybe someone with more context can. I can take a look if you want me to.
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.
9 remaining items
Pad fusion bufferization workaround.
Pad fusion bufferization workaround. (#12425)
Pad fusion bufferization workaround. (iree-org#12425)
MaheshRavishankar commentedon Apr 11, 2023
Moved to P2 for now.
Pad fusion bufferization workaround. (#12425)
Pad fusion bufferization workaround. (iree-org#12425)
Pad fusion bufferization workaround. (iree-org#12425)