1# RUN: %PYTHON %s | FileCheck %s
2
3from mlir.ir import *
4from mlir.dialects import arith
5from mlir.dialects import builtin
6from mlir.dialects import func
7
8
9def constructAndPrintInModule(f):
10  print("\nTEST:", f.__name__)
11  with Context(), Location.unknown():
12    module = Module.create()
13    with InsertionPoint(module.body):
14      f()
15    print(module)
16  return f
17
18
19# CHECK-LABEL: TEST: testConstantOp
20
21
22@constructAndPrintInModule
23def testConstantOp():
24  c1 = arith.ConstantOp(IntegerType.get_signless(32), 42)
25  c2 = arith.ConstantOp(IntegerType.get_signless(64), 100)
26  c3 = arith.ConstantOp(F32Type.get(), 3.14)
27  c4 = arith.ConstantOp(F64Type.get(), 1.23)
28  # CHECK: 42
29  print(c1.literal_value)
30
31  # CHECK: 100
32  print(c2.literal_value)
33
34  # CHECK: 3.140000104904175
35  print(c3.literal_value)
36
37  # CHECK: 1.23
38  print(c4.literal_value)
39
40
41# CHECK: = arith.constant 42 : i32
42# CHECK: = arith.constant 100 : i64
43# CHECK: = arith.constant 3.140000e+00 : f32
44# CHECK: = arith.constant 1.230000e+00 : f64
45
46
47# CHECK-LABEL: TEST: testVectorConstantOp
48@constructAndPrintInModule
49def testVectorConstantOp():
50  int_type = IntegerType.get_signless(32)
51  vec_type = VectorType.get([2, 2], int_type)
52  c1 = arith.ConstantOp(
53      vec_type,
54      DenseElementsAttr.get_splat(vec_type, IntegerAttr.get(int_type, 42)))
55  try:
56    print(c1.literal_value)
57  except ValueError as e:
58    assert "only integer and float constants have literal values" in str(e)
59  else:
60    assert False
61
62
63# CHECK: = arith.constant dense<42> : vector<2x2xi32>
64
65
66# CHECK-LABEL: TEST: testConstantIndexOp
67@constructAndPrintInModule
68def testConstantIndexOp():
69  c1 = arith.ConstantOp.create_index(10)
70  # CHECK: 10
71  print(c1.literal_value)
72
73
74# CHECK: = arith.constant 10 : index
75
76
77# CHECK-LABEL: TEST: testFunctionCalls
78@constructAndPrintInModule
79def testFunctionCalls():
80  foo = func.FuncOp("foo", ([], []))
81  foo.sym_visibility = StringAttr.get("private")
82  bar = func.FuncOp("bar", ([], [IndexType.get()]))
83  bar.sym_visibility = StringAttr.get("private")
84  qux = func.FuncOp("qux", ([], [F32Type.get()]))
85  qux.sym_visibility = StringAttr.get("private")
86
87  with InsertionPoint(func.FuncOp("caller", ([], [])).add_entry_block()):
88    func.CallOp(foo, [])
89    func.CallOp([IndexType.get()], "bar", [])
90    func.CallOp([F32Type.get()], FlatSymbolRefAttr.get("qux"), [])
91    func.ReturnOp([])
92
93
94# CHECK: func private @foo()
95# CHECK: func private @bar() -> index
96# CHECK: func private @qux() -> f32
97# CHECK: func @caller() {
98# CHECK:   call @foo() : () -> ()
99# CHECK:   %0 = call @bar() : () -> index
100# CHECK:   %1 = call @qux() : () -> f32
101# CHECK:   return
102# CHECK: }
103