tensorflow/mlir-hlo

mhlo-fusion bug

lipracer opened this issue · 4 comments

// CHECK-LABEL: func @elementwise_fusion
func @elementwise_fusion(%arg0: tensor<4x16xi32>, %arg1: tensor<4x16xi32>) -> tensor<2x4xi32> {
  %0 = "mhlo.add"(%arg0, %arg1) : (tensor<4x16xi32>, tensor<4x16xi32>) -> tensor<4x16xi32>
  %1 = "mhlo.subtract"(%0, %arg0) : (tensor<4x16xi32>, tensor<4x16xi32>) -> tensor<4x16xi32>
  %2 = "mhlo.slice"(%1) {limit_indices = dense<[2, 8]> : tensor<2xi64>, start_indices = dense<0> : tensor<2xi64>, strides = dense<1> : tensor<2xi64>} : (tensor<4x16xi32>) -> tensor<2x8xi32>
  %3 = "mhlo.multiply"(%0, %1) : (tensor<4x16xi32>, tensor<4x16xi32>) -> tensor<4x16xi32>
  %4 = "mhlo.slice"(%3) {limit_indices = dense<[2, 8]> : tensor<2xi64>, start_indices = dense<0> : tensor<2xi64>, strides = dense<[1, 2]> : tensor<2xi64>} : (tensor<4x16xi32>) -> tensor<2x4xi32>
  return %4 : tensor<2x4xi32>
}

Paste this paragraph after the test/ mhlo-fusion.mlir file and run ninja check-mlir-hlo
Run the IR above with mhlo-fusion to get the wrong result;
Run this section with pass alone, and the IR output is as follows:

loc("-":5:10): error: operand #0 does not dominate this use
// -----// IR Dump After MhloFusionPass Failed ('builtin.func' operation: @main) //----- //
"builtin.module"() ( {
  "builtin.func"() ( {
  ^bb0(%arg0: tensor<4x16xi32>, %arg1: tensor<4x16xi32>):  // no predecessors
    %0 = "mhlo.slice"(%1#0) {limit_indices = dense<[2, 8]> : tensor<2xi64>, start_indices = dense<0> : tensor<2xi64>, strides = dense<1> : tensor<2xi64>} : (tensor<4x16xi32>) -> tensor<2x8xi32>
    %1:2 = "mhlo.fusion"(%arg0, %arg1) ( {
      %3 = "mhlo.add"(%arg0, %arg1) : (tensor<4x16xi32>, tensor<4x16xi32>) -> tensor<4x16xi32>
      %4 = "mhlo.subtract"(%3, %arg0) : (tensor<4x16xi32>, tensor<4x16xi32>) -> tensor<4x16xi32>
      %5 = "mhlo.multiply"(%3, %4) : (tensor<4x16xi32>, tensor<4x16xi32>) -> tensor<4x16xi32>
      "mhlo.return"(%4, %5) : (tensor<4x16xi32>, tensor<4x16xi32>) -> ()
    }) : (tensor<4x16xi32>, tensor<4x16xi32>) -> (tensor<4x16xi32>, tensor<4x16xi32>)
    %2 = "mhlo.slice"(%1#1) {limit_indices = dense<[2, 8]> : tensor<2xi64>, start_indices = dense<0> : tensor<2xi64>, strides = dense<[1, 2]> : tensor<2xi64>} : (tensor<4x16xi32>) -> tensor<2x4xi32>
    "std.return"(%2) : (tensor<2x4xi32>) -> ()
  }) {sym_name = "main", type = (tensor<4x16xi32>, tensor<4x16xi32>) -> tensor<2x4xi32>} : () -> ()
}) : () -> ()

%0 = "mhlo.slice"(%1#0) should follow by fusion op

Opbuilder is created by this statement OpBuilder b(pattern.back());. Fusion OP is inserted after all fused ops, need to move others consumers between fused ops by post order.

Thanks for the bug report! Are you interested to submit a patch for fixing this maybe?

Ok,

#14
My first submission in the open source community. Forgive me for my shortcomings.

Fixed 14ddf54da51879e031507d34d37bae0923dc58a9