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
19namespace mlir {
20
21class Attribute;
22class TupleType;
23class Type;
24class TypeRange;
25class Value;
26
27//===----------------------------------------------------------------------===//
28// Utility Functions
29//===----------------------------------------------------------------------===//
30
31/// Return the element type or return the type itself.
32Type getElementTypeOrSelf(Type type);
33
34/// Return the element type or return the type itself.
35Type getElementTypeOrSelf(Attribute attr);
36Type getElementTypeOrSelf(Value val);
37
38/// Get the types within a nested Tuple. A helper for the class method that
39/// handles storage concerns, which is tricky to do in tablegen.
40SmallVector<Type, 10> getFlattenedTypes(TupleType t);
41
42/// Return true if the specified type is an opaque type with the specified
43/// dialect and typeData.
44bool isOpaqueTypeWithName(Type type, StringRef dialect, StringRef typeData);
45
46/// Returns success if the given two shapes are compatible. That is, they have
47/// the same size and each pair of the elements are equal or one of them is
48/// dynamic.
49LogicalResult verifyCompatibleShape(ArrayRef<int64_t> shape1,
50 ArrayRef<int64_t> shape2);
51
52/// Returns success if the given two types have compatible shape. That is,
53/// they are both scalars (not shaped), or they are both shaped types and at
54/// least one is unranked or they have compatible dimensions. Dimensions are
55/// compatible if at least one is dynamic or both are equal. The element type
56/// does not matter.
57LogicalResult verifyCompatibleShape(Type type1, Type type2);
58
59/// Returns success if the given two arrays have the same number of elements and
60/// each pair wise entries have compatible shape.
61LogicalResult verifyCompatibleShapes(TypeRange types1, TypeRange types2);
62
63/// Returns success if all given types have compatible shapes. That is, they are
64/// all scalars (not shaped), or they are all shaped types and any ranked shapes
65/// have compatible dimensions. The element type does not matter.
66LogicalResult verifyCompatibleShapes(TypeRange types);
67
68/// Dimensions are compatible if all non-dynamic dims are equal.
69LogicalResult verifyCompatibleDims(ArrayRef<int64_t> dims);
70
71/// Insert a set of `newTypes` into `oldTypes` at the given `indices`. If any
72/// types are inserted, `storage` is used to hold the new type list. The new
73/// type list is returned. `indices` must be sorted by increasing index.
74TypeRange insertTypesInto(TypeRange oldTypes, ArrayRef<unsigned> indices,
75 TypeRange newTypes, SmallVectorImpl<Type> &storage);
76
77/// Filters out any elements referenced by `indices`. If any types are removed,
78/// `storage` is used to hold the new type list. Returns the new type list.
79TypeRange filterTypesOut(TypeRange types, const BitVector &indices,
80 SmallVectorImpl<Type> &storage);
81
82//===----------------------------------------------------------------------===//
83// Utility Iterators
84//===----------------------------------------------------------------------===//
85
86// An iterator for the element types of an op's operands of shaped types.
87class OperandElementTypeIterator final
88 : public llvm::mapped_iterator_base<OperandElementTypeIterator,
89 Operation::operand_iterator, Type> {
90public:
91 using BaseT::BaseT;
92
93 /// Map the element to the iterator result type.
94 Type mapElement(Value value) const;
95};
96
97using OperandElementTypeRange = iterator_range<OperandElementTypeIterator>;
98
99// An iterator for the tensor element types of an op's results of shaped types.
100class ResultElementTypeIterator final
101 : public llvm::mapped_iterator_base<ResultElementTypeIterator,
102 Operation::result_iterator, Type> {
103public:
104 using BaseT::BaseT;
105
106 /// Map the element to the iterator result type.
107 Type mapElement(Value value) const;
108};
109
110using ResultElementTypeRange = iterator_range<ResultElementTypeIterator>;
111
112} // namespace mlir
113
114#endif // MLIR_IR_TYPEUTILITIES_H
115

source code of mlir/include/mlir/IR/TypeUtilities.h