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_INVARIANT_CODE_MOTION_H_ 17 #define TENSORFLOW_COMPILER_XLA_SERVICE_WHILE_LOOP_INVARIANT_CODE_MOTION_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 // HLO pass that rewrites while loops to hoist loop invariant instructions in 26 // the while body into the computation that contains the while instruction. 27 28 class WhileLoopInvariantCodeMotion : public HloModulePass { 29 public: 30 // If `hoist_constants` is true then constants are always hoisted out of while 31 // loop bodies. Otherwise they are only hoisted out if they enable other 32 // non-trivial computations to be hoisted out. 33 // 34 // Setting `hoist_constants` to false can be help if LICM is run in the mid 35 // level HLO pipeline because hoisting constants out of while loop bodies can 36 // break optimizations like constant folding. 37 // Setting `hoist_size_inflating_ops` to false will forbid hoisting 38 // instructions where the size of the output(s) is larger than the size of the 39 // input(s). This is useful on platforms on which it's important to prevent 40 // blow-ups in memory size. 41 explicit WhileLoopInvariantCodeMotion(bool hoist_constants = false, 42 bool hoist_size_inflating_ops = true) hoist_constants_(hoist_constants)43 : hoist_constants_(hoist_constants), 44 hoist_size_inflating_ops_(hoist_size_inflating_ops) {} 45 ~WhileLoopInvariantCodeMotion() override = default; 46 name()47 absl::string_view name() const override { 48 return "while-loop-invariant-code-motion"; 49 } 50 StatusOr<bool> Run(HloModule* module) override; 51 52 private: 53 bool NotWorthHoistingIndividually(const HloInstruction& instruction); 54 StatusOr<bool> TryHoistingInvariantInstructionsFromWhileBody( 55 HloInstruction* while_instr); 56 57 bool hoist_constants_; 58 bool hoist_size_inflating_ops_; 59 }; 60 } // namespace xla 61 62 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_WHILE_LOOP_INVARIANT_CODE_MOTION_H_ 63