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