Repository navigation
Closed
Description
The following input to bufferization creates allocations and unnecessary copies that seem to be unnecessary.
Starting with this IR before bufferization (--iree-codegen-iree-comprehensive-bufferize)
module {
func.func @multi_result() {
%cst = arith.constant dense<0.000000e+00> : vector<32x128xf32>
%cst_0 = arith.constant dense<0.000000e+00> : vector<8x32xf32>
%c16 = arith.constant 16 : index
%c8 = arith.constant 8 : index
%c32 = arith.constant 32 : index
%c128 = arith.constant 128 : index
%c256 = arith.constant 256 : index
%c64 = arith.constant 64 : index
%c0 = arith.constant 0 : index
%cst_1 = arith.constant 0.000000e+00 : f32
%0 = hal.interface.binding.subspan set(0) binding(0) type(storage_buffer) alignment(64) offset(%c0) flags(ReadOnly) : !flow.dispatch.tensor<readonly:tensor<64x128xf32>>
%1 = hal.interface.binding.subspan set(0) binding(1) type(storage_buffer) alignment(64) offset(%c0) flags(ReadOnly) : !flow.dispatch.tensor<readonly:tensor<128x256xf32>>
%2 = hal.interface.binding.subspan set(0) binding(2) type(storage_buffer) alignment(64) offset(%c0) flags(ReadOnly) : !flow.dispatch.tensor<readonly:tensor<256xf32>>
%3 = hal.interface.binding.subspan set(0) binding(3) type(storage_buffer) alignment(64) offset(%c0) flags(ReadOnly) : !flow.dispatch.tensor<writeonly:tensor<128x256xf32>>
%4 = hal.interface.binding.subspan set(0) binding(4) type(storage_buffer) alignment(64) offset(%c0) : !flow.dispatch.tensor<writeonly:tensor<64x256xf32>>
%5 = hal.interface.binding.subspan set(0) binding(5) type(storage_buffer) alignment(64) offset(%c0) : !flow.dispatch.tensor<writeonly:tensor<64x256xf32>>
%workgroup_id_x = hal.interface.workgroup.id[0] : index
%workgroup_count_x = hal.interface.workgroup.count[0] : index
%workgroup_id_y = hal.interface.workgroup.id[1] : index
%workgroup_count_y = hal.interface.workgroup.count[1] : index
%6 = affine.apply affine_map<()[s0] -> (s0 * 32)>()[%workgroup_id_y]
%7 = affine.apply affine_map<()[s0] -> (s0 * 32)>()[%workgroup_count_y]
%8 = affine.apply affine_map<()[s0] -> (s0 * 128)>()[%workgroup_id_x]
%9 = affine.apply affine_map<()[s0] -> (s0 * 128)>()[%workgroup_count_x]
scf.for %arg0 = %6 to %c64 step %7 {
%10 = flow.dispatch.tensor.load %0, offsets = [%arg0, 0], sizes = [32, 128], strides = [1, 1] : !flow.dispatch.tensor<readonly:tensor<64x128xf32>> -> tensor<32x128xf32>
scf.for %arg1 = %8 to %c256 step %9 {
%11 = flow.dispatch.tensor.load %4, offsets = [%arg0, %arg1], sizes = [32, 128], strides = [1, 1] : !flow.dispatch.tensor<writeonly:tensor<64x256xf32>> -> tensor<32x128xf32>
%12 = flow.dispatch.tensor.load %1, offsets = [0, %arg1], sizes = [128, 128], strides = [1, 1] : !flow.dispatch.tensor<readonly:tensor<128x256xf32>> -> tensor<128x128xf32>
%13 = vector.transfer_write %cst, %11[%c0, %c0] {in_bounds = [true, true]} : vector<32x128xf32>, tensor<32x128xf32>
%14 = flow.dispatch.tensor.load %2, offsets = [%arg1], sizes = [128], strides = [1] : !flow.dispatch.tensor<readonly:tensor<256xf32>> -> tensor<128xf32>
%15:2 = scf.for %arg2 = %c0 to %c32 step %c8 iter_args(%arg3 = %13, %arg4 = %11) -> (tensor<32x128xf32>, tensor<32x128xf32>) {
%16:2 = scf.for %arg5 = %c0 to %c128 step %c32 iter_args(%arg6 = %arg3, %arg7 = %arg4) -> (tensor<32x128xf32>, tensor<32x128xf32>) {
%17 = scf.for %arg8 = %c0 to %c128 step %c16 iter_args(%arg9 = %cst_0) -> (vector<8x32xf32>) {
%23 = vector.transfer_read %10[%arg2, %arg8], %cst_1 {in_bounds = [true, true]} : tensor<32x128xf32>, vector<8x16xf32>
%24 = vector.transfer_read %12[%arg8, %arg5], %cst_1 {in_bounds = [true, true]} : tensor<128x128xf32>, vector<16x32xf32>
%25 = vector.contract {indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, affine_map<(d0, d1, d2) -> (d2, d1)>, affine_map<(d0, d1, d2) -> (d0, d1)>], iterator_types = ["parallel", "parallel", "reduction"], kind = #vector.kind<add>} %23, %24, %arg9 : vector<8x16xf32>, vector<16x32xf32> i\
nto vector<8x32xf32>
scf.yield %25 : vector<8x32xf32>
}
%18 = vector.transfer_read %14[%arg5], %cst_1 {in_bounds = [true]} : tensor<128xf32>, vector<32xf32>
%19 = vector.broadcast %18 : vector<32xf32> to vector<8x32xf32>
%20 = arith.addf %17, %19 : vector<8x32xf32>
%21 = vector.transfer_write %20, %arg6[%arg2, %arg5] {in_bounds = [true, true]} : vector<8x32xf32>, tensor<32x128xf32>
%22 = vector.transfer_write %17, %arg7[%arg2, %arg5] {in_bounds = [true, true]} : vector<8x32xf32>, tensor<32x128xf32>
scf.yield %21, %22 : tensor<32x128xf32>, tensor<32x128xf32>
}
scf.yield %16#0, %16#1 : tensor<32x128xf32>, tensor<32x128xf32>
}
flow.dispatch.tensor.store %15#0, %5, offsets = [%arg0, %arg1], sizes = [32, 128], strides = [1, 1] : tensor<32x128xf32> -> !flow.dispatch.tensor<writeonly:tensor<64x256xf32>>
flow.dispatch.tensor.store %15#1, %4, offsets = [%arg0, %arg1], sizes = [32, 128], strides = [1, 1] : tensor<32x128xf32> -> !flow.dispatch.tensor<writeonly:tensor<64x256xf32>>
}
}
return
}
}
After bufferization I get the following IR
func.func @multi_result() {
%cst = arith.constant 0.000000e+00 : f32
%c0 = arith.constant 0 : index
%c64 = arith.constant 64 : index
%c256 = arith.constant 256 : index
%c128 = arith.constant 128 : index
%c32 = arith.constant 32 : index
%c8 = arith.constant 8 : index
%c16 = arith.constant 16 : index
%cst_0 = arith.constant dense<0.000000e+00> : vector<8x32xf32>
%cst_1 = arith.constant dense<0.000000e+00> : vector<32x128xf32>
%alloca = memref.alloca() {alignment = 64 : i64} : memref<32x128xf32, #hal.descriptor_type<storage_buffer>>
%0 = hal.interface.binding.subspan set(0) binding(0) type(storage_buffer) alignment(64) offset(%c0) flags(ReadOnly) : memref<64x128xf32, #hal.descriptor_type<storage_buffer>>
memref.assume_alignment %0, 64 : memref<64x128xf32, #hal.descriptor_type<storage_buffer>>
%1 = hal.interface.binding.subspan set(0) binding(1) type(storage_buffer) alignment(64) offset(%c0) flags(ReadOnly) : memref<128x256xf32, #hal.descriptor_type<storage_buffer>>
memref.assume_alignment %1, 64 : memref<128x256xf32, #hal.descriptor_type<storage_buffer>>
%2 = hal.interface.binding.subspan set(0) binding(2) type(storage_buffer) alignment(64) offset(%c0) flags(ReadOnly) : memref<256xf32, #hal.descriptor_type<storage_buffer>>
memref.assume_alignment %2, 64 : memref<256xf32, #hal.descriptor_type<storage_buffer>>
%3 = hal.interface.binding.subspan set(0) binding(4) type(storage_buffer) alignment(64) offset(%c0) : memref<64x256xf32, #hal.descriptor_type<storage_buffer>>
memref.assume_alignment %3, 64 : memref<64x256xf32, #hal.descriptor_type<storage_buffer>>
%4 = hal.interface.binding.subspan set(0) binding(5) type(storage_buffer) alignment(64) offset(%c0) : memref<64x256xf32, #hal.descriptor_type<storage_buffer>>
memref.assume_alignment %4, 64 : memref<64x256xf32, #hal.descriptor_type<storage_buffer>>
%workgroup_id_x = hal.interface.workgroup.id[0] : index
%workgroup_count_x = hal.interface.workgroup.count[0] : index
%workgroup_id_y = hal.interface.workgroup.id[1] : index
%workgroup_count_y = hal.interface.workgroup.count[1] : index
%5 = affine.apply affine_map<()[s0] -> (s0 * 32)>()[%workgroup_id_y]
%6 = affine.apply affine_map<()[s0] -> (s0 * 32)>()[%workgroup_count_y]
%7 = affine.apply affine_map<()[s0] -> (s0 * 128)>()[%workgroup_id_x]
%8 = affine.apply affine_map<()[s0] -> (s0 * 128)>()[%workgroup_count_x]
scf.for %arg0 = %5 to %c64 step %6 {
%subview = memref.subview %0[%arg0, 0] [32, 128] [1, 1] : memref<64x128xf32, #hal.descriptor_type<storage_buffer>> to memref<32x128xf32, strided<[128, 1], offset: ?>, #hal.descriptor_type<storage_buffer>>
scf.for %arg1 = %7 to %c256 step %8 {
%subview_2 = memref.subview %3[%arg0, %arg1] [32, 128] [1, 1] : memref<64x256xf32, #hal.descriptor_type<storage_buffer>> to memref<32x128xf32, strided<[256, 1], offset: ?>, #hal.descriptor_type<storage_buffer>>
%subview_3 = memref.subview %1[0, %arg1] [128, 128] [1, 1] : memref<128x256xf32, #hal.descriptor_type<storage_buffer>> to memref<128x128xf32, strided<[256, 1], offset: ?>, #hal.descriptor_type<storage_buffer>>
linalg.generic {indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0, d1)>], iterator_types = ["parallel", "parallel"]} ins(%subview_2 : memref<32x128xf32, strided<[256, 1], offset: ?>, #hal.descriptor_type<storage_buffer>>) outs(%alloca : memref<32x128xf32, #hal.descriptor_\
type<storage_buffer>>) {
^bb0(%in: f32, %out: f32):
linalg.yield %in : f32
}
vector.transfer_write %cst_1, %alloca[%c0, %c0] {in_bounds = [true, true]} : vector<32x128xf32>, memref<32x128xf32, #hal.descriptor_type<storage_buffer>>
%subview_4 = memref.subview %2[%arg1] [128] [1] : memref<256xf32, #hal.descriptor_type<storage_buffer>> to memref<128xf32, strided<[1], offset: ?>, #hal.descriptor_type<storage_buffer>>
scf.for %arg2 = %c0 to %c32 step %c8 {
scf.for %arg3 = %c0 to %c128 step %c32 {
%9 = scf.for %arg4 = %c0 to %c128 step %c16 iter_args(%arg5 = %cst_0) -> (vector<8x32xf32>) {
%13 = vector.transfer_read %subview[%arg2, %arg4], %cst {in_bounds = [true, true]} : memref<32x128xf32, strided<[128, 1], offset: ?>, #hal.descriptor_type<storage_buffer>>, vector<8x16xf32>
%14 = vector.transfer_read %subview_3[%arg4, %arg3], %cst {in_bounds = [true, true]} : memref<128x128xf32, strided<[256, 1], offset: ?>, #hal.descriptor_type<storage_buffer>>, vector<16x32xf32>
%15 = vector.contract {indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, affine_map<(d0, d1, d2) -> (d2, d1)>, affine_map<(d0, d1, d2) -> (d0, d1)>], iterator_types = ["parallel", "parallel", "reduction"], kind = #vector.kind<add>} %13, %14, %arg5 : vector<8x16xf32>, vector<16x32xf32> int\
o vector<8x32xf32>
scf.yield %15 : vector<8x32xf32>
}
%10 = vector.transfer_read %subview_4[%arg3], %cst {in_bounds = [true]} : memref<128xf32, strided<[1], offset: ?>, #hal.descriptor_type<storage_buffer>>, vector<32xf32>
%11 = vector.broadcast %10 : vector<32xf32> to vector<8x32xf32>
%12 = arith.addf %9, %11 : vector<8x32xf32>
vector.transfer_write %12, %alloca[%arg2, %arg3] {in_bounds = [true, true]} : vector<8x32xf32>, memref<32x128xf32, #hal.descriptor_type<storage_buffer>>
vector.transfer_write %9, %subview_2[%arg2, %arg3] {in_bounds = [true, true]} : vector<8x32xf32>, memref<32x128xf32, strided<[256, 1], offset: ?>, #hal.descriptor_type<storage_buffer>>
}
}
%subview_5 = memref.subview %4[%arg0, %arg1] [32, 128] [1, 1] : memref<64x256xf32, #hal.descriptor_type<storage_buffer>> to memref<32x128xf32, strided<[256, 1], offset: ?>, #hal.descriptor_type<storage_buffer>>
linalg.generic {indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0, d1)>], iterator_types = ["parallel", "parallel"]} ins(%alloca : memref<32x128xf32, #hal.descriptor_type<storage_buffer>>) outs(%subview_5 : memref<32x128xf32, strided<[256, 1], offset: ?>, #hal.descriptor_\
type<storage_buffer>>) {
^bb0(%in: f32, %out: f32):
linalg.yield %in : f32
}
}
}
return
}
The alloca created results in two unnecessary copies as well.
Metadata
Metadata
Assignees
Labels
No labels
Type
Projects
- StatusShow more project fieldsDone
Activity
matthias-springer commentedon Feb 1, 2023
The
transfer_writewrites into%11, but then the original%11is used by the loop. Can this actually be done without a copy?Adapt Tile+Fuse to handle multi-result dispatches. (#12020)
MaheshRavishankar commentedon Feb 2, 2023
Actually found the root cause. It is related to some change in convert to destination passing style that creates some weird IR. I think we should resolve this so that we can drop that pattern from within IREE and make upstream handle it (even if it is opt-in). I can walk you through the issue when we chat. So there is no issue right now. Thanks for looking into it.
Redo handling of operand reuse to prevent stack allocation.
Adapt Tile+Fuse to handle multi-result dispatches. (iree-org#12020)
Redo handling of operand reuse to prevent stack allocation.
Redo handling of operand reuse to prevent stack allocation. (#12041)
Adapt Tile+Fuse to handle multi-result dispatches. (iree-org#12020)
Redo handling of operand reuse to prevent stack allocation. (iree-org…
Adapt Tile+Fuse to handle multi-result dispatches. (#12020)
Redo handling of operand reuse to prevent stack allocation. (#12041)
Adapt Tile+Fuse to handle multi-result dispatches. (iree-org#12020)
Redo handling of operand reuse to prevent stack allocation. (iree-org…