xref: /llvm-project/mlir/include/mlir-c/AffineMap.h (revision 7557530f428a2f226d8d925c33d527dfcfdcb0c5)
1 //===-- mlir-c/AffineMap.h - C API for MLIR Affine maps -----------*- C -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM
4 // Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef MLIR_C_AFFINEMAP_H
11 #define MLIR_C_AFFINEMAP_H
12 
13 #include "mlir-c/AffineExpr.h"
14 #include "mlir-c/IR.h"
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 //===----------------------------------------------------------------------===//
21 // Opaque type declarations.
22 //
23 // Types are exposed to C bindings as structs containing opaque pointers. They
24 // are not supposed to be inspected from C. This allows the underlying
25 // representation to change without affecting the API users. The use of structs
26 // instead of typedefs enables some type safety as structs are not implicitly
27 // convertible to each other.
28 //
29 // Instances of these types may or may not own the underlying object. The
30 // ownership semantics is defined by how an instance of the type was obtained.
31 //===----------------------------------------------------------------------===//
32 
33 #define DEFINE_C_API_STRUCT(name, storage)                                     \
34   struct name {                                                                \
35     storage *ptr;                                                              \
36   };                                                                           \
37   typedef struct name name
38 
39 DEFINE_C_API_STRUCT(MlirAffineMap, const void);
40 
41 #undef DEFINE_C_API_STRUCT
42 
43 /// Gets the context that the given affine map was created with
44 MLIR_CAPI_EXPORTED MlirContext mlirAffineMapGetContext(MlirAffineMap affineMap);
45 
46 /// Checks whether an affine map is null.
mlirAffineMapIsNull(MlirAffineMap affineMap)47 static inline bool mlirAffineMapIsNull(MlirAffineMap affineMap) {
48   return !affineMap.ptr;
49 }
50 
51 /// Checks if two affine maps are equal.
52 MLIR_CAPI_EXPORTED bool mlirAffineMapEqual(MlirAffineMap a1, MlirAffineMap a2);
53 
54 /// Prints an affine map by sending chunks of the string representation and
55 /// forwarding `userData to `callback`. Note that the callback may be called
56 /// several times with consecutive chunks of the string.
57 MLIR_CAPI_EXPORTED void mlirAffineMapPrint(MlirAffineMap affineMap,
58                                            MlirStringCallback callback,
59                                            void *userData);
60 
61 /// Prints the affine map to the standard error stream.
62 MLIR_CAPI_EXPORTED void mlirAffineMapDump(MlirAffineMap affineMap);
63 
64 /// Creates a zero result affine map with no dimensions or symbols in the
65 /// context. The affine map is owned by the context.
66 MLIR_CAPI_EXPORTED MlirAffineMap mlirAffineMapEmptyGet(MlirContext ctx);
67 
68 /// Creates a zero result affine map of the given dimensions and symbols in the
69 /// context. The affine map is owned by the context.
70 MLIR_CAPI_EXPORTED MlirAffineMap mlirAffineMapZeroResultGet(
71     MlirContext ctx, intptr_t dimCount, intptr_t symbolCount);
72 
73 /// Creates an affine map with results defined by the given list of affine
74 /// expressions. The map resulting map also has the requested number of input
75 /// dimensions and symbols, regardless of them being used in the results.
76 
77 MLIR_CAPI_EXPORTED MlirAffineMap mlirAffineMapGet(MlirContext ctx,
78                                                   intptr_t dimCount,
79                                                   intptr_t symbolCount,
80                                                   intptr_t nAffineExprs,
81                                                   MlirAffineExpr *affineExprs);
82 
83 /// Creates a single constant result affine map in the context. The affine map
84 /// is owned by the context.
85 MLIR_CAPI_EXPORTED MlirAffineMap mlirAffineMapConstantGet(MlirContext ctx,
86                                                           int64_t val);
87 
88 /// Creates an affine map with 'numDims' identity in the context. The affine map
89 /// is owned by the context.
90 MLIR_CAPI_EXPORTED MlirAffineMap
91 mlirAffineMapMultiDimIdentityGet(MlirContext ctx, intptr_t numDims);
92 
93 /// Creates an identity affine map on the most minor dimensions in the context.
94 /// The affine map is owned by the context. The function asserts that the number
95 /// of dimensions is greater or equal to the number of results.
96 MLIR_CAPI_EXPORTED MlirAffineMap
97 mlirAffineMapMinorIdentityGet(MlirContext ctx, intptr_t dims, intptr_t results);
98 
99 /// Creates an affine map with a permutation expression and its size in the
100 /// context. The permutation expression is a non-empty vector of integers.
101 /// The elements of the permutation vector must be continuous from 0 and cannot
102 /// be repeated (i.e. `[1,2,0]` is a valid permutation. `[2,0]` or `[1,1,2]` is
103 /// an invalid permutation.) The affine map is owned by the context.
104 MLIR_CAPI_EXPORTED MlirAffineMap mlirAffineMapPermutationGet(
105     MlirContext ctx, intptr_t size, unsigned *permutation);
106 
107 /// Checks whether the given affine map is an identity affine map. The function
108 /// asserts that the number of dimensions is greater or equal to the number of
109 /// results.
110 MLIR_CAPI_EXPORTED bool mlirAffineMapIsIdentity(MlirAffineMap affineMap);
111 
112 /// Checks whether the given affine map is a minor identity affine map.
113 MLIR_CAPI_EXPORTED bool mlirAffineMapIsMinorIdentity(MlirAffineMap affineMap);
114 
115 /// Checks whether the given affine map is an empty affine map.
116 MLIR_CAPI_EXPORTED bool mlirAffineMapIsEmpty(MlirAffineMap affineMap);
117 
118 /// Checks whether the given affine map is a single result constant affine
119 /// map.
120 MLIR_CAPI_EXPORTED bool mlirAffineMapIsSingleConstant(MlirAffineMap affineMap);
121 
122 /// Returns the constant result of the given affine map. The function asserts
123 /// that the map has a single constant result.
124 MLIR_CAPI_EXPORTED int64_t
125 mlirAffineMapGetSingleConstantResult(MlirAffineMap affineMap);
126 
127 /// Returns the number of dimensions of the given affine map.
128 MLIR_CAPI_EXPORTED intptr_t mlirAffineMapGetNumDims(MlirAffineMap affineMap);
129 
130 /// Returns the number of symbols of the given affine map.
131 MLIR_CAPI_EXPORTED intptr_t mlirAffineMapGetNumSymbols(MlirAffineMap affineMap);
132 
133 /// Returns the number of results of the given affine map.
134 MLIR_CAPI_EXPORTED intptr_t mlirAffineMapGetNumResults(MlirAffineMap affineMap);
135 
136 /// Returns the result at the given position.
137 MLIR_CAPI_EXPORTED MlirAffineExpr
138 mlirAffineMapGetResult(MlirAffineMap affineMap, intptr_t pos);
139 
140 /// Returns the number of inputs (dimensions + symbols) of the given affine
141 /// map.
142 MLIR_CAPI_EXPORTED intptr_t mlirAffineMapGetNumInputs(MlirAffineMap affineMap);
143 
144 /// Checks whether the given affine map represents a subset of a symbol-less
145 /// permutation map.
146 MLIR_CAPI_EXPORTED bool
147 mlirAffineMapIsProjectedPermutation(MlirAffineMap affineMap);
148 
149 /// Checks whether the given affine map represents a symbol-less permutation
150 /// map.
151 MLIR_CAPI_EXPORTED bool mlirAffineMapIsPermutation(MlirAffineMap affineMap);
152 
153 /// Returns the affine map consisting of the `resultPos` subset.
154 MLIR_CAPI_EXPORTED MlirAffineMap mlirAffineMapGetSubMap(MlirAffineMap affineMap,
155                                                         intptr_t size,
156                                                         intptr_t *resultPos);
157 
158 /// Returns the affine map consisting of the most major `numResults` results.
159 /// Returns the null AffineMap if the `numResults` is equal to zero.
160 /// Returns the `affineMap` if `numResults` is greater or equals to number of
161 /// results of the given affine map.
162 MLIR_CAPI_EXPORTED MlirAffineMap
163 mlirAffineMapGetMajorSubMap(MlirAffineMap affineMap, intptr_t numResults);
164 
165 /// Returns the affine map consisting of the most minor `numResults` results.
166 /// Returns the null AffineMap if the `numResults` is equal to zero.
167 /// Returns the `affineMap` if `numResults` is greater or equals to number of
168 /// results of the given affine map.
169 MLIR_CAPI_EXPORTED MlirAffineMap
170 mlirAffineMapGetMinorSubMap(MlirAffineMap affineMap, intptr_t numResults);
171 
172 /// Apply AffineExpr::replace(`map`) to each of the results and return a new
173 /// new AffineMap with the new results and the specified number of dims and
174 /// symbols.
175 MLIR_CAPI_EXPORTED MlirAffineMap mlirAffineMapReplace(
176     MlirAffineMap affineMap, MlirAffineExpr expression,
177     MlirAffineExpr replacement, intptr_t numResultDims, intptr_t numResultSyms);
178 
179 /// Returns the simplified affine map resulting from dropping the symbols that
180 /// do not appear in any of the individual maps in `affineMaps`.
181 /// Asserts that all maps in `affineMaps` are normalized to the same number of
182 /// dims and symbols.
183 /// Takes a callback `populateResult` to fill the `res` container with value
184 /// `m` at entry `idx`. This allows returning without worrying about ownership
185 /// considerations.
186 MLIR_CAPI_EXPORTED void mlirAffineMapCompressUnusedSymbols(
187     MlirAffineMap *affineMaps, intptr_t size, void *result,
188     void (*populateResult)(void *res, intptr_t idx, MlirAffineMap m));
189 
190 #ifdef __cplusplus
191 }
192 #endif
193 
194 #endif // MLIR_C_AFFINEMAP_H
195