You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

cast_eliminate.h 2.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_CAST_ELIMINATE_H_
  17. #define MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_CAST_ELIMINATE_H_
  18. #include "frontend/optimizer/anf_visitor.h"
  19. #include "frontend/optimizer/irpass.h"
  20. #include "frontend/optimizer/optimizer.h"
  21. namespace mindspore {
  22. namespace opt {
  23. namespace irpass {
  24. // {prim::kPrimCast, X, T}
  25. class CastSameTypeEliminater : public AnfVisitor {
  26. public:
  27. AnfNodePtr operator()(const OptimizerPtr &, const AnfNodePtr &node) override;
  28. void Visit(const AnfNodePtr &node) override;
  29. void Reset() {
  30. src_ = nullptr;
  31. tgt_ = nullptr;
  32. }
  33. private:
  34. AnfNodePtr src_{nullptr}, tgt_{nullptr};
  35. };
  36. // {prim::kPrimCast, {prim::kPrimCast, X, Y}, T}
  37. class TwoCastEliminater : public AnfVisitor {
  38. public:
  39. AnfNodePtr operator()(const OptimizerPtr &, const AnfNodePtr &node) override;
  40. void Visit(const AnfNodePtr &node) override;
  41. void Reset() {
  42. x_ = nullptr;
  43. t_ = nullptr;
  44. }
  45. private:
  46. AnfNodePtr x_{nullptr}, t_{nullptr};
  47. };
  48. class CastEliminater : public OptimizerCaller {
  49. public:
  50. CastEliminater() : cast_same_type_eliminater_(), two_cast_eliminater_() {}
  51. ~CastEliminater() = default;
  52. AnfNodePtr operator()(const OptimizerPtr &optimizer, const AnfNodePtr &node) override {
  53. auto new_node = cast_same_type_eliminater_(optimizer, node);
  54. if (new_node != nullptr) {
  55. return new_node;
  56. }
  57. new_node = two_cast_eliminater_(optimizer, node);
  58. if (new_node != nullptr) {
  59. return new_node;
  60. }
  61. return nullptr;
  62. }
  63. private:
  64. CastSameTypeEliminater cast_same_type_eliminater_;
  65. TwoCastEliminater two_cast_eliminater_;
  66. };
  67. } // namespace irpass
  68. } // namespace opt
  69. } // namespace mindspore
  70. #endif // MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_CAST_ELIMINATE_H_