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/AffineMap.h"
12 #include "mlir/CAPI/IR.h"
13 #include "mlir/CAPI/Utils.h"
14 #include "mlir/IR/AffineMap.h"
15
16 // TODO: expose the C API related to `AffineExpr` and mutable affine map.
17
18 using namespace mlir;
19
mlirAffineMapGetContext(MlirAffineMap affineMap)20 MlirContext mlirAffineMapGetContext(MlirAffineMap affineMap) {
21 return wrap(unwrap(affineMap).getContext());
22 }
23
mlirAffineMapEqual(MlirAffineMap a1,MlirAffineMap a2)24 bool mlirAffineMapEqual(MlirAffineMap a1, MlirAffineMap a2) {
25 return unwrap(a1) == unwrap(a2);
26 }
27
mlirAffineMapPrint(MlirAffineMap affineMap,MlirStringCallback callback,void * userData)28 void mlirAffineMapPrint(MlirAffineMap affineMap, MlirStringCallback callback,
29 void *userData) {
30 mlir::detail::CallbackOstream stream(callback, userData);
31 unwrap(affineMap).print(stream);
32 }
33
mlirAffineMapDump(MlirAffineMap affineMap)34 void mlirAffineMapDump(MlirAffineMap affineMap) { unwrap(affineMap).dump(); }
35
mlirAffineMapEmptyGet(MlirContext ctx)36 MlirAffineMap mlirAffineMapEmptyGet(MlirContext ctx) {
37 return wrap(AffineMap::get(unwrap(ctx)));
38 }
39
mlirAffineMapGet(MlirContext ctx,intptr_t dimCount,intptr_t symbolCount)40 MlirAffineMap mlirAffineMapGet(MlirContext ctx, intptr_t dimCount,
41 intptr_t symbolCount) {
42 return wrap(AffineMap::get(dimCount, symbolCount, unwrap(ctx)));
43 }
44
mlirAffineMapConstantGet(MlirContext ctx,int64_t val)45 MlirAffineMap mlirAffineMapConstantGet(MlirContext ctx, int64_t val) {
46 return wrap(AffineMap::getConstantMap(val, unwrap(ctx)));
47 }
48
mlirAffineMapMultiDimIdentityGet(MlirContext ctx,intptr_t numDims)49 MlirAffineMap mlirAffineMapMultiDimIdentityGet(MlirContext ctx,
50 intptr_t numDims) {
51 return wrap(AffineMap::getMultiDimIdentityMap(numDims, unwrap(ctx)));
52 }
53
mlirAffineMapMinorIdentityGet(MlirContext ctx,intptr_t dims,intptr_t results)54 MlirAffineMap mlirAffineMapMinorIdentityGet(MlirContext ctx, intptr_t dims,
55 intptr_t results) {
56 return wrap(AffineMap::getMinorIdentityMap(dims, results, unwrap(ctx)));
57 }
58
mlirAffineMapPermutationGet(MlirContext ctx,intptr_t size,unsigned * permutation)59 MlirAffineMap mlirAffineMapPermutationGet(MlirContext ctx, intptr_t size,
60 unsigned *permutation) {
61 return wrap(AffineMap::getPermutationMap(
62 llvm::makeArrayRef(permutation, static_cast<size_t>(size)), unwrap(ctx)));
63 }
64
mlirAffineMapIsIdentity(MlirAffineMap affineMap)65 bool mlirAffineMapIsIdentity(MlirAffineMap affineMap) {
66 return unwrap(affineMap).isIdentity();
67 }
68
mlirAffineMapIsMinorIdentity(MlirAffineMap affineMap)69 bool mlirAffineMapIsMinorIdentity(MlirAffineMap affineMap) {
70 return unwrap(affineMap).isMinorIdentity();
71 }
72
mlirAffineMapIsEmpty(MlirAffineMap affineMap)73 bool mlirAffineMapIsEmpty(MlirAffineMap affineMap) {
74 return unwrap(affineMap).isEmpty();
75 }
76
mlirAffineMapIsSingleConstant(MlirAffineMap affineMap)77 bool mlirAffineMapIsSingleConstant(MlirAffineMap affineMap) {
78 return unwrap(affineMap).isSingleConstant();
79 }
80
mlirAffineMapGetSingleConstantResult(MlirAffineMap affineMap)81 int64_t mlirAffineMapGetSingleConstantResult(MlirAffineMap affineMap) {
82 return unwrap(affineMap).getSingleConstantResult();
83 }
84
mlirAffineMapGetNumDims(MlirAffineMap affineMap)85 intptr_t mlirAffineMapGetNumDims(MlirAffineMap affineMap) {
86 return unwrap(affineMap).getNumDims();
87 }
88
mlirAffineMapGetNumSymbols(MlirAffineMap affineMap)89 intptr_t mlirAffineMapGetNumSymbols(MlirAffineMap affineMap) {
90 return unwrap(affineMap).getNumSymbols();
91 }
92
mlirAffineMapGetNumResults(MlirAffineMap affineMap)93 intptr_t mlirAffineMapGetNumResults(MlirAffineMap affineMap) {
94 return unwrap(affineMap).getNumResults();
95 }
96
mlirAffineMapGetNumInputs(MlirAffineMap affineMap)97 intptr_t mlirAffineMapGetNumInputs(MlirAffineMap affineMap) {
98 return unwrap(affineMap).getNumInputs();
99 }
100
mlirAffineMapIsProjectedPermutation(MlirAffineMap affineMap)101 bool mlirAffineMapIsProjectedPermutation(MlirAffineMap affineMap) {
102 return unwrap(affineMap).isProjectedPermutation();
103 }
104
mlirAffineMapIsPermutation(MlirAffineMap affineMap)105 bool mlirAffineMapIsPermutation(MlirAffineMap affineMap) {
106 return unwrap(affineMap).isPermutation();
107 }
108
mlirAffineMapGetSubMap(MlirAffineMap affineMap,intptr_t size,intptr_t * resultPos)109 MlirAffineMap mlirAffineMapGetSubMap(MlirAffineMap affineMap, intptr_t size,
110 intptr_t *resultPos) {
111 SmallVector<unsigned, 8> pos;
112 pos.reserve(size);
113 for (intptr_t i = 0; i < size; ++i)
114 pos.push_back(static_cast<unsigned>(resultPos[i]));
115 return wrap(unwrap(affineMap).getSubMap(pos));
116 }
117
mlirAffineMapGetMajorSubMap(MlirAffineMap affineMap,intptr_t numResults)118 MlirAffineMap mlirAffineMapGetMajorSubMap(MlirAffineMap affineMap,
119 intptr_t numResults) {
120 return wrap(unwrap(affineMap).getMajorSubMap(numResults));
121 }
122
mlirAffineMapGetMinorSubMap(MlirAffineMap affineMap,intptr_t numResults)123 MlirAffineMap mlirAffineMapGetMinorSubMap(MlirAffineMap affineMap,
124 intptr_t numResults) {
125 return wrap(unwrap(affineMap).getMinorSubMap(numResults));
126 }
127