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