File: transform-tile-and-fuse.mlir

package info (click to toggle)
llvm-toolchain-16 1%3A16.0.6-15~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,634,792 kB
  • sloc: cpp: 6,179,261; ansic: 1,216,205; asm: 741,319; python: 196,614; objc: 75,325; f90: 49,640; lisp: 32,396; pascal: 12,286; sh: 9,394; perl: 7,442; ml: 5,494; awk: 3,523; makefile: 2,723; javascript: 1,206; xml: 886; fortran: 581; cs: 573
file content (113 lines) | stat: -rw-r--r-- 4,039 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// RUN: mlir-opt %s --test-transform-dialect-interpreter --split-input-file -canonicalize | FileCheck %s

// This is a simple tile-and-fuse example with a single fusion group.

module {
  // CHECK: func @foo
  // CHECK:   scf.foreach_thread {{.*}} {
  // CHECK:     linalg.fill
  // CHECK:     linalg.matmul
  // CHECK:     linalg.generic
  // CHECK:   }
  func.func @foo(%A: tensor<?x?xf32>, %B: tensor<?x?xf32>, %C: tensor<?xf32>,
                 %D: tensor<?x?xf32>, %sz0: index, %sz1: index)
      -> tensor<?x?xf32>
  {
    %cst = arith.constant 0.000000e+00 : f32
    %5 = linalg.fill
        {__producer__}
        ins(%cst : f32)
        outs(%D : tensor<?x?xf32>) -> tensor<?x?xf32>
    %6 = linalg.matmul
        {__producer__}
        ins(%A, %B : tensor<?x?xf32>, tensor<?x?xf32>)
        outs(%5 : tensor<?x?xf32>) -> tensor<?x?xf32>
    %7 = linalg.generic
        {__root__,
         indexing_maps = [affine_map<(d0, d1) -> (d0)>,
                          affine_map<(d0, d1) -> (d0, d1)>,
                          affine_map<(d0, d1) -> (d0, d1)>],
         iterator_types = ["parallel", "parallel"]
        }
        ins(%C, %6 : tensor<?xf32>, tensor<?x?xf32>)
        outs(%D : tensor<?x?xf32>) {
    ^bb0(%arg2: f32, %arg3: f32, %arg4: f32):
      %16 = arith.maxf %arg3, %cst : f32
      %17 = arith.cmpf ogt, %arg2, %cst : f32
      %18 = arith.select %17, %cst, %16 : f32
      linalg.yield %18 : f32
    } -> tensor<?x?xf32>
    return %7 : tensor<?x?xf32>
  }

  transform.sequence failures(propagate) {
  ^bb1(%arg1: !pdl.operation):
    // Find the root and all producers.
    %root = transform.structured.match attributes{"__root__"} in %arg1
    %producers = transform.structured.match attributes{"__producer__"} in %arg1

    // Tile the root.
    %foreach_thread_op, %tiled_op = transform.structured.tile_to_foreach_thread_op %root num_threads [10, 20]

    // Fuse all producers.
    transform.structured.fuse_into_containing_op %producers into %foreach_thread_op
  }
}

// -----

// Inverse the order of the payload ops passed to the tile_to_foreach_thread_op
// op. Fusion should still work.

module {
  // CHECK: func @foo
  // CHECK:   scf.foreach_thread {{.*}} {
  // CHECK:     linalg.fill
  // CHECK:     linalg.matmul
  // CHECK:     linalg.generic
  // CHECK:   }
  func.func @foo(%A: tensor<?x?xf32>, %B: tensor<?x?xf32>, %C: tensor<?xf32>,
                 %D: tensor<?x?xf32>, %sz0: index, %sz1: index)
      -> tensor<?x?xf32>
  {
    %cst = arith.constant 0.000000e+00 : f32
    %5 = linalg.fill
        {__producer__}
        ins(%cst : f32)
        outs(%D : tensor<?x?xf32>) -> tensor<?x?xf32>
    %6 = linalg.matmul
        {__producer__}
        ins(%A, %B : tensor<?x?xf32>, tensor<?x?xf32>)
        outs(%5 : tensor<?x?xf32>) -> tensor<?x?xf32>
    %7 = linalg.generic
        {__root__,
         indexing_maps = [affine_map<(d0, d1) -> (d0)>,
                          affine_map<(d0, d1) -> (d0, d1)>,
                          affine_map<(d0, d1) -> (d0, d1)>],
         iterator_types = ["parallel", "parallel"]
        }
        ins(%C, %6 : tensor<?xf32>, tensor<?x?xf32>)
        outs(%D : tensor<?x?xf32>) {
    ^bb0(%arg2: f32, %arg3: f32, %arg4: f32):
      %16 = arith.maxf %arg3, %cst : f32
      %17 = arith.cmpf ogt, %arg2, %cst : f32
      %18 = arith.select %17, %cst, %16 : f32
      linalg.yield %18 : f32
    } -> tensor<?x?xf32>
    return %7 : tensor<?x?xf32>
  }

  transform.sequence failures(propagate) {
  ^bb1(%arg1: !pdl.operation):
    // Find the root and all producers.
    %root = transform.structured.match attributes{"__root__"} in %arg1
    %producers = transform.structured.match attributes{"__producer__"} in %arg1
    %reversed_producers = transform.test_reverse_payload_ops %producers

    // Tile the root.
    %foreach_thread_op, %tiled_op = transform.structured.tile_to_foreach_thread_op %root num_threads [10, 20]

    // Fuse all producers.
    transform.structured.fuse_into_containing_op %reversed_producers into %foreach_thread_op
  }
}