1# RUN: %PYTHON %s | FileCheck %s
2
3from mlir.ir import *
4from mlir.dialects import builtin
5from mlir.dialects import func
6from mlir.dialects import linalg
7
8from mlir.dialects.linalg.opdsl.lang import *
9
10T1 = TV.T1
11T2 = TV.T2
12
13
14@linalg_structured_op
15def fill_poly(value=ScalarDef(T1), O=TensorDef(U, output=True)):
16  O[None] = TypeFn.cast_signed(U, value)
17
18@linalg_structured_op
19def fill_rank_zero_poly(I=TensorDef(T1), O=TensorDef(U, output=True)):
20  O[None] = TypeFn.cast_signed(U, I[None])
21
22with Context() as ctx, Location.unknown():
23  module = Module.create()
24  f32 = F32Type.get()
25  with InsertionPoint(module.body):
26
27    # Fill indexing maps.
28    # CHECK-DAG: #[[$MAP0:.+]] = affine_map<() -> ()>
29    # CHECK-DAG: #[[$MAP1:.+]] = affine_map<(d0, d1) -> ()>
30    # CHECK-DAG: #[[$MAP2:.+]] = affine_map<(d0, d1) -> (d0, d1)>
31    # CHECK-DAG: #[[$MAP3:.+]] = affine_map<(d0, d1, d2) -> ()>
32    # CHECK-DAG: #[[$MAP4:.+]] = affine_map<(d0, d1, d2) -> (d0, d1, d2)>
33
34    # CHECK-LABEL: @test_fill_0d
35    # CHECK: linalg.generic
36    # CHECK-SAME: indexing_maps = [#[[$MAP0]], #[[$MAP0]]
37    # CHECK-SAME: iterator_types = []
38    @func.FuncOp.from_py_func(f32, RankedTensorType.get([], f32))
39    def test_fill_0d(value, init_result):
40      return fill_poly(value, outs=[init_result])
41
42    # CHECK-LABEL: @test_fill_2d
43    # CHECK: linalg.generic
44    # CHECK-SAME: indexing_maps = [#[[$MAP1]], #[[$MAP2]]]
45    # CHECK-SAME: iterator_types = ["parallel", "parallel"]
46    @func.FuncOp.from_py_func(f32, RankedTensorType.get([4, 16], f32))
47    def test_fill_2d(value, init_result):
48      return fill_poly(value, outs=[init_result])
49
50    # CHECK-LABEL: @test_fill_rank_zero_3d
51    # CHECK: linalg.generic
52    # CHECK-SAME: indexing_maps = [#[[$MAP3]], #[[$MAP4]]]
53    # CHECK-SAME: iterator_types = ["parallel", "parallel", "parallel"]
54    @func.FuncOp.from_py_func(
55        RankedTensorType.get([], f32), RankedTensorType.get([4, 8, 16], f32))
56    def test_fill_rank_zero_3d(input, init_result):
57      return fill_rank_zero_poly(input, outs=[init_result])
58
59print(module)
60