1 # RUN: %PYTHON %s | FileCheck %s
8 print("\nTEST:", f
.__name
__)
11 assert Context
._get
_live
_count
() == 0
15 # CHECK-LABEL: TEST: testAffineExprCapsule
17 def testAffineExprCapsule():
18 with
Context() as ctx
:
19 affine_expr
= AffineExpr
.get_constant(42)
21 affine_expr_capsule
= affine_expr
._CAPIPtr
22 # CHECK: capsule object
23 # CHECK: mlir.ir.AffineExpr._CAPIPtr
24 print(affine_expr_capsule
)
26 affine_expr_2
= AffineExpr
._CAPICreate
(affine_expr_capsule
)
27 assert affine_expr
== affine_expr_2
28 assert affine_expr_2
.context
== ctx
31 # CHECK-LABEL: TEST: testAffineExprEq
33 def testAffineExprEq():
35 a1
= AffineExpr
.get_constant(42)
36 a2
= AffineExpr
.get_constant(42)
37 a3
= AffineExpr
.get_constant(43)
50 # CHECK-LABEL: TEST: testAffineExprContext
52 def testAffineExprContext():
54 a1
= AffineExpr
.get_constant(42)
56 a2
= AffineExpr
.get_constant(42)
62 run(testAffineExprContext
)
65 # CHECK-LABEL: TEST: testAffineExprConstant
67 def testAffineExprConstant():
69 a1
= AffineExpr
.get_constant(42)
75 a2
= AffineConstantExpr
.get(42)
84 # CHECK-LABEL: TEST: testAffineExprDim
86 def testAffineExprDim():
88 d1
= AffineExpr
.get_dim(1)
89 d11
= AffineDimExpr
.get(1)
90 d2
= AffineDimExpr
.get(2)
106 # CHECK-LABEL: TEST: testAffineExprSymbol
108 def testAffineExprSymbol():
110 s1
= AffineExpr
.get_symbol(1)
111 s11
= AffineSymbolExpr
.get(1)
112 s2
= AffineSymbolExpr
.get(2)
128 # CHECK-LABEL: TEST: testAffineAddExpr
130 def testAffineAddExpr():
132 d1
= AffineDimExpr
.get(1)
133 d2
= AffineDimExpr
.get(2)
134 d12
= AffineExpr
.get_add(d1
, d2
)
155 # CHECK-LABEL: TEST: testAffineMulExpr
157 def testAffineMulExpr():
159 d1
= AffineDimExpr
.get(1)
160 c2
= AffineConstantExpr
.get(2)
161 expr
= AffineExpr
.get_mul(d1
, c2
)
178 assert expr
== op_cst
179 assert expr
.lhs
== d1
180 assert expr
.rhs
== c2
183 # CHECK-LABEL: TEST: testAffineModExpr
185 def testAffineModExpr():
187 d1
= AffineDimExpr
.get(1)
188 c2
= AffineConstantExpr
.get(2)
189 expr
= AffineExpr
.get_mod(d1
, c2
)
205 assert expr
== op_cst
206 assert expr
.lhs
== d1
207 assert expr
.rhs
== c2
209 expr2
= AffineExpr
.get_mod(c2
, d1
)
210 expr3
= AffineExpr
.get_mod(2, d1
)
211 expr4
= AffineExpr
.get_mod(d1
, 2)
220 assert expr2
== expr3
224 # CHECK-LABEL: TEST: testAffineFloorDivExpr
226 def testAffineFloorDivExpr():
228 d1
= AffineDimExpr
.get(1)
229 c2
= AffineConstantExpr
.get(2)
230 expr
= AffineExpr
.get_floor_div(d1
, c2
)
231 # CHECK: d1 floordiv 2
234 assert expr
.lhs
== d1
235 assert expr
.rhs
== c2
237 expr2
= AffineExpr
.get_floor_div(c2
, d1
)
238 expr3
= AffineExpr
.get_floor_div(2, d1
)
239 expr4
= AffineExpr
.get_floor_div(d1
, 2)
241 # CHECK: 2 floordiv d1
243 # CHECK: 2 floordiv d1
245 # CHECK: d1 floordiv 2
248 assert expr2
== expr3
252 # CHECK-LABEL: TEST: testAffineCeilDivExpr
254 def testAffineCeilDivExpr():
256 d1
= AffineDimExpr
.get(1)
257 c2
= AffineConstantExpr
.get(2)
258 expr
= AffineExpr
.get_ceil_div(d1
, c2
)
259 # CHECK: d1 ceildiv 2
262 assert expr
.lhs
== d1
263 assert expr
.rhs
== c2
265 expr2
= AffineExpr
.get_ceil_div(c2
, d1
)
266 expr3
= AffineExpr
.get_ceil_div(2, d1
)
267 expr4
= AffineExpr
.get_ceil_div(d1
, 2)
269 # CHECK: 2 ceildiv d1
271 # CHECK: 2 ceildiv d1
273 # CHECK: d1 ceildiv 2
276 assert expr2
== expr3
280 # CHECK-LABEL: TEST: testAffineExprSub
282 def testAffineExprSub():
284 d1
= AffineDimExpr
.get(1)
285 d2
= AffineDimExpr
.get(2)
290 assert expr
.lhs
== d1
291 rhs
= AffineMulExpr(expr
.rhs
)
302 c42
= AffineConstantExpr
.get(42)
303 assert d1
- 42 == d1
- c42
304 assert 42 - d1
== c42
- d1
307 # CHECK-LABEL: TEST: testClassHierarchy
309 def testClassHierarchy():
311 d1
= AffineDimExpr
.get(1)
312 c2
= AffineConstantExpr
.get(2)
313 add
= AffineAddExpr
.get(d1
, c2
)
314 mul
= AffineMulExpr
.get(d1
, c2
)
315 mod
= AffineModExpr
.get(d1
, c2
)
316 floor_div
= AffineFloorDivExpr
.get(d1
, c2
)
317 ceil_div
= AffineCeilDivExpr
.get(d1
, c2
)
320 print(isinstance(d1
, AffineBinaryExpr
))
322 print(isinstance(c2
, AffineBinaryExpr
))
324 print(isinstance(add
, AffineBinaryExpr
))
326 print(isinstance(mul
, AffineBinaryExpr
))
328 print(isinstance(mod
, AffineBinaryExpr
))
330 print(isinstance(floor_div
, AffineBinaryExpr
))
332 print(isinstance(ceil_div
, AffineBinaryExpr
))
336 except ValueError as e
:
337 # CHECK: Cannot cast affine expression to AffineBinaryExpr
342 except ValueError as e
:
343 # CHECK: Cannot cast affine expression to AffineBinaryExpr
347 # CHECK-LABEL: TEST: testIsInstance
349 def testIsInstance():
351 d1
= AffineDimExpr
.get(1)
352 c2
= AffineConstantExpr
.get(2)
353 add
= AffineAddExpr
.get(d1
, c2
)
354 mul
= AffineMulExpr
.get(d1
, c2
)
357 print(AffineDimExpr
.isinstance(d1
))
359 print(AffineConstantExpr
.isinstance(d1
))
361 print(AffineConstantExpr
.isinstance(c2
))
363 print(AffineMulExpr
.isinstance(c2
))
365 print(AffineAddExpr
.isinstance(add
))
367 print(AffineMulExpr
.isinstance(add
))
369 print(AffineMulExpr
.isinstance(mul
))
371 print(AffineAddExpr
.isinstance(mul
))
374 # CHECK-LABEL: TEST: testCompose
379 expr
= AffineAddExpr
.get(AffineDimExpr
.get(0), AffineDimExpr
.get(2))
381 # (d0, d1, d2)[s0, s1] -> (d0 + s1, d1 + s0, d0 + d1 + d2)
382 map1
= AffineAddExpr
.get(AffineDimExpr
.get(0), AffineSymbolExpr
.get(1))
383 map2
= AffineAddExpr
.get(AffineDimExpr
.get(1), AffineSymbolExpr
.get(0))
384 map3
= AffineAddExpr
.get(
385 AffineAddExpr
.get(AffineDimExpr
.get(0), AffineDimExpr
.get(1)),
386 AffineDimExpr
.get(2),
388 map = AffineMap
.get(3, 2, [map1
, map2
, map3
])
390 # CHECK: d0 + s1 + d0 + d1 + d2
391 print(expr
.compose(map))
394 # CHECK-LABEL: TEST: testHash
398 d0
= AffineDimExpr
.get(0)
399 s1
= AffineSymbolExpr
.get(1)
400 assert hash(d0
) == hash(AffineDimExpr
.get(0))
401 assert hash(d0
+ s1
) == hash(AffineAddExpr
.get(d0
, s1
))
406 assert d0
in dictionary
407 assert s1
in dictionary