// RUN: mlir-hlo-opt --split-input-file --allow-unregistered-dialect --mhlo-move-up-dynamic-broadcasts-for-fusion --canonicalize --cse %s | FileCheck %s // Shape computations shall be reified. // CHECK-LABEL: @shape_of_unary // CHECK-SAME: (%[[ARG:.*]]: tensor) func @shape_of_unary(%arg : tensor) { // CHECK: %[[SHAPE:.*]] = shape.shape_of %[[ARG]] : tensor -> tensor // CHECK: "use"(%[[SHAPE]]) %0 = "mhlo.convert"(%arg) : (tensor) -> tensor %1 = shape.shape_of %0 : tensor -> tensor "use"(%1) : (tensor) -> () return } // ----- // Shape computations shall be reified. // CHECK-LABEL: @shape_of_nary // CHECK-SAME: (%[[ARG0:.*]]: tensor, %[[ARG1:.*]]: tensor) func @shape_of_nary(%arg0 : tensor, %arg1 : tensor) { // CHECK: %[[SHAPE:.*]] = shape.shape_of %[[ARG0]] : tensor -> tensor // CHECK: "use"(%[[SHAPE]]) %0 = mhlo.subtract %arg0, %arg1 : tensor %1 = mhlo.subtract %0, %arg1 : tensor %2 = shape.shape_of %1 : tensor -> tensor "use"(%2) : (tensor) -> () return } // ----- // Broadcasts can be moved up over unary shape-preserving operations. // CHECK-LABEL: @bcast_unary // CHECK-SAME: (%[[ARG:.*]]: tensor, %[[OUT_DIMS:.*]]: tensor<3xindex>) func @bcast_unary(%arg : tensor, %out_dims : tensor<3xindex>) -> tensor { // CHECK: %[[BCASTED_OPERAND:.*]] = "mhlo.dynamic_broadcast_in_dim"(%[[ARG]], %[[OUT_DIMS]]) // CHECK-SAME: broadcast_dimensions = dense<[0, 1]> : tensor<2xi64>} : (tensor, tensor<3xindex>) -> tensor // CHECK: "mhlo.convert"(%[[BCASTED_OPERAND]]) : (tensor) -> tensor %0 = "mhlo.convert"(%arg) : (tensor) -> tensor %1 = "mhlo.dynamic_broadcast_in_dim"(%0, %out_dims) { broadcast_dimensions = dense<[0, 1]> : tensor<2xi64> } : (tensor, tensor<3xindex>) -> tensor return %1 : tensor } // ----- // Broadcasts can be moved up over n-ary shape-preserving operations. // CHECK-LABEL: @bcast_nary // CHECK-SAME: (%[[ARG0:.*]]: tensor, %[[ARG1:.*]]: tensor, %[[OUT_DIMS:.*]]: tensor<3xindex>) func @bcast_nary(%arg0 : tensor, %arg1 : tensor, %out_dims : tensor<3xindex>) -> tensor { // CHECK-NOT: subtract // CHECK: %[[BCASTED_ARG0:.*]] = "mhlo.dynamic_broadcast_in_dim"(%[[ARG0]], %[[OUT_DIMS]]) // CHECK: %[[BCASTED_ARG1:.*]] = "mhlo.dynamic_broadcast_in_dim"(%[[ARG1]], %[[OUT_DIMS]]) // CHECK: %{{.*}} = mhlo.subtract %[[BCASTED_ARG0]], %[[BCASTED_ARG1]] : tensor %0 = mhlo.subtract %arg0, %arg1 : tensor %1 = "mhlo.dynamic_broadcast_in_dim"(%0, %out_dims) { broadcast_dimensions = dense<[0, 1]> : tensor<2xi64> } : (tensor, tensor<3xindex>) -> tensor return %1 : tensor } // ----- // Exemplary IR as it appears in the lowering with `tf.Sub` and `tf.Cast`. // CHECK-LABEL: @cast_sub // CHECK-SAME: (%[[ARG0:.*]]: tensor, %[[ARG1:.*]]: tensor) -> tensor func @cast_sub(%arg0: tensor, %arg1: tensor) -> tensor { // CHECK-NOT: convert // CHECK: %[[BCASTED_ARG1:.*]] = "mhlo.dynamic_broadcast_in_dim"(%[[ARG1]], %{{.*}}) // CHECK: %[[BCASTED_ARG0:.*]] = "mhlo.dynamic_broadcast_in_dim"(%[[ARG0]], %{{.*}}) // CHECK: %[[CONVERTED_BCASTED_ARG0:.*]] = "mhlo.convert"(%[[BCASTED_ARG0]]) : (tensor) -> tensor // CHECK: %{{.*}} = mhlo.subtract %[[BCASTED_ARG1]], %[[CONVERTED_BCASTED_ARG0]] : tensor %0 = "mhlo.convert"(%arg0) : (tensor) -> tensor %1 = shape.shape_of %arg1 : tensor -> tensor %2 = shape.shape_of %0 : tensor -> tensor %3 = shape.cstr_broadcastable %1, %2 : tensor, tensor %4 = shape.assuming %3 -> (tensor) { %5 = shape.shape_of %arg1 : tensor -> tensor %6 = shape.shape_of %0 : tensor -> tensor %7 = shape.broadcast %5, %6 : tensor, tensor -> tensor %8 = tensor.cast %7 : tensor to tensor<3xindex> %9 = "mhlo.dynamic_broadcast_in_dim"(%arg1, %8) { broadcast_dimensions = dense<[0, 1, 2]> : tensor<3xi64>} : (tensor, tensor<3xindex>) -> tensor %10 = "mhlo.dynamic_broadcast_in_dim"(%0, %8) { broadcast_dimensions = dense<[1, 2]> : tensor<2xi64>} : (tensor, tensor<3xindex>) -> tensor %11 = mhlo.subtract %9, %10 : tensor shape.assuming_yield %11 : tensor } return %4 : tensor } // ----- // CHECK-LABEL: @inline_bcasted_shape_operands // CHECK-SAME: (%[[A:.*]]: tensor, %[[B:.*]]: tensor, %[[C:.*]]: tensor) func @inline_bcasted_shape_operands(%a : tensor, %b : tensor, %c : tensor) -> !shape.witness { // CHECK-NOT: shape.broadcast // CHECK: %[[WITNESS:.*]] = shape.cstr_broadcastable %[[A]], %[[B]], %[[C]] // CHECK: return %[[WITNESS]] : !shape.witness %0 = shape.broadcast %a, %b : tensor, tensor -> tensor %1 = shape.cstr_broadcastable %0, %c : tensor, tensor return %1 : !shape.witness } // ----- // CHECK-LABEL: @move_shape_of_into_assuming // CHECK-SAME: (%[[ARG0:.*]]: !shape.witness, %[[ARG1:.*]]: tensor, %[[ARG2:.*]]: tensor) func @move_shape_of_into_assuming(%arg0 : !shape.witness, %arg1 : tensor, %arg2 : tensor) -> tensor<3xindex> { // CHECK: %[[ASSUMING_RESULTS:.*]]:3 = shape.assuming %[[ARG0]] -> (tensor, tensor, tensor<3xindex>) { // CHECK: %[[SHAPE:.*]] = shape.shape_of %[[ARG2]] // CHECK: shape.assuming_yield %[[ARG1]], %[[ARG2]], %[[SHAPE]] // CHECK: } // CHECK-NOT: shape_of // CHECK: return %[[ASSUMING_RESULTS]]#2 %0:2 = shape.assuming %arg0 -> (tensor, tensor) { shape.assuming_yield %arg1, %arg2 : tensor, tensor } %1 = shape.shape_of %0#1 : tensor -> tensor<3xindex> return %1 : tensor<3xindex> } // ----- // CHECK-LABEL: @move_cstr_broadcastable_into_assuming // CHECK-SAME: (%[[ARG0:.*]]: !shape.witness, %[[ARG1:.*]]: tensor<2xindex>, %[[ARG2:.*]]: tensor<3xindex>) func @move_cstr_broadcastable_into_assuming(%arg0 : !shape.witness, %arg1 : tensor<2xindex>, %arg2 : tensor<3xindex>) -> !shape.witness { // CHECK: %[[ASSUMING_RESULTS:.*]]:3 = shape.assuming %[[ARG0]] -> (tensor<2xindex>, tensor<3xindex>, !shape.witness) { // CHECK: %[[WITNESS:.*]] = shape.cstr_broadcastable %[[ARG1]], %[[ARG2]] // CHECK: shape.assuming_yield %[[ARG1]], %[[ARG2]], %[[WITNESS]] // CHECK: } // CHECK-NOT: cstr_broadcastable // CHECK: return %[[ASSUMING_RESULTS]]#2 %0:2 = shape.assuming %arg0 -> (tensor<2xindex>, tensor<3xindex>) { shape.assuming_yield %arg1, %arg2 : tensor<2xindex>, tensor<3xindex> } %1 = shape.cstr_broadcastable %arg1, %0#1 : tensor<2xindex>, tensor<3xindex> return %1 : !shape.witness } // ----- // CHECK-LABEL: @not_move_shape_of_into_assuming func @not_move_shape_of_into_assuming(%arg0 : !shape.witness, %arg1 : tensor, %arg2 : tensor) -> tensor<3xindex> { // CHECK: shape.assuming // CHECK-SAME: { // CHECK-NOT: shape_of // CHECK: } // CHECK: "some.other.op" // CHECK: shape_of %0:2 = shape.assuming %arg0 -> (tensor, tensor) { shape.assuming_yield %arg1, %arg2 : tensor, tensor } "some.other.op"() : () -> () %2 = shape.shape_of %0#1 : tensor -> tensor<3xindex> return %2 : tensor<3xindex> }