Skip to content

[CPU] CPU backend produces unfused IR in mmt4d->unpack->elem dispatch #20786

Closed

Description

@hanhanW

This is the issue for tracking CPU data-tiling fusion work at operation level. There could be several subtasks that are not yet expanded in the issue.

To repro: iree-compile --iree-hal-target-backends=llvm-cpu --iree-llvmcpu-target-cpu=generic ~/z.mlir -o /tmp/z.vmfb --iree-opt-data-tiling=false --iree-dispatch-creation-experimental-data-tiling

func.func @foo(%lhs: tensor<?x?xf32>, %rhs: tensor<?x?xf32>) -> tensor<?x?xf32> {
  %c0 = arith.constant 0 : index
  %c1 = arith.constant 1 : index
  %M = tensor.dim %lhs, %c0 : tensor<?x?xf32>
  %N = tensor.dim %rhs, %c1 : tensor<?x?xf32>
  %cst = arith.constant 0.0 : f32
  %init = tensor.empty(%M, %N) : tensor<?x?xf32>
  %fill = linalg.fill ins(%cst : f32) outs(%init : tensor<?x?xf32>) -> tensor<?x?xf32>
  %op = linalg.matmul
      ins(%lhs, %rhs : tensor<?x?xf32>, tensor<?x?xf32>)
      outs(%fill : tensor<?x?xf32>) -> tensor<?x?xf32>

  %init2 = tensor.empty(%M, %N) : tensor<?x?xf32>
  %add = linalg.generic {
      indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>,
                       affine_map<(d0, d1) -> (d0, d1)>],
      iterator_types = ["parallel", "parallel"]}
      ins(%op : tensor<?x?xf32>) outs(%init2 : tensor<?x?xf32>) {
    ^bb0(%in: f32, %out: f32):
      %12 = arith.addf %in, %in : f32
      linalg.yield %12 : f32
    } -> tensor<?x?xf32>

  return %add : tensor<?x?xf32>
}

