/*===- TableGen'erated file -------------------------------------*- C++ -*-===*\
|*                                                                            *|
|* Rewriters                                                                  *|
|*                                                                            *|
|* Automatically generated file, do not edit!                                 *|
|* From: StablehloCompatibilityExpanderPatterns.td                            *|
|*                                                                            *|
\*===----------------------------------------------------------------------===*/


static ::llvm::LogicalResult __mlir_ods_local_type_constraint_StablehloCompatibilityExpanderPatterns1(
    ::mlir::PatternRewriter &rewriter, ::mlir::Operation *op, ::mlir::Type type,
    ::llvm::StringRef failureStr) {
  if (!((!isa<ComplexType>(cast<ShapedType>(type).getElementType())))) {
    return rewriter.notifyMatchFailure(op, [&](::mlir::Diagnostic &diag) {
      diag << failureStr << ": Non-complex element type";
    });
  }
  return ::mlir::success();
}

static ::llvm::LogicalResult __mlir_ods_local_type_constraint_StablehloCompatibilityExpanderPatterns2(
    ::mlir::PatternRewriter &rewriter, ::mlir::Operation *op, ::mlir::Type type,
    ::llvm::StringRef failureStr) {
  if (!((isa<ComplexType>(cast<ShapedType>(type).getElementType())))) {
    return rewriter.notifyMatchFailure(op, [&](::mlir::Diagnostic &diag) {
      diag << failureStr << ": Complex element type";
    });
  }
  return ::mlir::success();
}

