Controlling memory space in EmptyTensorToAllocTensor pass

Hi
I have a tensor.empty() opertation that was added for the destination passing style of the next operation.
It eventually translates it to bufferization.alloc_tensor().
The problem I have is that for some uses I need a specific memory_space for the alloc_tensor.
I would like to add a callback to EmptyTensorToAllocTensor that would allow controlling the memory_space, but I’m not entirely sure if this is a good idea.
Is there a better way to do that?

1 Like

That makes sense, we could add a memory space pass option to EmptyTensorToAllocTensor.

However, generally speaking, if you want a certain memory space, it’s better to directly have a memref.alloc() in the input IR. E.g., instead of tensor.empty:

%0 = memref.alloc() : memref<5xf32, 3>
%1 = bufferization.to_tensor %0 restrict writable : memref<5xf32, 3>

tensor.empty can sometimes be optimized away by -eliminate-empty-tensors. -empty-tensor-to-alloc-tensor was meant as a follow-up pass that handles all tensor.empty that were not handled by -eliminate-empty-tensors. If you don’t need that (or want a guaranteed allocation), it’s better to explicitly spell out the allocation op.

Thanks @matthias-springer
Taking the explicit straight-forward approach makes sense.