1 //===- TypeUtilities.h - Helper function for type queries -------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines generic type utilities. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef MLIR_SUPPORT_TYPEUTILITIES_H 14 #define MLIR_SUPPORT_TYPEUTILITIES_H 15 16 #include "mlir/IR/Operation.h" 17 #include "llvm/ADT/STLExtras.h" 18 19 namespace mlir { 20 21 class Attribute; 22 class TupleType; 23 class Type; 24 class Value; 25 26 //===----------------------------------------------------------------------===// 27 // Utility Functions 28 //===----------------------------------------------------------------------===// 29 30 /// Return the element type or return the type itself. 31 Type getElementTypeOrSelf(Type type); 32 33 /// Return the element type or return the type itself. 34 Type getElementTypeOrSelf(Attribute attr); 35 Type getElementTypeOrSelf(Value val); 36 37 /// Get the types within a nested Tuple. A helper for the class method that 38 /// handles storage concerns, which is tricky to do in tablegen. 39 SmallVector<Type, 10> getFlattenedTypes(TupleType t); 40 41 /// Return true if the specified type is an opaque type with the specified 42 /// dialect and typeData. 43 bool isOpaqueTypeWithName(Type type, StringRef dialect, StringRef typeData); 44 45 /// Returns success if the given two shapes are compatible. That is, they have 46 /// the same size and each pair of the elements are equal or one of them is 47 /// dynamic. 48 LogicalResult verifyCompatibleShape(ArrayRef<int64_t> shape1, 49 ArrayRef<int64_t> shape2); 50 51 /// Returns success if the given two types have compatible shape. That is, 52 /// they are both scalars (not shaped), or they are both shaped types and at 53 /// least one is unranked or they have compatible dimensions. Dimensions are 54 /// compatible if at least one is dynamic or both are equal. The element type 55 /// does not matter. 56 LogicalResult verifyCompatibleShape(Type type1, Type type2); 57 58 //===----------------------------------------------------------------------===// 59 // Utility Iterators 60 //===----------------------------------------------------------------------===// 61 62 // An iterator for the element types of an op's operands of shaped types. 63 class OperandElementTypeIterator final 64 : public llvm::mapped_iterator<Operation::operand_iterator, 65 Type (*)(Value)> { 66 public: 67 using reference = Type; 68 69 /// Initializes the result element type iterator to the specified operand 70 /// iterator. 71 explicit OperandElementTypeIterator(Operation::operand_iterator it); 72 73 private: 74 static Type unwrap(Value value); 75 }; 76 77 using OperandElementTypeRange = iterator_range<OperandElementTypeIterator>; 78 79 // An iterator for the tensor element types of an op's results of shaped types. 80 class ResultElementTypeIterator final 81 : public llvm::mapped_iterator<Operation::result_iterator, 82 Type (*)(Value)> { 83 public: 84 using reference = Type; 85 86 /// Initializes the result element type iterator to the specified result 87 /// iterator. 88 explicit ResultElementTypeIterator(Operation::result_iterator it); 89 90 private: 91 static Type unwrap(Value value); 92 }; 93 94 using ResultElementTypeRange = iterator_range<ResultElementTypeIterator>; 95 96 } // end namespace mlir 97 98 #endif // MLIR_SUPPORT_TYPEUTILITIES_H 99