• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/IR.h"
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 //===----------------------------------------------------------------------===//
20 /** Opaque type declarations.
21  *
22  * Types are exposed to C bindings as structs containing opaque pointers. They
23  * are not supposed to be inspected from C. This allows the underlying
24  * representation to change without affecting the API users. The use of structs
25  * instead of typedefs enables some type safety as structs are not implicitly
26  * convertible to each other.
27  *
28  * Instances of these types may or may not own the underlying object. The
29  * ownership semantics is defined by how an instance of the type was obtained.
30  */
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 mlirAffineMapGet(MlirContext ctx,
71                                                   intptr_t dimCount,
72                                                   intptr_t symbolCount);
73 
74 /** Creates a single constant result affine map in the context. The affine map
75  * is owned by the context. */
76 MLIR_CAPI_EXPORTED MlirAffineMap mlirAffineMapConstantGet(MlirContext ctx,
77                                                           int64_t val);
78 
79 /** Creates an affine map with 'numDims' identity in the context. The affine map
80  * is owned by the context. */
81 MLIR_CAPI_EXPORTED MlirAffineMap
82 mlirAffineMapMultiDimIdentityGet(MlirContext ctx, intptr_t numDims);
83 
84 /** Creates an identity affine map on the most minor dimensions in the context.
85  * The affine map is owned by the context. The function asserts that the number
86  * of dimensions is greater or equal to the number of results. */
87 MLIR_CAPI_EXPORTED MlirAffineMap
88 mlirAffineMapMinorIdentityGet(MlirContext ctx, intptr_t dims, intptr_t results);
89 
90 /** Creates an affine map with a permutation expression and its size in the
91  * context. The permutation expression is a non-empty vector of integers.
92  * The elements of the permutation vector must be continuous from 0 and cannot
93  * be repeated (i.e. `[1,2,0]` is a valid permutation. `[2,0]` or `[1,1,2]` is
94  * an invalid invalid permutation.) The affine map is owned by the context. */
95 MLIR_CAPI_EXPORTED MlirAffineMap mlirAffineMapPermutationGet(
96     MlirContext ctx, intptr_t size, unsigned *permutation);
97 
98 /** Checks whether the given affine map is an identity affine map. The function
99  * asserts that the number of dimensions is greater or equal to the number of
100  * results. */
101 MLIR_CAPI_EXPORTED bool mlirAffineMapIsIdentity(MlirAffineMap affineMap);
102 
103 /// Checks whether the given affine map is a minor identity affine map.
104 MLIR_CAPI_EXPORTED bool mlirAffineMapIsMinorIdentity(MlirAffineMap affineMap);
105 
106 /// Checks whether the given affine map is an empty affine map.
107 MLIR_CAPI_EXPORTED bool mlirAffineMapIsEmpty(MlirAffineMap affineMap);
108 
109 /** Checks whether the given affine map is a single result constant affine
110  * map. */
111 MLIR_CAPI_EXPORTED bool mlirAffineMapIsSingleConstant(MlirAffineMap affineMap);
112 
113 /** Returns the constant result of the given affine map. The function asserts
114  * that the map has a single constant result. */
115 MLIR_CAPI_EXPORTED int64_t
116 mlirAffineMapGetSingleConstantResult(MlirAffineMap affineMap);
117 
118 /// Returns the number of dimensions of the given affine map.
119 MLIR_CAPI_EXPORTED intptr_t mlirAffineMapGetNumDims(MlirAffineMap affineMap);
120 
121 /// Returns the number of symbols of the given affine map.
122 MLIR_CAPI_EXPORTED intptr_t mlirAffineMapGetNumSymbols(MlirAffineMap affineMap);
123 
124 /// Returns the number of results of the given affine map.
125 MLIR_CAPI_EXPORTED intptr_t mlirAffineMapGetNumResults(MlirAffineMap affineMap);
126 
127 /** Returns the number of inputs (dimensions + symbols) of the given affine
128  * map. */
129 MLIR_CAPI_EXPORTED intptr_t mlirAffineMapGetNumInputs(MlirAffineMap affineMap);
130 
131 /** Checks whether the given affine map represents a subset of a symbol-less
132  * permutation map. */
133 MLIR_CAPI_EXPORTED bool
134 mlirAffineMapIsProjectedPermutation(MlirAffineMap affineMap);
135 
136 /** Checks whether the given affine map represents a symbol-less permutation
137  * map. */
138 MLIR_CAPI_EXPORTED bool mlirAffineMapIsPermutation(MlirAffineMap affineMap);
139 
140 /// Returns the affine map consisting of the `resultPos` subset.
141 MLIR_CAPI_EXPORTED MlirAffineMap mlirAffineMapGetSubMap(MlirAffineMap affineMap,
142                                                         intptr_t size,
143                                                         intptr_t *resultPos);
144 
145 /** Returns the affine map consisting of the most major `numResults` results.
146  * Returns the null AffineMap if the `numResults` is equal to zero.
147  * Returns the `affineMap` if `numResults` is greater or equals to number of
148  * results of the given affine map. */
149 MLIR_CAPI_EXPORTED MlirAffineMap
150 mlirAffineMapGetMajorSubMap(MlirAffineMap affineMap, intptr_t numResults);
151 
152 /** Returns the affine map consisting of the most minor `numResults` results.
153  * Returns the null AffineMap if the `numResults` is equal to zero.
154  * Returns the `affineMap` if `numResults` is greater or equals to number of
155  * results of the given affine map. */
156 MLIR_CAPI_EXPORTED MlirAffineMap
157 mlirAffineMapGetMinorSubMap(MlirAffineMap affineMap, intptr_t numResults);
158 
159 #ifdef __cplusplus
160 }
161 #endif
162 
163 #endif // MLIR_C_AFFINEMAP_H
164