Skip to content

LLVM CPU lowering does not handle multiple roots in a dispatch  #10208

Closed

Description

@MaheshRavishankar

With #10177 reduction with the compatible parallel iteration domains get fused into a single dispatch. For example

hal.executable private @multi_root_repro {
  hal.executable.variant public @embedded_elf_x86_64, target = <"llvm-cpu", "embedded-elf-x86_64", {cpu_features = "", data_layout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128", native_vector_size = 16 : index, target_triple = "x86_64-unknown-unknown-eabi-elf"}> {
    hal.executable.export public @_softmax_dispatch_0_generic_12x128x128 ordinal(0) layout(#hal.pipeline.layout<push_constants = 0, sets = [<0, bindings = [<0, storage_buffer, ReadOnly>, <1, storage_buffer, ReadOnly>, <2, storage_buffer>, <3, storage_buffer>]>]>) {
    ^bb0(%arg0: !hal.device, %arg1: index, %arg2: index, %arg3: index):
      %x, %y, %z = flow.dispatch.default_workgroup_count %arg1, %arg2, %arg3
      hal.return %x, %y, %z : index, index, index
    }
    builtin.module {
      func.func @_softmax_dispatch_0_generic_12x128x128() {
        %c0 = arith.constant 0 : index
        %c6144 = arith.constant 6144 : index
        %c792576 = arith.constant 792576 : index
        %cst = arith.constant 0.000000e+00 : f32
        %0 = hal.interface.binding.subspan set(0) binding(0) type(storage_buffer) offset(%c0) alignment(64) : !flow.dispatch.tensor<readonly:12x128x128xf32>
        %1 = hal.interface.binding.subspan set(0) binding(1) type(storage_buffer) offset(%c0) alignment(64) : !flow.dispatch.tensor<readonly:12x128xf32>
        %2 = hal.interface.binding.subspan set(0) binding(2) type(storage_buffer) offset(%c6144) alignment(64) : !flow.dispatch.tensor<writeonly:12x128x128xf32>
        %3 = hal.interface.binding.subspan set(0) binding(3) type(storage_buffer) offset(%c792576) alignment(64) : !flow.dispatch.tensor<writeonly:12x128xf32>
        %4 = flow.dispatch.tensor.load %0, offsets = [0, 0, 0], sizes = [12, 128, 128], strides = [1, 1, 1] : !flow.dispatch.tensor<readonly:12x128x128xf32> -> tensor<12x128x128xf32>
        %5 = flow.dispatch.tensor.load %1, offsets = [0, 0], sizes = [12, 128], strides = [1, 1] : !flow.dispatch.tensor<readonly:12x128xf32> -> tensor<12x128xf32>
        %6 = linalg.init_tensor [12, 128, 128] : tensor<12x128x128xf32>
        %7 = linalg.init_tensor [12, 128] : tensor<12x128xf32>
        %8 = linalg.fill ins(%cst : f32) outs(%7 : tensor<12x128xf32>) -> tensor<12x128xf32>
        %9 = linalg.generic {indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>], iterator_types = ["parallel", "parallel", "reduction"]} ins(%4 : tensor<12x128x128xf32>) outs(%5 : tensor<12x128xf32>) {
        ^bb0(%arg0: f32, %arg1: f32):
          %11 = arith.cmpf ogt, %arg0, %arg1 : f32
          %12 = arith.select %11, %arg0, %arg1 : f32
          linalg.yield %12 : f32
        } -> tensor<12x128xf32>
        %10:2 = linalg.generic {indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>, affine_map<(d0, d1, d2) -> (d0, d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>], iterator_types = ["parallel", "parallel", "reduction"]} ins(%4, %9 : tensor<12x128x128xf32>, tensor<12x128xf32>) outs(%6, %8 : tensor<12x128x128xf32>, tensor<12x128xf32>) {
        ^bb0(%arg0: f32, %arg1: f32, %arg2: f32, %arg3: f32):
          %11 = arith.subf %arg0, %arg1 : f32
          %12 = math.exp %11 : f32
          %13 = arith.addf %12, %arg3 : f32
          linalg.yield %12, %13 : f32, f32
        } -> (tensor<12x128x128xf32>, tensor<12x128xf32>)
        flow.dispatch.tensor.store %10#0, %2, offsets = [0, 0, 0], sizes = [12, 128, 128], strides = [1, 1, 1] : tensor<12x128x128xf32> -> !flow.dispatch.tensor<writeonly:12x128x128xf32>
        flow.dispatch.tensor.store %10#1, %3, offsets = [0, 0], sizes = [12, 128], strides = [1, 1] : tensor<12x128xf32> -> !flow.dispatch.tensor<writeonly:12x128xf32>
        return
      }
    }
  }
}

