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