static ::llvm::LogicalResult __mlir_ods_local_attr_constraint_StablehloCompatibilityExpanderPatterns1(
    ::mlir::PatternRewriter &rewriter, ::mlir::Operation *op, ::mlir::Attribute attr,
    ::llvm::StringRef failureStr) {
  if (!((attr == ::mlir::stablehlo::ResultAccuracyAttr::get(rewriter.getContext(), APFloat(0.0), APFloat(0.0), 0, ::mlir::stablehlo::ResultAccuracyModeAttr::get(rewriter.getContext(), ::mlir::stablehlo::ResultAccuracyMode::DEFAULT))))) {
    return rewriter.notifyMatchFailure(op, [&](::mlir::Diagnostic &diag) {
      diag << failureStr << ": constant attribute ::mlir::stablehlo::ResultAccuracyMode::DEFAULT";
    });
  }
  return ::mlir::success();
}
/* Generated from:
    external/stablehlo/stablehlo/transforms/StablehloCompatibilityExpanderPatterns.td:35
*/
struct TanOp_CompatiblityExpander : public ::mlir::RewritePattern {
  TanOp_CompatiblityExpander(::mlir::MLIRContext *context)
      : ::mlir::RewritePattern("stablehlo.tan", 1, context, {"stablehlo.cosine", "stablehlo.divide", "stablehlo.sine"}) {}
  ::llvm::LogicalResult matchAndRewrite(::mlir::Operation *op0,
      ::mlir::PatternRewriter &rewriter) const override {
    // Variables for capturing values and attributes used while creating ops
    ::mlir::Operation::operand_range input(op0->getOperands());
    ::llvm::SmallVector<::mlir::Operation *, 4> tblgen_ops;

    // Match
    tblgen_ops.push_back(op0);
    auto castedOp0 = ::llvm::dyn_cast<::mlir::stablehlo::TanOp>(op0); (void)castedOp0;
    if(::mlir::failed(__mlir_ods_local_type_constraint_StablehloCompatibilityExpanderPatterns1(rewriter, castedOp0, (*castedOp0.getODSOperands(0).begin()).getType(), "operand 0 of op 'stablehlo.tan' failed to satisfy constraint: 'Non-complex element type'"))) {
      return ::mlir::failure();
    }
    input = castedOp0.getODSOperands(0);
    {
      [[maybe_unused]] auto tblgen_attr = castedOp0.getProperties().getResultAccuracy();
      if (!tblgen_attr) tblgen_attr = ::mlir::stablehlo::ResultAccuracyAttr::get(rewriter.getContext(), APFloat(0.0), APFloat(0.0), 0, ::mlir::stablehlo::ResultAccuracyModeAttr::get(rewriter.getContext(), ::mlir::stablehlo::ResultAccuracyMode::DEFAULT));
      if (!tblgen_attr) return ::mlir::failure();
      if(::mlir::failed(__mlir_ods_local_attr_constraint_StablehloCompatibilityExpanderPatterns1(rewriter, castedOp0, tblgen_attr, "op 'stablehlo.tan' attribute 'result_accuracy' failed to satisfy constraint: 'constant attribute ::mlir::stablehlo::ResultAccuracyMode::DEFAULT'"))) {
        return ::mlir::failure();
      }
    }

    // Rewrite
    auto odsLoc = rewriter.getFusedLoc({tblgen_ops[0]->getLoc()}); (void)odsLoc;
    ::llvm::SmallVector<::mlir::Value, 4> tblgen_repl_values;
    ::mlir::stablehlo::SineOp tblgen_SineOp_0;
    {
      ::mlir::Value tblgen_value_0 = (*input.begin());
      tblgen_SineOp_0 = ::mlir::stablehlo::SineOp::create(rewriter, odsLoc,
        /*operand=*/tblgen_value_0,
        ::mlir::stablehlo::ResultAccuracyAttr::get(rewriter.getContext(), APFloat(0.0), APFloat(0.0), 0, ::mlir::stablehlo::ResultAccuracyModeAttr::get(rewriter.getContext(), ::mlir::stablehlo::ResultAccuracyMode::DEFAULT))
      );
    }
    ::mlir::stablehlo::CosineOp tblgen_CosineOp_1;
    {
      ::mlir::Value tblgen_value_0 = (*input.begin());
      tblgen_CosineOp_1 = ::mlir::stablehlo::CosineOp::create(rewriter, odsLoc,
        /*operand=*/tblgen_value_0,
        ::mlir::stablehlo::ResultAccuracyAttr::get(rewriter.getContext(), APFloat(0.0), APFloat(0.0), 0, ::mlir::stablehlo::ResultAccuracyModeAttr::get(rewriter.getContext(), ::mlir::stablehlo::ResultAccuracyMode::DEFAULT))
      );
    }
    ::mlir::stablehlo::DivOp tblgen_DivOp_2;
    {
      ::llvm::SmallVector<::mlir::Value, 4> tblgen_values; (void)tblgen_values;
      ::mlir::stablehlo::DivOp::Properties tblgen_props; (void)tblgen_props;
      tblgen_values.push_back((*tblgen_SineOp_0.getODSResults(0).begin()));
      tblgen_values.push_back((*tblgen_CosineOp_1.getODSResults(0).begin()));
      ::llvm::SmallVector<::mlir::Type, 4> tblgen_types; (void)tblgen_types;
      for (auto v: castedOp0.getODSResults(0)) {
        tblgen_types.push_back(v.getType());
      }
      tblgen_DivOp_2 = ::mlir::stablehlo::DivOp::create(rewriter, odsLoc, tblgen_types, tblgen_values, tblgen_props);
    }

    for (auto v: ::llvm::SmallVector<::mlir::Value, 4>{ tblgen_DivOp_2.getODSResults(0) }) {
      tblgen_repl_values.push_back(v);
    }

    rewriter.replaceOp(op0, tblgen_repl_values);
    return ::mlir::success();
  }
};

