1# RUN: %PYTHON %s | FileCheck %s
2
3from mlir.ir import *
4from mlir.dialects import builtin
5from mlir.dialects import linalg
6from mlir.dialects import std
7from mlir.dialects import arith
8
9from mlir.dialects.linalg.opdsl.lang import *
10
11
12def run(f):
13  print("\nTEST:", f.__name__)
14  f()
15  return f
16
17
18# CHECK-LABEL: TEST: testInitTensor
19@run
20def testInitTensor():
21  with Context() as ctx, Location.unknown():
22    module = Module.create()
23    f32 = F32Type.get()
24    with InsertionPoint(module.body):
25      # CHECK-LABEL: func @static_sizes
26      # CHECK: %0 = linalg.init_tensor [3, 4] : tensor<3x4xf32>
27      @builtin.FuncOp.from_py_func()
28      def static_sizes():
29        return linalg.InitTensorOp([3, 4], f32)
30
31      # CHECK-LABEL: func @dynamic_sizes
32      # CHECK: %0 = linalg.init_tensor [%arg0, %arg1] : tensor<?x?xf32>
33      @builtin.FuncOp.from_py_func(IndexType.get(), IndexType.get())
34      def dynamic_sizes(d0, d1):
35        return linalg.InitTensorOp([d0, d1], f32)
36
37      # CHECK-LABEL: func @zero_d
38      # CHECK: %0 = linalg.init_tensor [] : tensor<f32>
39      @builtin.FuncOp.from_py_func()
40      def zero_d():
41        return linalg.InitTensorOp([], f32)
42
43  print(module)
44
45
46# CHECK-LABEL: TEST: testInitTensorStaticSizesAttribute
47@run
48def testInitTensorStaticSizesAttribute():
49  with Context() as ctx, Location.unknown():
50    module = Module.create()
51    f32 = F32Type.get()
52    with InsertionPoint(module.body):
53      op = linalg.InitTensorOp([3, 4], f32)
54      # CHECK: [3, 4]
55      print(op.attributes["static_sizes"])
56
57
58# CHECK-LABEL: TEST: testFill
59@run
60def testFill():
61  with Context() as ctx, Location.unknown():
62    module = Module.create()
63    f32 = F32Type.get()
64    with InsertionPoint(module.body):
65      # CHECK-LABEL: func @fill_tensor
66      #  CHECK-SAME:   %[[OUT:[0-9a-z]+]]: tensor<12x?xf32>
67      #  CHECK-NEXT: %[[CST:.*]] = arith.constant 0.0{{.*}} : f32
68      #  CHECK-NEXT: %[[RES:.*]] = linalg.fill(%[[CST]], %[[OUT]]) : f32, tensor<12x?xf32> -> tensor<12x?xf32>
69      #  CHECK-NEXT: return %[[RES]] : tensor<12x?xf32>
70      @builtin.FuncOp.from_py_func(RankedTensorType.get((12, -1), f32))
71      def fill_tensor(out):
72        zero = arith.ConstantOp(value=FloatAttr.get(f32, 0.), result=f32).result
73        return linalg.FillOp(output=out, value=zero).result
74
75      # CHECK-LABEL: func @fill_buffer
76      #  CHECK-SAME:   %[[OUT:[0-9a-z]+]]: memref<12x?xf32>
77      #  CHECK-NEXT: %[[CST:.*]] = arith.constant 0.0{{.*}} : f32
78      #  CHECK-NEXT: linalg.fill(%[[CST]], %[[OUT]]) : f32, memref<12x?xf32>
79      #  CHECK-NEXT: return
80      @builtin.FuncOp.from_py_func(MemRefType.get((12, -1), f32))
81      def fill_buffer(out):
82        zero = arith.ConstantOp(value=FloatAttr.get(f32, 0.), result=f32).result
83        linalg.FillOp(output=out, value=zero)
84
85  print(module)
86
87
88# CHECK-LABEL: TEST: testNamedStructuredOpCustomForm
89@run
90def testNamedStructuredOpCustomForm():
91  with Context() as ctx, Location.unknown():
92    module = Module.create()
93    f32 = F32Type.get()
94    with InsertionPoint(module.body):
95
96      @builtin.FuncOp.from_py_func(
97          RankedTensorType.get((4, 16), f32), RankedTensorType.get((16, 8),
98                                                                   f32))
99      def named_form(lhs, rhs):
100        init_result = linalg.InitTensorOp([4, 8], f32)
101        # First check the named form with custom format
102        #      CHECK: linalg.matmul
103        #      CHECK: cast = #linalg.type_fn<cast_unsigned>
104        #  CHECK-NOT: linalg.memoized_indexing_maps
105        # CHECK-SAME:    ins(%{{.*}} : tensor<4x16xf32>, tensor<16x8xf32>)
106        # CHECK-SAME:   outs(%{{.*}} : tensor<4x8xf32>)
107        # CHECK-SAME:   -> tensor<4x8xf32>
108        # CHECK-NEXT: return
109        return linalg.matmul(
110            lhs, rhs, outs=[init_result.result], cast=TypeFn.cast_unsigned)
111
112  print(module)
113
114
115# CHECK-LABEL: TEST: testNamedStructuredOpGenericForm
116@run
117def testNamedStructuredOpGenericForm():
118  with Context() as ctx, Location.unknown():
119    module = Module.create()
120    f32 = F32Type.get()
121    with InsertionPoint(module.body):
122
123      @builtin.FuncOp.from_py_func(
124          RankedTensorType.get((4, 16), f32), RankedTensorType.get((16, 8),
125                                                                   f32))
126      def named_form(lhs, rhs):
127        init_result = linalg.InitTensorOp([4, 8], f32)
128        #      CHECK: "linalg.matmul"(%{{.*}})
129        # CHECK-NEXT:  ^bb0(%{{.*}}: f32, %{{.*}}: f32, %{{.*}}: f32):
130        # CHECK-NEXT:    arith.mulf{{.*}} (f32, f32) -> f32
131        # CHECK-NEXT:    arith.addf{{.*}} (f32, f32) -> f32
132        # CHECK-NEXT:    linalg.yield{{.*}} (f32) -> ()
133        # CHECK-NEXT:    operand_segment_sizes = dense<[2, 1]> : vector<2xi32>
134        # CHECK-SAME: (tensor<4x16xf32>, tensor<16x8xf32>, tensor<4x8xf32>) -> tensor<4x8xf32>
135        return linalg.matmul(lhs, rhs, outs=[init_result.result])
136
137  module.operation.print(print_generic_op_form=True)
138
139
140# CHECK-LABEL: TEST: testNamedStructuredAsGenericOp
141@run
142def testNamedStructuredAsGenericOp():
143  with Context() as ctx, Location.unknown():
144    module = Module.create()
145    f32 = F32Type.get()
146    with InsertionPoint(module.body):
147
148      @builtin.FuncOp.from_py_func(
149          RankedTensorType.get((4, 16), f32), RankedTensorType.get((16, 8),
150                                                                   f32))
151      def generic_form(lhs, rhs):
152        init_result = linalg.InitTensorOp([4, 8], f32)
153        # CHECK: linalg.generic
154        return linalg.matmul(
155            lhs, rhs, outs=[init_result.result], emit_generic=True)
156
157  print(module)
158
159
160# CHECK-LABEL: TEST: testOpResultFromOtherOp
161@run
162def testOpResultFromOtherOp():
163  with Context(), Location.unknown():
164    module = Module.create()
165    f32 = F32Type.get()
166    with InsertionPoint(module.body):
167
168      @builtin.FuncOp.from_py_func(
169          RankedTensorType.get((4, 16), f32), RankedTensorType.get((16, 8),
170                                                                   f32))
171      def pass_an_op_directly(arg0, arg1):
172        one = arith.ConstantOp(F32Type.get(), 1.0)
173        # CHECK: %[[LHS:.*]] = linalg.fill
174        lhs = linalg.FillOp(arg0, one)
175        # CHECK: %[[RHS:.*]] = linalg.fill
176        rhs = linalg.FillOp(arg1, one)
177        # CHECK: %[[INIT:.*]] = linalg.init_tensor
178        init = linalg.InitTensorOp([4, 8], f32)
179        # CHECK: linalg.matmul
180        # CHECK: ins(%[[LHS]], %[[RHS]]
181        # CHECK: outs(%[[INIT]]
182        return linalg.matmul(lhs, rhs, outs=init)
183
184  print(module)
185