1 /* Copyright 2018 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_WHILE_LOOP_CONSTANT_SINKING_H_ 17 #define TENSORFLOW_COMPILER_XLA_SERVICE_WHILE_LOOP_CONSTANT_SINKING_H_ 18 19 #include "tensorflow/compiler/xla/service/hlo_module.h" 20 #include "tensorflow/compiler/xla/service/hlo_pass_interface.h" 21 #include "tensorflow/compiler/xla/statusor.h" 22 23 namespace xla { 24 25 // Sinks while loop invariant values that happen to be constants into the while 26 // loop body and conditional. This is probably not a win in isolation but may 27 // unlock further optimizations like constant folding. 28 // 29 // state = (..., const, ...) 30 // while (pred(state)) { 31 // (..., v, ...) = state 32 // use(v) 33 // state = (..., v, ...) 34 // } 35 // 36 // => 37 // 38 // state = (..., const, ...) 39 // while (pred(state)) { 40 // (..., v, ...) = state 41 // use(const) 42 // state = (..., v, ...) 43 // } 44 // 45 // Note that it leaves the `v` in place to keep that component of the state 46 // tuple trivially loop invariant. WhileLoopSimplifier will later get rid of 47 // `v`. 48 // 49 // TODO(b/79121449): We should also sink broadcasts of constants. 50 class WhileLoopConstantSinking : public HloModulePass { 51 public: 52 ~WhileLoopConstantSinking() override = default; 53 name()54 absl::string_view name() const override { 55 return "while-loop-constant-sinking"; 56 } 57 58 StatusOr<bool> Run(HloModule* module) override; 59 60 private: 61 StatusOr<bool> TrySinkingConstantsIntoWhileLoop(HloInstruction* while_instr); 62 }; 63 } // namespace xla 64 65 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_WHILE_LOOP_CONSTANT_SINKING_H_ 66