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
19using namespace mlir;
20
21MlirContext mlirAffineMapGetContext(MlirAffineMap affineMap) {
22 return wrap(cpp: unwrap(c: affineMap).getContext());
23}
24
25bool mlirAffineMapEqual(MlirAffineMap a1, MlirAffineMap a2) {
26 return unwrap(c: a1) == unwrap(c: a2);
27}
28
29void mlirAffineMapPrint(MlirAffineMap affineMap, MlirStringCallback callback,
30 void *userData) {
31 mlir::detail::CallbackOstream stream(callback, userData);
32 unwrap(c: affineMap).print(os&: stream);
33}
34
35void mlirAffineMapDump(MlirAffineMap affineMap) { unwrap(c: affineMap).dump(); }
36
37MlirAffineMap mlirAffineMapEmptyGet(MlirContext ctx) {
38 return wrap(cpp: AffineMap::get(context: unwrap(c: ctx)));
39}
40
41MlirAffineMap mlirAffineMapZeroResultGet(MlirContext ctx, intptr_t dimCount,
42 intptr_t symbolCount) {
43 return wrap(cpp: AffineMap::get(dimCount, symbolCount, context: unwrap(c: ctx)));
44}
45
46MlirAffineMap mlirAffineMapGet(MlirContext ctx, intptr_t dimCount,
47 intptr_t symbolCount, intptr_t nAffineExprs,
48 MlirAffineExpr *affineExprs) {
49 SmallVector<AffineExpr, 4> exprs;
50 ArrayRef<AffineExpr> exprList = unwrapList(size: nAffineExprs, first: affineExprs, storage&: exprs);
51 return wrap(cpp: AffineMap::get(dimCount, symbolCount, results: exprList, context: unwrap(c: ctx)));
52}
53
54MlirAffineMap mlirAffineMapConstantGet(MlirContext ctx, int64_t val) {
55 return wrap(cpp: AffineMap::getConstantMap(val, context: unwrap(c: ctx)));
56}
57
58MlirAffineMap mlirAffineMapMultiDimIdentityGet(MlirContext ctx,
59 intptr_t numDims) {
60 return wrap(cpp: AffineMap::getMultiDimIdentityMap(numDims, context: unwrap(c: ctx)));
61}
62
63MlirAffineMap mlirAffineMapMinorIdentityGet(MlirContext ctx, intptr_t dims,
64 intptr_t results) {
65 return wrap(cpp: AffineMap::getMinorIdentityMap(dims, results, context: unwrap(c: ctx)));
66}
67
68MlirAffineMap mlirAffineMapPermutationGet(MlirContext ctx, intptr_t size,
69 unsigned *permutation) {
70 return wrap(cpp: AffineMap::getPermutationMap(
71 permutation: llvm::ArrayRef(permutation, static_cast<size_t>(size)), context: unwrap(c: ctx)));
72}
73
74bool mlirAffineMapIsIdentity(MlirAffineMap affineMap) {
75 return unwrap(c: affineMap).isIdentity();
76}
77
78bool mlirAffineMapIsMinorIdentity(MlirAffineMap affineMap) {
79 return unwrap(c: affineMap).isMinorIdentity();
80}
81
82bool mlirAffineMapIsEmpty(MlirAffineMap affineMap) {
83 return unwrap(c: affineMap).isEmpty();
84}
85
86bool mlirAffineMapIsSingleConstant(MlirAffineMap affineMap) {
87 return unwrap(c: affineMap).isSingleConstant();
88}
89
90int64_t mlirAffineMapGetSingleConstantResult(MlirAffineMap affineMap) {
91 return unwrap(c: affineMap).getSingleConstantResult();
92}
93
94intptr_t mlirAffineMapGetNumDims(MlirAffineMap affineMap) {
95 return unwrap(c: affineMap).getNumDims();
96}
97
98intptr_t mlirAffineMapGetNumSymbols(MlirAffineMap affineMap) {
99 return unwrap(c: affineMap).getNumSymbols();
100}
101
102intptr_t mlirAffineMapGetNumResults(MlirAffineMap affineMap) {
103 return unwrap(c: affineMap).getNumResults();
104}
105
106MlirAffineExpr mlirAffineMapGetResult(MlirAffineMap affineMap, intptr_t pos) {
107 return wrap(cpp: unwrap(c: affineMap).getResult(idx: static_cast<unsigned>(pos)));
108}
109
110intptr_t mlirAffineMapGetNumInputs(MlirAffineMap affineMap) {
111 return unwrap(c: affineMap).getNumInputs();
112}
113
114bool mlirAffineMapIsProjectedPermutation(MlirAffineMap affineMap) {
115 return unwrap(c: affineMap).isProjectedPermutation();
116}
117
118bool mlirAffineMapIsPermutation(MlirAffineMap affineMap) {
119 return unwrap(c: affineMap).isPermutation();
120}
121
122MlirAffineMap mlirAffineMapGetSubMap(MlirAffineMap affineMap, intptr_t size,
123 intptr_t *resultPos) {
124 SmallVector<unsigned, 8> pos;
125 pos.reserve(N: size);
126 for (intptr_t i = 0; i < size; ++i)
127 pos.push_back(Elt: static_cast<unsigned>(resultPos[i]));
128 return wrap(cpp: unwrap(c: affineMap).getSubMap(resultPos: pos));
129}
130
131MlirAffineMap mlirAffineMapGetMajorSubMap(MlirAffineMap affineMap,
132 intptr_t numResults) {
133 return wrap(cpp: unwrap(c: affineMap).getMajorSubMap(numResults));
134}
135
136MlirAffineMap mlirAffineMapGetMinorSubMap(MlirAffineMap affineMap,
137 intptr_t numResults) {
138 return wrap(cpp: unwrap(c: affineMap).getMinorSubMap(numResults));
139}
140
141MlirAffineMap mlirAffineMapReplace(MlirAffineMap affineMap,
142 MlirAffineExpr expression,
143 MlirAffineExpr replacement,
144 intptr_t numResultDims,
145 intptr_t numResultSyms) {
146 return wrap(cpp: unwrap(c: affineMap).replace(expr: unwrap(c: expression), replacement: unwrap(c: replacement),
147 numResultDims, numResultSyms));
148}
149
150void mlirAffineMapCompressUnusedSymbols(
151 MlirAffineMap *affineMaps, intptr_t size, void *result,
152 void (*populateResult)(void *res, intptr_t idx, MlirAffineMap m)) {
153 SmallVector<AffineMap> maps;
154 for (intptr_t idx = 0; idx < size; ++idx)
155 maps.push_back(Elt: unwrap(c: affineMaps[idx]));
156 intptr_t idx = 0;
157 for (auto m : mlir::compressUnusedSymbols(maps))
158 populateResult(result, idx++, wrap(cpp: m));
159}
160

source code of mlir/lib/CAPI/IR/AffineMap.cpp