/* Generated from:
    external/stablehlo/stablehlo/transforms/StablehloCompatibilityExpanderPatterns.td:43
*/
struct TanOp_ComplexElementType_CompatiblityExpander : public ::mlir::RewritePattern {
  TanOp_ComplexElementType_CompatiblityExpander(::mlir::MLIRContext *context)
      : ::mlir::RewritePattern("stablehlo.tan", 1, context, {"stablehlo.complex", "stablehlo.divide", "stablehlo.imag", "stablehlo.multiply", "stablehlo.negate", "stablehlo.real", "stablehlo.tan", "stablehlo.tanh"}) {}
  ::llvm::LogicalResult matchAndRewrite(::mlir::Operation *op0,
      ::mlir::PatternRewriter &rewriter) const override {
    // Variables for capturing values and attributes used while creating ops
    ::mlir::Operation::operand_range input(op0->getOperands());
    ::llvm::SmallVector<::mlir::Operation *, 4> tblgen_ops;

    // Match
    tblgen_ops.push_back(op0);
    auto castedOp0 = ::llvm::dyn_cast<::mlir::stablehlo::TanOp>(op0); (void)castedOp0;
    if(::mlir::failed(__mlir_ods_local_type_constraint_StablehloCompatibilityExpanderPatterns2(rewriter, castedOp0, (*castedOp0.getODSOperands(0).begin()).getType(), "operand 0 of op 'stablehlo.tan' failed to satisfy constraint: 'Complex element type'"))) {
      return ::mlir::failure();
    }
    input = castedOp0.getODSOperands(0);
    {
      [[maybe_unused]] auto tblgen_attr = castedOp0.getProperties().getResultAccuracy();
      if (!tblgen_attr) tblgen_attr = ::mlir::stablehlo::ResultAccuracyAttr::get(rewriter.getContext(), APFloat(0.0), APFloat(0.0), 0, ::mlir::stablehlo::ResultAccuracyModeAttr::get(rewriter.getContext(), ::mlir::stablehlo::ResultAccuracyMode::DEFAULT));
      if (!tblgen_attr) return ::mlir::failure();
      if(::mlir::failed(__mlir_ods_local_attr_constraint_StablehloCompatibilityExpanderPatterns1(rewriter, castedOp0, tblgen_attr, "op 'stablehlo.tan' attribute 'result_accuracy' failed to satisfy constraint: 'constant attribute ::mlir::stablehlo::ResultAccuracyMode::DEFAULT'"))) {
        return ::mlir::failure();
      }
    }

    // Rewrite
    auto odsLoc = rewriter.getFusedLoc({tblgen_ops[0]->getLoc()}); (void)odsLoc;
    ::llvm::SmallVector<::mlir::Value, 4> tblgen_repl_values;
    ::mlir::stablehlo::RealOp tblgen_RealOp_0;
    {
      ::mlir::Value tblgen_value_0 = (*input.begin());
      tblgen_RealOp_0 = ::mlir::stablehlo::RealOp::create(rewriter, odsLoc,
        /*operand=*/tblgen_value_0
      );
    }
    ::mlir::stablehlo::TanOp tan;
    {
      ::mlir::Value tblgen_value_0 = (*tblgen_RealOp_0.getODSResults(0).begin());
      tan = ::mlir::stablehlo::TanOp::create(rewriter, odsLoc,
        /*operand=*/tblgen_value_0,
        ::mlir::stablehlo::ResultAccuracyAttr::get(rewriter.getContext(), APFloat(0.0), APFloat(0.0), 0, ::mlir::stablehlo::ResultAccuracyModeAttr::get(rewriter.getContext(), ::mlir::stablehlo::ResultAccuracyMode::DEFAULT))
      );
    }
    ::mlir::stablehlo::ImagOp tblgen_ImagOp_1;
    {
      ::mlir::Value tblgen_value_0 = (*input.begin());
      tblgen_ImagOp_1 = ::mlir::stablehlo::ImagOp::create(rewriter, odsLoc,
        /*operand=*/tblgen_value_0
      );
    }
    ::mlir::stablehlo::TanhOp tanh;
    {
      ::mlir::Value tblgen_value_0 = (*tblgen_ImagOp_1.getODSResults(0).begin());
      tanh = ::mlir::stablehlo::TanhOp::create(rewriter, odsLoc,
        /*operand=*/tblgen_value_0,
        ::mlir::stablehlo::ResultAccuracyAttr::get(rewriter.getContext(), APFloat(0.0), APFloat(0.0), 0, ::mlir::stablehlo::ResultAccuracyModeAttr::get(rewriter.getContext(), ::mlir::stablehlo::ResultAccuracyMode::DEFAULT))
      );
    }
    ::mlir::stablehlo::ComplexOp tblgen_ComplexOp_2;
    {
      ::mlir::Value tblgen_value_0 = (*tan.getODSResults(0).begin());
      ::mlir::Value tblgen_value_1 = (*tanh.getODSResults(0).begin());
      tblgen_ComplexOp_2 = ::mlir::stablehlo::ComplexOp::create(rewriter, odsLoc,
        /*lhs=*/tblgen_value_0,
        /*rhs=*/tblgen_value_1
      );
    }
    auto nativeVar_3 = ::mlir::stablehlo::getConstantLike(rewriter, odsLoc, 1.0, (*tan.getODSResults(0).begin())); (void)nativeVar_3;
    ::mlir::stablehlo::MulOp tblgen_MulOp_4;
    {
      ::mlir::Value tblgen_value_0 = (*tan.getODSResults(0).begin());
      ::mlir::Value tblgen_value_1 = (*tanh.getODSResults(0).begin());
      tblgen_MulOp_4 = ::mlir::stablehlo::MulOp::create(rewriter, odsLoc,
        /*lhs=*/tblgen_value_0,
        /*rhs=*/tblgen_value_1
      );
    }
    ::mlir::stablehlo::NegOp tblgen_NegOp_5;
    {
      ::mlir::Value tblgen_value_0 = (*tblgen_MulOp_4.getODSResults(0).begin());
      tblgen_NegOp_5 = ::mlir::stablehlo::NegOp::create(rewriter, odsLoc,
        /*operand=*/tblgen_value_0
      );
    }
    ::mlir::stablehlo::ComplexOp tblgen_ComplexOp_6;
    {
      ::mlir::Value tblgen_value_0 = nativeVar_3;
      ::mlir::Value tblgen_value_1 = (*tblgen_NegOp_5.getODSResults(0).begin());
      tblgen_ComplexOp_6 = ::mlir::stablehlo::ComplexOp::create(rewriter, odsLoc,
        /*lhs=*/tblgen_value_0,
        /*rhs=*/tblgen_value_1
      );
    }
    ::mlir::stablehlo::DivOp tblgen_DivOp_7;
    {
      ::llvm::SmallVector<::mlir::Value, 4> tblgen_values; (void)tblgen_values;
      ::mlir::stablehlo::DivOp::Properties tblgen_props; (void)tblgen_props;
      tblgen_values.push_back((*tblgen_ComplexOp_2.getODSResults(0).begin()));
      tblgen_values.push_back((*tblgen_ComplexOp_6.getODSResults(0).begin()));
      ::llvm::SmallVector<::mlir::Type, 4> tblgen_types; (void)tblgen_types;
      for (auto v: castedOp0.getODSResults(0)) {
        tblgen_types.push_back(v.getType());
      }
      tblgen_DivOp_7 = ::mlir::stablehlo::DivOp::create(rewriter, odsLoc, tblgen_types, tblgen_values, tblgen_props);
    }

    for (auto v: ::llvm::SmallVector<::mlir::Value, 4>{ tblgen_DivOp_7.getODSResults(0) }) {
      tblgen_repl_values.push_back(v);
    }

    rewriter.replaceOp(op0, tblgen_repl_values);
    return ::mlir::success();
  }
};

[[maybe_unused]] void populateWithGenerated(::mlir::RewritePatternSet &patterns) {
  patterns.add<TanOp_CompatiblityExpander>(patterns.getContext());
  patterns.add<TanOp_ComplexElementType_CompatiblityExpander>(patterns.getContext());
}