This has two linalg.generic operation with "reduction" iterator types. Running this with

iree-opt -pass-pipeline="hal.executable(hal.executable.variant(iree-llvmcpu-lower-executable-target))" multi_root_repro.mlir

fails with the following error

multi_root_repro.mlir:29:15: error: 'linalg.generic' op unhandled multiple root operations in dispatch region
      %10:2 = linalg.generic {indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>, affine_map<(d0, d1, d2) -> (d0, d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>], iterator_types = ["parallel", "parallel", "reduction"]} ins(%4, %9 : tensor<12x128x128xf32>, tensor<12x128xf32>) outs(%6, %8 : tensor<12x128x128xf32>, tensor<12x128xf32>) {
              ^
multi_root_repro.mlir:29:15: note: see current operation: %14:2 = "linalg.generic"(%8, %13, %10, %12) ({
^bb0(%arg0: f32, %arg1: f32, %arg2: f32, %arg3: f32):
  %15 = "arith.subf"(%arg0, %arg1) : (f32, f32) -> f32
  %16 = "math.exp"(%15) : (f32) -> f32
  %17 = "arith.addf"(%16, %arg3) : (f32, f32) -> f32
  "linalg.yield"(%16, %17) : (f32, f32) -> ()
}) {indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>, affine_map<(d0, d1, d2) -> (d0, d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>], iterator_types = ["parallel", "parallel", "reduction"], operand_segment_sizes = array<i32: 2, 2>} : (tensor<12x128x128xf32>, tensor<12x128xf32>, tensor<12x128x128xf32>, tensor<12x128xf32>) -> (tensor<12x128x128xf32>, tensor<12x128xf32>)

This should be supportable. This happens on other backends too, but starting with CPU backend for now.

Activity

  1. added
    codegen/llvm
    on Aug 25, 2022
  2. MaheshRavishankar commented on Aug 25, 2022

    @MaheshRavishankar
    CollaboratorAuthor

    PR #10210 gets past the above error. This is the dump of IR before/after all with the iree-opt command in the first comment (https://gist.github.com/MaheshRavishankar/62954bf5ea0eadfa7673002bff4f5125)

    Issue now seems to be the op isnt vectorized

    // -----// IR Dump Before LinalgStrategyVectorizePass (linalg-strategy-vectorize-pass) //----- //
    func.func @_softmax_dispatch_0_generic_12x128x128() {
      %c1 = arith.constant 1 : index
      %c4 = arith.constant 4 : index
      %c32 = arith.constant 32 : index
      %c12 = arith.constant 12 : index
      %c128 = arith.constant 128 : index
      %c0 = arith.constant 0 : index
      %c6144 = arith.constant 6144 : index
      %c792576 = arith.constant 792576 : index
      %cst = arith.constant 0.000000e+00 : f32
      %0 = hal.interface.binding.subspan set(0) binding(0) type(storage_buffer) offset(%c0) alignment(64) : !flow.dispatch.tensor<readonly:12x128x128xf32>
      %1 = hal.interface.binding.subspan set(0) binding(1) type(storage_buffer) offset(%c0) alignment(64) : !flow.dispatch.tensor<readonly:12x128xf32>
      %2 = hal.interface.binding.subspan set(0) binding(2) type(storage_buffer) offset(%c6144) alignment(64) : !flow.dispatch.tensor<writeonly:12x128x128xf32>
      %3 = hal.interface.binding.subspan set(0) binding(3) type(storage_buffer) offset(%c792576) alignment(64) : !flow.dispatch.tensor<writeonly:12x128xf32>
      %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
      %4 = affine.apply affine_map<()[s0] -> (s0 * 4)>()[%workgroup_id_y]
      %5 = affine.apply affine_map<()[s0] -> (s0 * 4)>()[%workgroup_count_y]
      %6 = affine.apply affine_map<()[s0] -> (s0 * 32)>()[%workgroup_id_x]
      %7 = affine.apply affine_map<()[s0] -> (s0 * 32)>()[%workgroup_count_x]
      scf.for %arg0 = %4 to %c12 step %5 {
        scf.for %arg1 = %6 to %c128 step %7 {
          %8 = flow.dispatch.tensor.load %2, offsets = [%arg0, %arg1, 0], sizes = [4, 32, 128], strides = [1, 1, 1] : !flow.dispatch.tensor<writeonly:12x128x128xf32> -> tensor<4x32x128xf32>
          %9 = flow.dispatch.tensor.load %3, offsets = [%arg0, %arg1], sizes = [4, 32], strides = [1, 1] : !flow.dispatch.tensor<writeonly:12x128xf32> -> tensor<4x32xf32>
          %10 = flow.dispatch.tensor.load %0, offsets = [%arg0, %arg1, 0], sizes = [4, 32, 128], strides = [1, 1, 1] : !flow.dispatch.tensor<readonly:12x128x128xf32> -> tensor<4x32x128xf32>
          %11 = flow.dispatch.tensor.load %1, offsets = [%arg0, %arg1], sizes = [4, 32], strides = [1, 1] : !flow.dispatch.tensor<readonly:12x128xf32> -> tensor<4x32xf32>
          %12:2 = scf.for %arg2 = %c0 to %c4 step %c1 iter_args(%arg3 = %8, %arg4 = %9) -> (tensor<4x32x128xf32>, tensor<4x32xf32>) {
            %13:2 = scf.for %arg5 = %c0 to %c32 step %c4 iter_args(%arg6 = %arg3, %arg7 = %arg4) -> (tensor<4x32x128xf32>, tensor<4x32xf32>) {
              %14 = tensor.extract_slice %11[%arg2, %arg5] [1, 4] [1, 1] : tensor<4x32xf32> to tensor<1x4xf32>
              %15 = scf.for %arg8 = %c0 to %c128 step %c4 iter_args(%arg9 = %14) -> (tensor<1x4xf32>) {
                %22 = tensor.extract_slice %10[%arg2, %arg5, %arg8] [1, 4, 4] [1, 1, 1] : tensor<4x32x128xf32> to tensor<1x4x4xf32>
                %23 = linalg.generic {indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>], iterator_types = ["parallel", "parallel", "reduction"]} ins(%22 : tensor<1x4x4xf32>) outs(%arg9 : tensor<1x4xf32>) {
                ^bb0(%arg10: f32, %arg11: f32):
                  %24 = arith.cmpf ogt, %arg10, %arg11 : f32
                  %25 = arith.select %24, %arg10, %arg11 : f32
                  linalg.yield %25 : f32
                } -> tensor<1x4xf32>
                scf.yield %23 : tensor<1x4xf32>
              }
              %16 = tensor.extract_slice %arg6[%arg2, %arg5, 0] [1, 4, 128] [1, 1, 1] : tensor<4x32x128xf32> to tensor<1x4x128xf32>
              %17 = tensor.extract_slice %arg7[%arg2, %arg5] [1, 4] [1, 1] : tensor<4x32xf32> to tensor<1x4xf32>
              %18 = linalg.fill ins(%cst : f32) outs(%17 : tensor<1x4xf32>) -> tensor<1x4xf32>
              %19:2 = scf.for %arg8 = %c0 to %c128 step %c4 iter_args(%arg9 = %16, %arg10 = %18) -> (tensor<1x4x128xf32>, tensor<1x4xf32>) {
                %22 = tensor.extract_slice %10[%arg2, %arg5, %arg8] [1, 4, 4] [1, 1, 1] : tensor<4x32x128xf32> to tensor<1x4x4xf32>
                %23 = tensor.extract_slice %arg9[0, 0, %arg8] [1, 4, 4] [1, 1, 1] : tensor<1x4x128xf32> to tensor<1x4x4xf32>
                %24:2 = linalg.generic {indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>, affine_map<(d0, d1, d2) -> (d0, d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>], iterator_types = ["parallel", "parallel", "reduction"]} ins(%22, %15 : tensor<1x4x4xf32>, tensor<1x4xf32>) outs(%23, %arg10 : tensor<1x4x4xf32>, tensor<1x4xf32>) attrs =  {lowering_config = #iree\
    _codegen.lowering_config<tile_sizes = [[4, 32, 0], [1, 4, 0], [0, 0, 4]]>} {
                ^bb0(%arg11: f32, %arg12: f32, %arg13: f32, %arg14: f32):
                  %26 = arith.subf %arg11, %arg12 : f32
                  %27 = math.exp %26 : f32
                  %28 = arith.addf %27, %arg14 : f32
                  linalg.yield %27, %28 : f32, f32
                } -> (tensor<1x4x4xf32>, tensor<1x4xf32>)
                %25 = tensor.insert_slice %24#0 into %arg9[0, 0, %arg8] [1, 4, 4] [1, 1, 1] : tensor<1x4x4xf32> into tensor<1x4x128xf32>
                scf.yield %25, %24#1 : tensor<1x4x128xf32>, tensor<1x4xf32>
              }
              %20 = tensor.insert_slice %19#0 into %arg6[%arg2, %arg5, 0] [1, 4, 128] [1, 1, 1] : tensor<1x4x128xf32> into tensor<4x32x128xf32>
              %21 = tensor.insert_slice %19#1 into %arg7[%arg2, %arg5] [1, 4] [1, 1] : tensor<1x4xf32> into tensor<4x32xf32>
              scf.yield %20, %21 : tensor<4x32x128xf32>, tensor<4x32xf32>
            }
            scf.yield %13#0, %13#1 : tensor<4x32x128xf32>, tensor<4x32xf32>
          }
          flow.dispatch.tensor.store %12#0, %2, offsets = [%arg0, %arg1, 0], sizes = [4, 32, 128], strides = [1, 1, 1] : tensor<4x32x128xf32> -> !flow.dispatch.tensor<writeonly:12x128x128xf32>
          flow.dispatch.tensor.store %12#1, %3, offsets = [%arg0, %arg1], sizes = [4, 32], strides = [1, 1] : tensor<4x32xf32> -> !flow.dispatch.tensor<writeonly:12x128xf32>
        }
      }
      return
    }
    
    
    // -----// IR Dump After LinalgStrategyVectorizePass (linalg-strategy-vectorize-pass) //----- //
    func.func @_softmax_dispatch_0_generic_12x128x128() {
      %cst = arith.constant dense<0.000000e+00> : vector<1x4xf32>
      %c1 = arith.constant 1 : index
      %c4 = arith.constant 4 : index
      %c32 = arith.constant 32 : index
      %c12 = arith.constant 12 : index
      %c128 = arith.constant 128 : index
      %c0 = arith.constant 0 : index
      %c6144 = arith.constant 6144 : index
      %c792576 = arith.constant 792576 : index
      %0 = hal.interface.binding.subspan set(0) binding(0) type(storage_buffer) offset(%c0) alignment(64) : !flow.dispatch.tensor<readonly:12x128x128xf32>
      %1 = hal.interface.binding.subspan set(0) binding(1) type(storage_buffer) offset(%c0) alignment(64) : !flow.dispatch.tensor<readonly:12x128xf32>
      %2 = hal.interface.binding.subspan set(0) binding(2) type(storage_buffer) offset(%c6144) alignment(64) : !flow.dispatch.tensor<writeonly:12x128x128xf32>
      %3 = hal.interface.binding.subspan set(0) binding(3) type(storage_buffer) offset(%c792576) alignment(64) : !flow.dispatch.tensor<writeonly:12x128xf32>
      %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
      %4 = affine.apply affine_map<()[s0] -> (s0 * 4)>()[%workgroup_id_y]
      %5 = affine.apply affine_map<()[s0] -> (s0 * 4)>()[%workgroup_count_y]
      %6 = affine.apply affine_map<()[s0] -> (s0 * 32)>()[%workgroup_id_x]
      %7 = affine.apply affine_map<()[s0] -> (s0 * 32)>()[%workgroup_count_x]
      scf.for %arg0 = %4 to %c12 step %5 {
        scf.for %arg1 = %6 to %c128 step %7 {
          %8 = flow.dispatch.tensor.load %2, offsets = [%arg0, %arg1, 0], sizes = [4, 32, 128], strides = [1, 1, 1] : !flow.dispatch.tensor<writeonly:12x128x128xf32> -> tensor<4x32x128xf32>
          %9 = flow.dispatch.tensor.load %3, offsets = [%arg0, %arg1], sizes = [4, 32], strides = [1, 1] : !flow.dispatch.tensor<writeonly:12x128xf32> -> tensor<4x32xf32>
          %10 = flow.dispatch.tensor.load %0, offsets = [%arg0, %arg1, 0], sizes = [4, 32, 128], strides = [1, 1, 1] : !flow.dispatch.tensor<readonly:12x128x128xf32> -> tensor<4x32x128xf32>
          %11 = flow.dispatch.tensor.load %1, offsets = [%arg0, %arg1], sizes = [4, 32], strides = [1, 1] : !flow.dispatch.tensor<readonly:12x128xf32> -> tensor<4x32xf32>
          %12:2 = scf.for %arg2 = %c0 to %c4 step %c1 iter_args(%arg3 = %8, %arg4 = %9) -> (tensor<4x32x128xf32>, tensor<4x32xf32>) {
            %13:2 = scf.for %arg5 = %c0 to %c32 step %c4 iter_args(%arg6 = %arg3, %arg7 = %arg4) -> (tensor<4x32x128xf32>, tensor<4x32xf32>) {
              %14 = tensor.extract_slice %11[%arg2, %arg5] [1, 4] [1, 1] : tensor<4x32xf32> to tensor<1x4xf32>
              %15 = scf.for %arg8 = %c0 to %c128 step %c4 iter_args(%arg9 = %14) -> (tensor<1x4xf32>) {
                %22 = tensor.extract_slice %10[%arg2, %arg5, %arg8] [1, 4, 4] [1, 1, 1] : tensor<4x32x128xf32> to tensor<1x4x4xf32>
                %23 = linalg.generic {indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>], iterator_types = ["parallel", "parallel", "reduction"]} ins(%22 : tensor<1x4x4xf32>) outs(%arg9 : tensor<1x4xf32>) {
                ^bb0(%arg10: f32, %arg11: f32):
                  %24 = arith.cmpf ogt, %arg10, %arg11 : f32
                  %25 = arith.select %24, %arg10, %arg11 : f32
                  linalg.yield %25 : f32
                } -> tensor<1x4xf32>
                scf.yield %23 : tensor<1x4xf32>
              }
              %16 = tensor.extract_slice %arg6[%arg2, %arg5, 0] [1, 4, 128] [1, 1, 1] : tensor<4x32x128xf32> to tensor<1x4x128xf32>
              %17 = tensor.extract_slice %arg7[%arg2, %arg5] [1, 4] [1, 1] : tensor<4x32xf32> to tensor<1x4xf32>
              %18 = vector.transfer_write %cst, %17[%c0, %c0] {in_bounds = [true, true]} : vector<1x4xf32>, tensor<1x4xf32>
              %19:2 = scf.for %arg8 = %c0 to %c128 step %c4 iter_args(%arg9 = %16, %arg10 = %18) -> (tensor<1x4x128xf32>, tensor<1x4xf32>) {
                %22 = tensor.extract_slice %10[%arg2, %arg5, %arg8] [1, 4, 4] [1, 1, 1] : tensor<4x32x128xf32> to tensor<1x4x4xf32>
                %23 = tensor.extract_slice %arg9[0, 0, %arg8] [1, 4, 4] [1, 1, 1] : tensor<1x4x128xf32> to tensor<1x4x4xf32>
                %24:2 = linalg.generic {indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>, affine_map<(d0, d1, d2) -> (d0, d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>], iterator_types = ["parallel", "parallel", "reduction"]} ins(%22, %15 : tensor<1x4x4xf32>, tensor<1x4xf32>) outs(%23, %arg10 : tensor<1x4x4xf32>, tensor<1x4xf32>) attrs =  {lowering_config = #iree\
    _codegen.lowering_config<tile_sizes = [[4, 32, 0], [1, 4, 0], [0, 0, 4]]>} {
                ^bb0(%arg11: f32, %arg12: f32, %arg13: f32, %arg14: f32):
                  %26 = arith.subf %arg11, %arg12 : f32
                  %27 = math.exp %26 : f32
                  %28 = arith.addf %27, %arg14 : f32
                  linalg.yield %27, %28 : f32, f32
                } -> (tensor<1x4x4xf32>, tensor<1x4xf32>)
                %25 = tensor.insert_slice %24#0 into %arg9[0, 0, %arg8] [1, 4, 4] [1, 1, 1] : tensor<1x4x4xf32> into tensor<1x4x128xf32>
                scf.yield %25, %24#1 : tensor<1x4x128xf32>, tensor<1x4xf32>
              }
              %20 = tensor.insert_slice %19#0 into %arg6[%arg2, %arg5, 0] [1, 4, 128] [1, 1, 1] : tensor<1x4x128xf32> into tensor<4x32x128xf32>
              %21 = tensor.insert_slice %19#1 into %arg7[%arg2, %arg5] [1, 4] [1, 1] : tensor<1x4xf32> into tensor<4x32xf32>
              scf.yield %20, %21 : tensor<4x32x128xf32>, tensor<4x32xf32>
            }
            scf.yield %13#0, %13#1 : tensor<4x32x128xf32>, tensor<4x32xf32>
          }
          flow.dispatch.tensor.store %12#0, %2, offsets = [%arg0, %arg1, 0], sizes = [4, 32, 128], strides = [1, 1, 1] : tensor<4x32x128xf32> -> !flow.dispatch.tensor<writeonly:12x128x128xf32>
          flow.dispatch.tensor.store %12#1, %3, offsets = [%arg0, %arg1], sizes = [4, 32], strides = [1, 1] : tensor<4x32xf32> -> !flow.dispatch.tensor<writeonly:12x128xf32>
        }
      }
      return
    }
    

    @hanhanW in case he has some quick insights into this while I dig deeper.

  3. MaheshRavishankar commented on Aug 25, 2022

    @MaheshRavishankar
    CollaboratorAuthor
  4. hanhanW commented on Aug 25, 2022

    @hanhanW
    Contributor

    Looks like it failed to meet precondition.

      * Pattern mlir::linalg::LinalgVectorizationPattern : 'linalg.generic -> ()' {
    Trying to match "mlir::linalg::LinalgVectorizationPattern"                                                                                                                               [linalg-vectorization] reduction precondition failed: reduction detection failed[linalg-vectorization] precondition failed: reduction preconditions"mlir::linalg::LinalgVectorizationPatt
    ern" result 0
    

    Might fail in this check:

    /// Check whether `outputOperand` is a reduction with a single combiner
    /// operation. Return the combiner operation of the reduction. Return
    /// nullptr otherwise. Multiple reduction operations would impose an
    /// ordering between reduction dimensions and is currently unsupported in
    /// Linalg. This limitation is motivated by the fact that e.g. min(max(X)) !=
    /// max(min(X))
    // TODO: use in LinalgOp verification, there is a circular dependency atm.
    static Operation *matchLinalgReduction(OpOperand *outputOperand) {
      auto linalgOp = cast<LinalgOp>(outputOperand->getOwner());
      unsigned outputPos =
          outputOperand->getOperandNumber() - linalgOp.getNumInputs();
      // Only single combiner operations are supported for now.
      SmallVector<Operation *, 4> combinerOps;
      if (!matchReduction(linalgOp.getRegionOutputArgs(), outputPos, combinerOps) ||
          combinerOps.size() != 1)
        return nullptr;
    
      // Return the combiner operation.
      return combinerOps[0];
    }
    
  5. MaheshRavishankar commented on Aug 25, 2022

    @MaheshRavishankar
    CollaboratorAuthor

    Thanks @hanhanW , couple of fixes seems to make things work great.

    1. This fix to the vectorizer https://github.com/iree-org/iree-llvm-fork/commit/a529938513d1e6e2752e20f717d3f7c7ff6bc7ba
    2. Changing
           %9 = linalg.generic {indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>], iterator_types = ["parallel", "parallel", "reduction"]} ins(%4 : tensor<12x128x128xf32>) outs(%5 : tensor<12x128xf32>) {
            ^bb0(%arg0: f32, %arg1: f32):
              %11 = arith.cmpf ogt, %arg0, %arg1 : f32
              %12 = arith.select %11, %arg0, %arg1 : f32
              linalg.yield %12 : f32
            } -> tensor<12x128xf32>
    

    to

           %9 = linalg.generic {indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>], iterator_types = ["parallel", "parallel", "reduction"]} ins(%4 : tensor<12x128x128xf32>) outs(%5 : tensor<12x128xf32>) {
            ^bb0(%arg0: f32, %arg1: f32):
              %11 = arith.maxf %arg0, %arg1 : f32
              linalg.yield %11 : f32
            } -> tensor<12x128xf32>
    

    Then I get the desired output

             scf.for %arg0 = %c0 to %c4 step %c1 {
                scf.for %arg1 = %c0 to %c32 step %c4 {
                  %10 = vector.load %9[%arg0, %arg1] : memref<4x32xf32, #map5>, vector<4xf32>
                  %11 = vector.broadcast %10 : vector<4xf32> to vector<1x4xf32>
                  %12 = affine.apply #map6(%arg1)
                  %13 = affine.apply #map7(%arg1)
                  %14 = affine.apply #map8(%arg1)
                  %15 = scf.for %arg2 = %c0 to %c128 step %c4 iter_args(%arg3 = %11) -> (vector<1x4xf32>) {
                    %39 = vector.load %8[%arg0, %arg1, %arg2] : memref<4x32x128xf32, #map4>, vector<4xf32>
                    %40 = vector.load %8[%arg0, %12, %arg2] : memref<4x32x128xf32, #map4>, vector<4xf32>
                    %41 = vector.load %8[%arg0, %13, %arg2] : memref<4x32x128xf32, #map4>, vector<4xf32>
                    %42 = vector.load %8[%arg0, %14, %arg2] : memref<4x32x128xf32, #map4>, vector<4xf32>
                    %43 = vector.extract %arg3[0, 0] : vector<1x4xf32>
                    %44 = vector.reduction <maxf>, %39, %43 : vector<4xf32> into f32
                    %45 = vector.insertelement %44, %cst_0[%c0 : index] : vector<4xf32>
                    %46 = vector.extract %arg3[0, 1] : vector<1x4xf32>
                    %47 = vector.reduction <maxf>, %40, %46 : vector<4xf32> into f32
                    %48 = vector.insertelement %47, %45[%c1 : index] : vector<4xf32>
                    %49 = vector.extract %arg3[0, 2] : vector<1x4xf32>
                    %50 = vector.reduction <maxf>, %41, %49 : vector<4xf32> into f32
                    %51 = vector.insertelement %50, %48[%c2 : index] : vector<4xf32>
                    %52 = vector.extract %arg3[0, 3] : vector<1x4xf32>
                    %53 = vector.reduction <maxf>, %42, %52 : vector<4xf32> into f32
                    %54 = vector.insertelement %53, %51[%c3 : index] : vector<4xf32>
                    %55 = vector.broadcast %54 : vector<4xf32> to vector<1x4xf32>
                    scf.yield %55 : vector<1x4xf32>
                  }
                  %16 = memref.subview %6[%arg0, %arg1, 0] [1, 4, 128] [1, 1, 1] : memref<4x32x128xf32, #map4> to memref<1x4x128xf32, #map4>
                  %17 = vector.extract %15[0, 0] : vector<1x4xf32>
                  %18 = vector.insert %17, %cst [0, 0, 0] : f32 into vector<1x4x4xf32>
                  %19 = vector.extract %15[0, 1] : vector<1x4xf32>
                  %20 = vector.insert %19, %18 [0, 1, 0] : f32 into vector<1x4x4xf32>
                  %21 = vector.extract %15[0, 2] : vector<1x4xf32>
                  %22 = vector.insert %21, %20 [0, 2, 0] : f32 into vector<1x4x4xf32>
                  %23 = vector.extract %15[0, 3] : vector<1x4xf32>
                  %24 = vector.insert %23, %22 [0, 3, 0] : f32 into vector<1x4x4xf32>
                  %25 = vector.insert %17, %24 [0, 0, 1] : f32 into vector<1x4x4xf32>
                  %26 = vector.insert %19, %25 [0, 1, 1] : f32 into vector<1x4x4xf32>
                  %27 = vector.insert %21, %26 [0, 2, 1] : f32 into vector<1x4x4xf32>
                  %28 = vector.insert %23, %27 [0, 3, 1] : f32 into vector<1x4x4xf32>
                  %29 = vector.insert %17, %28 [0, 0, 2] : f32 into vector<1x4x4xf32>
                  %30 = vector.insert %19, %29 [0, 1, 2] : f32 into vector<1x4x4xf32>
                  %31 = vector.insert %21, %30 [0, 2, 2] : f32 into vector<1x4x4xf32>
                  %32 = vector.insert %23, %31 [0, 3, 2] : f32 into vector<1x4x4xf32>
                  %33 = vector.insert %17, %32 [0, 0, 3] : f32 into vector<1x4x4xf32>
                  %34 = vector.insert %19, %33 [0, 1, 3] : f32 into vector<1x4x4xf32>
                  %35 = vector.insert %21, %34 [0, 2, 3] : f32 into vector<1x4x4xf32>
                  %36 = vector.insert %23, %35 [0, 3, 3] : f32 into vector<1x4x4xf32>
                  %37 = scf.for %arg2 = %c0 to %c128 step %c4 iter_args(%arg3 = %cst_1) -> (vector<1x4xf32>) {
                    %39 = vector.load %8[%arg0, %arg1, %arg2] : memref<4x32x128xf32, #map4>, vector<4xf32>
                    %40 = vector.insert %39, %cst [0, 0] : vector<4xf32> into vector<1x4x4xf32>
                    %41 = vector.load %8[%arg0, %12, %arg2] : memref<4x32x128xf32, #map4>, vector<4xf32>
                    %42 = vector.insert %41, %40 [0, 1] : vector<4xf32> into vector<1x4x4xf32>
                    %43 = vector.load %8[%arg0, %13, %arg2] : memref<4x32x128xf32, #map4>, vector<4xf32>
                    %44 = vector.insert %43, %42 [0, 2] : vector<4xf32> into vector<1x4x4xf32>
                    %45 = vector.load %8[%arg0, %14, %arg2] : memref<4x32x128xf32, #map4>, vector<4xf32>
                    %46 = vector.insert %45, %44 [0, 3] : vector<4xf32> into vector<1x4x4xf32>
                    %47 = arith.subf %46, %36 : vector<1x4x4xf32>
                    %48 = math.exp %47 : vector<1x4x4xf32>
                    %49 = vector.extract %48[0, 0] : vector<1x4x4xf32>
                    %50 = vector.extract %arg3[0, 0] : vector<1x4xf32>
                    %51 = vector.reduction <add>, %49, %50 : vector<4xf32> into f32
                    %52 = vector.insertelement %51, %cst_0[%c0 : index] : vector<4xf32>
                    %53 = vector.extract %48[0, 1] : vector<1x4x4xf32>
                    %54 = vector.extract %arg3[0, 1] : vector<1x4xf32>
                    %55 = vector.reduction <add>, %53, %54 : vector<4xf32> into f32
                    %56 = vector.insertelement %55, %52[%c1 : index] : vector<4xf32>
                    %57 = vector.extract %48[0, 2] : vector<1x4x4xf32>
                    %58 = vector.extract %arg3[0, 2] : vector<1x4xf32>
                    %59 = vector.reduction <add>, %57, %58 : vector<4xf32> into f32
                    %60 = vector.insertelement %59, %56[%c2 : index] : vector<4xf32>
                    %61 = vector.extract %48[0, 3] : vector<1x4x4xf32>
                    %62 = vector.extract %arg3[0, 3] : vector<1x4xf32>
                    %63 = vector.reduction <add>, %61, %62 : vector<4xf32> into f32
                    %64 = vector.insertelement %63, %60[%c3 : index] : vector<4xf32>
                    %65 = vector.broadcast %64 : vector<4xf32> to vector<1x4xf32>
                    vector.store %49, %16[%c0, %c0, %arg2] : memref<1x4x128xf32, #map4>, vector<4xf32>
                    vector.store %53, %16[%c0, %c1, %arg2] : memref<1x4x128xf32, #map4>, vector<4xf32>
                    vector.store %57, %16[%c0, %c2, %arg2] : memref<1x4x128xf32, #map4>, vector<4xf32>
                    vector.store %61, %16[%c0, %c3, %arg2] : memref<1x4x128xf32, #map4>, vector<4xf32>
                    scf.yield %65 : vector<1x4xf32>
                  }
                  %38 = vector.extract %37[0] : vector<1x4xf32>
                  vector.store %38, %7[%arg0, %arg1] : memref<4x32xf32, #map5>, vector<4xf32>
    

    Looks kind of like what I am looking for..

  6. MaheshRavishankar commented on Aug 26, 2022

    @MaheshRavishankar
    CollaboratorAuthor

    Closed by #10210

  7. added a commit that references this issue on Aug 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

codegen/llvm

Type

No type

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions