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_COPY_INSERTION_H_ 17 #define TENSORFLOW_COMPILER_XLA_SERVICE_COPY_INSERTION_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 // Copy insertion is a legalization HLO pass which inserts copies (kCopy 28 // instructions) to eliminate several kinds of problems in the HLO module. 29 // 30 // (1) Entry parameter or a constant live out of the entry computation. Entry 31 // computation arguments and constants have different lifetimes than the 32 // computation result and cannot share the same allocation. Parameters and 33 // constants live out of non-entry computations do not need copies. 34 // 35 // (2) Different values which are simultaneously live and which must be held 36 // in the same buffer. This can occur in while bodies. Specifically, the 37 // while loop state (the arguments to the while instruction) is updated 38 // in-place and the update may clobber the value from the previous 39 // iteration before the previous value is dead. Computations called from 40 // kCall instructions do not need such copies because kCall has no update 41 // in-place semantics. 42 // 43 // (3) The buffer set of the root instruction of the entry computation must be 44 // unambiguous and distinct. That is, InstructionAliasSet::IsAmbiguous and 45 // InstructionAliasSet::IsDistinct return true. 46 class CopyInsertion : public HloModulePass { 47 public: name()48 absl::string_view name() const override { return "copy-insertion"; } 49 50 // backend specific function that decides whether an instruction 51 // can share buffer with its operand. 52 // 53 // TODO(b/80315712): Find a better way to tell whether a fusion can share 54 // buffer. 55 explicit CopyInsertion( 56 const HloDataflowAnalysis::CanShareBuffer& can_share_buffer = nullptr, 57 bool use_region_based_live_range_analysis = false) can_share_buffer_(can_share_buffer)58 : can_share_buffer_(can_share_buffer), 59 use_region_based_live_range_analysis_( 60 use_region_based_live_range_analysis) {} 61 62 // Run the pass on the given module. Returns whether the module was changed 63 // (copies were inserted). 64 StatusOr<bool> Run(HloModule* module) override; 65 66 // Try to remove as many copies from the module as possible without 67 // introducing live range interference. Only copy instructions that are 68 // eligible for copy elision are considered for removal. 69 // If check_live_range_ordering is true, check that live ranges are ordered 70 // in all the existing aliased buffers. 71 Status RemoveUnnecessaryCopies(HloOrdering* ordering, HloModule* module, 72 bool check_live_range_ordering = false); 73 74 // Add copies to address special constraints on the roots of computations not 75 // related to live range interference: 76 // 77 // (1) Entry computation root must be unambiguous and distinct. 78 // 79 // (2) Any computation called by a kCall instruction must have an 80 // unambiguous root. 81 // 82 // (3) Constants and parameters cannot be live out of the entry computation 83 // 84 Status AddSpecialCaseCopies(HloModule* module); 85 86 protected: 87 // Override which requires the caller to pass in a call graph. 88 virtual Status AddSpecialCaseCopies(const CallGraph& call_graph, 89 HloModule* module); 90 91 // Add copies for conditional instructions. 92 virtual Status AddCopiesForConditional(const HloAliasAnalysis& alias_analysis, 93 HloInstruction* conditional); 94 95 // Backend specific function that decides whether an instruction can share 96 // buffer with its operand. 97 HloDataflowAnalysis::CanShareBuffer can_share_buffer_; 98 99 private: 100 Status AddCopiesToResolveInterference(HloModule* module); 101 // TODO(b/189898980): the region based live range analysis currently 102 // does not enforce a strict ordering of the merged live ranges. This may 103 // cause problems for parallel workloads (e.g., in SPMD). 104 bool use_region_based_live_range_analysis_; 105 }; 106 107 } // namespace xla 108 109 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_COPY_INSERTION_H_ 110