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_IR_TYPEUTILITIES_H
14 #define MLIR_IR_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 /// Returns success if the given two arrays have the same number of elements and
59 /// each pair wise entries have compatible shape.
60 LogicalResult verifyCompatibleShapes(TypeRange types1, TypeRange types2);
61 
62 /// Returns success if all given types have compatible shapes. That is, they are
63 /// all scalars (not shaped), or they are all shaped types and any ranked shapes
64 /// have compatible dimensions. The element type does not matter.
65 LogicalResult verifyCompatibleShapes(TypeRange types);
66 
67 /// Dimensions are compatible if all non-dynamic dims are equal.
68 LogicalResult verifyCompatibleDims(ArrayRef<int64_t> dims);
69 
70 //===----------------------------------------------------------------------===//
71 // Utility Iterators
72 //===----------------------------------------------------------------------===//
73 
74 // An iterator for the element types of an op's operands of shaped types.
75 class OperandElementTypeIterator final
76     : public llvm::mapped_iterator_base<OperandElementTypeIterator,
77                                         Operation::operand_iterator, Type> {
78 public:
79   using BaseT::BaseT;
80 
81   /// Map the element to the iterator result type.
82   Type mapElement(Value value) const;
83 };
84 
85 using OperandElementTypeRange = iterator_range<OperandElementTypeIterator>;
86 
87 // An iterator for the tensor element types of an op's results of shaped types.
88 class ResultElementTypeIterator final
89     : public llvm::mapped_iterator_base<ResultElementTypeIterator,
90                                         Operation::result_iterator, Type> {
91 public:
92   using BaseT::BaseT;
93 
94   /// Map the element to the iterator result type.
95   Type mapElement(Value value) const;
96 };
97 
98 using ResultElementTypeRange = iterator_range<ResultElementTypeIterator>;
99 
100 } // namespace mlir
101 
102 #endif // MLIR_IR_TYPEUTILITIES_H
103