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/buffer_liveness.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 // fusion_can_share_buffer: backend specific function that decides whether a 51 // fusion 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 CopyInsertion(const HloDataflowAnalysis::FusionCanShareBufferFunction& 56 fusion_can_share_buffer = nullptr) fusion_can_share_buffer_(fusion_can_share_buffer)57 : fusion_can_share_buffer_(fusion_can_share_buffer) {} 58 59 // Run the pass on the given module. Returns whether the module was changed 60 // (copies were inserted). 61 StatusOr<bool> Run(HloModule* module) override; 62 63 // The CPU and GPU backend need additional copies added due to deficiencies in 64 // buffer assignment. Specifically, copies are needed for constants live-out 65 // of computations, and for values which are live-in and live-out of the same 66 // computation. These copies are needed because buffer-assignment uses a 67 // computation-scoped analyis (TuplePointsToAnalysis) and has limited 68 // visibility across computation boundaries. This method adds these necessary 69 // copies. Returns whether the module was modified. 70 // 71 // TODO(b/62548313): Remove this when buffer assignment is module-scoped. 72 static StatusOr<bool> AddCopiesForBufferAssignment(HloModule* module); 73 74 // Try to remove as many copies from the module as possible without 75 // introducing live range interference. Only copy instructions that are 76 // eligible for copy elision are considered for removal. 77 Status RemoveUnnecessaryCopies(const HloOrdering& ordering, 78 HloModule* module); 79 80 // Add copies to address special constraints on the roots of computations not 81 // related to live range interference: 82 // 83 // (1) Entry computation root must be unambiguous and distinct. 84 // 85 // (2) Any computation called by a kCall instruction must have an 86 // unambiguous root. 87 // 88 // (3) Constants and parameters cannot be live out of the entry computation 89 // 90 Status AddSpecialCaseCopies(HloModule* module); 91 92 // Verifies that no HLO values have interfering live ranges using the given 93 // ordering. 94 Status VerifyNoLiveRangeInterference(const HloOrdering& ordering, 95 HloModule* module); 96 97 protected: 98 // Override which requires the caller to pass in a call graph. 99 virtual Status AddSpecialCaseCopies(const CallGraph& call_graph, 100 HloModule* module); 101 102 private: 103 Status AddCopiesToResolveInterference(HloModule* module); 104 105 // Backend specific function that decides whether a fusion can share buffer 106 // with its operand. 107 HloDataflowAnalysis::FusionCanShareBufferFunction fusion_can_share_buffer_; 108 }; 109 110 } // namespace xla 111 112 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_COPY_INSERTION_H_ 113