Home | History | Annotate | Download | only in service
      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 #include "tensorflow/compiler/xla/service/hlo_reachability.h"
     17 
     18 namespace xla {
     19 
     20 HloReachabilityMap::HloReachabilityMap(
     21     const std::list<HloInstruction*>& instructions)
     22     : size_(instructions.size()) {
     23   bit_vectors_.reserve(size_);
     24   for (const HloInstruction* hlo : instructions) {
     25     indices_[hlo] = bit_vectors_.size();
     26     bit_vectors_.emplace_back(size_);
     27   }
     28   CHECK_EQ(size_, indices_.size());  // instructions should be unique
     29 }
     30 
     31 bool HloReachabilityMap::SetReachabilityToUnion(
     32     tensorflow::gtl::ArraySlice<const HloInstruction*> inputs,
     33     const HloInstruction* instruction) {
     34   BitVector& bit_vector = GetBitVector(instruction);
     35   tmp_bit_vector_ = bit_vector;
     36 
     37   // If instruction is part of inputs, don't reset the bit_vector.
     38   if (std::find(inputs.begin(), inputs.end(), instruction) == inputs.end()) {
     39     bit_vector.SetToZero();
     40   }
     41   bit_vector.Set(GetIndex(instruction));
     42   for (const HloInstruction* input : inputs) {
     43     bit_vector.OrWith(GetBitVector(input));
     44   }
     45 
     46   return bit_vector != tmp_bit_vector_;
     47 }
     48 
     49 void HloReachabilityMap::SetReachable(const HloInstruction* a,
     50                                       const HloInstruction* b) {
     51   GetBitVector(b).Set(GetIndex(a));
     52 }
     53 
     54 bool HloReachabilityMap::IsReachable(const HloInstruction* a,
     55                                      const HloInstruction* b) const {
     56   return GetBitVector(b).Get(GetIndex(a));
     57 }
     58 
     59 bool HloReachabilityMap::IsConnected(const HloInstruction* a,
     60                                      const HloInstruction* b) const {
     61   return IsReachable(a, b) || IsReachable(b, a);
     62 }
     63 
     64 }  // namespace xla
     65