1 //===- AffineExpr.cpp - C API for MLIR Affine Expressions -----------------===//
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/AffineExpr.h"
10 #include "mlir-c/AffineMap.h"
11 #include "mlir-c/IR.h"
12 #include "mlir/CAPI/AffineExpr.h"
13 #include "mlir/CAPI/AffineMap.h"
14 #include "mlir/CAPI/IR.h"
15 #include "mlir/CAPI/Utils.h"
16 #include "mlir/IR/AffineExpr.h"
17 
18 using namespace mlir;
19 
mlirAffineExprGetContext(MlirAffineExpr affineExpr)20 MlirContext mlirAffineExprGetContext(MlirAffineExpr affineExpr) {
21   return wrap(unwrap(affineExpr).getContext());
22 }
23 
mlirAffineExprPrint(MlirAffineExpr affineExpr,MlirStringCallback callback,void * userData)24 void mlirAffineExprPrint(MlirAffineExpr affineExpr, MlirStringCallback callback,
25                          void *userData) {
26   mlir::detail::CallbackOstream stream(callback, userData);
27   unwrap(affineExpr).print(stream);
28 }
29 
mlirAffineExprDump(MlirAffineExpr affineExpr)30 void mlirAffineExprDump(MlirAffineExpr affineExpr) {
31   unwrap(affineExpr).dump();
32 }
33 
mlirAffineExprIsSymbolicOrConstant(MlirAffineExpr affineExpr)34 bool mlirAffineExprIsSymbolicOrConstant(MlirAffineExpr affineExpr) {
35   return unwrap(affineExpr).isSymbolicOrConstant();
36 }
37 
mlirAffineExprIsPureAffine(MlirAffineExpr affineExpr)38 bool mlirAffineExprIsPureAffine(MlirAffineExpr affineExpr) {
39   return unwrap(affineExpr).isPureAffine();
40 }
41 
mlirAffineExprGetLargestKnownDivisor(MlirAffineExpr affineExpr)42 int64_t mlirAffineExprGetLargestKnownDivisor(MlirAffineExpr affineExpr) {
43   return unwrap(affineExpr).getLargestKnownDivisor();
44 }
45 
mlirAffineExprIsMultipleOf(MlirAffineExpr affineExpr,int64_t factor)46 bool mlirAffineExprIsMultipleOf(MlirAffineExpr affineExpr, int64_t factor) {
47   return unwrap(affineExpr).isMultipleOf(factor);
48 }
49 
mlirAffineExprIsFunctionOfDim(MlirAffineExpr affineExpr,intptr_t position)50 bool mlirAffineExprIsFunctionOfDim(MlirAffineExpr affineExpr,
51                                    intptr_t position) {
52   return unwrap(affineExpr).isFunctionOfDim(position);
53 }
54 
55 //===----------------------------------------------------------------------===//
56 // Affine Dimension Expression.
57 //===----------------------------------------------------------------------===//
58 
mlirAffineDimExprGet(MlirContext ctx,intptr_t position)59 MlirAffineExpr mlirAffineDimExprGet(MlirContext ctx, intptr_t position) {
60   return wrap(getAffineDimExpr(position, unwrap(ctx)));
61 }
62 
mlirAffineDimExprGetPosition(MlirAffineExpr affineExpr)63 intptr_t mlirAffineDimExprGetPosition(MlirAffineExpr affineExpr) {
64   return unwrap(affineExpr).cast<AffineDimExpr>().getPosition();
65 }
66 
67 //===----------------------------------------------------------------------===//
68 // Affine Symbol Expression.
69 //===----------------------------------------------------------------------===//
70 
mlirAffineSymbolExprGet(MlirContext ctx,intptr_t position)71 MlirAffineExpr mlirAffineSymbolExprGet(MlirContext ctx, intptr_t position) {
72   return wrap(getAffineSymbolExpr(position, unwrap(ctx)));
73 }
74 
mlirAffineSymbolExprGetPosition(MlirAffineExpr affineExpr)75 intptr_t mlirAffineSymbolExprGetPosition(MlirAffineExpr affineExpr) {
76   return unwrap(affineExpr).cast<AffineSymbolExpr>().getPosition();
77 }
78 
79 //===----------------------------------------------------------------------===//
80 // Affine Constant Expression.
81 //===----------------------------------------------------------------------===//
82 
mlirAffineConstantExprGet(MlirContext ctx,int64_t constant)83 MlirAffineExpr mlirAffineConstantExprGet(MlirContext ctx, int64_t constant) {
84   return wrap(getAffineConstantExpr(constant, unwrap(ctx)));
85 }
86 
mlirAffineConstantExprGetValue(MlirAffineExpr affineExpr)87 int64_t mlirAffineConstantExprGetValue(MlirAffineExpr affineExpr) {
88   return unwrap(affineExpr).cast<AffineConstantExpr>().getValue();
89 }
90 
91 //===----------------------------------------------------------------------===//
92 // Affine Add Expression.
93 //===----------------------------------------------------------------------===//
94 
mlirAffineExprIsAAdd(MlirAffineExpr affineExpr)95 bool mlirAffineExprIsAAdd(MlirAffineExpr affineExpr) {
96   return unwrap(affineExpr).getKind() == mlir::AffineExprKind::Add;
97 }
98 
mlirAffineAddExprGet(MlirAffineExpr lhs,MlirAffineExpr rhs)99 MlirAffineExpr mlirAffineAddExprGet(MlirAffineExpr lhs, MlirAffineExpr rhs) {
100   return wrap(getAffineBinaryOpExpr(mlir::AffineExprKind::Add, unwrap(lhs),
101                                     unwrap(rhs)));
102 }
103 
104 //===----------------------------------------------------------------------===//
105 // Affine Mul Expression.
106 //===----------------------------------------------------------------------===//
107 
mlirAffineExprIsAMul(MlirAffineExpr affineExpr)108 bool mlirAffineExprIsAMul(MlirAffineExpr affineExpr) {
109   return unwrap(affineExpr).getKind() == mlir::AffineExprKind::Mul;
110 }
111 
mlirAffineMulExprGet(MlirAffineExpr lhs,MlirAffineExpr rhs)112 MlirAffineExpr mlirAffineMulExprGet(MlirAffineExpr lhs, MlirAffineExpr rhs) {
113   return wrap(getAffineBinaryOpExpr(mlir::AffineExprKind::Mul, unwrap(lhs),
114                                     unwrap(rhs)));
115 }
116 
117 //===----------------------------------------------------------------------===//
118 // Affine Mod Expression.
119 //===----------------------------------------------------------------------===//
120 
mlirAffineExprIsAMod(MlirAffineExpr affineExpr)121 bool mlirAffineExprIsAMod(MlirAffineExpr affineExpr) {
122   return unwrap(affineExpr).getKind() == mlir::AffineExprKind::Mod;
123 }
124 
mlirAffineModExprGet(MlirAffineExpr lhs,MlirAffineExpr rhs)125 MlirAffineExpr mlirAffineModExprGet(MlirAffineExpr lhs, MlirAffineExpr rhs) {
126   return wrap(getAffineBinaryOpExpr(mlir::AffineExprKind::Mod, unwrap(lhs),
127                                     unwrap(rhs)));
128 }
129 
130 //===----------------------------------------------------------------------===//
131 // Affine FloorDiv Expression.
132 //===----------------------------------------------------------------------===//
133 
mlirAffineExprIsAFloorDiv(MlirAffineExpr affineExpr)134 bool mlirAffineExprIsAFloorDiv(MlirAffineExpr affineExpr) {
135   return unwrap(affineExpr).getKind() == mlir::AffineExprKind::FloorDiv;
136 }
137 
mlirAffineFloorDivExprGet(MlirAffineExpr lhs,MlirAffineExpr rhs)138 MlirAffineExpr mlirAffineFloorDivExprGet(MlirAffineExpr lhs,
139                                          MlirAffineExpr rhs) {
140   return wrap(getAffineBinaryOpExpr(mlir::AffineExprKind::FloorDiv, unwrap(lhs),
141                                     unwrap(rhs)));
142 }
143 
144 //===----------------------------------------------------------------------===//
145 // Affine CeilDiv Expression.
146 //===----------------------------------------------------------------------===//
147 
mlirAffineExprIsACeilDiv(MlirAffineExpr affineExpr)148 bool mlirAffineExprIsACeilDiv(MlirAffineExpr affineExpr) {
149   return unwrap(affineExpr).getKind() == mlir::AffineExprKind::CeilDiv;
150 }
151 
mlirAffineCeilDivExprGet(MlirAffineExpr lhs,MlirAffineExpr rhs)152 MlirAffineExpr mlirAffineCeilDivExprGet(MlirAffineExpr lhs,
153                                         MlirAffineExpr rhs) {
154   return wrap(getAffineBinaryOpExpr(mlir::AffineExprKind::CeilDiv, unwrap(lhs),
155                                     unwrap(rhs)));
156 }
157 
158 //===----------------------------------------------------------------------===//
159 // Affine Binary Operation Expression.
160 //===----------------------------------------------------------------------===//
161 
mlirAffineBinaryOpExprGetLHS(MlirAffineExpr affineExpr)162 MlirAffineExpr mlirAffineBinaryOpExprGetLHS(MlirAffineExpr affineExpr) {
163   return wrap(unwrap(affineExpr).cast<AffineBinaryOpExpr>().getLHS());
164 }
165 
mlirAffineBinaryOpExprGetRHS(MlirAffineExpr affineExpr)166 MlirAffineExpr mlirAffineBinaryOpExprGetRHS(MlirAffineExpr affineExpr) {
167   return wrap(unwrap(affineExpr).cast<AffineBinaryOpExpr>().getRHS());
168 }
169