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.

pass_manager.cc 3.3 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * Copyright 2019-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. #include "backend/optimizer/common/pass_manager.h"
  17. #include <sys/time.h>
  18. #include <deque>
  19. #include <string>
  20. #include "ir/anf.h"
  21. #include "ir/func_graph.h"
  22. #include "ir/manager.h"
  23. #include "utils/ms_context.h"
  24. #include "debug/anf_ir_dump.h"
  25. namespace mindspore {
  26. namespace opt {
  27. const std::vector<PassPtr> &PassManager::Passes() const { return passes_; }
  28. void PassManager::AddPass(const PassPtr &pass) {
  29. if (pass != nullptr) {
  30. passes_.push_back(pass);
  31. }
  32. }
  33. bool PassManager::Run(const FuncGraphPtr &func_graph, const std::vector<PassPtr> &passes) const {
  34. if (func_graph == nullptr) {
  35. return false;
  36. }
  37. auto context_ptr = MsContext::GetInstance();
  38. MS_EXCEPTION_IF_NULL(context_ptr);
  39. bool save_graphs = context_ptr->get_param<bool>(MS_CTX_SAVE_GRAPHS_FLAG);
  40. bool changed = false;
  41. size_t num = 0;
  42. for (const auto &pass : passes) {
  43. if (pass != nullptr) {
  44. #if defined(_WIN32) || defined(_WIN64)
  45. auto start_time = std::chrono::steady_clock::now();
  46. #else
  47. struct timeval start_time {};
  48. struct timeval end_time {};
  49. (void)gettimeofday(&start_time, nullptr);
  50. #endif
  51. if (pass->Run(func_graph)) {
  52. changed = true;
  53. }
  54. #if defined(_WIN32) || defined(_WIN64)
  55. auto end_time = std::chrono::steady_clock::now();
  56. std::chrono::duration<double, std::ratio<1, 1000000>> cost = end_time - start_time;
  57. MS_LOG(INFO) << "Run pass hwopt_" + name() + "_" << num << "_" + pass->name() + " in " << cost.count() << " us";
  58. #else
  59. (void)gettimeofday(&end_time, nullptr);
  60. // time unit: us
  61. uint64_t cost = 1000000 * static_cast<uint64_t>(end_time.tv_sec - start_time.tv_sec);
  62. cost += static_cast<uint64_t>(end_time.tv_usec - start_time.tv_usec);
  63. MS_LOG(INFO) << "Run pass hwopt_" + name() + "_" << num << "_" + pass->name() + " in " << cost << " us";
  64. #endif
  65. static const auto enable_dump = (common::GetEnv("ENV_NO_DUMP_BE_PASS_IR") != "1");
  66. if (save_graphs && enable_dump) {
  67. std::ostringstream oss;
  68. oss << "verbose_ir_files"
  69. << "/";
  70. oss << "hwopt_" + name() + "_" + std::to_string(num) + "_" + pass->name() + ".ir";
  71. DumpIR(oss.str(), func_graph, true);
  72. }
  73. num++;
  74. }
  75. }
  76. return changed;
  77. }
  78. bool PassManager::Run(const FuncGraphPtr &func_graph) const {
  79. bool changed = false;
  80. // run all passes
  81. bool change = true;
  82. while (change) {
  83. change = Run(func_graph, passes_);
  84. changed = change || changed;
  85. if (run_only_once_) {
  86. break;
  87. }
  88. }
  89. return changed;
  90. }
  91. } // namespace opt
  92. } // namespace mindspore