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 "absl/container/flat_hash_map.h" 20 #include "tensorflow/compiler/xla/service/dfs_hlo_visitor_with_default.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/logical_buffer.h" 24 #include "tensorflow/compiler/xla/statusor.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 size_t num_logical_buffers() const { return logical_buffers_.size(); } 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 HandleAddDependency(HloInstruction* add_dependency) override; 70 Status HandleCustomCall(HloInstruction* custom_call) override; 71 72 // A map from the buffer ID to the logical buffer 73 std::vector<std::unique_ptr<LogicalBuffer>> logical_buffers_; 74 75 // A map from an hlo + shape index to the logical buffer representing 76 // the appropriate output. 77 absl::flat_hash_map<std::pair<const HloInstruction*, const ShapeIndex>, 78 LogicalBuffer*> 79 output_buffers_; 80 }; 81 82 } // namespace xla 83 84 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_LOGICAL_BUFFER_ANALYSIS_H_ 85