iree: [CPU] TileRootAndFuseProducerConsumer causes redundant stack allocation
The current implementation yields two tensors when tiling on parallel loops, see the lit test. (I will replace the link with the file after the change is landed to main.)
It causes an additional stack allocation. To repro:
Run: iree-opt --pass-pipeline='builtin.module(iree-llvmcpu-select-lowering-strategy, func.func(iree-llvmcpu-lower-executable-target))' /repro.mlir. Then you will find an memref.alloca op at the result IR.
#executable_target_embedded_elf_x86_64 = #hal.executable.target<"llvm-cpu", "embedded-elf-x86_64", {cpu = "generic", cpu_features = "", data_layout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128", iree.encoding.resolver = #iree_cpu.cpu_encoding_layout<>, max_stack_allocation_size = 32768 : i64, native_vector_size = 16 : i64, target_triple = "x86_64-unknown-unknown-eabi-elf", ukernels = "none"}>
#map = affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>
#map1 = affine_map<(d0, d1, d2, d3) -> (d0, d2)>
#pipeline_layout = #hal.pipeline.layout<constants = 10, bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>
module {
func.func @mmt4d_bias_relu_dispatch_0_mmt4d_DxDxDx16x16x1_f32() attributes {hal.executable.target = #executable_target_embedded_elf_x86_64} {
%c0 = arith.constant 0 : index
%c32_i64 = arith.constant 32 : i64
%cst = arith.constant 0.000000e+00 : f32
%0 = hal.interface.constant.load layout(#pipeline_layout) ordinal(0) : i32
%1 = hal.interface.constant.load layout(#pipeline_layout) ordinal(1) : i32
%2 = hal.interface.constant.load layout(#pipeline_layout) ordinal(2) : i32
%3 = hal.interface.constant.load layout(#pipeline_layout) ordinal(3) : i32
%4 = hal.interface.constant.load layout(#pipeline_layout) ordinal(4) : i32
%5 = hal.interface.constant.load layout(#pipeline_layout) ordinal(5) : i32
%6 = hal.interface.constant.load layout(#pipeline_layout) ordinal(6) : i32
%7 = hal.interface.constant.load layout(#pipeline_layout) ordinal(7) : i32
%8 = hal.interface.constant.load layout(#pipeline_layout) ordinal(8) : i32
%9 = hal.interface.constant.load layout(#pipeline_layout) ordinal(9) : i32
%10 = arith.extui %0 : i32 to i64
%11 = arith.extui %1 : i32 to i64
%12 = arith.shli %11, %c32_i64 : i64
%13 = arith.ori %10, %12 : i64
%14 = arith.index_castui %13 : i64 to index
%15 = arith.extui %2 : i32 to i64
%16 = arith.extui %3 : i32 to i64
%17 = arith.shli %16, %c32_i64 : i64
%18 = arith.ori %15, %17 : i64
%19 = arith.index_castui %18 : i64 to index
%20 = arith.extui %4 : i32 to i64
%21 = arith.extui %5 : i32 to i64
%22 = arith.shli %21, %c32_i64 : i64
%23 = arith.ori %20, %22 : i64
%24 = arith.index_castui %23 : i64 to index
%25 = arith.extui %6 : i32 to i64
%26 = arith.extui %7 : i32 to i64
%27 = arith.shli %26, %c32_i64 : i64
%28 = arith.ori %25, %27 : i64
%29 = arith.index_castui %28 : i64 to index
%30 = arith.extui %8 : i32 to i64
%31 = arith.extui %9 : i32 to i64
%32 = arith.shli %31, %c32_i64 : i64
%33 = arith.ori %30, %32 : i64
%34 = arith.index_castui %33 : i64 to index
%35:5 = util.assume.int
%14<umin = 0, umax = 9007199254740991>,
%19<umin = 0, umax = 9007199254740991>,
%24<umin = 0, umax = 9007199254740991>,
%29<umin = 0, umax = 9007199254740991>,
%34<umin = 0, umax = 9007199254740991>
: index, index, index, index, index
%36 = iree_tensor_ext.dispatch.workload.ordinal %35#0, 0 : index
%37 = iree_tensor_ext.dispatch.workload.ordinal %35#1, 1 : index
%38 = iree_tensor_ext.dispatch.workload.ordinal %35#2, 2 : index
%39 = iree_tensor_ext.dispatch.workload.ordinal %35#3, 3 : index
%40 = iree_tensor_ext.dispatch.workload.ordinal %35#4, 4 : index
%41 = hal.interface.binding.subspan layout(#pipeline_layout) binding(0) alignment(64) offset(%c0) flags("ReadOnly|Indirect") : !iree_tensor_ext.dispatch.tensor<readonly:tensor<?x?x16x1xf32>>{%39, %36}
%42 = hal.interface.binding.subspan layout(#pipeline_layout) binding(1) alignment(64) offset(%c0) flags("ReadOnly|Indirect") : !iree_tensor_ext.dispatch.tensor<readonly:tensor<?x?x16x1xf32>>{%37, %40}
%43 = hal.interface.binding.subspan layout(#pipeline_layout) binding(2) alignment(64) offset(%c0) flags("ReadOnly|Indirect") : !iree_tensor_ext.dispatch.tensor<readonly:tensor<?x16xf32>>{%38}
%44 = hal.interface.binding.subspan layout(#pipeline_layout) binding(3) alignment(64) offset(%c0) flags(Indirect) : !iree_tensor_ext.dispatch.tensor<writeonly:tensor<?x?x16x16xf32>>{%39, %40}
%45 = iree_tensor_ext.dispatch.tensor.load %41, offsets = [0, 0, 0, 0], sizes = [%39, %36, 16, 1], strides = [1, 1, 1, 1] : !iree_tensor_ext.dispatch.tensor<readonly:tensor<?x?x16x1xf32>>{%39, %36} -> tensor<?x?x16x1xf32>
%46 = iree_tensor_ext.dispatch.tensor.load %42, offsets = [0, 0, 0, 0], sizes = [%37, %40, 16, 1], strides = [1, 1, 1, 1] : !iree_tensor_ext.dispatch.tensor<readonly:tensor<?x?x16x1xf32>>{%37, %40} -> tensor<?x?x16x1xf32>
%47 = iree_tensor_ext.dispatch.tensor.load %43, offsets = [0, 0], sizes = [%38, 16], strides = [1, 1] : !iree_tensor_ext.dispatch.tensor<readonly:tensor<?x16xf32>>{%38} -> tensor<?x16xf32>
%48 = tensor.empty(%39, %40) : tensor<?x?x16x16xf32>
%49 = linalg.fill ins(%cst : f32) outs(%48 : tensor<?x?x16x16xf32>) -> tensor<?x?x16x16xf32>
%50 = linalg.mmt4d ins(%45, %46 : tensor<?x?x16x1xf32>, tensor<?x?x16x1xf32>) outs(%49 : tensor<?x?x16x16xf32>) -> tensor<?x?x16x16xf32>
%51 = linalg.generic {indexing_maps = [#map, #map1, #map], iterator_types = ["parallel", "parallel", "parallel", "parallel"]} ins(%50, %47 : tensor<?x?x16x16xf32>, tensor<?x16xf32>) outs(%48 : tensor<?x?x16x16xf32>) {
^bb0(%in: f32, %in_0: f32, %out: f32):
%52 = arith.addf %in, %in_0 : f32
%53 = arith.maximumf %52, %cst : f32
linalg.yield %53 : f32
} -> tensor<?x?x16x16xf32>
iree_tensor_ext.dispatch.tensor.store %51, %44, offsets = [0, 0, 0, 0], sizes = [%39, %40, 16, 16], strides = [1, 1, 1, 1] : tensor<?x?x16x16xf32> -> !iree_tensor_ext.dispatch.tensor<writeonly:tensor<?x?x16x16xf32>>{%39, %40}
return
}
}
There is a path showing that mmt4d fusion codegen does not need any stack allocation, when we use TileAndFuse path with preset configuration.
Replace the path with TileAndFuse in mmt4d pipeline, and run iree-opt --pass-pipeline='builtin.module(func.func(iree-llvmcpu-lower-executable-target))' ~/repro2.mlir
#config = #iree_codegen.lowering_config<tile_sizes = [[1, 2, 0, 0, 0, 0], [1, 1, 0, 16, 16, 0], [0, 0, 1, 0, 0, 1]]>
#config1 = #iree_codegen.lowering_config<tile_sizes = [[1, 2, 0, 0], [1, 1, 16, 16], [0, 0, 0, 0], [0, 0, 0, 0]]>
#executable_target_embedded_elf_x86_64 = #hal.executable.target<"llvm-cpu", "embedded-elf-x86_64", {cpu = "generic", cpu_features = "", data_layout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128", iree.encoding.resolver = #iree_cpu.cpu_encoding_layout<>, max_stack_allocation_size = 32768 : i64, native_vector_size = 16 : i64, target_triple = "x86_64-unknown-unknown-eabi-elf", ukernels = "none"}>
#map = affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>
#map1 = affine_map<(d0, d1, d2, d3) -> (d0, d2)>
#pipeline_layout = #hal.pipeline.layout<constants = 10, bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>
#translation = #iree_codegen.translation_info<pipeline = Mmt4dTilingExpert>
module {
func.func @mmt4d_bias_relu_fusion_dispatch_0_generic_DxDx16x16_f32() attributes {hal.executable.target = #executable_target_embedded_elf_x86_64, translation_info = #translation} {
%c0 = arith.constant 0 : index
%c32_i64 = arith.constant 32 : i64
%cst = arith.constant 0.000000e+00 : f32
%0 = hal.interface.constant.load layout(#pipeline_layout) ordinal(0) : i32
%1 = hal.interface.constant.load layout(#pipeline_layout) ordinal(1) : i32
%2 = hal.interface.constant.load layout(#pipeline_layout) ordinal(2) : i32
%3 = hal.interface.constant.load layout(#pipeline_layout) ordinal(3) : i32
%4 = hal.interface.constant.load layout(#pipeline_layout) ordinal(4) : i32
%5 = hal.interface.constant.load layout(#pipeline_layout) ordinal(5) : i32
%6 = hal.interface.constant.load layout(#pipeline_layout) ordinal(6) : i32
%7 = hal.interface.constant.load layout(#pipeline_layout) ordinal(7) : i32
%8 = hal.interface.constant.load layout(#pipeline_layout) ordinal(8) : i32
%9 = hal.interface.constant.load layout(#pipeline_layout) ordinal(9) : i32
%10 = arith.extui %0 : i32 to i64
%11 = arith.extui %1 : i32 to i64
%12 = arith.shli %11, %c32_i64 : i64
%13 = arith.ori %10, %12 : i64
%14 = arith.index_castui %13 : i64 to index
%15 = arith.extui %2 : i32 to i64
%16 = arith.extui %3 : i32 to i64
%17 = arith.shli %16, %c32_i64 : i64
%18 = arith.ori %15, %17 : i64
%19 = arith.index_castui %18 : i64 to index
%20 = arith.extui %4 : i32 to i64
%21 = arith.extui %5 : i32 to i64
%22 = arith.shli %21, %c32_i64 : i64
%23 = arith.ori %20, %22 : i64
%24 = arith.index_castui %23 : i64 to index
%25 = arith.extui %6 : i32 to i64
%26 = arith.extui %7 : i32 to i64
%27 = arith.shli %26, %c32_i64 : i64
%28 = arith.ori %25, %27 : i64
%29 = arith.index_castui %28 : i64 to index
%30 = arith.extui %8 : i32 to i64
%31 = arith.extui %9 : i32 to i64
%32 = arith.shli %31, %c32_i64 : i64
%33 = arith.ori %30, %32 : i64
%34 = arith.index_castui %33 : i64 to index
%35 = iree_tensor_ext.dispatch.workload.ordinal %14, 0 : index
%36 = iree_tensor_ext.dispatch.workload.ordinal %19, 1 : index
%37 = iree_tensor_ext.dispatch.workload.ordinal %24, 2 : index
%38 = iree_tensor_ext.dispatch.workload.ordinal %29, 3 : index
%39 = iree_tensor_ext.dispatch.workload.ordinal %34, 4 : index
%40 = hal.interface.binding.subspan layout(#pipeline_layout) binding(0) alignment(64) offset(%c0) flags(ReadOnly) : !iree_tensor_ext.dispatch.tensor<readonly:tensor<?x?x16x1xf32>>{%38, %35}
%41 = hal.interface.binding.subspan layout(#pipeline_layout) binding(1) alignment(64) offset(%c0) flags(ReadOnly) : !iree_tensor_ext.dispatch.tensor<readonly:tensor<?x?x16x1xf32>>{%39, %36}
%42 = hal.interface.binding.subspan layout(#pipeline_layout) binding(2) alignment(64) offset(%c0) flags(ReadOnly) : !iree_tensor_ext.dispatch.tensor<readonly:tensor<?x16xf32>>{%37}
%43 = hal.interface.binding.subspan layout(#pipeline_layout) binding(3) alignment(64) offset(%c0) : !iree_tensor_ext.dispatch.tensor<writeonly:tensor<?x?x16x16xf32>>{%38, %39}
%44 = iree_tensor_ext.dispatch.tensor.load %40, offsets = [0, 0, 0, 0], sizes = [%38, %35, 16, 1], strides = [1, 1, 1, 1] : !iree_tensor_ext.dispatch.tensor<readonly:tensor<?x?x16x1xf32>>{%38, %35} -> tensor<?x?x16x1xf32>
%45 = iree_tensor_ext.dispatch.tensor.load %41, offsets = [0, 0, 0, 0], sizes = [%39, %36, 16, 1], strides = [1, 1, 1, 1] : !iree_tensor_ext.dispatch.tensor<readonly:tensor<?x?x16x1xf32>>{%39, %36} -> tensor<?x?x16x1xf32>
%46 = iree_tensor_ext.dispatch.tensor.load %42, offsets = [0, 0], sizes = [%37, 16], strides = [1, 1] : !iree_tensor_ext.dispatch.tensor<readonly:tensor<?x16xf32>>{%37} -> tensor<?x16xf32>
%47 = tensor.empty(%38, %39) : tensor<?x?x16x16xf32>
%48 = linalg.fill ins(%cst : f32) outs(%47 : tensor<?x?x16x16xf32>) -> tensor<?x?x16x16xf32>
%49 = linalg.mmt4d {lowering_config = #config} ins(%44, %45 : tensor<?x?x16x1xf32>, tensor<?x?x16x1xf32>) outs(%48 : tensor<?x?x16x16xf32>) -> tensor<?x?x16x16xf32>
%50 = linalg.generic {indexing_maps = [#map, #map1, #map], iterator_types = ["parallel", "parallel", "parallel", "parallel"]} ins(%49, %46 : tensor<?x?x16x16xf32>, tensor<?x16xf32>) outs(%47 : tensor<?x?x16x16xf32>) attrs = {lowering_config = #config1} {
^bb0(%in: f32, %in_0: f32, %out: f32):
%51 = arith.addf %in, %in_0 : f32
%52 = arith.maximumf %51, %cst : f32
linalg.yield %52 : f32
} -> tensor<?x?x16x16xf32>
iree_tensor_ext.dispatch.tensor.store %50, %43, offsets = [0, 0, 0, 0], sizes = [%38, %39, 16, 16], strides = [1, 1, 1, 1] : tensor<?x?x16x16xf32> -> !iree_tensor_ext.dispatch.tensor<writeonly:tensor<?x?x16x16xf32>>{%38, %39}
return
}
}
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 45 (45 by maintainers)
Commits related to this issue
- [Codegen][NFC] Create tiling utilities file This PR is part of a larger change moving `LLVMCPUTileRootAndFuseProducerConsumer`'s tiling of parallel dimensions to use `scf.forall` rather than `scf.for... — committed to AaronStGeorge/iree by AaronStGeorge a year ago
- [Codegen] Create `scf.forall` -> `scf.for` pass This PR is part of a larger change moving `LLVMCPUTileRootAndFuseProducerConsumer`'s tiling of parallel dimensions to use `scf.forall` rather than `scf... — committed to AaronStGeorge/iree by AaronStGeorge a year ago
- [Codegen][NFC] Create tiling utilities file This PR is part of a larger change moving `LLVMCPUTileRootAndFuseProducerConsumer`'s tiling of parallel dimensions to use `scf.forall` rather than `scf.for... — committed to AaronStGeorge/iree by AaronStGeorge a year ago
- [CPU] Add option to `LLVMCPUTileRootAndFuseProducerConsumer` for tiling with `scf.forall` This PR is part of a larger change moving `LLVMCPUTileRootAndFuseProducerConsumer`'s tiling of parallel dimen... — committed to AaronStGeorge/iree by AaronStGeorge a year ago
- [CPU] Add option to `LLVMCPUTileRootAndFuseProducerConsumer` for tiling with `scf.forall` This PR is part of a larger change moving `LLVMCPUTileRootAndFuseProducerConsumer`'s tiling of parallel dimen... — committed to AaronStGeorge/iree by AaronStGeorge a year ago
- [CPU] Add option to `LLVMCPUTileRootAndFuseProducerConsumer` for tiling with `scf.forall` This PR is part of a larger change moving `LLVMCPUTileRootAndFuseProducerConsumer`'s tiling of parallel dimen... — committed to AaronStGeorge/iree by AaronStGeorge a year ago
- [CPU] Add option to `LLVMCPUTileRootAndFuseProducerConsumer` for tiling with `scf.forall` This PR is part of a larger change moving `LLVMCPUTileRootAndFuseProducerConsumer`'s tiling of parallel dimen... — committed to AaronStGeorge/iree by AaronStGeorge a year ago
- [CPU] Use option to tile with `scf.forall` in TileRootAndFuseProducerConsumer pass This PR is part of a larger change moving LLVMCPUTileRootAndFuseProducerConsumer's tiling of parallel dimensions to ... — committed to AaronStGeorge/iree by AaronStGeorge a year ago
- [Codegen][NFC] Create tiling utilities file (#20961) This PR is part of a larger change moving `LLVMCPUTileRootAndFuseProducerConsumer`'s tiling of parallel dimensions to use `scf.forall` rather than... — committed to iree-org/iree by AaronStGeorge a year ago
- [Codegen] Create `scf.forall` -> `scf.for` pass (#20962) This PR is part of a larger change moving `LLVMCPUTileRootAndFuseProducerConsumer`'s tiling of parallel dimensions to use `scf.forall` rather ... — committed to iree-org/iree by AaronStGeorge a year ago
- [LLVMCPU] Add option to `LLVMCPUTileRootAndFuseProducerConsumer` for tiling with `scf.forall` This PR is part of a larger change moving `LLVMCPUTileRootAndFuseProducerConsumer`'s tiling of parallel d... — committed to AaronStGeorge/iree by AaronStGeorge a year ago
- [LLVMCPU] Add option to `LLVMCPUTileRootAndFuseProducerConsumer` for tiling with `scf.forall` This PR is part of a larger change moving `LLVMCPUTileRootAndFuseProducerConsumer`'s tiling of parallel d... — committed to AaronStGeorge/iree by AaronStGeorge a year ago
- [CPU] Use option to tile with `scf.forall` in TileRootAndFuseProducerConsumer pass This PR is part of a larger change moving LLVMCPUTileRootAndFuseProducerConsumer's tiling of parallel dimensions to ... — committed to AaronStGeorge/iree by AaronStGeorge a year ago
- [CPU] Use option to tile with `scf.forall` in TileRootAndFuseProducerConsumer pass This PR is part of a larger change moving LLVMCPUTileRootAndFuseProducerConsumer's tiling of parallel dimensions to ... — committed to AaronStGeorge/iree by AaronStGeorge a year ago
- [LLVMCPU] Add option to `LLVMCPUTileRootAndFuseProducerConsumer` for tiling with `scf.forall` This PR is part of a larger change moving `LLVMCPUTileRootAndFuseProducerConsumer`'s tiling of parallel d... — committed to AaronStGeorge/iree by AaronStGeorge a year ago
- [CPU] Use option to tile with `scf.forall` in TileRootAndFuseProducerConsumer pass This PR is part of a larger change moving LLVMCPUTileRootAndFuseProducerConsumer's tiling of parallel dimensions to ... — committed to AaronStGeorge/iree by AaronStGeorge a year ago
- [CPU] Add option to `LLVMCPUTileRootAndFuseProducerConsumer` to tiling with `scf.forall` (#21009) This PR is part of a larger change moving `LLVMCPUTileRootAndFuseProducerConsumer`'s tiling of parall... — committed to iree-org/iree by AaronStGeorge a year ago
- [CPU] Use option to tile with `scf.forall` in TileRootAndFuseProducerConsumer pass This PR is part of a larger change moving LLVMCPUTileRootAndFuseProducerConsumer's tiling of parallel dimensions to ... — committed to AaronStGeorge/iree by AaronStGeorge a year ago
- [CPU] Use option to tile with `scf.forall` in TileRootAndFuseProducerConsumer pass (#21198) This PR is part of a larger change moving LLVMCPUTileRootAndFuseProducerConsumer's tiling of parallel dimen... — committed to iree-org/iree by AaronStGeorge a year ago
No, I’m not talking about moving ops around. Typically what tiling does is using extract_slice ops to get slices and pass them to compute ops. I just didn’t see that when I skimmed through the IR, but I think I missed the reduction loop in the beginning. So I was wrong on it.
The IR looks fine to me, and I think we just need to remove the parallel_insert_slice op from the IR. How to achieve it is replacing it with the insert_slice op. (I don’t know how the current insert_slice op is generated in the example, so I just ignore it now.) Does it make sense?
We’ll need it for all the pipelines, IMO.
I think it is just what’s happening today… I don’t know what the plan for GPU, and it may take much more time if they are catching up the same strategy.
@pashu123 I just added the tiling options change, nothing else.