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_LOGICAL_BUFFER_ANALYSIS_H_ 17 #define TENSORFLOW_COMPILER_XLA_SERVICE_LOGICAL_BUFFER_ANALYSIS_H_ 18 19 #include "tensorflow/compiler/xla/service/dfs_hlo_visitor_with_default.h" 20 #include "tensorflow/compiler/xla/service/hlo_instruction.h" 21 #include "tensorflow/compiler/xla/service/hlo_module.h" 22 #include "tensorflow/compiler/xla/service/logical_buffer.h" 23 #include "tensorflow/compiler/xla/statusor.h" 24 #include "tensorflow/core/lib/hash/hash.h" 25 26 namespace xla { 27 // A class to create all the logical buffers defined by the HLO ops in a module. 28 class LogicalBufferAnalysis : public DfsHloVisitorWithDefault { 29 public: 30 // Runs points-to analysis on 'module'. 31 static StatusOr<std::unique_ptr<LogicalBufferAnalysis>> Run( 32 const HloModule* module); 33 34 // Returns the logical buffer with the given ID. 35 LogicalBuffer& GetBuffer(LogicalBuffer::Id id) const; 36 37 // Returns the logical buffer that represents the output of a given HLO 38 // at a given index. 39 LogicalBuffer& GetBuffer(HloInstruction* instruction, 40 const ShapeIndex& index) const; 41 logical_buffers()42 const std::vector<std::unique_ptr<LogicalBuffer>>& logical_buffers() const { 43 return logical_buffers_; 44 } num_logical_buffers()45 LogicalBuffer::Id num_logical_buffers() const { return next_buffer_id_; } 46 47 private: LogicalBufferAnalysis(const HloModule * module)48 explicit LogicalBufferAnalysis(const HloModule* module) : module_(module) {} 49 Status Analyze(); 50 51 // The module this analysis is performed on. 52 const HloModule* module_; 53 54 // Create a new logical buffer and return a reference to it. The newly created 55 // buffer is stored in an internal vector of LogicalBuffers and can be 56 // accessed with GetBuffer. 57 void NewLogicalBuffer(HloInstruction* instruction, const ShapeIndex& index); 58 59 Status DefaultAction(HloInstruction* hlo_instruction) override; 60 Status HandleTuple(HloInstruction* tuple) override; 61 Status HandleGetTupleElement(HloInstruction* get_tuple_element) override; 62 Status HandleBitcast(HloInstruction* bitcast) override; 63 Status HandleDomain(HloInstruction* domain) override; 64 Status HandleCopy(HloInstruction* copy) override; 65 Status HandleCopyStart(HloInstruction* copy_start) override; 66 Status HandleCopyDone(HloInstruction* copy_done) override; 67 Status HandleRecvDone(HloInstruction* recv_done) override; 68 Status HandleSend(HloInstruction* send) override; 69 Status HandleTupleSelect(HloInstruction* tuple_select) override; 70 Status HandleAddDependency(HloInstruction* add_dependency) override; 71 Status HandleCustomCall(HloInstruction* custom_call) override; 72 73 // A map from the buffer ID to the logical buffer 74 std::vector<std::unique_ptr<LogicalBuffer>> logical_buffers_; 75 76 struct Hasher { operatorHasher77 size_t operator()( 78 std::pair<const HloInstruction*, const ShapeIndex> p) const { 79 size_t inst_hash = tensorflow::hash<const HloInstruction*>()(p.first); 80 for (auto index = p.second.begin(); index != p.second.end(); ++index) { 81 inst_hash = tensorflow::Hash64Combine(*index, inst_hash); 82 } 83 return inst_hash; 84 } 85 }; 86 87 // A map from an hlo + shape index to the logical buffer representing 88 // the appropriate output. 89 std::unordered_map<std::pair<const HloInstruction*, const ShapeIndex>, 90 LogicalBuffer*, Hasher> 91 output_buffers_; 92 93 // The ID of the next logical buffer created. 94 LogicalBuffer::Id next_buffer_id_ = 0; 95 }; 96 97 } // namespace xla 98 99 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_LOGICAL_BUFFER_ANALYSIS_H_ 100