Activity

  1. added
    codegen/llvm
    on May 12, 2025
  2. hanhanW commented on May 12, 2025

    @hanhanW
    ContributorAuthor

    IR dump: https://gist.github.com/hanhanW/c76837b22276d8acfe760ba968e06c45

    The distribution using scf.forall creates extract_slice between unpack and generic op. It may be the reason that generic op is not fused in later parallel loops tileAndFuse.

    func.func @foo_dispatch_0_matmul_DxDxD_f32() attributes {translation_info = #iree_codegen.translation_info<pipeline = Mmt4dTilingExpert>} {
      %c8 = arith.constant 8 : index
      %c0 = arith.constant 0 : index
      %c32_i64 = arith.constant 32 : i64
      %cst = arith.constant 0.000000e+00 : f32
      %0 = hal.interface.constant.load layout(<constants = 10, bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) ordinal(0) : i32
      %1 = hal.interface.constant.load layout(<constants = 10, bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) ordinal(1) : i32
      %2 = hal.interface.constant.load layout(<constants = 10, bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) ordinal(2) : i32
      %3 = hal.interface.constant.load layout(<constants = 10, bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) ordinal(3) : i32
      %4 = hal.interface.constant.load layout(<constants = 10, bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) ordinal(4) : i32
      %5 = hal.interface.constant.load layout(<constants = 10, bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) ordinal(5) : i32
      %6 = hal.interface.constant.load layout(<constants = 10, bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) ordinal(6) : i32
      %7 = hal.interface.constant.load layout(<constants = 10, bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) ordinal(7) : i32
      %8 = hal.interface.constant.load layout(<constants = 10, bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) ordinal(8) : i32
      %9 = hal.interface.constant.load layout(<constants = 10, bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) 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 {stream.alignment = 64 : index} : 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<udiv = 64>, 
          %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#1, 0 : index
      %37 = iree_tensor_ext.dispatch.workload.ordinal %35#2, 1 : index
      %38 = iree_tensor_ext.dispatch.workload.ordinal %35#3, 2 : index
      %39 = iree_tensor_ext.dispatch.workload.ordinal %35#4, 3 : index
      %40 = affine.apply affine_map<()[s0] -> (s0 ceildiv 8)>()[%36]
      %41 = hal.interface.binding.subspan layout(<constants = 10, bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) binding(0) alignment(64) offset(%c0) flags("ReadOnly|Indirect") : !iree_tensor_ext.dispatch.tensor<readonly:tensor<?x?x8x1xf32>>{%40, %37}
      %42 = affine.apply affine_map<()[s0] -> (s0 ceildiv 4)>()[%39]
      %43 = hal.interface.binding.subspan layout(<constants = 10, bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) binding(0) alignment(64) offset(%35#0) flags("ReadOnly|Indirect") : !iree_tensor_ext.dispatch.tensor<readonly:tensor<?x?x4x1xf32>>{%42, %38}
      %44 = hal.interface.binding.subspan layout(<constants = 10, bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) binding(1) alignment(64) offset(%c0) flags(Indirect) : !iree_tensor_ext.dispatch.tensor<writeonly:tensor<?x?xf32>>{%36, %39}
      %45 = iree_tensor_ext.dispatch.tensor.load %41, offsets = [0, 0, 0, 0], sizes = [%40, %37, 8, 1], strides = [1, 1, 1, 1] : !iree_tensor_ext.dispatch.tensor<readonly:tensor<?x?x8x1xf32>>{%40, %37} -> tensor<?x?x8x1xf32>
      %46 = iree_tensor_ext.dispatch.tensor.load %43, offsets = [0, 0, 0, 0], sizes = [%42, %38, 4, 1], strides = [1, 1, 1, 1] : !iree_tensor_ext.dispatch.tensor<readonly:tensor<?x?x4x1xf32>>{%42, %38} -> tensor<?x?x4x1xf32>
      %47 = tensor.empty(%36, %39) : tensor<?x?xf32>
      %48 = scf.forall (%arg0, %arg1) = (0, 0) to (%36, %39) step (2, 4) shared_outs(%arg2 = %47) -> (tensor<?x?xf32>) {
        %49 = affine.min affine_map<(d0)[s0] -> (-d0 + s0, 2)>(%arg0)[%36]
        %50 = affine.min affine_map<(d0)[s0] -> (-d0 + s0, 4)>(%arg1)[%39]
        %51 = affine.apply affine_map<(d0) -> (d0 mod 8)>(%arg0)
        %52 = affine.apply affine_map<(d0, d1) -> ((d0 + d1 - 1) floordiv 8 - d0 floordiv 8 + 1)>(%arg0, %49)
        %53 = arith.muli %52, %c8 : index
        %54 = affine.apply affine_map<(d0) -> (d0 floordiv 8)>(%arg0)
        %55 = affine.apply affine_map<(d0) -> (d0 floordiv 4)>(%arg1)
        %extracted_slice = tensor.extract_slice %45[%54, 0, 0, 0] [%52, %37, 8, 1] [1, 1, 1, 1] : tensor<?x?x8x1xf32> to tensor<?x?x8x1xf32>
        %extracted_slice_0 = tensor.extract_slice %46[%55, 0, 0, 0] [1, %37, 4, 1] [1, 1, 1, 1] : tensor<?x?x4x1xf32> to tensor<1x?x4x1xf32>
        %56 = tensor.empty(%52) : tensor<?x1x8x4xf32>
        %57 = linalg.fill {lowering_config = #iree_codegen.lowering_config<tile_sizes = [[2, 4, 0, 0], [1, 1, 0, 8], [0, 0, 0, 0], [0, 0, 0, 0]]>} ins(%cst : f32) outs(%56 : tensor<?x1x8x4xf32>) -> tensor<?x1x8x4xf32>
        %58 = linalg.mmt4d {lowering_config = #iree_codegen.lowering_config<tile_sizes = [[2, 4, 0, 0, 0, 0], [1, 1, 0, 8, 4, 0], [0, 0, 1, 0, 0, 1]]>} ins(%extracted_slice, %extracted_slice_0 : tensor<?x?x8x1xf32>, tensor<1x?x4x1xf32>) outs(%57 : tensor<?x1x8x4xf32>) -> tensor<?x1x8x4xf32>
        %59 = tensor.empty(%53, %50) : tensor<?x?xf32>
        %unpack = linalg.unpack %58 outer_dims_perm = [0, 1] inner_dims_pos = [0, 1] inner_tiles = [8, 4] into %59 {lowering_config = #iree_codegen.lowering_config<tile_sizes = [[2, 4], [1, 1], [0, 0], [0, 0]]>} : tensor<?x1x8x4xf32> -> tensor<?x?xf32>
        %extracted_slice_1 = tensor.extract_slice %unpack[%51, 0] [%49, %50] [1, 1] : tensor<?x?xf32> to tensor<?x?xf32>
        %extracted_slice_2 = tensor.extract_slice %arg2[%arg0, %arg1] [%49, %50] [1, 1] : tensor<?x?xf32> to tensor<?x?xf32>
        %60 = linalg.generic {indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0, d1)>], iterator_types = ["parallel", "parallel"]} ins(%extracted_slice_1 : tensor<?x?xf32>) outs(%extracted_slice_2 : tensor<?x?xf32>) attrs =  {lowering_config = #iree_codegen.lowering_config<tile_sizes = [[2, 4], [1, 1], [0, 0], [0, 0]]>} {
        ^bb0(%in: f32, %out: f32):
          %61 = arith.addf %in, %in : f32
          linalg.yield %61 : f32
        } -> tensor<?x?xf32>
        scf.forall.in_parallel {
          tensor.parallel_insert_slice %60 into %arg2[%arg0, %arg1] [%49, %50] [1, 1] : tensor<?x?xf32> into tensor<?x?xf32>
        }
      } {mapping = [#iree_codegen.workgroup_mapping<y>, #iree_codegen.workgroup_mapping<x>]}
      iree_tensor_ext.dispatch.tensor.store %48, %44, offsets = [0, 0], sizes = [%36, %39], strides = [1, 1] : tensor<?x?xf32> -> !iree_tensor_ext.dispatch.tensor<writeonly:tensor<?x?xf32>>{%36, %39}
      return
    }
  3. hanhanW commented on May 15, 2025

    @hanhanW
    ContributorAuthor

    I haven't looked at details yet, but I want to put a note here. According to @pashu123 , #19821 may fix the issue. I'll revisit this when I have cycles.

  4. egebeysel commented on May 20, 2025

    @egebeysel
    Contributor

    The reason that extra extract_slice op is there is because of the tile sizes on the generic op. The tile sizes of the generic op does not take into account the layout transformation by the unpack op in the middle. Therefore, although you've computed a result of shape [M0 x m1, N0 x n1] where [m1, n1] are your inner tile sizes, you try to extract a [M0, N0] slice from it and therefore the overall forall loop calculates that same result m1 * n1 times. For better visualization, I'm attaching an example with static sizes:

    module @module {
      util.global private @__auto.constant_4096_4096_torch.float32 = dense_resource<__auto.constant_4096_4096_torch.float32> : tensor<4096x4096xf32>
      util.global private @__auto.constant_4096_torch.float32 = dense_resource<__auto.constant_4096_torch.float32> : tensor<4096xf32>
      func.func @main(%arg0: !torch.vtensor<[4096,4096],f32>) -> !torch.vtensor<[4096,4096],f32> attributes {torch.assume_strict_symbolic_shapes} {
        %__auto.constant_4096_4096_torch.float32 = util.global.load @__auto.constant_4096_4096_torch.float32 : tensor<4096x4096xf32>
        %0 = torch_c.from_builtin_tensor %__auto.constant_4096_4096_torch.float32 : tensor<4096x4096xf32> -> !torch.vtensor<[4096,4096],f32>
        %__auto.constant_4096_torch.float32 = util.global.load @__auto.constant_4096_torch.float32 : tensor<4096xf32>
        %1 = torch_c.from_builtin_tensor %__auto.constant_4096_torch.float32 : tensor<4096xf32> -> !torch.vtensor<[4096],f32>
        %2 = torch.aten.linear %arg0, %0, %1 : !torch.vtensor<[4096,4096],f32>, !torch.vtensor<[4096,4096],f32>, !torch.vtensor<[4096],f32> -> !torch.vtensor<[4096,4096],f32>
        return %2 : !torch.vtensor<[4096,4096],f32>
      }
    }

    to compile:
    iree-compile --iree-hal-target-backends=llvm-cpu --iree-llvmcpu-target-cpu=generic ~/repro.mlir -o /tmp/linear.vmfb --iree-opt-data-tiling=false --iree-dispatch-creation-experimental-data-tiling --mlir-print-ir-after=iree-codegen-tile-and-distribute-to-workgroups-using-forall-op --compile-to=hal 2> ~/repro_logs.mlir

    You get something like this one:

    // -----// IR Dump After TileAndDistributeToWorkgroupsUsingForallOpPass (iree-codegen-tile-and-distribute-to-workgroups-using-forall-op) //----- //
    func.func @main$async_dispatch_0_matmul_transpose_b_4096x4096x4096_f32() attributes {translation_info = #iree_codegen.translation_info<pipeline = Mmt4dTilingExpert>} {
      %cst = arith.constant 0.000000e+00 : f32
      %c0 = arith.constant 0 : index
      %c67108864 = arith.constant 67108864 : index
      %0 = hal.interface.binding.subspan layout(<bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, ReadOnly>, #hal.pipeline.binding<storage_buffer, ReadOnly>, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) binding(0) alignment(64) offset(%c0) flags("ReadOnly|Indirect") : !iree_tensor_ext.dispatch.tensor<readonly:tensor<512x4096x8x1xf32>>
      %1 = hal.interface.binding.subspan layout(<bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, ReadOnly>, #hal.pipeline.binding<storage_buffer, ReadOnly>, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) binding(1) alignment(64) offset(%c0) flags(ReadOnly) : !iree_tensor_ext.dispatch.tensor<readonly:tensor<1024x4096x4x1xf32>>
      %2 = hal.interface.binding.subspan layout(<bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, ReadOnly>, #hal.pipeline.binding<storage_buffer, ReadOnly>, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) binding(2) alignment(64) offset(%c67108864) flags(ReadOnly) : !iree_tensor_ext.dispatch.tensor<readonly:tensor<4096xf32>>
      %3 = hal.interface.binding.subspan layout(<bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, ReadOnly>, #hal.pipeline.binding<storage_buffer, ReadOnly>, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) binding(3) alignment(64) offset(%c0) flags(Indirect) : !iree_tensor_ext.dispatch.tensor<writeonly:tensor<4096x4096xf32>>
      %4 = iree_tensor_ext.dispatch.tensor.load %0, offsets = [0, 0, 0, 0], sizes = [512, 4096, 8, 1], strides = [1, 1, 1, 1] : !iree_tensor_ext.dispatch.tensor<readonly:tensor<512x4096x8x1xf32>> -> tensor<512x4096x8x1xf32>
      %5 = iree_tensor_ext.dispatch.tensor.load %1, offsets = [0, 0, 0, 0], sizes = [1024, 4096, 4, 1], strides = [1, 1, 1, 1] : !iree_tensor_ext.dispatch.tensor<readonly:tensor<1024x4096x4x1xf32>> -> tensor<1024x4096x4x1xf32>
      %6 = iree_tensor_ext.dispatch.tensor.load %2, offsets = [0], sizes = [4096], strides = [1] : !iree_tensor_ext.dispatch.tensor<readonly:tensor<4096xf32>> -> tensor<4096xf32>
      %7 = tensor.empty() : tensor<4096x4096xf32>
      %8 = scf.forall (%arg0, %arg1) in (4096, 4096) shared_outs(%arg2 = %7) -> (tensor<4096x4096xf32>) {
        %9 = affine.apply affine_map<(d0) -> (d0 mod 8)>(%arg0)
        %10 = affine.apply affine_map<(d0) -> (d0 mod 4)>(%arg1)
        %11 = affine.apply affine_map<(d0) -> (d0 floordiv 8)>(%arg0)
        %12 = affine.apply affine_map<(d0) -> (d0 floordiv 4)>(%arg1)
        %extracted_slice = tensor.extract_slice %4[%11, 0, 0, 0] [1, 4096, 8, 1] [1, 1, 1, 1] : tensor<512x4096x8x1xf32> to tensor<1x4096x8x1xf32>
        %extracted_slice_0 = tensor.extract_slice %5[%12, 0, 0, 0] [1, 4096, 4, 1] [1, 1, 1, 1] : tensor<1024x4096x4x1xf32> to tensor<1x4096x4x1xf32>
        %13 = tensor.empty() : tensor<1x1x8x4xf32>
        %14 = linalg.fill {lowering_config = #iree_codegen.lowering_config<tile_sizes = [[1, 1, 0, 0], [1, 1, 0, 8], [0, 0, 0, 0], [0, 0, 0, 0]]>} ins(%cst : f32) outs(%13 : tensor<1x1x8x4xf32>) -> tensor<1x1x8x4xf32>
        %15 = linalg.mmt4d {lowering_config = #iree_codegen.lowering_config<tile_sizes = [[1, 1, 0, 0, 0, 0], [1, 1, 0, 8, 4, 0], [0, 0, 1, 0, 0, 1]]>} ins(%extracted_slice, %extracted_slice_0 : tensor<1x4096x8x1xf32>, tensor<1x4096x4x1xf32>) outs(%14 : tensor<1x1x8x4xf32>) -> tensor<1x1x8x4xf32>
        %16 = tensor.empty() : tensor<8x4xf32>
        %unpack = linalg.unpack %15 outer_dims_perm = [0, 1] inner_dims_pos = [0, 1] inner_tiles = [8, 4] into %16 {lowering_config = #iree_codegen.lowering_config<tile_sizes = [[1, 1], [1, 1], [0, 0], [0, 0]]>} : tensor<1x1x8x4xf32> -> tensor<8x4xf32>
        %extracted_slice_1 = tensor.extract_slice %unpack[%9, %10] [1, 1] [1, 1] : tensor<8x4xf32> to tensor<1x1xf32>
        %extracted_slice_2 = tensor.extract_slice %6[%arg1] [1] [1] : tensor<4096xf32> to tensor<1xf32>
        %extracted_slice_3 = tensor.extract_slice %arg2[%arg0, %arg1] [1, 1] [1, 1] : tensor<4096x4096xf32> to tensor<1x1xf32>
        %17 = linalg.generic {indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d1)>, affine_map<(d0, d1) -> (d0, d1)>], iterator_types = ["parallel", "parallel"]} ins(%extracted_slice_1, %extracted_slice_2 : tensor<1x1xf32>, tensor<1xf32>) outs(%extracted_slice_3 : tensor<1x1xf32>) attrs =  {lowering_config = #iree_codegen.lowering_config<tile_sizes = [[1, 1], [1, 1], [0, 0], [0, 0]]>} {
        ^bb0(%in: f32, %in_4: f32, %out: f32):
          %18 = arith.addf %in, %in_4 : f32
          linalg.yield %18 : f32
        } -> tensor<1x1xf32>
        scf.forall.in_parallel {
          tensor.parallel_insert_slice %17 into %arg2[%arg0, %arg1] [1, 1] [1, 1] : tensor<1x1xf32> into tensor<4096x4096xf32>
        }
      } {mapping = [#iree_codegen.workgroup_mapping<y>, #iree_codegen.workgroup_mapping<x>]}
      iree_tensor_ext.dispatch.tensor.store %8, %3, offsets = [0, 0], sizes = [4096, 4096], strides = [1, 1] : tensor<4096x4096xf32> -> !iree_tensor_ext.dispatch.tensor<writeonly:tensor<4096x4096xf32>>
      return
    }
    

    As I said, you calculate the same matmul result multiple times. This happens with dynamic sizes in your example as well, it's just harder to see.

    On the other hand, if we were to switch to choosing the root operation and fusing producers+consumers instead of the last compute op and then fusing the producers while tiling forall, it gets proper tiling without duplicated computations. But that one fails compilation with other dispatches, e.g. generic -> pack ones like this one. I think by improving the consumer fusion and then doing the above switch is the way to handle this. I'd like to take a look.

  5. hanhanW commented on May 20, 2025

    @hanhanW
    ContributorAuthor

    I see, thanks for the sharing! The static cases make it easier to think. This is what I'd like to discuss in the data-tiling fusion codegen. In the beginning, I'm thinking that if this is the time to revisit local data-layout propagation. I'd like to avoid this as a first approach because it introduces pack ops when the consumer has other input operands -- and codegen may be struggled about it.

    After taking a second look, I think we can kick in the unpack -> extract_slice folding because the unpack op has slicing semantic. We implemented the FoldUnpackWithExtractSliceOp pattern in upstream, and maybe we can populate it after tiling. This way, we'd have a sequence of compute ops. There are no slicing ops in between.

  6. hanhanW commented on May 21, 2025

    @hanhanW
    ContributorAuthor

    I haven't looked at details yet, but I want to put a note here. According to @pashu123 , #19821 may fix the issue. I'll revisit this when I have cycles.

    @pashu123 please chime in if you have context. You claimed this in a private conversation, and it'd be good if you can provide more context. It is okay if it is not the solution; it'd save others time to look into your PR. Thanks!

  7. pashu123 commented on May 21, 2025

    @pashu123
    Contributor

    I haven't looked at details yet, but I want to put a note here. According to @pashu123 , #19821 may fix the issue. I'll revisit this when I have cycles.

    @pashu123 please chime in if you have context. You claimed this in a private conversation, and it'd be good if you can provide more context. It is okay if it is not the solution; it'd save others time to look into your PR. Thanks!

    Sure, I'll go over the IRs and let you know.

  8. pashu123 commented on May 21, 2025

    @pashu123
    Contributor

    I am experimenting with https://github.com/pashu123/iree/tree/test_cpu, and many mixed issues are floating around, so I'll tackle them one by one.

    1. Let's not discuss {linalg.generic | maxpool} + linalg.pack fusion here. That's the problem of consumer fusion not happening, and we'll have to implement that.
    2. We'll have to select the root operation in distribution tiling as the above branch does. I use the following compile command:
      ~/iree-build/tools/iree-compile --iree-hal-target-backends=llvm-cpu --iree-llvmcpu-target-cpu=generic test2.mlir -o /tmp/linear.vmfb --iree-opt-data-tiling=false --iree-dispatch-creation-experimental-data-tiling --mlir-print-ir-after-all 2> repro_logs.txt
      Here's the mlir: https://gist.github.com/pashu123/7b5b7be862ad49e7dbe85c8ad51ed505
      Here's the dump: https://gist.github.com/pashu123/77be9c734f96c3ddd2e22369863f7cc9
      Looking at the IRs, it does the right thing, as mentioned in the previous comments.

    We are selecting the wrong tile sizes from the unpack op (last op), which the distribution pass uses. Later, tileRootFuseProducerConsumer is confused because it chooses the root op mmt4d in our case. We must be consistent in choosing which op should be selected as the root across the pipeline. I think we discussed this, we can have a flag selectRootOp or selectLastOp in the pass that chooses the root op according to the flag and pipeline.

    #19821 borrows logic from TileDispatchUsingForall, which does (producer + consumer + producer) fusion in TileRootFuseProducerConsumer pass.

  9. hanhanW commented on May 21, 2025

    @hanhanW
    ContributorAuthor

    The log is not easy to read because the IRs are interleaved. Can you update it with using --mlir-disable-threading flag? E.g.,

    // -----// IR Dump After OptimizeTensorInsertExtractSlicesPass (iree-codegen-optimize-tensor-insert-extract-slices) //----- //
    func.func @_encoding_1_encode_DxDxf32_to_DxDxf32() attributes {translation_info = #iree_codegen.translation_info<pipeline = CPUDataTiling>} {
     
    // .... and the next dump is for other dispatch
    
    
    // -----// IR Dump After LLVMCPUTileRootAndFuseProducerConsumerPass (iree-llvmcpu-tile-root-and-fuse-producer-consumer) //----- //
    func.func @foo_dispatch_0_matmul_DxDxD_f32() attributes {translation_info = #iree_codegen.translation_info<pipeline = Mmt4dTilingExpert>} {
    
    

    We must be consistent in choosing which op should be selected as the root across the pipeline.

    I think you meant across the passes? Did you dump the IRs with your branch? ukernel is a different story that we might need a temporary buffer, so we still see stack allocation in the dump. Can you try with disabling ukernels, so we can verify that there are no stack allocations?

    I think we discussed this, we can have a flag selectRootOp or selectLastOp in the pass that chooses the root op according to the flag and pipeline.

    Yes, we discussed this last week, and it is doable. Now I remember the old approach that I discussed with you last year, and could you remind me what the status of that? In the past, we discussed disabling lowering config propagation for the Mmt4D pipeline, so the dispatch can always pick mmt4d op as a root op. This way, we don't need the selectRootOp flag, and we can move the pipeline to do this type of tile and fuse. Did we try that before?

  10. hanhanW commented on May 21, 2025

    @hanhanW
    ContributorAuthor

    #19821 borrows logic from TileDispatchUsingForall, which does (producer + consumer + producer) fusion in TileRootFuseProducerConsumer pass.

    Is it something different from what we've discussed how to choose root op? Is it an improvement for the pass, which solves a different problem?

  11. pashu123 commented on May 21, 2025

    @pashu123
    Contributor

    #19821 borrows logic from TileDispatchUsingForall, which does (producer + consumer + producer) fusion in TileRootFuseProducerConsumer pass.

    Is it something different from what we've discussed how to choose root op? Is it an improvement for the pass, which solves a different problem?

    It solves a different problem.

  12. pashu123 commented on May 21, 2025

    @pashu123
    Contributor

    The log is not easy to read because the IRs are interleaved. Can you update it with using --mlir-disable-threading flag? E.g.,

    // -----// IR Dump After OptimizeTensorInsertExtractSlicesPass (iree-codegen-optimize-tensor-insert-extract-slices) //----- //
    func.func @_encoding_1_encode_DxDxf32_to_DxDxf32() attributes {translation_info = #iree_codegen.translation_info<pipeline = CPUDataTiling>} {
     
    // .... and the next dump is for other dispatch
    
    
    // -----// IR Dump After LLVMCPUTileRootAndFuseProducerConsumerPass (iree-llvmcpu-tile-root-and-fuse-producer-consumer) //----- //
    func.func @foo_dispatch_0_matmul_DxDxD_f32() attributes {translation_info = #iree_codegen.translation_info<pipeline = Mmt4dTilingExpert>} {
    

    We must be consistent in choosing which op should be selected as the root across the pipeline.

    I think you meant across the passes? Did you dump the IRs with your branch? ukernel is a different story that we might need a temporary buffer, so we still see stack allocation in the dump. Can you try with disabling ukernels, so we can verify that there are no stack allocations?

    I think we discussed this, we can have a flag selectRootOp or selectLastOp in the pass that chooses the root op according to the flag and pipeline.

    Yes, we discussed this last week, and it is doable. Now I remember the old approach that I discussed with you last year, and could you remind me what the status of that? In the past, we discussed disabling lowering config propagation for the Mmt4D pipeline, so the dispatch can always pick mmt4d op as a root op. This way, we don't need the selectRootOp flag, and we can move the pipeline to do this type of tile and fuse. Did we try that before?

    No, we didn't try disabling the lowering_config propagation. I wasn't actively working on this and was pulled into different things.

  13. pashu123 commented on May 21, 2025

    @pashu123
    Contributor

    Running with
    ~/iree-build/tools/iree-compile --iree-hal-target-backends=llvm-cpu --iree-llvmcpu-target-cpu=generic test.mlir -o /tmp/linear.vmfb --iree-opt-data-tiling=false -iree-llvmcpu-enable-ukernels=none --iree-dispatch-creation-experimental-data-tiling --mlir-print-ir-after-all 2> repro_logs.txt -mlir-disable-threading
    IR Dump: https://gist.github.com/pashu123/2a35cfb322e0fdd7e71f1564fd9f047a

  14. assigned and unassigned on Jun 27, 2025
  15. added 2 commits that reference this issue on Jul 8, 2025
    41e3b65
    eba406a
  16. added a commit that references this issue on Jul 8, 2025
    1993d5c
  17. added a commit that references this issue on Dec 1, 2025
    98d6be3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

codegen/llvm

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions