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_HLO_QUERY_H_ 17 #define TENSORFLOW_COMPILER_XLA_SERVICE_HLO_QUERY_H_ 18 19 #include "absl/container/flat_hash_set.h" 20 #include "tensorflow/compiler/xla/service/hlo_computation.h" 21 #include "tensorflow/compiler/xla/service/hlo_instruction.h" 22 23 namespace xla { 24 25 // Helper interface for making queries about the HLO IR. 26 namespace hlo_query { 27 28 // Returns whether the instruction provided is a constant rank-0 float32, and 29 // if so, places the constant value into out. 30 // Precondition: out != nullptr 31 bool IsConstantR0F32(HloInstruction* instruction, float* out); 32 33 // Returns whether all of an instruction's operands are of the types constants 34 // and parameters. 35 bool AllOperandsAreParametersOrConstants(const HloInstruction& instruction); 36 37 // Returns whether all of an instruction's operands are parameters. 38 bool AllOperandsAreParameters(const HloInstruction& instruction); 39 40 // Returns whether all of an instruction's operands are constants. 41 bool AllOperandsAreConstants(const HloInstruction& instruction); 42 43 // Returns whether the instruction is a scalar constant. 44 bool IsScalarConstant(const HloInstruction* instruction); 45 46 // Determines whether the given computation contains an instruction with one of 47 // the given opcodes. Checks both comp's instructions and the instructions of 48 // any computations nested within it. 49 bool ContainsInstrWithOpcode(const HloComputation* comp, 50 const absl::flat_hash_set<HloOpcode>& opcodes); 51 52 // Returns an operand of an instruction with the given opcode. If there are 53 // multiple matching operands, then the first matching operand is returned. If 54 // there are no matching operands then nullptr is returned. 55 HloInstruction* GetMatchingOperand( 56 const std::function<bool(const HloInstruction*)>& matcher, 57 HloInstruction* instruction); 58 59 // Returns whether a binary instruction has a matching operand. Sets 60 // matching_operand to the matching operand and the other operand to 61 // other_operand. Note: in the case where both operands match, the first operand 62 // of the instruction is returned. 63 bool MatchBinaryInstructionOperand( 64 const std::function<bool(const HloInstruction*)>& matcher, 65 HloInstruction* instruction, HloInstruction** matching_operand, 66 HloInstruction** other_operand); 67 68 // Returns whether a binary instruction has a operand with a given opcode. 69 // This is a special case of MatchingBinaryInstructionOperand. 70 bool MatchBinaryInstructionOperandOpcode(HloOpcode opcode, 71 HloInstruction* instruction, 72 HloInstruction** matching_operand, 73 HloInstruction** other_operand); 74 75 } // namespace hlo_query 76 } // namespace xla 77 78 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_HLO_QUERY_H_ 79