1 /* Copyright 2020 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_LOOP_SCHEDULE_LINEARIZER_H_ 17 #define TENSORFLOW_COMPILER_XLA_SERVICE_LOOP_SCHEDULE_LINEARIZER_H_ 18 19 #include "tensorflow/compiler/xla/service/hlo_alias_analysis.h" 20 #include "tensorflow/compiler/xla/service/hlo_computation.h" 21 #include "tensorflow/compiler/xla/service/hlo_instruction.h" 22 #include "tensorflow/compiler/xla/service/hlo_module.h" 23 #include "tensorflow/compiler/xla/service/hlo_pass_interface.h" 24 25 namespace xla { 26 27 // Adds control dependency edges from instructions which "write" values inside 28 // the loop, to instructions which "read" those same values, in order to avoid 29 // extraneous copies. This is not always possible with our buffer layout 30 // constraints (that is, assuming that every element of the tuple the while loop 31 // operates upon gets the same buffer) as it may create cycles (an easiest 32 // example of a dependency cycle is a loop doing `(a, b) = (b, a)`). Thus we 33 // take a best-effort approach instead: add dependency edges only if we can show 34 // they don't create a cycle. 35 class LoopScheduleLinearizer : public HloModulePass { 36 public: name()37 absl::string_view name() const override { return "loop-schedule-linearizer"; } 38 39 explicit LoopScheduleLinearizer( 40 const HloDataflowAnalysis::CanShareBuffer& can_share_buffer = nullptr) can_share_buffer_(can_share_buffer)41 : can_share_buffer_(can_share_buffer) {} 42 43 StatusOr<bool> Run(HloModule* module) override; 44 45 private: 46 // Backend specific function that decides whether an instruction can share 47 // buffer with its operand. 48 HloDataflowAnalysis::CanShareBuffer can_share_buffer_; 49 }; 50 51 } // namespace xla 52 53 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_LOOP_SCHEDULE_LINEARIZER_H_ 54