MLIR  20.0.0git
AffineMap.cpp
Go to the documentation of this file.
1 //===- AffineMap.cpp - C API for MLIR Affine Maps -------------------------===//
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 #include "mlir-c/AffineMap.h"
10 #include "mlir-c/IR.h"
11 #include "mlir/CAPI/AffineExpr.h"
12 #include "mlir/CAPI/AffineMap.h"
13 #include "mlir/CAPI/IR.h"
14 #include "mlir/CAPI/Utils.h"
15 #include "mlir/IR/AffineMap.h"
16 
17 // TODO: expose the C API related to `AffineExpr` and mutable affine map.
18 
19 using namespace mlir;
20 
21 MlirContext mlirAffineMapGetContext(MlirAffineMap affineMap) {
22  return wrap(unwrap(affineMap).getContext());
23 }
24 
25 bool mlirAffineMapEqual(MlirAffineMap a1, MlirAffineMap a2) {
26  return unwrap(a1) == unwrap(a2);
27 }
28 
29 void mlirAffineMapPrint(MlirAffineMap affineMap, MlirStringCallback callback,
30  void *userData) {
31  mlir::detail::CallbackOstream stream(callback, userData);
32  unwrap(affineMap).print(stream);
33 }
34 
35 void mlirAffineMapDump(MlirAffineMap affineMap) { unwrap(affineMap).dump(); }
36 
37 MlirAffineMap mlirAffineMapEmptyGet(MlirContext ctx) {
38  return wrap(AffineMap::get(unwrap(ctx)));
39 }
40 
41 MlirAffineMap mlirAffineMapZeroResultGet(MlirContext ctx, intptr_t dimCount,
42  intptr_t symbolCount) {
43  return wrap(AffineMap::get(dimCount, symbolCount, unwrap(ctx)));
44 }
45 
46 MlirAffineMap mlirAffineMapGet(MlirContext ctx, intptr_t dimCount,
47  intptr_t symbolCount, intptr_t nAffineExprs,
48  MlirAffineExpr *affineExprs) {
50  ArrayRef<AffineExpr> exprList = unwrapList(nAffineExprs, affineExprs, exprs);
51  return wrap(AffineMap::get(dimCount, symbolCount, exprList, unwrap(ctx)));
52 }
53 
54 MlirAffineMap mlirAffineMapConstantGet(MlirContext ctx, int64_t val) {
55  return wrap(AffineMap::getConstantMap(val, unwrap(ctx)));
56 }
57 
58 MlirAffineMap mlirAffineMapMultiDimIdentityGet(MlirContext ctx,
59  intptr_t numDims) {
60  return wrap(AffineMap::getMultiDimIdentityMap(numDims, unwrap(ctx)));
61 }
62 
63 MlirAffineMap mlirAffineMapMinorIdentityGet(MlirContext ctx, intptr_t dims,
64  intptr_t results) {
65  return wrap(AffineMap::getMinorIdentityMap(dims, results, unwrap(ctx)));
66 }
67 
68 MlirAffineMap mlirAffineMapPermutationGet(MlirContext ctx, intptr_t size,
69  unsigned *permutation) {
71  llvm::ArrayRef(permutation, static_cast<size_t>(size)), unwrap(ctx)));
72 }
73 
74 bool mlirAffineMapIsIdentity(MlirAffineMap affineMap) {
75  return unwrap(affineMap).isIdentity();
76 }
77 
78 bool mlirAffineMapIsMinorIdentity(MlirAffineMap affineMap) {
79  return unwrap(affineMap).isMinorIdentity();
80 }
81 
82 bool mlirAffineMapIsEmpty(MlirAffineMap affineMap) {
83  return unwrap(affineMap).isEmpty();
84 }
85 
86 bool mlirAffineMapIsSingleConstant(MlirAffineMap affineMap) {
87  return unwrap(affineMap).isSingleConstant();
88 }
89 
90 int64_t mlirAffineMapGetSingleConstantResult(MlirAffineMap affineMap) {
91  return unwrap(affineMap).getSingleConstantResult();
92 }
93 
94 intptr_t mlirAffineMapGetNumDims(MlirAffineMap affineMap) {
95  return unwrap(affineMap).getNumDims();
96 }
97 
98 intptr_t mlirAffineMapGetNumSymbols(MlirAffineMap affineMap) {
99  return unwrap(affineMap).getNumSymbols();
100 }
101 
102 intptr_t mlirAffineMapGetNumResults(MlirAffineMap affineMap) {
103  return unwrap(affineMap).getNumResults();
104 }
105 
106 MlirAffineExpr mlirAffineMapGetResult(MlirAffineMap affineMap, intptr_t pos) {
107  return wrap(unwrap(affineMap).getResult(static_cast<unsigned>(pos)));
108 }
109 
110 intptr_t mlirAffineMapGetNumInputs(MlirAffineMap affineMap) {
111  return unwrap(affineMap).getNumInputs();
112 }
113 
114 bool mlirAffineMapIsProjectedPermutation(MlirAffineMap affineMap) {
115  return unwrap(affineMap).isProjectedPermutation();
116 }
117 
118 bool mlirAffineMapIsPermutation(MlirAffineMap affineMap) {
119  return unwrap(affineMap).isPermutation();
120 }
121 
122 MlirAffineMap mlirAffineMapGetSubMap(MlirAffineMap affineMap, intptr_t size,
123  intptr_t *resultPos) {
125  pos.reserve(size);
126  for (intptr_t i = 0; i < size; ++i)
127  pos.push_back(static_cast<unsigned>(resultPos[i]));
128  return wrap(unwrap(affineMap).getSubMap(pos));
129 }
130 
131 MlirAffineMap mlirAffineMapGetMajorSubMap(MlirAffineMap affineMap,
132  intptr_t numResults) {
133  return wrap(unwrap(affineMap).getMajorSubMap(numResults));
134 }
135 
136 MlirAffineMap mlirAffineMapGetMinorSubMap(MlirAffineMap affineMap,
137  intptr_t numResults) {
138  return wrap(unwrap(affineMap).getMinorSubMap(numResults));
139 }
140 
141 MlirAffineMap mlirAffineMapReplace(MlirAffineMap affineMap,
142  MlirAffineExpr expression,
143  MlirAffineExpr replacement,
144  intptr_t numResultDims,
145  intptr_t numResultSyms) {
146  return wrap(unwrap(affineMap).replace(unwrap(expression), unwrap(replacement),
147  numResultDims, numResultSyms));
148 }
149 
151  MlirAffineMap *affineMaps, intptr_t size, void *result,
152  void (*populateResult)(void *res, intptr_t idx, MlirAffineMap m)) {
154  for (intptr_t idx = 0; idx < size; ++idx)
155  maps.push_back(unwrap(affineMaps[idx]));
156  intptr_t idx = 0;
157  for (auto m : mlir::compressUnusedSymbols(maps))
158  populateResult(result, idx++, wrap(m));
159 }
MlirAffineMap mlirAffineMapMultiDimIdentityGet(MlirContext ctx, intptr_t numDims)
Creates an affine map with 'numDims' identity in the context.
Definition: AffineMap.cpp:58
MlirAffineMap mlirAffineMapPermutationGet(MlirContext ctx, intptr_t size, unsigned *permutation)
Creates an affine map with a permutation expression and its size in the context.
Definition: AffineMap.cpp:68
void mlirAffineMapDump(MlirAffineMap affineMap)
Prints the affine map to the standard error stream.
Definition: AffineMap.cpp:35
bool mlirAffineMapIsProjectedPermutation(MlirAffineMap affineMap)
Checks whether the given affine map represents a subset of a symbol-less permutation map.
Definition: AffineMap.cpp:114
bool mlirAffineMapIsEmpty(MlirAffineMap affineMap)
Checks whether the given affine map is an empty affine map.
Definition: AffineMap.cpp:82
MlirAffineMap mlirAffineMapConstantGet(MlirContext ctx, int64_t val)
Creates a single constant result affine map in the context.
Definition: AffineMap.cpp:54
MlirAffineExpr mlirAffineMapGetResult(MlirAffineMap affineMap, intptr_t pos)
Returns the result at the given position.
Definition: AffineMap.cpp:106
intptr_t mlirAffineMapGetNumInputs(MlirAffineMap affineMap)
Returns the number of inputs (dimensions + symbols) of the given affine map.
Definition: AffineMap.cpp:110
bool mlirAffineMapIsMinorIdentity(MlirAffineMap affineMap)
Checks whether the given affine map is a minor identity affine map.
Definition: AffineMap.cpp:78
MlirAffineMap mlirAffineMapZeroResultGet(MlirContext ctx, intptr_t dimCount, intptr_t symbolCount)
Creates a zero result affine map of the given dimensions and symbols in the context.
Definition: AffineMap.cpp:41
int64_t mlirAffineMapGetSingleConstantResult(MlirAffineMap affineMap)
Returns the constant result of the given affine map.
Definition: AffineMap.cpp:90
MlirAffineMap mlirAffineMapGet(MlirContext ctx, intptr_t dimCount, intptr_t symbolCount, intptr_t nAffineExprs, MlirAffineExpr *affineExprs)
Creates an affine map with results defined by the given list of affine expressions.
Definition: AffineMap.cpp:46
MlirAffineMap mlirAffineMapGetSubMap(MlirAffineMap affineMap, intptr_t size, intptr_t *resultPos)
Returns the affine map consisting of the resultPos subset.
Definition: AffineMap.cpp:122
MlirContext mlirAffineMapGetContext(MlirAffineMap affineMap)
Gets the context that the given affine map was created with.
Definition: AffineMap.cpp:21
void mlirAffineMapPrint(MlirAffineMap affineMap, MlirStringCallback callback, void *userData)
Prints an affine map by sending chunks of the string representation and forwarding userData tocallbac...
Definition: AffineMap.cpp:29
MlirAffineMap mlirAffineMapEmptyGet(MlirContext ctx)
Creates a zero result affine map with no dimensions or symbols in the context.
Definition: AffineMap.cpp:37
MlirAffineMap mlirAffineMapGetMajorSubMap(MlirAffineMap affineMap, intptr_t numResults)
Returns the affine map consisting of the most major numResults results.
Definition: AffineMap.cpp:131
bool mlirAffineMapIsSingleConstant(MlirAffineMap affineMap)
Checks whether the given affine map is a single result constant affine map.
Definition: AffineMap.cpp:86
MlirAffineMap mlirAffineMapMinorIdentityGet(MlirContext ctx, intptr_t dims, intptr_t results)
Creates an identity affine map on the most minor dimensions in the context.
Definition: AffineMap.cpp:63
bool mlirAffineMapIsPermutation(MlirAffineMap affineMap)
Checks whether the given affine map represents a symbol-less permutation map.
Definition: AffineMap.cpp:118
MlirAffineMap mlirAffineMapGetMinorSubMap(MlirAffineMap affineMap, intptr_t numResults)
Returns the affine map consisting of the most minor numResults results.
Definition: AffineMap.cpp:136
bool mlirAffineMapEqual(MlirAffineMap a1, MlirAffineMap a2)
Checks if two affine maps are equal.
Definition: AffineMap.cpp:25
intptr_t mlirAffineMapGetNumDims(MlirAffineMap affineMap)
Returns the number of dimensions of the given affine map.
Definition: AffineMap.cpp:94
intptr_t mlirAffineMapGetNumResults(MlirAffineMap affineMap)
Returns the number of results of the given affine map.
Definition: AffineMap.cpp:102
MlirAffineMap mlirAffineMapReplace(MlirAffineMap affineMap, MlirAffineExpr expression, MlirAffineExpr replacement, intptr_t numResultDims, intptr_t numResultSyms)
Apply AffineExpr::replace(map) to each of the results and return a new new AffineMap with the new res...
Definition: AffineMap.cpp:141
void mlirAffineMapCompressUnusedSymbols(MlirAffineMap *affineMaps, intptr_t size, void *result, void(*populateResult)(void *res, intptr_t idx, MlirAffineMap m))
Returns the simplified affine map resulting from dropping the symbols that do not appear in any of th...
Definition: AffineMap.cpp:150
intptr_t mlirAffineMapGetNumSymbols(MlirAffineMap affineMap)
Returns the number of symbols of the given affine map.
Definition: AffineMap.cpp:98
bool mlirAffineMapIsIdentity(MlirAffineMap affineMap)
Checks whether the given affine map is an identity affine map.
Definition: AffineMap.cpp:74
static MLIRContext * getContext(OpFoldResult val)
static llvm::ArrayRef< CppTy > unwrapList(size_t size, CTy *first, llvm::SmallVectorImpl< CppTy > &storage)
Definition: Wrap.h:40
static AffineMap getMinorIdentityMap(unsigned dims, unsigned results, MLIRContext *context)
Returns an identity affine map (d0, ..., dn) -> (dp, ..., dn) on the most minor dimensions.
Definition: AffineMap.cpp:135
static AffineMap getMultiDimIdentityMap(unsigned numDims, MLIRContext *context)
Returns an AffineMap with 'numDims' identity result dim exprs.
Definition: AffineMap.cpp:334
static AffineMap get(MLIRContext *context)
Returns a zero result affine map with no dimensions or symbols: () -> ().
static AffineMap getPermutationMap(ArrayRef< unsigned > permutation, MLIRContext *context)
Returns an AffineMap representing a permutation.
Definition: AffineMap.cpp:264
static AffineMap getConstantMap(int64_t val, MLIRContext *context)
Returns a single constant result affine map.
Definition: AffineMap.cpp:128
void print(raw_ostream &os) const
Outputs this diagnostic to a stream.
A simple raw ostream subclass that forwards write_impl calls to the user-supplied callback together w...
Definition: Utils.h:30
mlir::Diagnostic & unwrap(MlirDiagnostic diagnostic)
Definition: Diagnostics.h:19
MlirDiagnostic wrap(mlir::Diagnostic &diagnostic)
Definition: Diagnostics.h:24
void(* MlirStringCallback)(MlirStringRef, void *)
A callback for returning string references.
Definition: Support.h:105
Include the generated interface declarations.
AffineMap compressUnusedSymbols(AffineMap map)
Drop the symbols that are not used.
Definition: AffineMap.cpp:736