1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_HLO_PASS_FIX_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_HLO_PASS_FIX_H_
18 
19 #include <algorithm>
20 
21 #include "tensorflow/compiler/xla/service/hlo_module.h"
22 #include "tensorflow/compiler/xla/service/hlo_module_group.h"
23 #include "tensorflow/compiler/xla/status_macros.h"
24 #include "tensorflow/compiler/xla/statusor.h"
25 #include "tensorflow/compiler/xla/types.h"
26 #include "tensorflow/core/platform/macros.h"
27 
28 namespace xla {
29 
30 // Do an HLO pass to a fix point.
31 template <typename Pass>
32 class HloPassFix : public Pass {
33  public:
34   template <typename... Args>
HloPassFix(Args &&...args)35   explicit HloPassFix(Args&&... args) : Pass(args...) {}
36 
Run(HloModule * module)37   StatusOr<bool> Run(HloModule* module) override {
38     bool changed = false;
39     bool changed_this_iteration = true;
40     int64 iteration_count = 0;
41     int64 limit =
42         std::max(static_cast<int64>(1000), module->instruction_count());
43     VLOG(3) << "Running HloPassFix.";
44     while (changed_this_iteration) {
45       TF_ASSIGN_OR_RETURN(changed_this_iteration, Pass::Run(module));
46       changed |= changed_this_iteration;
47       VLOG(3) << "changed_this_iteration: " << changed_this_iteration;
48       ++iteration_count;
49       if (iteration_count == limit) {
50         LOG(ERROR)
51             << "Unexpectedly high number of iterations in HLO passes ("
52             << iteration_count
53             << ")\nIf compilation hangs here, please file a bug with XLA.";
54       }
55     }
56     return changed;
57   }
58 
RunOnModuleGroup(HloModuleGroup * module_group)59   StatusOr<bool> RunOnModuleGroup(HloModuleGroup* module_group) override {
60     bool changed = false;
61     bool changed_this_iteration = true;
62     int64 iteration_count = 0;
63     int64 limit = 1000;
64     for (const HloModule* module : module_group->modules()) {
65       limit = std::max<int64>(limit, module->instruction_count());
66     }
67     VLOG(3) << "Running HloPassFix.";
68     while (changed_this_iteration) {
69       TF_ASSIGN_OR_RETURN(changed_this_iteration,
70                           Pass::RunOnModuleGroup(module_group));
71       changed |= changed_this_iteration;
72       VLOG(3) << "changed_this_iteration: " << changed_this_iteration;
73       ++iteration_count;
74       if (iteration_count == limit) {
75         LOG(ERROR)
76             << "Unexpectedly high number of iterations in HLO passes ("
77             << iteration_count
78             << ")\nIf compilation hangs here, please file a bug with XLA.";
79       }
80     }
81     return changed;
82   }
83 };
84 
85 }  // namespace xla
86 
87 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_HLO_PASS_FIX_H_
88