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 9 10def run(f): 11 print("\nTEST:", f.__name__) 12 f() 13 return f 14 15 16# CHECK-LABEL: TEST: testInitTensor 17@run 18def testInitTensor(): 19 with Context() as ctx, Location.unknown(): 20 module = Module.create() 21 f32 = F32Type.get() 22 with InsertionPoint(module.body): 23 # CHECK-LABEL: func @static_sizes 24 # CHECK: %0 = linalg.init_tensor [3, 4] : tensor<3x4xf32> 25 @builtin.FuncOp.from_py_func() 26 def static_sizes(): 27 return linalg.InitTensorOp([3, 4], f32) 28 29 # CHECK-LABEL: func @dynamic_sizes 30 # CHECK: %0 = linalg.init_tensor [%arg0, %arg1] : tensor<?x?xf32> 31 @builtin.FuncOp.from_py_func(IndexType.get(), IndexType.get()) 32 def dynamic_sizes(d0, d1): 33 return linalg.InitTensorOp([d0, d1], f32) 34 35 # CHECK-LABEL: func @zero_d 36 # CHECK: %0 = linalg.init_tensor [] : tensor<f32> 37 @builtin.FuncOp.from_py_func() 38 def zero_d(): 39 return linalg.InitTensorOp([], f32) 40 41 print(module) 42 43 44# CHECK-LABEL: TEST: testInitTensorStaticSizesAttribute 45@run 46def testInitTensorStaticSizesAttribute(): 47 with Context() as ctx, Location.unknown(): 48 module = Module.create() 49 f32 = F32Type.get() 50 with InsertionPoint(module.body): 51 op = linalg.InitTensorOp([3, 4], f32) 52 # CHECK: [3, 4] 53 print(op.attributes["static_sizes"]) 54 55 56# CHECK-LABEL: TEST: testFill 57@run 58def testFill(): 59 with Context() as ctx, Location.unknown(): 60 module = Module.create() 61 f32 = F32Type.get() 62 with InsertionPoint(module.body): 63 # CHECK-LABEL: func @fill_tensor 64 # CHECK-SAME: %[[OUT:[0-9a-z]+]]: tensor<12x?xf32> 65 # CHECK-NEXT: %[[CST:.*]] = arith.constant 0.0{{.*}} : f32 66 # CHECK-NEXT: %[[RES:.*]] = linalg.fill(%[[CST]], %[[OUT]]) : f32, tensor<12x?xf32> -> tensor<12x?xf32> 67 # CHECK-NEXT: return %[[RES]] : tensor<12x?xf32> 68 @builtin.FuncOp.from_py_func(RankedTensorType.get((12, -1), f32)) 69 def fill_tensor(out): 70 zero = arith.ConstantOp(value=FloatAttr.get(f32, 0.), result=f32).result 71 return linalg.FillOp(output=out, value=zero).result 72 73 # CHECK-LABEL: func @fill_buffer 74 # CHECK-SAME: %[[OUT:[0-9a-z]+]]: memref<12x?xf32> 75 # CHECK-NEXT: %[[CST:.*]] = arith.constant 0.0{{.*}} : f32 76 # CHECK-NEXT: linalg.fill(%[[CST]], %[[OUT]]) : f32, memref<12x?xf32> 77 # CHECK-NEXT: return 78 @builtin.FuncOp.from_py_func(MemRefType.get((12, -1), f32)) 79 def fill_buffer(out): 80 zero = arith.ConstantOp(value=FloatAttr.get(f32, 0.), result=f32).result 81 linalg.FillOp(output=out, value=zero) 82 83 print(module) 84 85 86# CHECK-LABEL: TEST: testStructuredOpOnTensors 87@run 88def testStructuredOpOnTensors(): 89 with Context() as ctx, Location.unknown(): 90 module = Module.create() 91 f32 = F32Type.get() 92 tensor_type = RankedTensorType.get((2, 3, 4), f32) 93 with InsertionPoint(module.body): 94 func = builtin.FuncOp( 95 name="matmul_test", 96 type=FunctionType.get( 97 inputs=[tensor_type, tensor_type], results=[tensor_type])) 98 with InsertionPoint(func.add_entry_block()): 99 lhs, rhs = func.entry_block.arguments 100 result = linalg.MatmulOp([lhs, rhs], results=[tensor_type]).result 101 std.ReturnOp([result]) 102 103 # CHECK: %[[R:.*]] = linalg.matmul ins(%arg0, %arg1 : tensor<2x3x4xf32>, tensor<2x3x4xf32>) -> tensor<2x3x4xf32> 104 print(module) 105 106 107# CHECK-LABEL: TEST: testStructuredOpOnBuffers 108@run 109def testStructuredOpOnBuffers(): 110 with Context() as ctx, Location.unknown(): 111 module = Module.create() 112 f32 = F32Type.get() 113 memref_type = MemRefType.get((2, 3, 4), f32) 114 with InsertionPoint(module.body): 115 func = builtin.FuncOp( 116 name="matmul_test", 117 type=FunctionType.get( 118 inputs=[memref_type, memref_type, memref_type], results=[])) 119 with InsertionPoint(func.add_entry_block()): 120 lhs, rhs, result = func.entry_block.arguments 121 # TODO: prperly hook up the region. 122 linalg.MatmulOp([lhs, rhs], outputs=[result]) 123 std.ReturnOp([]) 124 125 # CHECK: linalg.matmul ins(%arg0, %arg1 : memref<2x3x4xf32>, memref<2x3x4xf32>) outs(%arg2 : memref<2x3x4xf32>) 126 print(module) 127 128 129# CHECK-LABEL: TEST: testNamedStructuredOpCustomForm 130@run 131def testNamedStructuredOpCustomForm(): 132 with Context() as ctx, Location.unknown(): 133 module = Module.create() 134 f32 = F32Type.get() 135 with InsertionPoint(module.body): 136 137 @builtin.FuncOp.from_py_func( 138 RankedTensorType.get((4, 16), f32), RankedTensorType.get((16, 8), 139 f32)) 140 def named_form(lhs, rhs): 141 init_result = linalg.InitTensorOp([4, 8], f32) 142 # First check the named form with custom format 143 # CHECK: linalg.matmul 144 # CHECK-NOT: linalg.memoized_indexing_maps 145 # CHECK-SAME: ins(%{{.*}} : tensor<4x16xf32>, tensor<16x8xf32>) 146 # CHECK-SAME: outs(%{{.*}} : tensor<4x8xf32>) 147 # CHECK-SAME: -> tensor<4x8xf32> 148 # CHECK-NEXT: return 149 return linalg.matmul(lhs, rhs, outs=[init_result.result]) 150 151 print(module) 152 153 154# CHECK-LABEL: TEST: testNamedStructuredOpGenericForm 155@run 156def testNamedStructuredOpGenericForm(): 157 with Context() as ctx, Location.unknown(): 158 module = Module.create() 159 f32 = F32Type.get() 160 with InsertionPoint(module.body): 161 162 @builtin.FuncOp.from_py_func( 163 RankedTensorType.get((4, 16), f32), RankedTensorType.get((16, 8), 164 f32)) 165 def named_form(lhs, rhs): 166 init_result = linalg.InitTensorOp([4, 8], f32) 167 # CHECK: "linalg.matmul"(%{{.*}}) 168 # CHECK-NEXT: ^bb0(%{{.*}}: f32, %{{.*}}: f32, %{{.*}}: f32): 169 # CHECK-NEXT: arith.mulf{{.*}} (f32, f32) -> f32 170 # CHECK-NEXT: arith.addf{{.*}} (f32, f32) -> f32 171 # CHECK-NEXT: linalg.yield{{.*}} (f32) -> () 172 # CHECK-NEXT: {linalg.memoized_indexing_maps{{.*}}operand_segment_sizes = dense<[2, 1]> : vector<2xi32>} : 173 # CHECK-SAME: (tensor<4x16xf32>, tensor<16x8xf32>, tensor<4x8xf32>) -> tensor<4x8xf32> 174 return linalg.matmul(lhs, rhs, outs=[init_result.result]) 175 176 module.operation.print(print_generic_op_form=True) 177 178 179# CHECK-LABEL: TEST: testNamedStructuredAsGenericOp 180@run 181def testNamedStructuredAsGenericOp(): 182 with Context() as ctx, Location.unknown(): 183 module = Module.create() 184 f32 = F32Type.get() 185 with InsertionPoint(module.body): 186 187 @builtin.FuncOp.from_py_func( 188 RankedTensorType.get((4, 16), f32), RankedTensorType.get((16, 8), 189 f32)) 190 def generic_form(lhs, rhs): 191 init_result = linalg.InitTensorOp([4, 8], f32) 192 # CHECK: linalg.generic 193 return linalg.matmul( 194 lhs, rhs, outs=[init_result.result], emit_generic=True) 195 196 print(module) 197 198 199# CHECK-LABEL: TEST: testOpResultFromOtherOp 200@run 201def testOpResultFromOtherOp(): 202 with Context(), Location.unknown(): 203 module = Module.create() 204 f32 = F32Type.get() 205 with InsertionPoint(module.body): 206 207 @builtin.FuncOp.from_py_func( 208 RankedTensorType.get((4, 16), f32), RankedTensorType.get((16, 8), 209 f32)) 210 def pass_an_op_directly(arg0, arg1): 211 one = arith.ConstantOp(F32Type.get(), 1.0) 212 # CHECK: %[[LHS:.*]] = linalg.fill 213 lhs = linalg.FillOp(arg0, one) 214 # CHECK: %[[RHS:.*]] = linalg.fill 215 rhs = linalg.FillOp(arg1, one) 216 # CHECK: %[[INIT:.*]] = linalg.init_tensor 217 init = linalg.InitTensorOp([4, 8], f32) 218 # CHECK: linalg.matmul 219 # CHECK: ins(%[[LHS]], %[[RHS]] 220 # CHECK: outs(%[[INIT]] 221 return linalg.matmul(lhs, rhs, outs=init) 222 223 print(module) 224