I’m doing the bufferlize thing right now and I’m implementing BufferizableOpInterface,but I’m having problems and I’d like to ask for advice.
Here are specific questions about bufferizesToMemoryRead
, bufferizesToMemoryWrite
.My understanding of these two functions is purely that the memref after bufferize may be read or written.So I set them to return true.But I generated the following code.
%0 = bufferization.to_memref %arg4 : memref<2x3x8xf32>
......
%alloc = memref.alloc() {alignment = 64 : i64} : memref<2x3x8xf32>
memref.copy %0, %alloc : memref<2x3x8xf32> to memref<2x3x8xf32>
%1 = my.op %alloc : (memref<2x3x8xf32>) -> memref<2x3x8xf32>
%2 = bufferization.to_tensor %1 : memref<2x3x8xf32>
After my testing, if I only set bufferizesToMemoryWrite
returns true.The following code will be generated.
// You can see that there is no more memref.copy operation here, but there is still alloc.
%alloc = memref.alloc() {alignment = 64 : i64} : memref<2x3x8xf32>
%0 = my.op %alloc : (memref<2x3x8xf32>) -> memref<2x3x8xf32>
%1 = bufferization.to_tensor %0 : memref<2x3x8xf32>
If I set both settings to return false, then the following code is generated.
%0 = bufferization.to_memref %arg4 : memref<2x3x8xf32>
%1 = my.op %0 (memref<2x3x8xf32>) -> memref<2x3x8xf32>
%2 = bufferization.to_tensor %1 : memref<2x3x8xf32>
Here’s what I want to say.
- I actually didn’t understand the above behavior.I think the
memref
afterbufferization.to_memref
is read and written, so I set it to return true, but the generated code I can’t understand, and I think the operations alloc,copy are redundant.But theBufferizableOpInterface
documentation describes the function, and if you look at it according to the documentation, it should still return true, but actually false is the best, or am I misunderstanding. - Another thing is that I don’t understand why bufferizesToMemoryWrite returning true does alloc and bufferizesToMemoryRead returning true does copy.
I hope you can help me, I would appreciate it. Really thank you all.