message: refactor deplicate code
diff: @@ -255,6 +255,27 @@ bool CreateDynamicBroadcastsForBinaryOp(SrcOp op, PatternRewriter *rewriter,
  return true;
  }
  +template <typename SrcOp>
  +bool GenerateBroadcastForBinaryOp(SrcOp op, PatternRewriter *rewriter,
  + Value *out_lhs, Value *out_rhs) {
  +
  + auto op_ranked_type = op.getType().template dyn_cast<RankedTensorType>();
  + if (!op_ranked_type) return false;
  +
  + if (op_ranked_type.hasStaticShape()) {
  + if (!CreateBroadcastsForBinaryOp(op, rewriter, out_lhs, out_rhs)) {
  + return false;
  + }
  + } else {
  + if (!CreateDynamicBroadcastsForBinaryOp(op, rewriter, out_lhs,
  + out_rhs)) {
  + return false;
  + }
  + }
  + return true;
  +}
  +
  +
  template <typename SrcOp>
  struct BinaryOpWithBroadcastConvert : public OpRewritePattern<SrcOp> {
  explicit BinaryOpWithBroadcastConvert(MLIRContext *context)
  @@ -265,20 +286,8 @@ struct BinaryOpWithBroadcastConvert : public OpRewritePattern<SrcOp> {
  Value new_lhs;
  Value new_rhs;
  - auto op_ranked_type = op.getType().template dyn_cast<RankedTensorType>();
  - if (!op_ranked_type) return failure();
  -
  - if (op_ranked_type.hasStaticShape()) {
  - if (!CreateBroadcastsForBinaryOp(op, &rewriter, &new_lhs, &new_rhs)) {
  - return failure();
  - }
  - } else {
  - if (!CreateDynamicBroadcastsForBinaryOp(op, &rewriter, &new_lhs,
  - &new_rhs)) {
  + if (!GenerateBroadcastForBinaryOp(op, &rewriter, &new_lhs, &new_rhs))
  return failure();
  - }
  - }
  -
  // Replace the original op with a new one that uses the new args.
  // New args are broadcasts, so no dims are needed on the replacement op.
  rewriter.replaceOpWithNewOp<SrcOp>(op, op.getType(), new_lhs, new_rhs,
  @@ -297,18 +306,8 @@ struct CompareWithBroadcastConvert : public OpRewritePattern<CompareOp> {
  Value new_lhs;
  Value new_rhs;
  - auto op_ranked_type = op.getType().template dyn_cast<RankedTensorType>();
  - if (!op_ranked_type) return failure();
  -
  - if (op_ranked_type.hasStaticShape()) {
  - if (!CreateBroadcastsForBinaryOp(op, &rewriter, &new_lhs, &new_rhs)) {
  - return failure();
  - }
  - } else {
  - if (!CreateDynamicBroadcastsForBinaryOp(op, &rewriter, &new_lhs, &new_rhs)) {
  + if (!GenerateBroadcastForBinaryOp(op, &rewriter, &new_lhs, &new_rhs))
  return failure();
  - }
  - }
  rewriter.replaceOpWithNewOp<CompareOp>(op, op.getType(), new_lhs, new_rhs,
  /*broadcast_dims=*/nullptr,
  