2019-06-20 02:30:16 +08:00
|
|
|
// RUN: mlir-opt %s -affine-loop-fusion -split-input-file | FileCheck %s
|
|
|
|
// RUN: mlir-opt %s -affine-loop-fusion -fusion-maximal -split-input-file | FileCheck %s --check-prefix=MAXIMAL
|
2018-12-18 01:57:14 +08:00
|
|
|
|
|
|
|
// TODO(andydavis) Add more tests:
|
|
|
|
// *) Add nested fusion test cases when non-constant loop bound support is
|
|
|
|
// added to iteration domain in dependence check.
|
|
|
|
// *) Add a test w/ floordiv/ceildiv/mod when supported in dependence check.
|
|
|
|
// *) Add tests which check fused computation slice indexing and loop bounds.
|
2019-01-03 02:20:00 +08:00
|
|
|
// TODO(andydavis) Test clean up: move memref allocs to func args.
|
2018-12-18 01:57:14 +08:00
|
|
|
|
|
|
|
// -----
|
|
|
|
|
2019-01-23 05:58:52 +08:00
|
|
|
// CHECK: [[MAP0:#map[0-9]+]] = (d0, d1) -> (-d0 + d1)
|
2018-12-18 01:57:14 +08:00
|
|
|
|
2019-01-03 02:20:00 +08:00
|
|
|
// CHECK-LABEL: func @should_fuse_raw_dep_for_locality() {
|
|
|
|
func @should_fuse_raw_dep_for_locality() {
|
2018-12-18 01:57:14 +08:00
|
|
|
%m = alloc() : memref<10xf32>
|
|
|
|
%cf7 = constant 7.0 : f32
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
store %cf7, %m[%i0] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i1 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
%v0 = load %m[%i1] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 10 {
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %1 = affine.apply [[MAP0]](%i0, %i0)
|
2019-01-23 05:58:52 +08:00
|
|
|
// CHECK-NEXT: store %cst, %0[%1] : memref<1xf32>
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %2 = affine.apply [[MAP0]](%i0, %i0)
|
2019-01-23 05:58:52 +08:00
|
|
|
// CHECK-NEXT: %3 = load %0[%2] : memref<1xf32>
|
2018-12-18 01:57:14 +08:00
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
|
|
|
|
2019-01-23 05:58:52 +08:00
|
|
|
// CHECK-DAG: [[MAP0:#map[0-9]+]] = (d0, d1) -> (-d0 + d1)
|
2018-12-18 01:57:14 +08:00
|
|
|
|
2019-01-03 02:20:00 +08:00
|
|
|
// CHECK-LABEL: func @should_fuse_reduction_to_pointwise() {
|
|
|
|
func @should_fuse_reduction_to_pointwise() {
|
2018-12-18 01:57:14 +08:00
|
|
|
%a = alloc() : memref<10x10xf32>
|
|
|
|
%b = alloc() : memref<10xf32>
|
|
|
|
%c = alloc() : memref<10xf32>
|
|
|
|
|
|
|
|
%cf7 = constant 7.0 : f32
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 10 {
|
|
|
|
affine.for %i1 = 0 to 10 {
|
2018-12-20 12:42:55 +08:00
|
|
|
%v0 = load %b[%i0] : memref<10xf32>
|
2018-12-18 01:57:14 +08:00
|
|
|
%v1 = load %a[%i0, %i1] : memref<10x10xf32>
|
|
|
|
%v3 = addf %v0, %v1 : f32
|
|
|
|
store %v3, %b[%i0] : memref<10xf32>
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i2 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
%v4 = load %b[%i2] : memref<10xf32>
|
|
|
|
store %v4, %c[%i2] : memref<10xf32>
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should fuse in entire inner loop on %i1 from source loop nest, as %i1
|
|
|
|
// is not used in the access function of the store/load on %b.
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 10 {
|
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 10 {
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %3 = affine.apply [[MAP0]](%i0, %i0)
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: %4 = load %0[%3] : memref<1xf32>
|
|
|
|
// CHECK-NEXT: %5 = load %1[%i0, %i1] : memref<10x10xf32>
|
2018-12-20 12:42:55 +08:00
|
|
|
// CHECK-NEXT: %6 = addf %4, %5 : f32
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %7 = affine.apply [[MAP0]](%i0, %i0)
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: store %6, %0[%7] : memref<1xf32>
|
2018-12-20 12:42:55 +08:00
|
|
|
// CHECK-NEXT: }
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %8 = affine.apply [[MAP0]](%i0, %i0)
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: %9 = load %0[%8] : memref<1xf32>
|
|
|
|
// CHECK-NEXT: store %9, %2[%i0] : memref<10xf32>
|
2018-12-18 01:57:14 +08:00
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
|
|
|
|
2019-01-23 05:58:52 +08:00
|
|
|
// CHECK-DAG: [[MAP_SHIFT_MINUS_ONE_R1:#map[0-9]+]] = (d0) -> (d0 - 1)
|
2019-01-26 08:00:50 +08:00
|
|
|
// CHECK-DAG: [[MAP_SHIFT_BY_ONE:#map[0-9]+]] = (d0) -> (d0 + 1)
|
|
|
|
// CHECK-DAG: [[MAP_SHIFT_MINUS_IV_R2_EVEN:#map[0-9]+]] = (d0, d1, d2, d3) -> (-d0 + d2)
|
|
|
|
// CHECK-DAG: [[MAP_SHIFT_MINUS_IV_R2_ODD:#map[0-9]+]] = (d0, d1, d2, d3) -> (-d1 + d3)
|
2018-12-18 01:57:14 +08:00
|
|
|
|
2019-01-03 02:20:00 +08:00
|
|
|
// CHECK-LABEL: func @should_fuse_loop_nests_with_shifts() {
|
|
|
|
func @should_fuse_loop_nests_with_shifts() {
|
2018-12-18 01:57:14 +08:00
|
|
|
%a = alloc() : memref<10x10xf32>
|
|
|
|
%cf7 = constant 7.0 : f32
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 9 {
|
|
|
|
affine.for %i1 = 0 to 9 {
|
2019-02-07 03:08:18 +08:00
|
|
|
%idx = affine.apply (d0) -> (d0 + 1) (%i0)
|
|
|
|
%idy = affine.apply (d0) -> (d0 + 1) (%i1)
|
2019-01-26 08:00:50 +08:00
|
|
|
store %cf7, %a[%idx, %idy] : memref<10x10xf32>
|
2018-12-18 01:57:14 +08:00
|
|
|
}
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i2 = 1 to 10 {
|
|
|
|
affine.for %i3 = 1 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
%v0 = load %a[%i2, %i3] : memref<10x10xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-19 00:56:27 +08:00
|
|
|
// Source slice affine apply sequence:
|
|
|
|
// *) First two affine apply's map from the dst to src iteration space.
|
|
|
|
// *) Third affine apply is access function around src store.
|
|
|
|
// *) Fourth affine apply shifts the stores access function by '-1', because
|
|
|
|
// of the offset induced by reducing the memref shape from 10x10 to 9x9.
|
|
|
|
// *) Fifth affine apply shifts the loads access function by '-1', because
|
|
|
|
// of the offset induced by reducing the memref shape from 10x10 to 9x9.
|
|
|
|
// NOTE: Should create a private memref with reduced shape 9x9xf32.
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 1 to 10 {
|
|
|
|
// CHECK-NEXT: affine.for %i1 = 1 to 10 {
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %1 = affine.apply [[MAP_SHIFT_MINUS_ONE_R1]](%i0)
|
|
|
|
// CHECK-NEXT: %2 = affine.apply [[MAP_SHIFT_MINUS_ONE_R1]](%i1)
|
|
|
|
// CHECK-NEXT: %3 = affine.apply [[MAP_SHIFT_BY_ONE]](%1)
|
|
|
|
// CHECK-NEXT: %4 = affine.apply [[MAP_SHIFT_BY_ONE]](%2)
|
|
|
|
// CHECK-NEXT: %5 = affine.apply [[MAP_SHIFT_MINUS_IV_R2_EVEN]](%i0, %i1, %3, %4)
|
|
|
|
// CHECK-NEXT: %6 = affine.apply [[MAP_SHIFT_MINUS_IV_R2_ODD]](%i0, %i1, %3, %4)
|
2019-01-26 08:00:50 +08:00
|
|
|
// CHECK-NEXT: store %cst, %0[%5, %6] : memref<1x1xf32>
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %7 = affine.apply [[MAP_SHIFT_MINUS_IV_R2_EVEN]](%i0, %i1, %i0, %i1)
|
|
|
|
// CHECK-NEXT: %8 = affine.apply [[MAP_SHIFT_MINUS_IV_R2_ODD]](%i0, %i1, %i0, %i1)
|
2019-01-26 08:00:50 +08:00
|
|
|
// CHECK-NEXT: %9 = load %0[%7, %8] : memref<1x1xf32>
|
2018-12-18 01:57:14 +08:00
|
|
|
// CHECK-NEXT: }
|
2018-11-01 22:26:00 +08:00
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
2018-12-18 01:57:14 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
|
|
|
|
2019-01-26 08:00:50 +08:00
|
|
|
// CHECK-DAG: [[MAP_D2_D0_DIFF:#map[0-9]+]] = (d0, d1, d2, d3) -> (-d0 + d2)
|
|
|
|
// CHECK-DAG: [[MAP_D3_D1_DIFF:#map[0-9]+]] = (d0, d1, d2, d3) -> (-d1 + d3)
|
2018-12-18 01:57:14 +08:00
|
|
|
|
2019-01-03 02:20:00 +08:00
|
|
|
// CHECK-LABEL: func @should_fuse_loop_nest() {
|
|
|
|
func @should_fuse_loop_nest() {
|
2018-12-18 01:57:14 +08:00
|
|
|
%a = alloc() : memref<10x10xf32>
|
|
|
|
%b = alloc() : memref<10x10xf32>
|
|
|
|
%cf7 = constant 7.0 : f32
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 10 {
|
|
|
|
affine.for %i1 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
store %cf7, %a[%i0, %i1] : memref<10x10xf32>
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i2 = 0 to 10 {
|
|
|
|
affine.for %i3 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
%v0 = load %a[%i3, %i2] : memref<10x10xf32>
|
|
|
|
store %v0, %b[%i2, %i3] : memref<10x10xf32>
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i4 = 0 to 10 {
|
|
|
|
affine.for %i5 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
%v1 = load %b[%i4, %i5] : memref<10x10xf32>
|
|
|
|
}
|
|
|
|
}
|
2019-01-30 01:36:41 +08:00
|
|
|
// Expecting private memref for '%a' first, then private memref for '%b'.
|
|
|
|
// CHECK-DAG: [[NEWA:%[0-9]+]] = alloc() : memref<1x1xf32>
|
|
|
|
// CHECK-DAG: [[NEWB:%[0-9]+]] = alloc() : memref<1x1xf32>
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 10 {
|
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 10 {
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %2 = affine.apply [[MAP_D2_D0_DIFF]](%i1, %i0, %i1, %i0)
|
|
|
|
// CHECK-NEXT: %3 = affine.apply [[MAP_D3_D1_DIFF]](%i1, %i0, %i1, %i0)
|
2019-01-26 08:00:50 +08:00
|
|
|
// CHECK-NEXT: store %cst, [[NEWA]][%2, %3] : memref<1x1xf32>
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %4 = affine.apply [[MAP_D2_D0_DIFF]](%i1, %i0, %i1, %i0)
|
|
|
|
// CHECK-NEXT: %5 = affine.apply [[MAP_D3_D1_DIFF]](%i1, %i0, %i1, %i0)
|
2019-01-26 08:00:50 +08:00
|
|
|
// CHECK-NEXT: %6 = load [[NEWA]][%4, %5] : memref<1x1xf32>
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %7 = affine.apply [[MAP_D2_D0_DIFF]](%i0, %i1, %i0, %i1)
|
|
|
|
// CHECK-NEXT: %8 = affine.apply [[MAP_D3_D1_DIFF]](%i0, %i1, %i0, %i1)
|
2019-01-26 08:00:50 +08:00
|
|
|
// CHECK-NEXT: store %6, [[NEWB]][%7, %8] : memref<1x1xf32>
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %9 = affine.apply [[MAP_D2_D0_DIFF]](%i0, %i1, %i0, %i1)
|
|
|
|
// CHECK-NEXT: %10 = affine.apply [[MAP_D3_D1_DIFF]](%i0, %i1, %i0, %i1)
|
2019-01-26 08:00:50 +08:00
|
|
|
// CHECK-NEXT: %11 = load [[NEWB]][%9, %10] : memref<1x1xf32>
|
2018-11-01 22:26:00 +08:00
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
2018-12-18 01:57:14 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
|
|
|
|
2019-01-23 05:58:52 +08:00
|
|
|
// CHECK-DAG: [[MAP0:#map[0-9]+]] = (d0, d1) -> (-d0 + d1)
|
2018-12-18 01:57:14 +08:00
|
|
|
|
2019-01-03 02:20:00 +08:00
|
|
|
// CHECK-LABEL: func @should_fuse_across_intermediate_loop_with_no_deps() {
|
|
|
|
func @should_fuse_across_intermediate_loop_with_no_deps() {
|
2018-12-18 01:57:14 +08:00
|
|
|
%a = alloc() : memref<10xf32>
|
|
|
|
%b = alloc() : memref<10xf32>
|
|
|
|
%c = alloc() : memref<10xf32>
|
|
|
|
|
|
|
|
%cf7 = constant 7.0 : f32
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
%v0 = load %a[%i0] : memref<10xf32>
|
|
|
|
store %v0, %b[%i0] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i1 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
store %cf7, %c[%i1] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i2 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
%v1 = load %b[%i2] : memref<10xf32>
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should fuse first loop (past second loop with no dependences) into third.
|
2019-01-19 00:56:27 +08:00
|
|
|
// Note that fusion creates a private memref '%2' for the fused loop nest.
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 10 {
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: store %cst, %2[%i0] : memref<10xf32>
|
2018-12-18 01:57:14 +08:00
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i1 = 0 to 10 {
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: %3 = load %1[%i1] : memref<10xf32>
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %4 = affine.apply [[MAP0]](%i1, %i1)
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: store %3, %0[%4] : memref<1xf32>
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %5 = affine.apply [[MAP0]](%i1, %i1)
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: %6 = load %0[%5] : memref<1xf32>
|
2018-12-18 01:57:14 +08:00
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
|
|
|
|
2019-01-23 05:58:52 +08:00
|
|
|
// CHECK: [[MAP0:#map[0-9]+]] = (d0, d1) -> (-d0 + d1)
|
2018-12-18 01:57:14 +08:00
|
|
|
|
2019-01-03 02:20:00 +08:00
|
|
|
// CHECK-LABEL: func @should_fuse_all_loops() {
|
|
|
|
func @should_fuse_all_loops() {
|
2018-12-18 01:57:14 +08:00
|
|
|
%a = alloc() : memref<10xf32>
|
|
|
|
%b = alloc() : memref<10xf32>
|
|
|
|
%cf7 = constant 7.0 : f32
|
|
|
|
|
|
|
|
// Set up flow dependences from first and second loops to third.
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
store %cf7, %a[%i0] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i1 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
store %cf7, %b[%i1] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i2 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
%v0 = load %a[%i2] : memref<10xf32>
|
|
|
|
%v1 = load %b[%i2] : memref<10xf32>
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should fuse first and second loops into third.
|
2019-01-30 01:36:41 +08:00
|
|
|
// Expecting private memref for '%a' first, then private memref for '%b'.
|
|
|
|
// CHECK-DAG: [[NEWA:%[0-9]+]] = alloc() : memref<1xf32>
|
|
|
|
// CHECK-DAG: [[NEWB:%[0-9]+]] = alloc() : memref<1xf32>
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 10 {
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %2 = affine.apply [[MAP0]](%i0, %i0)
|
2019-01-23 05:58:52 +08:00
|
|
|
// CHECK-NEXT: store %cst, [[NEWA]][%2] : memref<1xf32>
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %3 = affine.apply [[MAP0]](%i0, %i0)
|
2019-01-23 05:58:52 +08:00
|
|
|
// CHECK-NEXT: store %cst, [[NEWB]][%3] : memref<1xf32>
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %4 = affine.apply [[MAP0]](%i0, %i0)
|
2019-01-23 05:58:52 +08:00
|
|
|
// CHECK-NEXT: %5 = load [[NEWA]][%4] : memref<1xf32>
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %6 = affine.apply [[MAP0]](%i0, %i0)
|
2019-01-23 05:58:52 +08:00
|
|
|
// CHECK-NEXT: %7 = load [[NEWB]][%6] : memref<1xf32>
|
2018-12-18 01:57:14 +08:00
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
|
|
|
|
2019-01-23 05:58:52 +08:00
|
|
|
// CHECK: [[MAP0:#map[0-9]+]] = (d0, d1) -> (-d0 + d1)
|
2018-12-18 01:57:14 +08:00
|
|
|
|
2019-01-03 02:20:00 +08:00
|
|
|
// CHECK-LABEL: func @should_fuse_first_and_second_loops() {
|
|
|
|
func @should_fuse_first_and_second_loops() {
|
2018-12-18 01:57:14 +08:00
|
|
|
%a = alloc() : memref<10xf32>
|
|
|
|
%b = alloc() : memref<10xf32>
|
|
|
|
%c = alloc() : memref<10xf32>
|
|
|
|
|
|
|
|
%cf7 = constant 7.0 : f32
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
store %cf7, %a[%i0] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i1 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
%v0 = load %a[%i1] : memref<10xf32>
|
|
|
|
store %cf7, %b[%i1] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i2 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
%v1 = load %c[%i2] : memref<10xf32>
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should fuse first loop into the second (last loop should not be fused).
|
2019-01-19 00:56:27 +08:00
|
|
|
// Should create private memref '%2' for fused loop.
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 10 {
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %3 = affine.apply [[MAP0]](%i0, %i0)
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: store %cst, %0[%3] : memref<1xf32>
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %4 = affine.apply [[MAP0]](%i0, %i0)
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: %5 = load %0[%4] : memref<1xf32>
|
|
|
|
// CHECK-NEXT: store %cst, %1[%i0] : memref<10xf32>
|
2018-12-18 01:57:14 +08:00
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i1 = 0 to 10 {
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: %6 = load %2[%i1] : memref<10xf32>
|
2018-12-18 01:57:14 +08:00
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
|
|
|
|
2019-01-03 02:20:00 +08:00
|
|
|
// CHECK-LABEL: func @should_not_fuse_would_create_cycle() {
|
|
|
|
func @should_not_fuse_would_create_cycle() {
|
2018-12-18 01:57:14 +08:00
|
|
|
%a = alloc() : memref<10xf32>
|
|
|
|
%b = alloc() : memref<10xf32>
|
|
|
|
%c = alloc() : memref<10xf32>
|
|
|
|
|
|
|
|
%cf7 = constant 7.0 : f32
|
|
|
|
|
|
|
|
// Set up the following dependences:
|
|
|
|
// 1) loop0 -> loop1 on memref '%a'
|
|
|
|
// 2) loop0 -> loop2 on memref '%b'
|
|
|
|
// 3) loop1 -> loop2 on memref '%c'
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
%v0 = load %a[%i0] : memref<10xf32>
|
|
|
|
store %cf7, %b[%i0] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i1 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
store %cf7, %a[%i1] : memref<10xf32>
|
|
|
|
%v1 = load %c[%i1] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i2 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
%v2 = load %b[%i2] : memref<10xf32>
|
|
|
|
store %cf7, %c[%i2] : memref<10xf32>
|
|
|
|
}
|
|
|
|
// Should not fuse: fusing loop first loop into last would create a cycle.
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
// CHECK-NEXT: %3 = load %0[%i0] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: store %cst, %1[%i0] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i1 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
// CHECK-NEXT: store %cst, %0[%i1] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: %4 = load %2[%i1] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i2 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
// CHECK-NEXT: %5 = load %1[%i2] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: store %cst, %2[%i2] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
|
|
|
|
2019-03-29 05:54:49 +08:00
|
|
|
// CHECK: [[MAP0:#map[0-9]+]] = (d0, d1) -> (-d0 + d1)
|
|
|
|
|
|
|
|
// CHECK-LABEL: func @should_fuse_producer_consumer() {
|
|
|
|
func @should_fuse_producer_consumer() {
|
2018-12-18 01:57:14 +08:00
|
|
|
%m = alloc() : memref<10xf32>
|
|
|
|
%cf7 = constant 7.0 : f32
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
store %cf7, %m[%i0] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i1 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
store %cf7, %m[%i1] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i2 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
%v1 = load %m[%i2] : memref<10xf32>
|
|
|
|
}
|
2019-03-29 05:54:49 +08:00
|
|
|
// Fusing loop %i0 to %i2 would violate the WAW dependence between %i0 and
|
|
|
|
// %i1, but OK to fuse %i1 into %i2.
|
|
|
|
// TODO(andydavis) When the fusion pass is run to a fixed-point, it should
|
|
|
|
// fuse all three of these loop nests.
|
|
|
|
// CHECK: %0 = alloc() : memref<1xf32>
|
|
|
|
// CHECK: %1 = alloc() : memref<10xf32>
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 10 {
|
2019-03-29 05:54:49 +08:00
|
|
|
// CHECK-NEXT: store %cst, %1[%i0] : memref<10xf32>
|
2018-12-18 01:57:14 +08:00
|
|
|
// CHECK-NEXT: }
|
2019-03-29 05:54:49 +08:00
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 10 {
|
|
|
|
// CHECK-NEXT: %2 = affine.apply [[MAP0]](%i1, %i1)
|
|
|
|
// CHECK-NEXT: store %cst, %0[%2] : memref<1xf32>
|
|
|
|
// CHECK-NEXT: %3 = affine.apply [[MAP0]](%i1, %i1)
|
|
|
|
// CHECK-NEXT: %4 = load %0[%3] : memref<1xf32>
|
2018-12-18 01:57:14 +08:00
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
|
|
|
|
2019-01-31 03:33:32 +08:00
|
|
|
// CHECK: [[MAP0:#map[0-9]+]] = (d0, d1) -> (-d0 + d1)
|
|
|
|
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-LABEL: func @should_fuse_and_move_to_preserve_war_dep() {
|
|
|
|
func @should_fuse_and_move_to_preserve_war_dep() {
|
2018-12-18 01:57:14 +08:00
|
|
|
%a = alloc() : memref<10xf32>
|
|
|
|
%b = alloc() : memref<10xf32>
|
|
|
|
%cf7 = constant 7.0 : f32
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
%v0 = load %a[%i0] : memref<10xf32>
|
|
|
|
store %v0, %b[%i0] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i1 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
store %cf7, %a[%i1] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i2 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
%v1 = load %b[%i2] : memref<10xf32>
|
|
|
|
}
|
2019-01-30 01:36:41 +08:00
|
|
|
// Loops '%i1' and '%i2' have no dependences. We can fuse a slice of '%i0'
|
|
|
|
// into '%i2' if we move the fused loop nest before '%i1', which preserves
|
|
|
|
// the WAR dependence from load '%a' in '%i0' to the store '%a' in loop '%i1'.
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 10 {
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: %2 = load %1[%i0] : memref<10xf32>
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %3 = affine.apply [[MAP0]](%i0, %i0)
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: store %2, %0[%3] : memref<1xf32>
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %4 = affine.apply [[MAP0]](%i0, %i0)
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: %5 = load %0[%4] : memref<1xf32>
|
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 10 {
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: store %cst, %1[%i1] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
2018-12-18 01:57:14 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
|
|
|
|
2019-01-31 03:33:32 +08:00
|
|
|
// CHECK: [[MAP0:#map[0-9]+]] = (d0, d1) -> (-d0 + d1)
|
|
|
|
|
2019-01-19 00:56:27 +08:00
|
|
|
// CHECK-LABEL: func @should_fuse_with_private_memref_if_top_level_access() {
|
|
|
|
func @should_fuse_with_private_memref_if_top_level_access() {
|
2018-12-18 01:57:14 +08:00
|
|
|
%m = alloc() : memref<10xf32>
|
|
|
|
%cf7 = constant 7.0 : f32
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
store %cf7, %m[%i0] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i1 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
%v0 = load %m[%i1] : memref<10xf32>
|
|
|
|
}
|
|
|
|
|
|
|
|
%c0 = constant 4 : index
|
|
|
|
%v1 = load %m[%c0] : memref<10xf32>
|
|
|
|
// Top-level load to '%m' should prevent fusion.
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 10 {
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: store %cst, %1[%i0] : memref<10xf32>
|
2018-12-18 01:57:14 +08:00
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 10 {
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %2 = affine.apply [[MAP0]](%i1, %i1)
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: store %cst, %0[%2] : memref<1xf32>
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %3 = affine.apply [[MAP0]](%i1, %i1)
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: %4 = load %0[%3] : memref<1xf32>
|
2018-12-18 01:57:14 +08:00
|
|
|
// CHECK-NEXT: }
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
|
|
|
|
2019-01-23 05:58:52 +08:00
|
|
|
// CHECK: [[MAP0:#map[0-9]+]] = (d0, d1) -> (-d0 + d1)
|
2018-12-18 01:57:14 +08:00
|
|
|
|
2019-01-03 02:20:00 +08:00
|
|
|
// CHECK-LABEL: func @should_fuse_no_top_level_access() {
|
|
|
|
func @should_fuse_no_top_level_access() {
|
2018-12-18 01:57:14 +08:00
|
|
|
%m = alloc() : memref<10xf32>
|
|
|
|
%cf7 = constant 7.0 : f32
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
store %cf7, %m[%i0] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i1 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
%v0 = load %m[%i1] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 10 {
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %1 = affine.apply [[MAP0]](%i0, %i0)
|
2019-01-23 05:58:52 +08:00
|
|
|
// CHECK-NEXT: store %cst, %0[%1] : memref<1xf32>
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %2 = affine.apply [[MAP0]](%i0, %i0)
|
2019-01-23 05:58:52 +08:00
|
|
|
// CHECK-NEXT: %3 = load %0[%2] : memref<1xf32>
|
2018-12-18 01:57:14 +08:00
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
|
|
|
|
|
|
|
#set0 = (d0) : (1 == 0)
|
|
|
|
|
2019-01-03 02:20:00 +08:00
|
|
|
// CHECK-LABEL: func @should_not_fuse_if_inst_at_top_level() {
|
|
|
|
func @should_not_fuse_if_inst_at_top_level() {
|
2018-12-18 01:57:14 +08:00
|
|
|
%m = alloc() : memref<10xf32>
|
|
|
|
%cf7 = constant 7.0 : f32
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
store %cf7, %m[%i0] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i1 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
%v0 = load %m[%i1] : memref<10xf32>
|
|
|
|
}
|
|
|
|
%c0 = constant 4 : index
|
2019-03-25 11:35:07 +08:00
|
|
|
affine.if #set0(%c0) {
|
2018-12-18 01:57:14 +08:00
|
|
|
}
|
2019-01-29 13:23:53 +08:00
|
|
|
// Top-level IfOp should prevent fusion.
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
// CHECK-NEXT: store %cst, %0[%i0] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i1 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
// CHECK-NEXT: %1 = load %0[%i1] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
|
|
|
|
|
|
|
#set0 = (d0) : (1 == 0)
|
|
|
|
|
2019-01-03 02:20:00 +08:00
|
|
|
// CHECK-LABEL: func @should_not_fuse_if_inst_in_loop_nest() {
|
|
|
|
func @should_not_fuse_if_inst_in_loop_nest() {
|
2018-12-18 01:57:14 +08:00
|
|
|
%m = alloc() : memref<10xf32>
|
|
|
|
%cf7 = constant 7.0 : f32
|
|
|
|
%c4 = constant 4 : index
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
store %cf7, %m[%i0] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i1 = 0 to 10 {
|
2019-03-25 11:35:07 +08:00
|
|
|
affine.if #set0(%c4) {
|
2018-11-01 22:26:00 +08:00
|
|
|
}
|
2018-12-18 01:57:14 +08:00
|
|
|
%v0 = load %m[%i1] : memref<10xf32>
|
2018-11-01 22:26:00 +08:00
|
|
|
}
|
|
|
|
|
2019-01-29 13:23:53 +08:00
|
|
|
// IfOp in ForInst should prevent fusion.
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 10 {
|
2018-12-18 01:57:14 +08:00
|
|
|
// CHECK-NEXT: store %cst, %0[%i0] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i1 = 0 to 10 {
|
2019-03-25 11:35:07 +08:00
|
|
|
// CHECK-NEXT: affine.if #set0(%c4) {
|
2018-12-18 01:57:14 +08:00
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: %1 = load %0[%i1] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
|
|
|
|
2019-01-26 08:00:50 +08:00
|
|
|
// CHECK: [[MAP0:#map[0-9]+]] = (d0, d1, d2, d3, d4, d5) -> (-d0 + d3)
|
|
|
|
// CHECK: [[MAP1:#map[0-9]+]] = (d0, d1, d2, d3, d4, d5) -> (-d1 + d4)
|
|
|
|
// CHECK: [[MAP2:#map[0-9]+]] = (d0, d1, d2, d3, d4, d5) -> (-d2 + d5)
|
2018-12-18 01:57:14 +08:00
|
|
|
|
2019-01-26 08:00:50 +08:00
|
|
|
// CHECK-LABEL: func @permute_and_fuse() {
|
|
|
|
func @permute_and_fuse() {
|
2018-12-18 01:57:14 +08:00
|
|
|
%m = alloc() : memref<10x20x30xf32>
|
|
|
|
|
|
|
|
%cf7 = constant 7.0 : f32
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 10 {
|
|
|
|
affine.for %i1 = 0 to 20 {
|
|
|
|
affine.for %i2 = 0 to 30 {
|
2019-01-26 08:00:50 +08:00
|
|
|
store %cf7, %m[%i0, %i1, %i2] : memref<10x20x30xf32>
|
2018-12-18 01:57:14 +08:00
|
|
|
}
|
2018-11-01 22:26:00 +08:00
|
|
|
}
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i3 = 0 to 30 {
|
|
|
|
affine.for %i4 = 0 to 10 {
|
|
|
|
affine.for %i5 = 0 to 20 {
|
2019-01-26 08:00:50 +08:00
|
|
|
%v0 = load %m[%i4, %i5, %i3] : memref<10x20x30xf32>
|
|
|
|
"foo"(%v0) : (f32) -> ()
|
2018-12-18 01:57:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 30 {
|
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 10 {
|
|
|
|
// CHECK-NEXT: affine.for %i2 = 0 to 20 {
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %1 = affine.apply [[MAP0]](%i1, %i2, %i0, %i1, %i2, %i0)
|
|
|
|
// CHECK-NEXT: %2 = affine.apply [[MAP1]](%i1, %i2, %i0, %i1, %i2, %i0)
|
|
|
|
// CHECK-NEXT: %3 = affine.apply [[MAP2]](%i1, %i2, %i0, %i1, %i2, %i0)
|
2019-01-26 08:00:50 +08:00
|
|
|
// CHECK-NEXT: store %cst, %0[%1, %2, %3] : memref<1x1x1xf32>
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %4 = affine.apply [[MAP0]](%i1, %i2, %i0, %i1, %i2, %i0)
|
|
|
|
// CHECK-NEXT: %5 = affine.apply [[MAP1]](%i1, %i2, %i0, %i1, %i2, %i0)
|
|
|
|
// CHECK-NEXT: %6 = affine.apply [[MAP2]](%i1, %i2, %i0, %i1, %i2, %i0)
|
2019-01-26 08:00:50 +08:00
|
|
|
// CHECK-NEXT: %7 = load %0[%4, %5, %6] : memref<1x1x1xf32>
|
|
|
|
// CHECK-NEXT: "foo"(%7) : (f32) -> ()
|
2018-12-18 01:57:14 +08:00
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
2018-11-01 22:26:00 +08:00
|
|
|
|
|
|
|
return
|
2018-12-18 01:57:14 +08:00
|
|
|
}
|
2018-12-22 03:06:23 +08:00
|
|
|
|
|
|
|
// -----
|
|
|
|
|
2019-01-31 03:33:32 +08:00
|
|
|
// CHECK-DAG: [[MAP0:#map[0-9]+]] = (d0, d1) -> (d0 * 4 + d1)
|
|
|
|
// CHECK-DAG: [[MAP1:#map[0-9]+]] = (d0) -> (d0 floordiv 4)
|
|
|
|
// CHECK-DAG: [[MAP2:#map[0-9]+]] = (d0) -> (d0 mod 4)
|
2019-01-05 06:52:21 +08:00
|
|
|
|
|
|
|
// Reshape from a 64 x f32 to 16 x 4 x f32.
|
|
|
|
// CHECK-LABEL: func @fuse_reshape_64_16_4
|
|
|
|
func @fuse_reshape_64_16_4(%in : memref<64xf32>) {
|
|
|
|
%out = alloc() : memref<16x4xf32>
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 64 {
|
2019-01-05 06:52:21 +08:00
|
|
|
%v = load %in[%i0] : memref<64xf32>
|
2019-02-07 03:08:18 +08:00
|
|
|
%idx = affine.apply (d0) -> (d0 floordiv 4) (%i0)
|
|
|
|
%idy = affine.apply (d0) -> (d0 mod 4) (%i0)
|
2019-01-26 08:00:50 +08:00
|
|
|
store %v, %out[%idx, %idy] : memref<16x4xf32>
|
2019-01-05 06:52:21 +08:00
|
|
|
}
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i1 = 0 to 16 {
|
|
|
|
affine.for %i2 = 0 to 4 {
|
2019-01-05 06:52:21 +08:00
|
|
|
%w = load %out[%i1, %i2] : memref<16x4xf32>
|
|
|
|
"foo"(%w) : (f32) -> ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 =
|
|
|
|
// CHECK-NEXT: affine.for %i1 =
|
2019-01-05 06:52:21 +08:00
|
|
|
// CHECK-NOT: for
|
|
|
|
// CHECK: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
}
|
2019-01-08 09:34:26 +08:00
|
|
|
|
|
|
|
// -----
|
2019-01-31 03:33:32 +08:00
|
|
|
// CHECK-DAG: [[MAP0:#map[0-9]+]] = (d0) -> (d0 floordiv 4)
|
|
|
|
// CHECK-DAG: [[MAP1:#map[0-9]+]] = (d0) -> (d0 mod 4)
|
2019-01-23 05:58:52 +08:00
|
|
|
// CHECK-DAG: [[MAP2:#map[0-9]+]] = (d0, d1) -> (d0 * 4 + d1)
|
|
|
|
// CHECK-DAG: [[MAP3:#map[0-9]+]] = (d0, d1) -> (-d0 + d1)
|
2019-01-08 09:34:26 +08:00
|
|
|
|
|
|
|
// Reshape a 16x4xf32 to 64xf32.
|
|
|
|
// CHECK-LABEL: func @fuse_reshape_16_4_64
|
|
|
|
func @fuse_reshape_16_4_64() {
|
|
|
|
%in = alloc() : memref<16x4xf32>
|
|
|
|
%out = alloc() : memref<64xf32>
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 16 {
|
|
|
|
affine.for %i1 = 0 to 4 {
|
2019-01-08 09:34:26 +08:00
|
|
|
%v = load %in[%i0, %i1] : memref<16x4xf32>
|
2019-02-07 03:08:18 +08:00
|
|
|
%idx = affine.apply (d0, d1) -> (4*d0 + d1) (%i0, %i1)
|
2019-01-08 09:34:26 +08:00
|
|
|
store %v, %out[%idx] : memref<64xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i2 = 0 to 64 {
|
2019-01-08 09:34:26 +08:00
|
|
|
%w = load %out[%i2] : memref<64xf32>
|
|
|
|
"foo"(%w) : (f32) -> ()
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 64 {
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %2 = affine.apply [[MAP0]](%i0)
|
|
|
|
// CHECK-NEXT: %3 = affine.apply [[MAP1]](%i0)
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: %4 = load %1[%2, %3] : memref<16x4xf32>
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %5 = affine.apply [[MAP2]](%2, %3)
|
|
|
|
// CHECK-NEXT: %6 = affine.apply [[MAP3]](%i0, %5)
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: store %4, %0[%6] : memref<1xf32>
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %7 = affine.apply [[MAP3]](%i0, %i0)
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: %8 = load %0[%7] : memref<1xf32>
|
2019-01-23 05:58:52 +08:00
|
|
|
// CHECK-NEXT: "foo"(%8) : (f32) -> ()
|
2019-01-08 09:34:26 +08:00
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// -----
|
|
|
|
|
|
|
|
// All three loop nests below (6-d one, 2-d one, 2-d one is fused into a single
|
|
|
|
// 2-d loop nest).
|
|
|
|
func @R6_to_R2_reshape_square() -> memref<64x9xi32> {
|
|
|
|
%in = alloc() : memref<2x2x3x3x16x1xi32>
|
|
|
|
%out = alloc() : memref<64x9xi32>
|
2019-01-19 00:56:27 +08:00
|
|
|
%live_out = alloc() : memref<64x9xi32>
|
2019-01-08 09:34:26 +08:00
|
|
|
|
2019-01-18 04:51:48 +08:00
|
|
|
// Initialize input.
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 2 {
|
|
|
|
affine.for %i1 = 0 to 2 {
|
|
|
|
affine.for %i2 = 0 to 3 {
|
|
|
|
affine.for %i3 = 0 to 3 {
|
|
|
|
affine.for %i4 = 0 to 16 {
|
|
|
|
affine.for %i5 = 0 to 1 {
|
2019-01-08 09:34:26 +08:00
|
|
|
%val = "foo"(%i0, %i1, %i2, %i3, %i4, %i5) : (index, index, index, index, index, index) -> i32
|
|
|
|
store %val, %in[%i0, %i1, %i2, %i3, %i4, %i5] : memref<2x2x3x3x16x1xi32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %ii = 0 to 64 {
|
|
|
|
affine.for %jj = 0 to 9 {
|
2019-01-08 09:34:26 +08:00
|
|
|
// Convert output coordinates to linear index.
|
2019-02-07 03:08:18 +08:00
|
|
|
%a0 = affine.apply (d0, d1) -> (d0 * 9 + d1) (%ii, %jj)
|
|
|
|
%0 = affine.apply (d0) -> (d0 floordiv (2 * 3 * 3 * 16 * 1))(%a0)
|
|
|
|
%1 = affine.apply (d0) -> ((d0 mod 288) floordiv (3 * 3 * 16 * 1))(%a0)
|
|
|
|
%2 = affine.apply (d0) -> (((d0 mod 288) mod 144) floordiv (3 * 16 * 1))(%a0)
|
|
|
|
%3 = affine.apply (d0) -> ((((d0 mod 288) mod 144) mod 48) floordiv (16 * 1))(%a0)
|
|
|
|
%4 = affine.apply (d0) -> ((((d0 mod 288) mod 144) mod 48) mod 16)(%a0)
|
|
|
|
%5 = affine.apply (d0) -> (((((d0 mod 144) mod 144) mod 48) mod 16) mod 1)(%a0)
|
2019-01-26 08:00:50 +08:00
|
|
|
%v = load %in[%0, %1, %2, %3, %4, %5] : memref<2x2x3x3x16x1xi32>
|
2019-01-08 09:34:26 +08:00
|
|
|
store %v, %out[%ii, %jj] : memref<64x9xi32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i = 0 to 64 {
|
|
|
|
affine.for %j = 0 to 9 {
|
2019-01-08 09:34:26 +08:00
|
|
|
%a = load %out[%i, %j] : memref<64x9xi32>
|
|
|
|
%b = muli %a, %a : i32
|
2019-01-19 00:56:27 +08:00
|
|
|
store %b, %live_out[%i, %j] : memref<64x9xi32>
|
2019-01-08 09:34:26 +08:00
|
|
|
}
|
|
|
|
}
|
2019-01-19 00:56:27 +08:00
|
|
|
return %live_out : memref<64x9xi32>
|
2019-01-08 09:34:26 +08:00
|
|
|
}
|
|
|
|
// Everything above is fused to a single 2-d loop nest, and the 6-d tensor %in
|
|
|
|
// is eliminated if -memref-dataflow-opt is also supplied.
|
|
|
|
//
|
2019-01-31 03:33:32 +08:00
|
|
|
// CHECK-DAG: [[MAP0:#map[0-9]+]] = (d0, d1) -> ((d0 * 9 + d1) floordiv 288)
|
|
|
|
// CHECK-DAG: [[MAP1:#map[0-9]+]] = (d0, d1) -> (((d0 * 9 + d1) mod 288) floordiv 144)
|
|
|
|
// CHECK-DAG: [[MAP2:#map[0-9]+]] = (d0, d1) -> ((((d0 * 9 + d1) mod 288) mod 144) floordiv 48)
|
|
|
|
// CHECK-DAG: [[MAP3:#map[0-9]+]] = (d0, d1) -> (((((d0 * 9 + d1) mod 288) mod 144) mod 48) floordiv 16)
|
|
|
|
// CHECK-DAG: [[MAP4:#map[0-9]+]] = (d0, d1) -> (((((d0 * 9 + d1) mod 288) mod 144) mod 48) mod 16)
|
2019-03-05 06:58:59 +08:00
|
|
|
// CHECK-DAG: [[MAP5:#map[0-9]+]] = (d0, d1, d2, d3, d4, d5, d6, d7) -> (d2 - (d0 * 9 + d1) floordiv 288)
|
2019-01-31 03:33:32 +08:00
|
|
|
// CHECK-DAG: [[MAP6:#map[0-9]+]] = (d0, d1, d2, d3, d4, d5, d6, d7) -> (d3)
|
|
|
|
// CHECK-DAG: [[MAP7:#map[0-9]+]] = (d0, d1, d2, d3, d4, d5, d6, d7) -> (d4)
|
|
|
|
// CHECK-DAG: [[MAP8:#map[0-9]+]] = (d0, d1, d2, d3, d4, d5, d6, d7) -> (d5)
|
|
|
|
// CHECK-DAG: [[MAP9:#map[0-9]+]] = (d0, d1, d2, d3, d4, d5, d6, d7) -> (d6)
|
|
|
|
// CHECK-DAG: [[MAP10:#map[0-9]+]] = (d0, d1, d2, d3, d4, d5, d6, d7) -> (d7)
|
|
|
|
// CHECK-DAG: [[MAP11:#map[0-9]+]] = (d0, d1) -> (d0 * 9 + d1)
|
|
|
|
// CHECK-DAG: [[MAP12:#map[0-9]+]] = (d0) -> (d0 floordiv 288)
|
|
|
|
// CHECK-DAG: [[MAP13:#map[0-9]+]] = (d0) -> ((d0 mod 288) floordiv 144)
|
|
|
|
// CHECK-DAG: [[MAP14:#map[0-9]+]] = (d0) -> (((d0 mod 288) mod 144) floordiv 48)
|
|
|
|
// CHECK-DAG: [[MAP15:#map[0-9]+]] = (d0) -> ((((d0 mod 288) mod 144) mod 48) floordiv 16)
|
|
|
|
// CHECK-DAG: [[MAP16:#map[0-9]+]] = (d0) -> ((((d0 mod 288) mod 144) mod 48) mod 16)
|
|
|
|
// CHECK-DAG: [[MAP17:#map[0-9]+]] = (d0) -> (0)
|
|
|
|
// CHECK-DAG: [[MAP18:#map[0-9]+]] = (d0, d1, d2, d3) -> (-d0 + d2)
|
|
|
|
// CHECK-DAG: [[MAP19:#map[0-9]+]] = (d0, d1, d2, d3) -> (-d1 + d3)
|
|
|
|
//
|
|
|
|
// CHECK-LABEL: func @R6_to_R2_reshape
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK: %0 = alloc() : memref<1x2x3x3x16x1xi32>
|
|
|
|
// CHECK: %1 = alloc() : memref<1x1xi32>
|
|
|
|
// CHECK: %2 = alloc() : memref<64x9xi32>
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i0 = 0 to 64 {
|
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 9 {
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %3 = affine.apply [[MAP0]](%i0, %i1)
|
|
|
|
// CHECK-NEXT: %4 = affine.apply [[MAP1]](%i0, %i1)
|
|
|
|
// CHECK-NEXT: %5 = affine.apply [[MAP2]](%i0, %i1)
|
|
|
|
// CHECK-NEXT: %6 = affine.apply [[MAP3]](%i0, %i1)
|
|
|
|
// CHECK-NEXT: %7 = affine.apply [[MAP4]](%i0, %i1)
|
2019-01-19 04:55:46 +08:00
|
|
|
// CHECK-NEXT: %8 = "foo"(%3, %4, %5, %6, %7, %c0) : (index, index, index, index, index, index) -> i32
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %9 = affine.apply [[MAP5]](%i0, %i1, %3, %4, %5, %6, %7, %c0)
|
|
|
|
// CHECK-NEXT: %10 = affine.apply [[MAP6]](%i0, %i1, %3, %4, %5, %6, %7, %c0)
|
|
|
|
// CHECK-NEXT: %11 = affine.apply [[MAP7]](%i0, %i1, %3, %4, %5, %6, %7, %c0)
|
|
|
|
// CHECK-NEXT: %12 = affine.apply [[MAP8]](%i0, %i1, %3, %4, %5, %6, %7, %c0)
|
|
|
|
// CHECK-NEXT: %13 = affine.apply [[MAP9]](%i0, %i1, %3, %4, %5, %6, %7, %c0)
|
|
|
|
// CHECK-NEXT: %14 = affine.apply [[MAP10]](%i0, %i1, %3, %4, %5, %6, %7, %c0)
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: store %8, %0[%9, %10, %11, %12, %13, %14] : memref<1x2x3x3x16x1xi32>
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %15 = affine.apply [[MAP11]](%i0, %i1)
|
|
|
|
// CHECK-NEXT: %16 = affine.apply [[MAP12]](%15)
|
|
|
|
// CHECK-NEXT: %17 = affine.apply [[MAP13]](%15)
|
|
|
|
// CHECK-NEXT: %18 = affine.apply [[MAP14]](%15)
|
|
|
|
// CHECK-NEXT: %19 = affine.apply [[MAP15]](%15)
|
|
|
|
// CHECK-NEXT: %20 = affine.apply [[MAP16]](%15)
|
|
|
|
// CHECK-NEXT: %21 = affine.apply [[MAP17]](%15)
|
|
|
|
// CHECK-NEXT: %22 = affine.apply [[MAP5]](%i0, %i1, %16, %17, %18, %19, %20, %21)
|
|
|
|
// CHECK-NEXT: %23 = affine.apply [[MAP6]](%i0, %i1, %16, %17, %18, %19, %20, %21)
|
|
|
|
// CHECK-NEXT: %24 = affine.apply [[MAP7]](%i0, %i1, %16, %17, %18, %19, %20, %21)
|
|
|
|
// CHECK-NEXT: %25 = affine.apply [[MAP8]](%i0, %i1, %16, %17, %18, %19, %20, %21)
|
|
|
|
// CHECK-NEXT: %26 = affine.apply [[MAP9]](%i0, %i1, %16, %17, %18, %19, %20, %21)
|
|
|
|
// CHECK-NEXT: %27 = affine.apply [[MAP10]](%i0, %i1, %16, %17, %18, %19, %20, %21)
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: %28 = load %0[%22, %23, %24, %25, %26, %27] : memref<1x2x3x3x16x1xi32>
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %29 = affine.apply [[MAP18]](%i0, %i1, %i0, %i1)
|
|
|
|
// CHECK-NEXT: %30 = affine.apply [[MAP19]](%i0, %i1, %i0, %i1)
|
2019-01-26 08:00:50 +08:00
|
|
|
// CHECK-NEXT: store %28, %1[%29, %30] : memref<1x1xi32>
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %31 = affine.apply [[MAP18]](%i0, %i1, %i0, %i1)
|
|
|
|
// CHECK-NEXT: %32 = affine.apply [[MAP19]](%i0, %i1, %i0, %i1)
|
2019-01-26 08:00:50 +08:00
|
|
|
// CHECK-NEXT: %33 = load %1[%31, %32] : memref<1x1xi32>
|
|
|
|
// CHECK-NEXT: %34 = muli %33, %33 : i32
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: store %34, %2[%i0, %i1] : memref<64x9xi32>
|
2019-01-19 04:55:46 +08:00
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: return %2 : memref<64x9xi32>
|
2019-01-08 09:34:26 +08:00
|
|
|
|
|
|
|
// -----
|
|
|
|
|
|
|
|
// CHECK-LABEL: func @fuse_symbolic_bounds
|
|
|
|
func @fuse_symbolic_bounds(%M : index, %N : index) {
|
2019-02-07 03:08:18 +08:00
|
|
|
%N_plus_5 = affine.apply (d0) -> (d0 + 5)(%N)
|
2019-01-19 04:55:46 +08:00
|
|
|
%m = alloc(%M, %N_plus_5) : memref<? x ? x f32>
|
2019-01-08 09:34:26 +08:00
|
|
|
|
|
|
|
%c0 = constant 0.0 : f32
|
|
|
|
%s = constant 5 : index
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to %M {
|
|
|
|
affine.for %i1 = 0 to (d0) -> (d0 + 5) (%N) {
|
2019-01-19 04:55:46 +08:00
|
|
|
store %c0, %m[%i0, %i1] : memref<? x ? x f32>
|
2019-01-08 09:34:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i2 = 0 to %M {
|
|
|
|
affine.for %i3 = 0 to %N {
|
2019-02-07 03:08:18 +08:00
|
|
|
%idy = affine.apply (d0)[s0] -> (d0 + s0) (%i3)[%s]
|
2019-01-26 08:00:50 +08:00
|
|
|
%v = load %m[%i2, %idy] : memref<? x ? x f32>
|
2019-01-08 09:34:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2019-01-15 03:26:25 +08:00
|
|
|
|
|
|
|
// -----
|
2019-01-31 03:33:32 +08:00
|
|
|
// CHECK-DAG: [[MAP0:#map[0-9]+]] = (d0, d1) -> (-d0 + d1)
|
2019-01-15 03:26:25 +08:00
|
|
|
|
|
|
|
// CHECK-LABEL: func @should_fuse_reduction_at_depth1
|
|
|
|
func @should_fuse_reduction_at_depth1() {
|
|
|
|
%a = alloc() : memref<10x100xf32>
|
|
|
|
%b = alloc() : memref<10xf32>
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 10 {
|
|
|
|
affine.for %i1 = 0 to 100 {
|
2019-01-15 03:26:25 +08:00
|
|
|
%v0 = load %b[%i0] : memref<10xf32>
|
|
|
|
%v1 = load %a[%i0, %i1] : memref<10x100xf32>
|
|
|
|
%v2 = "maxf"(%v0, %v1) : (f32, f32) -> f32
|
|
|
|
store %v2, %b[%i0] : memref<10xf32>
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i2 = 0 to 10 {
|
|
|
|
affine.for %i3 = 0 to 100 {
|
2019-01-15 03:26:25 +08:00
|
|
|
%v3 = load %b[%i2] : memref<10xf32>
|
|
|
|
%v4 = load %a[%i2, %i3] : memref<10x100xf32>
|
|
|
|
%v5 = subf %v4, %v3 : f32
|
|
|
|
store %v5, %b[%i2] : memref<10xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// This test should fuse the src reduction loop at depth 1 in the destination
|
|
|
|
// loop nest, which improves locality and enables subsequence passes to
|
|
|
|
// decrease the reduction memref size and possibly place it in a faster
|
|
|
|
// memory space.
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 10 {
|
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 100 {
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %2 = affine.apply [[MAP0]](%i0, %i0)
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: %3 = load %0[%2] : memref<1xf32>
|
|
|
|
// CHECK-NEXT: %4 = load %1[%i0, %i1] : memref<10x100xf32>
|
2019-01-15 03:26:25 +08:00
|
|
|
// CHECK-NEXT: %5 = "maxf"(%3, %4) : (f32, f32) -> f32
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %6 = affine.apply [[MAP0]](%i0, %i0)
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: store %5, %0[%6] : memref<1xf32>
|
2019-01-15 03:26:25 +08:00
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i2 = 0 to 100 {
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %7 = affine.apply [[MAP0]](%i0, %i0)
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: %8 = load %0[%7] : memref<1xf32>
|
|
|
|
// CHECK-NEXT: %9 = load %1[%i0, %i2] : memref<10x100xf32>
|
2019-01-23 05:58:52 +08:00
|
|
|
// CHECK-NEXT: %10 = subf %9, %8 : f32
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %11 = affine.apply [[MAP0]](%i0, %i0)
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: store %10, %0[%11] : memref<1xf32>
|
2019-01-15 03:26:25 +08:00
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
2019-01-31 03:33:32 +08:00
|
|
|
// CHECK: [[MAP0:#map[0-9]+]] = (d0, d1, d2) -> (-d0 + d1)
|
|
|
|
// CHECK: [[MAP1:#map[0-9]+]] = (d0, d1, d2) -> (d2)
|
2019-01-15 03:26:25 +08:00
|
|
|
|
|
|
|
// CHECK-LABEL: func @should_fuse_at_src_depth1_and_dst_depth1
|
|
|
|
func @should_fuse_at_src_depth1_and_dst_depth1() {
|
|
|
|
%a = alloc() : memref<100x16xf32>
|
|
|
|
%b = alloc() : memref<100x16xf32>
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 100 {
|
|
|
|
affine.for %i1 = 0 to 16 {
|
2019-01-15 03:26:25 +08:00
|
|
|
%v0 = load %a[%i0, %i1] : memref<100x16xf32>
|
|
|
|
"op0"(%v0) : (f32) -> ()
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i2 = 0 to 16 {
|
2019-01-15 03:26:25 +08:00
|
|
|
%v1 = "op1"() : () -> (f32)
|
|
|
|
store %v1, %b[%i0, %i2] : memref<100x16xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i3 = 0 to 100 {
|
|
|
|
affine.for %i4 = 0 to 16 {
|
2019-01-15 03:26:25 +08:00
|
|
|
%v2 = load %b[%i3, %i4] : memref<100x16xf32>
|
|
|
|
"op2"(%v2) : (f32) -> ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// We can slice iterations of the '%i0' and '%i1' loops in the the source
|
|
|
|
// loop nest, but slicing at depth 2 and inserting the slice in the
|
|
|
|
// destination loop nest at depth2 causes extra computation. Instead,
|
|
|
|
// the fusion algorithm should detect that the source loop should be sliced
|
|
|
|
// at depth 1 and the slice should be inserted at depth 1.
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 100 {
|
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 16 {
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: %2 = load %1[%i0, %i1] : memref<100x16xf32>
|
2019-01-23 05:58:52 +08:00
|
|
|
// CHECK-NEXT: "op0"(%2) : (f32) -> ()
|
2019-01-15 03:26:25 +08:00
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i2 = 0 to 16 {
|
2019-01-23 05:58:52 +08:00
|
|
|
// CHECK-NEXT: %3 = "op1"() : () -> f32
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %4 = affine.apply [[MAP0]](%i0, %i0, %i2)
|
|
|
|
// CHECK-NEXT: %5 = affine.apply [[MAP1]](%i0, %i0, %i2)
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: store %3, %0[%4, %5] : memref<1x16xf32>
|
2019-01-15 03:26:25 +08:00
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i3 = 0 to 16 {
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %6 = affine.apply [[MAP0]](%i0, %i0, %i3)
|
|
|
|
// CHECK-NEXT: %7 = affine.apply [[MAP1]](%i0, %i0, %i3)
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: %8 = load %0[%6, %7] : memref<1x16xf32>
|
2019-01-26 08:00:50 +08:00
|
|
|
// CHECK-NEXT: "op2"(%8) : (f32) -> ()
|
2019-01-15 03:26:25 +08:00
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
2019-01-31 03:33:32 +08:00
|
|
|
// CHECK: [[MAP0:#map[0-9]+]] = (d0, d1) -> (d0 * 10 + d1)
|
|
|
|
// CHECK: [[MAP1:#map[0-9]+]] = (d0, d1, d2) -> (d0 * -10 - d1 + d2)
|
2019-01-15 03:26:25 +08:00
|
|
|
|
|
|
|
// CHECK-LABEL: func @should_fuse_src_depth1_at_dst_depth2
|
|
|
|
func @should_fuse_src_depth1_at_dst_depth2() {
|
|
|
|
%a = alloc() : memref<100xf32>
|
|
|
|
%c0 = constant 0.0 : f32
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 100 {
|
2019-01-15 03:26:25 +08:00
|
|
|
store %c0, %a[%i0] : memref<100xf32>
|
|
|
|
}
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i1 = 0 to 10 {
|
|
|
|
affine.for %i2 = 0 to 10 {
|
2019-02-07 03:08:18 +08:00
|
|
|
%a0 = affine.apply (d0, d1) -> (d0 * 10 + d1) (%i1, %i2)
|
2019-01-15 03:26:25 +08:00
|
|
|
%v0 = load %a[%a0] : memref<100xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// The source loop nest slice loop bound is a function of both destination
|
|
|
|
// loop IVs, so we should slice at depth 1 and insert the slice at depth 2.
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 10 {
|
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 10 {
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %1 = affine.apply [[MAP0]](%i0, %i1)
|
|
|
|
// CHECK-NEXT: %2 = affine.apply [[MAP1]](%i0, %i1, %1)
|
2019-01-23 05:58:52 +08:00
|
|
|
// CHECK-NEXT: store %cst, %0[%2] : memref<1xf32>
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %3 = affine.apply [[MAP0]](%i0, %i1)
|
|
|
|
// CHECK-NEXT: %4 = affine.apply [[MAP1]](%i0, %i1, %3)
|
2019-01-23 05:58:52 +08:00
|
|
|
// CHECK-NEXT: %5 = load %0[%4] : memref<1xf32>
|
2019-01-15 03:26:25 +08:00
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
|
|
|
|
|
|
|
// CHECK-LABEL: func @fusion_at_depth0_not_currently_supported
|
|
|
|
func @fusion_at_depth0_not_currently_supported() {
|
|
|
|
%0 = alloc() : memref<10xf32>
|
|
|
|
%c0 = constant 0 : index
|
|
|
|
%cst = constant 0.000000e+00 : f32
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 10 {
|
2019-01-15 03:26:25 +08:00
|
|
|
store %cst, %0[%i0] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i1 = 0 to 10 {
|
2019-01-15 03:26:25 +08:00
|
|
|
%1 = load %0[%c0] : memref<10xf32>
|
|
|
|
}
|
2019-01-19 00:56:27 +08:00
|
|
|
// NOTE: Should shrink memref size to 1 element access by load in dst loop
|
|
|
|
// nest, and make the store in the slice store to the same element.
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-DAG: %0 = alloc() : memref<1xf32>
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 10 {
|
2019-02-15 05:25:46 +08:00
|
|
|
// CHECK-NEXT: store %cst, %0[%c0] : memref<1xf32>
|
|
|
|
// CHECK-NEXT: %1 = load %0[%c0_0] : memref<1xf32>
|
2019-01-15 03:26:25 +08:00
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
2019-01-23 05:58:52 +08:00
|
|
|
|
2019-01-31 03:33:32 +08:00
|
|
|
// CHECK: [[MAP0:#map[0-9]+]] = (d0, d1, d2, d3, d4, d5, d6, d7, d8, d9) -> (-d0 + d4)
|
|
|
|
// CHECK: [[MAP1:#map[0-9]+]] = (d0, d1, d2, d3, d4, d5, d6, d7, d8, d9) -> (-d1 + d5)
|
|
|
|
// CHECK: [[MAP2:#map[0-9]+]] = (d0, d1, d2, d3, d4, d5, d6, d7, d8, d9) -> (-d2 + d6)
|
|
|
|
// CHECK: [[MAP3:#map[0-9]+]] = (d0, d1, d2, d3, d4, d5, d6, d7, d8, d9) -> (-d3 + d7)
|
|
|
|
// CHECK: [[MAP4:#map[0-9]+]] = (d0, d1, d2, d3, d4, d5, d6, d7, d8, d9) -> (d8)
|
|
|
|
// CHECK: [[MAP5:#map[0-9]+]] = (d0, d1, d2, d3, d4, d5, d6, d7, d8, d9) -> (d9)
|
2019-01-15 03:26:25 +08:00
|
|
|
|
|
|
|
// CHECK-LABEL: func @should_fuse_deep_loop_nests
|
|
|
|
func @should_fuse_deep_loop_nests() {
|
|
|
|
%0 = alloc() : memref<2x2x3x3x16x10xf32, 2>
|
|
|
|
%1 = alloc() : memref<2x2x3x3x16x10xf32, 2>
|
|
|
|
%2 = alloc() : memref<3x3x3x3x16x10xf32, 2>
|
|
|
|
%c0 = constant 0 : index
|
|
|
|
%c1 = constant 1 : index
|
|
|
|
%c1_0 = constant 1 : index
|
|
|
|
%cst = constant 0.000000e+00 : f32
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 2 {
|
|
|
|
affine.for %i1 = 0 to 2 {
|
|
|
|
affine.for %i2 = 0 to 3 {
|
|
|
|
affine.for %i3 = 0 to 3 {
|
|
|
|
affine.for %i4 = 0 to 16 {
|
|
|
|
affine.for %i5 = 0 to 10 {
|
2019-01-15 03:26:25 +08:00
|
|
|
%3 = load %0[%i0, %i1, %i2, %i3, %i4, %i5]
|
|
|
|
: memref<2x2x3x3x16x10xf32, 2>
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i6 = 0 to 16 {
|
|
|
|
affine.for %i7 = 0 to 10 {
|
2019-01-15 03:26:25 +08:00
|
|
|
store %cst, %1[%i0, %i1, %i2, %i3, %i6, %i7]
|
|
|
|
: memref<2x2x3x3x16x10xf32, 2>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i8 = 0 to 3 {
|
|
|
|
affine.for %i9 = 0 to 3 {
|
|
|
|
affine.for %i10 = 0 to 2 {
|
|
|
|
affine.for %i11 = 0 to 2 {
|
|
|
|
affine.for %i12 = 0 to 3 {
|
|
|
|
affine.for %i13 = 0 to 3 {
|
|
|
|
affine.for %i14 = 0 to 2 {
|
|
|
|
affine.for %i15 = 0 to 2 {
|
|
|
|
affine.for %i16 = 0 to 16 {
|
|
|
|
affine.for %i17 = 0 to 10 {
|
2019-01-15 03:26:25 +08:00
|
|
|
%5 = load %0[%i14, %i15, %i12, %i13, %i16, %i17]
|
|
|
|
: memref<2x2x3x3x16x10xf32, 2>
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i18 = 0 to 16 {
|
|
|
|
affine.for %i19 = 0 to 10 {
|
2019-01-15 03:26:25 +08:00
|
|
|
%6 = load %1[%i10, %i11, %i8, %i9, %i18, %i19]
|
|
|
|
: memref<2x2x3x3x16x10xf32, 2>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// The first four loops of the source loop nest can be sliced with iteration
|
|
|
|
// bounds which are a function of the first four loops of destination loop nest,
|
|
|
|
// where the destination loops nests have been interchanged.
|
2019-01-26 08:00:50 +08:00
|
|
|
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-DAG: %0 = alloc() : memref<1x1x1x1x16x10xf32, 2>
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 3 {
|
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 3 {
|
|
|
|
// CHECK-NEXT: affine.for %i2 = 0 to 2 {
|
|
|
|
// CHECK-NEXT: affine.for %i3 = 0 to 2 {
|
|
|
|
// CHECK-NEXT: affine.for %i4 = 0 to 3 {
|
|
|
|
// CHECK-NEXT: affine.for %i5 = 0 to 3 {
|
|
|
|
// CHECK-NEXT: affine.for %i6 = 0 to 16 {
|
|
|
|
// CHECK-NEXT: affine.for %i7 = 0 to 10 {
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: %3 = load %1[%i2, %i3, %i0, %i1, %i6, %i7] : memref<2x2x3x3x16x10xf32, 2>
|
2019-01-24 01:16:24 +08:00
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i8 = 0 to 16 {
|
|
|
|
// CHECK-NEXT: affine.for %i9 = 0 to 10 {
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %4 = affine.apply [[MAP0]](%i2, %i3, %i0, %i1, %i2, %i3, %i0, %i1, %i8, %i9)
|
|
|
|
// CHECK-NEXT: %5 = affine.apply [[MAP1]](%i2, %i3, %i0, %i1, %i2, %i3, %i0, %i1, %i8, %i9)
|
|
|
|
// CHECK-NEXT: %6 = affine.apply [[MAP2]](%i2, %i3, %i0, %i1, %i2, %i3, %i0, %i1, %i8, %i9)
|
|
|
|
// CHECK-NEXT: %7 = affine.apply [[MAP3]](%i2, %i3, %i0, %i1, %i2, %i3, %i0, %i1, %i8, %i9)
|
|
|
|
// CHECK-NEXT: %8 = affine.apply [[MAP4]](%i2, %i3, %i0, %i1, %i2, %i3, %i0, %i1, %i8, %i9)
|
|
|
|
// CHECK-NEXT: %9 = affine.apply [[MAP5]](%i2, %i3, %i0, %i1, %i2, %i3, %i0, %i1, %i8, %i9)
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: store %cst, %0[%4, %5, %6, %7, %8, %9] : memref<1x1x1x1x16x10xf32, 2>
|
2019-01-24 01:16:24 +08:00
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i10 = 0 to 2 {
|
|
|
|
// CHECK-NEXT: affine.for %i11 = 0 to 2 {
|
|
|
|
// CHECK-NEXT: affine.for %i12 = 0 to 16 {
|
|
|
|
// CHECK-NEXT: affine.for %i13 = 0 to 10 {
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: %10 = load %1[%i10, %i11, %i4, %i5, %i12, %i13] : memref<2x2x3x3x16x10xf32, 2>
|
2019-01-15 03:26:25 +08:00
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i14 = 0 to 16 {
|
|
|
|
// CHECK-NEXT: affine.for %i15 = 0 to 10 {
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %11 = affine.apply [[MAP0]](%i2, %i3, %i0, %i1, %i2, %i3, %i0, %i1, %i14, %i15)
|
|
|
|
// CHECK-NEXT: %12 = affine.apply [[MAP1]](%i2, %i3, %i0, %i1, %i2, %i3, %i0, %i1, %i14, %i15)
|
|
|
|
// CHECK-NEXT: %13 = affine.apply [[MAP2]](%i2, %i3, %i0, %i1, %i2, %i3, %i0, %i1, %i14, %i15)
|
|
|
|
// CHECK-NEXT: %14 = affine.apply [[MAP3]](%i2, %i3, %i0, %i1, %i2, %i3, %i0, %i1, %i14, %i15)
|
|
|
|
// CHECK-NEXT: %15 = affine.apply [[MAP4]](%i2, %i3, %i0, %i1, %i2, %i3, %i0, %i1, %i14, %i15)
|
|
|
|
// CHECK-NEXT: %16 = affine.apply [[MAP5]](%i2, %i3, %i0, %i1, %i2, %i3, %i0, %i1, %i14, %i15)
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: %17 = load %0[%11, %12, %13, %14, %15, %16] : memref<1x1x1x1x16x10xf32, 2>
|
2019-01-15 03:26:25 +08:00
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
return
|
|
|
|
}
|
2019-01-17 01:55:02 +08:00
|
|
|
|
|
|
|
// -----
|
2019-01-31 03:33:32 +08:00
|
|
|
// CHECK: [[MAP0:#map[0-9]+]] = (d0, d1, d2) -> (-d0 + d1)
|
|
|
|
// CHECK: [[MAP1:#map[0-9]+]] = (d0, d1, d2) -> (d2)
|
2019-01-17 01:55:02 +08:00
|
|
|
|
|
|
|
// CHECK-LABEL: func @should_fuse_at_depth1_and_reduce_slice_trip_count
|
|
|
|
func @should_fuse_at_depth1_and_reduce_slice_trip_count() {
|
|
|
|
%a = alloc() : memref<4x256xf32>
|
|
|
|
%b = alloc() : memref<4x256xf32>
|
|
|
|
|
|
|
|
%c0 = constant 0 : index
|
|
|
|
%cf0 = constant 0.0 : f32
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 4 {
|
|
|
|
affine.for %i1 = 0 to 256 {
|
2019-01-17 01:55:02 +08:00
|
|
|
%v0 = load %b[%i0, %i1] : memref<4x256xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i2 = 0 to 256 {
|
2019-01-17 01:55:02 +08:00
|
|
|
store %cf0, %a[%i0, %i2] : memref<4x256xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %d0 = 0 to 4 {
|
|
|
|
affine.for %d1 = 0 to 16 {
|
2019-01-17 01:55:02 +08:00
|
|
|
%v1 = load %a[%d0, %d1] : memref<4x256xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// The cost of fusing at depth 2 is greater than the cost of fusing at depth 1
|
|
|
|
// for two reasons:
|
|
|
|
// 1) Inserting the unsliceable src loop %i1 to a higher depth removes
|
|
|
|
// redundant computation and reduces costs.
|
|
|
|
// 2) Inserting the sliceable src loop %i2 at depth 1, we can still reduce
|
|
|
|
// its trip count to 16 (from 256) reducing costs.
|
2019-01-19 00:56:27 +08:00
|
|
|
// NOTE: the size of the private memref created for the fused loop nest
|
|
|
|
// is reduced from the original shape from 4x256 to 4x16 because of the
|
|
|
|
// data accessed by the load.
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-DAG: %0 = alloc() : memref<1x16xf32>
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 4 {
|
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 256 {
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: %2 = load %1[%i0, %i1] : memref<4x256xf32>
|
2019-01-17 01:55:02 +08:00
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i2 = 0 to 16 {
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %3 = affine.apply [[MAP0]](%i0, %i0, %i2)
|
|
|
|
// CHECK-NEXT: %4 = affine.apply [[MAP1]](%i0, %i0, %i2)
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: store %cst, %0[%3, %4] : memref<1x16xf32>
|
2019-01-17 01:55:02 +08:00
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i3 = 0 to 16 {
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %5 = affine.apply [[MAP0]](%i0, %i0, %i3)
|
|
|
|
// CHECK-NEXT: %6 = affine.apply [[MAP1]](%i0, %i0, %i3)
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: %7 = load %0[%5, %6] : memref<1x16xf32>
|
2019-01-17 01:55:02 +08:00
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
|
|
|
|
|
|
|
// CHECK-LABEL: func @should_fuse_at_depth1_with_trip_count_20
|
|
|
|
func @should_fuse_at_depth1_with_trip_count_20() {
|
|
|
|
%a = alloc() : memref<100xf32>
|
|
|
|
%c0 = constant 0 : index
|
|
|
|
%cf0 = constant 0.0 : f32
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 100 {
|
2019-01-17 01:55:02 +08:00
|
|
|
store %cf0, %a[%i0]: memref<100xf32>
|
|
|
|
}
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i1 = 0 to 5 {
|
|
|
|
affine.for %i2 = 0 to 10 {
|
2019-01-17 01:55:02 +08:00
|
|
|
%v0 = load %a[%i2]: memref<100xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i3 = 0 to 10 {
|
|
|
|
affine.for %i4 = 0 to 20 {
|
2019-01-17 01:55:02 +08:00
|
|
|
%v1 = load %a[%i4]: memref<100xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-19 00:56:27 +08:00
|
|
|
// NOTE: The size of the private memref created for fusion is shrunk to 20xf32
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-DAG: %0 = alloc() : memref<20xf32>
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 5 {
|
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 20 {
|
2019-01-19 00:56:27 +08:00
|
|
|
// CHECK-NEXT: store %cst, %0[%i1] : memref<20xf32>
|
2019-01-17 01:55:02 +08:00
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i2 = 0 to 10 {
|
2019-01-19 00:56:27 +08:00
|
|
|
// CHECK-NEXT: %1 = load %0[%i2] : memref<20xf32>
|
2019-01-17 01:55:02 +08:00
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i3 = 0 to 10 {
|
|
|
|
// CHECK-NEXT: affine.for %i4 = 0 to 20 {
|
2019-01-19 00:56:27 +08:00
|
|
|
// CHECK-NEXT: %2 = load %0[%i4] : memref<20xf32>
|
2019-01-17 01:55:02 +08:00
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
|
|
|
|
|
|
|
// CHECK-LABEL: func @should_fuse_at_depth1_with_trip_count_19
|
|
|
|
func @should_fuse_at_depth1_with_trip_count_19() {
|
|
|
|
%a = alloc() : memref<100xf32>
|
|
|
|
%c0 = constant 0 : index
|
|
|
|
%cf0 = constant 0.0 : f32
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 100 {
|
2019-01-17 01:55:02 +08:00
|
|
|
store %cf0, %a[%i0]: memref<100xf32>
|
|
|
|
}
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i1 = 0 to 5 {
|
|
|
|
affine.for %i2 = 0 to 19 {
|
2019-01-17 01:55:02 +08:00
|
|
|
%v0 = load %a[%i2]: memref<100xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i3 = 0 to 10 {
|
|
|
|
affine.for %i4 = 0 to 10 {
|
2019-01-17 01:55:02 +08:00
|
|
|
%v1 = load %a[%i4]: memref<100xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-19 00:56:27 +08:00
|
|
|
// NOTE: The size of the private memref created for fusion is shrunk to 19xf32
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-DAG: %0 = alloc() : memref<19xf32>
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 5 {
|
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 19 {
|
2019-01-19 00:56:27 +08:00
|
|
|
// CHECK-NEXT: store %cst, %0[%i1] : memref<19xf32>
|
2019-01-17 01:55:02 +08:00
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i2 = 0 to 19 {
|
2019-01-19 00:56:27 +08:00
|
|
|
// CHECK-NEXT: %1 = load %0[%i2] : memref<19xf32>
|
2019-01-17 01:55:02 +08:00
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i3 = 0 to 10 {
|
|
|
|
// CHECK-NEXT: affine.for %i4 = 0 to 10 {
|
2019-01-19 00:56:27 +08:00
|
|
|
// CHECK-NEXT: %2 = load %0[%i4] : memref<19xf32>
|
2019-01-17 01:55:02 +08:00
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
return
|
|
|
|
}
|
2019-01-19 00:56:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
// -----
|
2019-01-23 05:58:52 +08:00
|
|
|
// CHECK: [[MAP0:#map[0-9]+]] = (d0, d1) -> (-d0 + d1)
|
2019-01-19 00:56:27 +08:00
|
|
|
|
|
|
|
// CHECK-LABEL: func @should_fuse_with_private_memrefs_with_diff_shapes() {
|
|
|
|
func @should_fuse_with_private_memrefs_with_diff_shapes() {
|
|
|
|
%m = alloc() : memref<100xf32>
|
|
|
|
%cf7 = constant 7.0 : f32
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 100 {
|
2019-01-19 00:56:27 +08:00
|
|
|
store %cf7, %m[%i0] : memref<100xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i1 = 0 to 17 {
|
2019-01-19 00:56:27 +08:00
|
|
|
%v0 = load %m[%i1] : memref<100xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i2 = 0 to 82 {
|
2019-01-19 00:56:27 +08:00
|
|
|
%v1 = load %m[%i2] : memref<100xf32>
|
|
|
|
}
|
|
|
|
// Should create two new private memrefs customized to the shapes accessed
|
|
|
|
// by loops %i1 and %i2.
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-DAG: %0 = alloc() : memref<1xf32>
|
|
|
|
// CHECK-DAG: %1 = alloc() : memref<1xf32>
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 17 {
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %2 = affine.apply [[MAP0]](%i0, %i0)
|
2019-02-05 22:57:08 +08:00
|
|
|
// CHECK-NEXT: store %cst, %1[%2] : memref<1xf32>
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %3 = affine.apply [[MAP0]](%i0, %i0)
|
2019-02-05 22:57:08 +08:00
|
|
|
// CHECK-NEXT: %4 = load %1[%3] : memref<1xf32>
|
2019-01-19 00:56:27 +08:00
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 82 {
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %5 = affine.apply [[MAP0]](%i1, %i1)
|
2019-02-05 22:57:08 +08:00
|
|
|
// CHECK-NEXT: store %cst, %0[%5] : memref<1xf32>
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %6 = affine.apply [[MAP0]](%i1, %i1)
|
2019-02-05 22:57:08 +08:00
|
|
|
// CHECK-NEXT: %7 = load %0[%6] : memref<1xf32>
|
2019-01-19 00:56:27 +08:00
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
return
|
2019-01-19 04:55:46 +08:00
|
|
|
}
|
2019-01-23 05:23:37 +08:00
|
|
|
|
|
|
|
// -----
|
|
|
|
|
2019-01-31 07:53:41 +08:00
|
|
|
// CHECK-LABEL: func @should_not_fuse_live_out_arg(%arg0: memref<10xf32>) {
|
|
|
|
func @should_not_fuse_live_out_arg(%arg0: memref<10xf32>) {
|
2019-01-23 05:23:37 +08:00
|
|
|
%cf7 = constant 7.0 : f32
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 10 {
|
2019-01-23 05:23:37 +08:00
|
|
|
store %cf7, %arg0[%i0] : memref<10xf32>
|
|
|
|
}
|
2019-04-02 21:37:40 +08:00
|
|
|
affine.for %i1 = 0 to 9 {
|
2019-01-23 05:23:37 +08:00
|
|
|
%v0 = load %arg0[%i1] : memref<10xf32>
|
|
|
|
}
|
|
|
|
// This tests that the loop nest '%i0' should not be removed after fusion
|
2019-04-02 21:37:40 +08:00
|
|
|
// because it writes to memref argument '%arg0', and its read region
|
|
|
|
// does not cover its write region (so fusion would shrink the write region
|
|
|
|
// in the fused loop nest, so complete live out data region would not
|
|
|
|
// be written).
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 10 {
|
2019-01-23 05:23:37 +08:00
|
|
|
// CHECK-NEXT: store %cst, %arg0[%i0] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: }
|
2019-04-02 21:37:40 +08:00
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 9 {
|
2019-01-31 07:53:41 +08:00
|
|
|
// CHECK-NEXT: %0 = load %arg0[%i1] : memref<10xf32>
|
2019-01-23 05:23:37 +08:00
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
|
|
|
|
2019-04-02 21:37:40 +08:00
|
|
|
// CHECK-LABEL: func @should_fuse_live_out_arg(%arg0: memref<10xf32>) {
|
|
|
|
func @should_fuse_live_out_arg(%arg0: memref<10xf32>) {
|
|
|
|
%cf7 = constant 7.0 : f32
|
|
|
|
|
|
|
|
affine.for %i0 = 0 to 10 {
|
|
|
|
store %cf7, %arg0[%i0] : memref<10xf32>
|
|
|
|
}
|
|
|
|
affine.for %i1 = 0 to 10 {
|
|
|
|
%v0 = load %arg0[%i1] : memref<10xf32>
|
|
|
|
}
|
|
|
|
// The read/write regions for memref '%arg0' are the same for both
|
|
|
|
// loops, so they should fuse.
|
|
|
|
|
|
|
|
// CHECK: affine.for %i0 = 0 to 10 {
|
|
|
|
// CHECK-NEXT: store %cst, %arg0[%i0] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: %0 = load %arg0[%i0] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
|
|
|
|
2019-01-31 07:53:41 +08:00
|
|
|
// CHECK-LABEL: func @should_not_fuse_escaping_memref() -> memref<10xf32>
|
|
|
|
func @should_not_fuse_escaping_memref() -> memref<10xf32> {
|
2019-01-23 05:23:37 +08:00
|
|
|
%cf7 = constant 7.0 : f32
|
|
|
|
%m = alloc() : memref<10xf32>
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 10 {
|
2019-01-23 05:23:37 +08:00
|
|
|
store %cf7, %m[%i0] : memref<10xf32>
|
|
|
|
}
|
2019-04-02 21:37:40 +08:00
|
|
|
affine.for %i1 = 0 to 9 {
|
2019-01-23 05:23:37 +08:00
|
|
|
%v0 = load %m[%i1] : memref<10xf32>
|
|
|
|
}
|
|
|
|
// This tests that the loop nest '%i0' should not be removed after fusion
|
|
|
|
// because it writes to memref '%m' which is returned by the function.
|
2019-01-31 07:53:41 +08:00
|
|
|
// CHECK-DAG: %0 = alloc() : memref<10xf32>
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 10 {
|
2019-01-31 07:53:41 +08:00
|
|
|
// CHECK-NEXT: store %cst, %0[%i0] : memref<10xf32>
|
2019-01-23 05:23:37 +08:00
|
|
|
// CHECK-NEXT: }
|
2019-04-02 21:37:40 +08:00
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 9 {
|
2019-01-31 07:53:41 +08:00
|
|
|
// CHECK-NEXT: %1 = load %0[%i1] : memref<10xf32>
|
2019-01-23 05:23:37 +08:00
|
|
|
// CHECK-NEXT: }
|
2019-01-31 07:53:41 +08:00
|
|
|
// CHECK-NEXT: return %0 : memref<10xf32>
|
2019-01-23 05:23:37 +08:00
|
|
|
return %m : memref<10xf32>
|
|
|
|
}
|
2019-01-23 05:58:52 +08:00
|
|
|
|
|
|
|
// -----
|
|
|
|
|
|
|
|
// This should fuse with the %in becoming a 1x1x1.
|
|
|
|
func @R3_to_R2_reshape() {
|
|
|
|
%in = alloc() : memref<2x3x16xi32>
|
|
|
|
|
|
|
|
%c0 = constant 0 : index
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 2 {
|
|
|
|
affine.for %i1 = 0 to 3 {
|
|
|
|
affine.for %i2 = 0 to 16 {
|
2019-01-23 05:58:52 +08:00
|
|
|
%val = "foo"(%i0, %i1, %i2) : (index, index, index) -> i32
|
|
|
|
store %val, %in[%i0, %i1, %i2] : memref<2x3x16xi32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %ii = 0 to 32 {
|
|
|
|
affine.for %jj = 0 to 3 {
|
2019-02-07 03:08:18 +08:00
|
|
|
%a0 = affine.apply (d0, d1) -> (d0 * 3 + d1) (%ii, %jj)
|
|
|
|
%idx = affine.apply (d0) -> (d0 floordiv (3 * 16)) (%a0)
|
2019-01-26 08:00:50 +08:00
|
|
|
%v = load %in[%idx, %jj, %c0]
|
2019-01-23 05:58:52 +08:00
|
|
|
: memref<2x3x16xi32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2019-01-31 03:33:32 +08:00
|
|
|
// CHECK: [[MAP0:#map[0-9]+]] = (d0, d1) -> ((d0 * 3 + d1) floordiv 48)
|
2019-03-05 06:58:59 +08:00
|
|
|
// CHECK-NEXT: [[MAP2:#map[0-9]+]] = (d0, d1, d2, d3, d4) -> (d2 - (d0 * 3 + d1) floordiv 48)
|
2019-01-31 03:33:32 +08:00
|
|
|
// CHECK-NEXT: [[MAP3:#map[0-9]+]] = (d0, d1, d2, d3, d4) -> (-d1 + d3)
|
|
|
|
// CHECK-NEXT: [[MAP4:#map[0-9]+]] = (d0, d1, d2, d3, d4) -> (d4)
|
|
|
|
// CHECK-NEXT: [[MAP5:#map[0-9]+]] = (d0, d1) -> (d0 * 3 + d1)
|
|
|
|
// CHECK-NEXT: [[MAP6:#map[0-9]+]] = (d0) -> (d0 floordiv 48)
|
2019-01-26 08:00:50 +08:00
|
|
|
|
2019-01-23 05:58:52 +08:00
|
|
|
// CHECK-LABEL: func @R3_to_R2_reshape()
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-DAG: %0 = alloc() : memref<1x1x1xi32>
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 32 {
|
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 3 {
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %1 = affine.apply [[MAP0]](%i0, %i1)
|
2019-02-15 05:25:46 +08:00
|
|
|
// CHECK-NEXT: %2 = "foo"(%1, %i1, %c0) : (index, index, index) -> i32
|
|
|
|
// CHECK-NEXT: %3 = affine.apply [[MAP2]](%i0, %i1, %1, %i1, %c0)
|
|
|
|
// CHECK-NEXT: %4 = affine.apply [[MAP3]](%i0, %i1, %1, %i1, %c0)
|
|
|
|
// CHECK-NEXT: %5 = affine.apply [[MAP4]](%i0, %i1, %1, %i1, %c0)
|
|
|
|
// CHECK-NEXT: store %2, %0[%3, %4, %5] : memref<1x1x1xi32>
|
|
|
|
// CHECK-NEXT: %6 = affine.apply [[MAP5]](%i0, %i1)
|
|
|
|
// CHECK-NEXT: %7 = affine.apply [[MAP6]](%6)
|
|
|
|
// CHECK-NEXT: %8 = affine.apply [[MAP2]](%i0, %i1, %7, %i1, %c0_0)
|
|
|
|
// CHECK-NEXT: %9 = affine.apply [[MAP3]](%i0, %i1, %7, %i1, %c0_0)
|
|
|
|
// CHECK-NEXT: %10 = affine.apply [[MAP4]](%i0, %i1, %7, %i1, %c0_0)
|
|
|
|
// CHECK-NEXT: %11 = load %0[%8, %9, %10] : memref<1x1x1xi32>
|
2019-01-26 08:00:50 +08:00
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
2019-01-24 03:11:43 +08:00
|
|
|
|
|
|
|
// -----
|
|
|
|
|
|
|
|
// CHECK-LABEL: func @should_not_fuse_multi_output_producer() {
|
|
|
|
func @should_not_fuse_multi_output_producer() {
|
|
|
|
%a = alloc() : memref<10xf32>
|
|
|
|
%b = alloc() : memref<10xf32>
|
|
|
|
|
|
|
|
%cf7 = constant 7.0 : f32
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 10 {
|
2019-01-24 03:11:43 +08:00
|
|
|
store %cf7, %a[%i0] : memref<10xf32>
|
|
|
|
store %cf7, %b[%i0] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i1 = 0 to 10 {
|
2019-01-24 03:11:43 +08:00
|
|
|
%v0 = load %a[%i1] : memref<10xf32>
|
|
|
|
}
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 10 {
|
2019-01-24 03:11:43 +08:00
|
|
|
// CHECK-NEXT: store %cst, %0[%i0] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: store %cst, %1[%i0] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 10 {
|
2019-01-24 03:11:43 +08:00
|
|
|
// CHECK-NEXT: %2 = load %0[%i1] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
return
|
|
|
|
}
|
2019-01-30 01:36:41 +08:00
|
|
|
|
|
|
|
// -----
|
|
|
|
|
|
|
|
// CHECK-LABEL: func @fusion_preventing_deps_on_middle_loop() {
|
|
|
|
func @fusion_preventing_deps_on_middle_loop() {
|
|
|
|
%a = alloc() : memref<10xf32>
|
|
|
|
%b = alloc() : memref<10xf32>
|
|
|
|
%c = alloc() : memref<10xf32>
|
|
|
|
|
|
|
|
%cf7 = constant 7.0 : f32
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 10 {
|
2019-01-30 01:36:41 +08:00
|
|
|
%v0 = load %a[%i0] : memref<10xf32>
|
|
|
|
store %v0, %b[%i0] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i1 = 0 to 10 {
|
2019-01-30 01:36:41 +08:00
|
|
|
store %cf7, %a[%i1] : memref<10xf32>
|
|
|
|
%v1 = load %c[%i1] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i2 = 0 to 10 {
|
2019-01-30 01:36:41 +08:00
|
|
|
%v2 = load %b[%i2] : memref<10xf32>
|
|
|
|
store %v2, %c[%i2] : memref<10xf32>
|
|
|
|
}
|
|
|
|
// Loops '%i0' and '%i2' cannot fuse along producer/consumer edge on memref
|
|
|
|
// '%b', because of the WAR dep from '%i0' to '%i1' on memref '%a' and
|
|
|
|
// because of the WAR dep from '%i1' to '%i2' on memref '%c'.
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 10 {
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: %3 = load %0[%i0] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: store %3, %1[%i0] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 10 {
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: store %cst, %0[%i1] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: %4 = load %2[%i1] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i2 = 0 to 10 {
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: %5 = load %1[%i2] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: store %5, %2[%i2] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
|
|
|
|
2019-01-31 03:33:32 +08:00
|
|
|
// CHECK: [[MAP0:#map[0-9]+]] = (d0, d1) -> (-d0 + d1)
|
|
|
|
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-LABEL: func @should_fuse_and_move_to_preserve_war_dep() {
|
|
|
|
func @should_fuse_and_move_to_preserve_war_dep() {
|
|
|
|
%a = alloc() : memref<10xf32>
|
|
|
|
%b = alloc() : memref<10xf32>
|
|
|
|
%c = alloc() : memref<10xf32>
|
|
|
|
|
|
|
|
%cf7 = constant 7.0 : f32
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 10 {
|
2019-01-30 01:36:41 +08:00
|
|
|
%v0 = load %b[%i0] : memref<10xf32>
|
|
|
|
store %v0, %a[%i0] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i1 = 0 to 3 {
|
2019-01-30 01:36:41 +08:00
|
|
|
%v2 = load %c[%i1] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i2 = 0 to 5 {
|
2019-01-30 01:36:41 +08:00
|
|
|
store %cf7, %b[%i2] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i3 = 0 to 10 {
|
2019-01-30 01:36:41 +08:00
|
|
|
%v1 = load %a[%i3] : memref<10xf32>
|
|
|
|
store %cf7, %c[%i3] : memref<10xf32>
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dependence graph:
|
|
|
|
//
|
|
|
|
// %i0 ---------
|
|
|
|
// | |
|
|
|
|
// --- %i1 | %b | %a
|
|
|
|
// | | |
|
|
|
|
// %c | %i2 <-- |
|
|
|
|
// | |
|
|
|
|
// --> %i3 <--------
|
|
|
|
//
|
|
|
|
// It is possible to fuse loop '%i0' into '%i3' and preserve dependences
|
|
|
|
// if the fused loop nest is inserted between loops '%i1' and '%i2'.
|
|
|
|
|
|
|
|
// CHECK-DAG: %0 = alloc() : memref<1xf32>
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 3 {
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: %3 = load %2[%i0] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 10 {
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: %4 = load %1[%i1] : memref<10xf32>
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %5 = affine.apply [[MAP0]](%i1, %i1)
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: store %4, %0[%5] : memref<1xf32>
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %6 = affine.apply [[MAP0]](%i1, %i1)
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: %7 = load %0[%6] : memref<1xf32>
|
|
|
|
// CHECK-NEXT: store %cst, %2[%i1] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i2 = 0 to 5 {
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: store %cst, %1[%i2] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
|
|
|
|
|
|
|
// CHECK-LABEL: func @fusion_preventing_dep_on_constant() {
|
|
|
|
func @fusion_preventing_dep_on_constant() {
|
|
|
|
%a = alloc() : memref<10xf32>
|
|
|
|
%b = alloc() : memref<10xf32>
|
|
|
|
%c = alloc() : memref<10xf32>
|
|
|
|
|
|
|
|
%cf7 = constant 7.0 : f32
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 10 {
|
2019-01-30 01:36:41 +08:00
|
|
|
%v0 = load %b[%i0] : memref<10xf32>
|
|
|
|
store %cf7, %a[%i0] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i1 = 0 to 10 {
|
2019-01-30 01:36:41 +08:00
|
|
|
store %cf7, %b[%i1] : memref<10xf32>
|
|
|
|
}
|
|
|
|
%cf11 = constant 11.0 : f32
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i2 = 0 to 10 {
|
2019-01-30 01:36:41 +08:00
|
|
|
%v2 = load %a[%i2] : memref<10xf32>
|
|
|
|
store %cf11, %c[%i2] : memref<10xf32>
|
|
|
|
}
|
|
|
|
// Loops '%i0' and '%i2' cannot fuse along producer/consumer edge on memref
|
|
|
|
// '%a', because of the WAR dep from '%i0' to '%i1' on memref '%b' and
|
|
|
|
// because of the SSA value dep from '%cf11' def to use in '%i2'.
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 10 {
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: %3 = load %1[%i0] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: store %cst, %0[%i0] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 10 {
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: store %cst, %1[%i1] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: %cst_0 = constant 1.100000e+01 : f32
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i2 = 0 to 10 {
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: %4 = load %0[%i2] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: store %cst_0, %2[%i2] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
|
|
|
|
2019-01-31 03:33:32 +08:00
|
|
|
// CHECK: [[MAP0:#map[0-9]+]] = (d0, d1) -> (-d0 + d1)
|
|
|
|
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-LABEL: func @should_fuse_and_preserve_dep_on_constant() {
|
|
|
|
func @should_fuse_and_preserve_dep_on_constant() {
|
|
|
|
%a = alloc() : memref<10xf32>
|
|
|
|
%b = alloc() : memref<10xf32>
|
|
|
|
%c = alloc() : memref<10xf32>
|
|
|
|
|
|
|
|
%cf7 = constant 7.0 : f32
|
|
|
|
%cf11 = constant 11.0 : f32
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 10 {
|
2019-01-30 01:36:41 +08:00
|
|
|
%v0 = load %b[%i0] : memref<10xf32>
|
|
|
|
store %cf7, %a[%i0] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i1 = 0 to 10 {
|
2019-01-30 01:36:41 +08:00
|
|
|
store %cf7, %b[%i1] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i2 = 0 to 10 {
|
2019-01-30 01:36:41 +08:00
|
|
|
%v2 = load %a[%i2] : memref<10xf32>
|
|
|
|
store %cf11, %c[%i2] : memref<10xf32>
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loops '%i0' and '%i2' can fuse along producer/consumer edge on memref
|
|
|
|
// '%a', and preserve the WAR dep from '%i0' to '%i1' on memref '%b', and
|
|
|
|
// the SSA value dep from '%cf11' def to use in '%i2'.
|
|
|
|
|
|
|
|
// CHECK: %cst_0 = constant 1.100000e+01 : f32
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i0 = 0 to 10 {
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: %3 = load %1[%i0] : memref<10xf32>
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %4 = affine.apply [[MAP0]](%i0, %i0)
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: store %cst, %0[%4] : memref<1xf32>
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %5 = affine.apply [[MAP0]](%i0, %i0)
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: %6 = load %0[%5] : memref<1xf32>
|
|
|
|
// CHECK-NEXT: store %cst_0, %2[%i0] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 10 {
|
2019-01-30 01:36:41 +08:00
|
|
|
// CHECK-NEXT: store %cst, %1[%i1] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
return
|
|
|
|
}
|
2019-01-31 07:53:41 +08:00
|
|
|
|
|
|
|
// -----
|
|
|
|
|
|
|
|
// CHECK: [[MAP0:#map[0-9]+]] = (d0, d1, d2) -> (d1)
|
|
|
|
// CHECK: [[MAP1:#map[0-9]+]] = (d0, d1, d2) -> (-d0 + d2)
|
|
|
|
// CHECK: [[MAP2:#map[0-9]+]] = (d0, d1) -> (d0 * 16 - d1 + 15)
|
|
|
|
// CHECK: [[MAP3:#map[0-9]+]] = (d0, d1) -> (d0 * 16 + d1)
|
|
|
|
|
|
|
|
// CHECK-LABEL: func @should_fuse_at_depth_above_loop_carried_dependence(%arg0: memref<64x4xf32>, %arg1: memref<64x4xf32>) {
|
|
|
|
func @should_fuse_at_depth_above_loop_carried_dependence(%arg0: memref<64x4xf32>, %arg1: memref<64x4xf32>) {
|
|
|
|
%out = alloc() : memref<64x4xf32>
|
|
|
|
%0 = constant 0.0 : f32
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 64 {
|
|
|
|
affine.for %i1 = 0 to 4 {
|
2019-01-31 07:53:41 +08:00
|
|
|
store %0, %out[%i0, %i1] : memref<64x4xf32>
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i2 = 0 to 4 {
|
|
|
|
affine.for %i3 = 0 to 4 {
|
|
|
|
affine.for %i4 = 0 to 16 {
|
2019-02-07 03:08:18 +08:00
|
|
|
%1 = affine.apply (d0, d1) -> (d0 * 16 - d1 + 15)(%i3, %i4)
|
2019-01-31 07:53:41 +08:00
|
|
|
%2 = load %arg1[%1, %i2] : memref<64x4xf32>
|
|
|
|
"op0"(%2) : (f32) -> ()
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i5 = 0 to 4 {
|
|
|
|
affine.for %i6 = 0 to 16 {
|
2019-02-07 03:08:18 +08:00
|
|
|
%3 = affine.apply (d0, d1) -> (d0 * 16 - d1 + 15)(%i5, %i6)
|
2019-01-31 07:53:41 +08:00
|
|
|
%4 = load %arg0[%3, %i3] : memref<64x4xf32>
|
|
|
|
"op1"(%4) : (f32) -> ()
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i7 = 0 to 16 {
|
2019-01-31 07:53:41 +08:00
|
|
|
%5 = "op2"() : () -> (f32)
|
2019-02-07 03:08:18 +08:00
|
|
|
%6 = affine.apply (d0, d1) -> (d0 * 16 + d1)(%i5, %i7)
|
2019-01-31 07:53:41 +08:00
|
|
|
%7 = load %out[%6, %i2] : memref<64x4xf32>
|
|
|
|
%8 = addf %7, %5 : f32
|
|
|
|
store %8, %out[%6, %i2] : memref<64x4xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can fuse source loop nest '%i0' into dst loop nest '%i2', but the
|
|
|
|
// depth at which we can insert the src loop nest slice into the dst loop
|
|
|
|
// lest must be decreased because of a loop carried dependence on loop '%i3'.
|
|
|
|
// As a result, the source loop nest is inserted at dst loop nest depth 1,
|
|
|
|
// just above the loop with the carried depenence. In addition, the source
|
|
|
|
// loop nest iteration bounds on its loop '%i1' are reduced to 1, so the
|
|
|
|
// memref size can be reduced to 128x1xf32.
|
|
|
|
|
|
|
|
// CHECK: %0 = alloc() : memref<64x1xf32>
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 4 {
|
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 64 {
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %1 = affine.apply [[MAP0]](%i0, %i1, %i0)
|
|
|
|
// CHECK-NEXT: %2 = affine.apply [[MAP1]](%i0, %i1, %i0)
|
2019-01-31 07:53:41 +08:00
|
|
|
// CHECK-NEXT: store %cst, %0[%1, %2] : memref<64x1xf32>
|
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i2 = 0 to 4 {
|
|
|
|
// CHECK-NEXT: affine.for %i3 = 0 to 16 {
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %3 = affine.apply [[MAP2]](%i2, %i3)
|
2019-01-31 07:53:41 +08:00
|
|
|
// CHECK-NEXT: %4 = load %arg1[%3, %i0] : memref<64x4xf32>
|
|
|
|
// CHECK-NEXT: "op0"(%4) : (f32) -> ()
|
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i4 = 0 to 4 {
|
|
|
|
// CHECK-NEXT: affine.for %i5 = 0 to 16 {
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %5 = affine.apply [[MAP2]](%i4, %i5)
|
2019-01-31 07:53:41 +08:00
|
|
|
// CHECK-NEXT: %6 = load %arg0[%5, %i2] : memref<64x4xf32>
|
|
|
|
// CHECK-NEXT: "op1"(%6) : (f32) -> ()
|
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i6 = 0 to 16 {
|
2019-01-31 07:53:41 +08:00
|
|
|
// CHECK-NEXT: %7 = "op2"() : () -> f32
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %8 = affine.apply [[MAP3]](%i4, %i6)
|
|
|
|
// CHECK-NEXT: %9 = affine.apply [[MAP0]](%i0, %8, %i0)
|
|
|
|
// CHECK-NEXT: %10 = affine.apply [[MAP1]](%i0, %8, %i0)
|
2019-01-31 07:53:41 +08:00
|
|
|
// CHECK-NEXT: %11 = load %0[%9, %10] : memref<64x1xf32>
|
|
|
|
// CHECK-NEXT: %12 = addf %11, %7 : f32
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %13 = affine.apply [[MAP0]](%i0, %8, %i0)
|
|
|
|
// CHECK-NEXT: %14 = affine.apply [[MAP1]](%i0, %8, %i0)
|
2019-01-31 07:53:41 +08:00
|
|
|
// CHECK-NEXT: store %12, %0[%13, %14] : memref<64x1xf32>
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
return
|
|
|
|
}
|
2019-02-05 22:57:08 +08:00
|
|
|
|
|
|
|
// -----
|
|
|
|
|
|
|
|
// CHECK: [[MAP0:#map[0-9]+]] = (d0, d1) -> (-d0 + d1)
|
|
|
|
|
|
|
|
// CHECK-LABEL: func @should_fuse_after_private_memref_creation() {
|
|
|
|
func @should_fuse_after_private_memref_creation() {
|
|
|
|
%a = alloc() : memref<10xf32>
|
|
|
|
%b = alloc() : memref<10xf32>
|
|
|
|
|
|
|
|
%cf7 = constant 7.0 : f32
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 10 {
|
2019-02-05 22:57:08 +08:00
|
|
|
store %cf7, %a[%i0] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i1 = 0 to 10 {
|
2019-02-05 22:57:08 +08:00
|
|
|
%v0 = load %a[%i1] : memref<10xf32>
|
|
|
|
store %v0, %b[%i1] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i2 = 0 to 10 {
|
2019-02-05 22:57:08 +08:00
|
|
|
%v1 = load %a[%i2] : memref<10xf32>
|
|
|
|
store %v1, %b[%i2] : memref<10xf32>
|
|
|
|
}
|
|
|
|
|
|
|
|
// On the first visit to '%i2', the fusion algorithm can not fuse loop nest
|
|
|
|
// '%i0' into '%i2' because of the dependences '%i0' and '%i2' each have on
|
|
|
|
// '%i1'. However, once the loop nest '%i0' is fused into '%i1' with a
|
|
|
|
// private memref, the dependence between '%i0' and '%i1' on memref '%a' no
|
|
|
|
// longer exists, so '%i0' can now be fused into '%i2'.
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 10 {
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %3 = affine.apply [[MAP0]](%i0, %i0)
|
2019-02-05 22:57:08 +08:00
|
|
|
// CHECK-NEXT: store %cst, %1[%3] : memref<1xf32>
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %4 = affine.apply [[MAP0]](%i0, %i0)
|
2019-02-05 22:57:08 +08:00
|
|
|
// CHECK-NEXT: %5 = load %1[%4] : memref<1xf32>
|
|
|
|
// CHECK-NEXT: store %5, %2[%i0] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 10 {
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %6 = affine.apply [[MAP0]](%i1, %i1)
|
2019-02-05 22:57:08 +08:00
|
|
|
// CHECK-NEXT: store %cst, %0[%6] : memref<1xf32>
|
2019-02-07 03:08:18 +08:00
|
|
|
// CHECK-NEXT: %7 = affine.apply [[MAP0]](%i1, %i1)
|
2019-02-05 22:57:08 +08:00
|
|
|
// CHECK-NEXT: %8 = load %0[%7] : memref<1xf32>
|
|
|
|
// CHECK-NEXT: store %8, %2[%i1] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
return
|
|
|
|
}
|
2019-02-16 01:32:18 +08:00
|
|
|
|
|
|
|
// -----
|
|
|
|
|
|
|
|
// CHECK: [[MAP0:#map[0-9]+]] = (d0, d1) -> (-d0 + d1)
|
|
|
|
|
|
|
|
// CHECK-LABEL: func @should_fuse_after_one_loop_interchange() {
|
|
|
|
func @should_fuse_after_one_loop_interchange() {
|
|
|
|
%a = alloc() : memref<10xf32>
|
|
|
|
|
|
|
|
%cf0 = constant 0.0 : f32
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 10 {
|
2019-02-16 01:32:18 +08:00
|
|
|
store %cf0, %a[%i0] : memref<10xf32>
|
|
|
|
}
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i1 = 0 to 5 {
|
|
|
|
affine.for %i2 = 0 to 10 {
|
2019-02-16 01:32:18 +08:00
|
|
|
%v0 = load %a[%i2] : memref<10xf32>
|
|
|
|
store %v0, %a[%i2] : memref<10xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The dependence between the load and store is carried on loop '%i1', and
|
|
|
|
// cannot be fused with loop '%i0' without violating this dependence.
|
|
|
|
// Once loops '%i1' and %i2' are interchanged, loop '%i0' can be fused
|
|
|
|
// at loop depth 1, because the loop carrying the dependence has been
|
|
|
|
// interchanged and is now at depth 2.
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 10 {
|
2019-02-16 01:32:18 +08:00
|
|
|
// CHECK-NEXT: %1 = affine.apply [[MAP0]](%i0, %i0)
|
|
|
|
// CHECK-NEXT: store %cst, %0[%1] : memref<1xf32>
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 5 {
|
2019-02-16 01:32:18 +08:00
|
|
|
// CHECK-NEXT: %2 = affine.apply [[MAP0]](%i0, %i0)
|
|
|
|
// CHECK-NEXT: %3 = load %0[%2] : memref<1xf32>
|
|
|
|
// CHECK-NEXT: %4 = affine.apply [[MAP0]](%i0, %i0)
|
|
|
|
// CHECK-NEXT: store %3, %0[%4] : memref<1xf32>
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
|
|
|
|
|
|
|
// CHECK: [[MAP0:#map[0-9]+]] = (d0, d1, d2, d3) -> (-d0 + d2)
|
|
|
|
// CHECK: [[MAP1:#map[0-9]+]] = (d0, d1, d2, d3) -> (-d1 + d3)
|
|
|
|
|
|
|
|
// CHECK-LABEL: func @should_fuse_after_two_loop_interchanges() {
|
|
|
|
func @should_fuse_after_two_loop_interchanges() {
|
|
|
|
%a = alloc() : memref<6x8xf32>
|
|
|
|
|
|
|
|
%cf0 = constant 0.0 : f32
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 6 {
|
|
|
|
affine.for %i1 = 0 to 8 {
|
2019-02-16 01:32:18 +08:00
|
|
|
store %cf0, %a[%i0, %i1] : memref<6x8xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i2 = 0 to 4 {
|
|
|
|
affine.for %i3 = 0 to 6 {
|
|
|
|
affine.for %i4 = 0 to 2 {
|
|
|
|
affine.for %i5 = 0 to 8 {
|
2019-02-16 01:32:18 +08:00
|
|
|
%v0 = load %a[%i3, %i5] : memref<6x8xf32>
|
|
|
|
%v1 = addf %v0, %v0 : f32
|
|
|
|
store %v1, %a[%i3, %i5] : memref<6x8xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The dependence between the load and store is carried on loops '%i2' and
|
|
|
|
// '%i4', and cannot be fused with loop '%i0' without violating this
|
|
|
|
// dependence.
|
|
|
|
// Once loop '%i2' is interchanged with loop '%i3', and again with loop
|
|
|
|
// '%i5', then loop '%i0' can be fused at loop depth 2, because the loop
|
|
|
|
// carring the dependences have been interchanged with loops at depth > 2.
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 6 {
|
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 8 {
|
2019-02-16 01:32:18 +08:00
|
|
|
// CHECK-NEXT: %1 = affine.apply [[MAP0]](%i0, %i1, %i0, %i1)
|
|
|
|
// CHECK-NEXT: %2 = affine.apply [[MAP1]](%i0, %i1, %i0, %i1)
|
|
|
|
// CHECK-NEXT: store %cst, %0[%1, %2] : memref<1x1xf32>
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i2 = 0 to 4 {
|
|
|
|
// CHECK-NEXT: affine.for %i3 = 0 to 2 {
|
2019-02-16 01:32:18 +08:00
|
|
|
// CHECK-NEXT: %3 = affine.apply [[MAP0]](%i0, %i1, %i0, %i1)
|
|
|
|
// CHECK-NEXT: %4 = affine.apply [[MAP1]](%i0, %i1, %i0, %i1)
|
|
|
|
// CHECK-NEXT: %5 = load %0[%3, %4] : memref<1x1xf32>
|
|
|
|
// CHECK-NEXT: %6 = addf %5, %5 : f32
|
|
|
|
// CHECK-NEXT: %7 = affine.apply [[MAP0]](%i0, %i1, %i0, %i1)
|
|
|
|
// CHECK-NEXT: %8 = affine.apply [[MAP1]](%i0, %i1, %i0, %i1)
|
|
|
|
// CHECK-NEXT: store %6, %0[%7, %8] : memref<1x1xf32>
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
return
|
|
|
|
}
|
2019-02-16 09:12:19 +08:00
|
|
|
|
|
|
|
// -----
|
|
|
|
|
|
|
|
func @should_fuse_live_out_writer(%arg0 : memref<10xf32>) -> memref<10xf32> {
|
|
|
|
%cst = constant 0.000000e+00 : f32
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 10 {
|
2019-02-16 09:12:19 +08:00
|
|
|
store %cst, %arg0[%i0] : memref<10xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i1 = 0 to 10 {
|
2019-02-16 09:12:19 +08:00
|
|
|
%1 = load %arg0[%i1] : memref<10xf32>
|
|
|
|
store %1, %arg0[%i1] : memref<10xf32>
|
|
|
|
}
|
|
|
|
return %arg0 : memref<10xf32>
|
|
|
|
|
|
|
|
// CHECK: %cst = constant 0.000000e+00 : f32
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i0 = 0 to 10 {
|
2019-02-16 09:12:19 +08:00
|
|
|
// CHECK-NEXT: store %cst, %arg0[%i0] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: %0 = load %arg0[%i0] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: store %0, %arg0[%i0] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return %arg0 : memref<10xf32>
|
|
|
|
}
|
2019-02-20 10:17:19 +08:00
|
|
|
|
|
|
|
// -----
|
|
|
|
|
|
|
|
// The fused slice has 16 iterations from along %i0.
|
|
|
|
|
|
|
|
// CHECK-DAG: [[MAP_LB:#map[0-9]+]] = (d0) -> (d0 * 16)
|
|
|
|
// CHECK-DAG: [[MAP_UB:#map[0-9]+]] = (d0) -> (d0 * 16 + 16)
|
|
|
|
|
|
|
|
#map = (d0, d1) -> (d0 * 16 + d1)
|
|
|
|
|
|
|
|
// CHECK-LABEL: slice_tile
|
2019-03-05 09:26:46 +08:00
|
|
|
func @slice_tile(%arg0: memref<128x8xf32>, %arg1: memref<32x8xf32>, %0 : f32) -> memref<32x8xf32> {
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 32 {
|
|
|
|
affine.for %i1 = 0 to 8 {
|
2019-03-05 09:26:46 +08:00
|
|
|
store %0, %arg1[%i0, %i1] : memref<32x8xf32>
|
2019-02-20 10:17:19 +08:00
|
|
|
}
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i = 0 to 2 {
|
|
|
|
affine.for %j = 0 to 8 {
|
|
|
|
affine.for %k = 0 to 8 {
|
|
|
|
affine.for %kk = 0 to 16 {
|
2019-02-20 10:17:19 +08:00
|
|
|
%1 = affine.apply #map(%k, %kk)
|
2019-03-05 09:26:46 +08:00
|
|
|
%2 = load %arg0[%1, %j] : memref<128x8xf32>
|
2019-02-20 10:17:19 +08:00
|
|
|
%3 = "foo"(%2) : (f32) -> f32
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %ii = 0 to 16 {
|
2019-02-20 10:17:19 +08:00
|
|
|
%6 = affine.apply #map(%i, %ii)
|
2019-03-05 09:26:46 +08:00
|
|
|
%7 = load %arg1[%6, %j] : memref<32x8xf32>
|
2019-02-20 10:17:19 +08:00
|
|
|
%8 = addf %7, %7 : f32
|
2019-03-05 09:26:46 +08:00
|
|
|
store %8, %arg1[%6, %j] : memref<32x8xf32>
|
2019-02-20 10:17:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-05 09:26:46 +08:00
|
|
|
return %arg1 : memref<32x8xf32>
|
2019-02-20 10:17:19 +08:00
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 2 {
|
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 8 {
|
|
|
|
// CHECK-NEXT: affine.for %i2 = [[MAP_LB]](%i0) to [[MAP_UB]](%i0) {
|
2019-02-20 10:17:19 +08:00
|
|
|
// CHECK-NEXT: store %arg2, %arg1[%i2, %i1] : memref<32x8xf32>
|
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i3 = 0 to 8 {
|
|
|
|
// CHECK-NEXT: affine.for %i4 = 0 to 16 {
|
2019-02-20 10:17:19 +08:00
|
|
|
// CHECK-NEXT: %0 = affine.apply #map{{[0-9]+}}(%i3, %i4)
|
2019-03-05 09:26:46 +08:00
|
|
|
// CHECK-NEXT: %1 = load %arg0[%0, %i1] : memref<128x8xf32>
|
2019-02-20 10:17:19 +08:00
|
|
|
// CHECK-NEXT: %2 = "foo"(%1) : (f32) -> f32
|
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i5 = 0 to 16 {
|
2019-02-20 10:17:19 +08:00
|
|
|
// CHECK-NEXT: %3 = affine.apply #map{{[0-9]+}}(%i0, %i5)
|
|
|
|
// CHECK-NEXT: %4 = load %arg1[%3, %i1] : memref<32x8xf32>
|
|
|
|
// CHECK-NEXT: %5 = addf %4, %4 : f32
|
|
|
|
// CHECK-NEXT: store %5, %arg1[%3, %i1] : memref<32x8xf32>
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return %arg1 : memref<32x8xf32>
|
|
|
|
// CHECK-NEXT:}
|
2019-02-28 03:01:49 +08:00
|
|
|
|
|
|
|
// -----
|
|
|
|
|
|
|
|
// Test case which illustrates fix for b/126454413
|
|
|
|
func @test_add_slice_bounds() {
|
|
|
|
%a = alloc() : memref<10xf32>
|
|
|
|
%b = alloc() : memref<10xf32>
|
|
|
|
%cf7 = constant 7.0 : f32
|
|
|
|
%c0 = constant 0 : index
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 10 {
|
|
|
|
affine.for %i1 = 0 to 10 {
|
|
|
|
affine.for %i2 = 0 to 10 {
|
2019-02-28 03:01:49 +08:00
|
|
|
%a0 = affine.apply (d0) -> (d0) (%i0)
|
|
|
|
%a1 = affine.apply (d0) -> (d0) (%i0)
|
|
|
|
%a2 = affine.apply (d0, d1) -> (d0 - d1) (%a0, %a1)
|
|
|
|
store %cf7, %a[%a2] : memref<10xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i3 = 0 to 10 {
|
|
|
|
affine.for %i4 = 0 to 10 {
|
|
|
|
affine.for %i5 = 0 to 10 {
|
2019-02-28 03:01:49 +08:00
|
|
|
%v0 = load %a[%c0] : memref<10xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 10 {
|
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 10 {
|
|
|
|
// CHECK-NEXT: affine.for %i2 = 0 to 10 {
|
2019-04-05 02:13:02 +08:00
|
|
|
// CHECK-NEXT: %2 = affine.apply #map0(%i0)
|
|
|
|
// CHECK-NEXT: %3 = affine.apply #map0(%i0)
|
|
|
|
// CHECK-NEXT: %4 = affine.apply #map1(%2, %3)
|
2019-02-28 03:01:49 +08:00
|
|
|
// CHECK-NEXT: store %cst, %0[%4] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i3 = 0 to 10 {
|
|
|
|
// CHECK-NEXT: affine.for %i4 = 0 to 10 {
|
|
|
|
// CHECK-NEXT: affine.for %i5 = 0 to 10 {
|
2019-02-28 03:01:49 +08:00
|
|
|
// CHECK-NEXT: %5 = load %0[%c0] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
return
|
|
|
|
}
|
2019-03-02 03:50:25 +08:00
|
|
|
|
|
|
|
// -----
|
|
|
|
// CHECK-DAG: [[MAP0:#map[0-9]+]] = (d0, d1, d2, d3) -> (-d0 + d2)
|
|
|
|
// CHECK-DAG: [[MAP1:#map[0-9]+]] = (d0, d1, d2, d3) -> (-d1 + d3)
|
|
|
|
|
|
|
|
func @should_fuse_init_loops_siblings_then_shared_producer(%arg0: memref<10x10xf32>, %arg1: memref<10x10xf32>) {
|
|
|
|
%0 = alloc() : memref<10x10xf32>
|
|
|
|
%cst = constant 0.000000e+00 : f32
|
|
|
|
%cst_0 = constant 1.000000e+00 : f32
|
|
|
|
%cst_1 = constant 7.000000e+00 : f32
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 10 {
|
|
|
|
affine.for %i1 = 0 to 10 {
|
2019-03-02 03:50:25 +08:00
|
|
|
store %cst_1, %0[%i0, %i1] : memref<10x10xf32>
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i2 = 0 to 3 {
|
|
|
|
affine.for %i3 = 0 to 3 {
|
2019-03-02 03:50:25 +08:00
|
|
|
store %cst, %arg0[%i2, %i3] : memref<10x10xf32>
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i4 = 0 to 3 {
|
|
|
|
affine.for %i5 = 0 to 3 {
|
2019-03-02 03:50:25 +08:00
|
|
|
%1 = load %0[%i4, %i5] : memref<10x10xf32>
|
|
|
|
%2 = load %arg0[%i4, %i5] : memref<10x10xf32>
|
|
|
|
%3 = mulf %1, %2 : f32
|
|
|
|
store %3, %arg0[%i4, %i5] : memref<10x10xf32>
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i6 = 0 to 3 {
|
|
|
|
affine.for %i7 = 0 to 3 {
|
2019-03-02 03:50:25 +08:00
|
|
|
store %cst_0, %arg1[%i6, %i7] : memref<10x10xf32>
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i8 = 0 to 3 {
|
|
|
|
affine.for %i9 = 0 to 3 {
|
2019-03-02 03:50:25 +08:00
|
|
|
%4 = load %0[%i8, %i9] : memref<10x10xf32>
|
|
|
|
%5 = load %arg1[%i8, %i9] : memref<10x10xf32>
|
|
|
|
%6 = addf %4, %5 : f32
|
|
|
|
store %6, %arg1[%i8, %i9] : memref<10x10xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass 1: should fuse single-use producer loop nests into their unique user,
|
|
|
|
// so '%i2' will fuse into '%i4' and '%i6' will fuse into '%i8'.
|
|
|
|
// Pass 2: should fuse sibling loop nests which share no dependence edges,
|
|
|
|
// so should fuse '%i4' into '%i8'.
|
|
|
|
// Pass 3: should fuse single-use producer loop nest '%i0' into '%i8'. Note
|
|
|
|
// that loop nest '%i0' now has a single user after Pass 2 fused its
|
|
|
|
// two users together).
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 3 {
|
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 3 {
|
2019-03-02 03:50:25 +08:00
|
|
|
// CHECK-NEXT: %1 = affine.apply [[MAP0]](%i0, %i1, %i0, %i1)
|
|
|
|
// CHECK-NEXT: %2 = affine.apply [[MAP1]](%i0, %i1, %i0, %i1)
|
|
|
|
// CHECK-NEXT: store %cst_1, %0[%1, %2] : memref<1x1xf32>
|
|
|
|
// CHECK-NEXT: store %cst, %arg0[%i0, %i1] : memref<10x10xf32>
|
|
|
|
// CHECK-NEXT: %3 = affine.apply [[MAP0]](%i0, %i1, %i0, %i1)
|
|
|
|
// CHECK-NEXT: %4 = affine.apply [[MAP1]](%i0, %i1, %i0, %i1)
|
|
|
|
// CHECK-NEXT: %5 = load %0[%3, %4] : memref<1x1xf32>
|
|
|
|
// CHECK-NEXT: %6 = load %arg0[%i0, %i1] : memref<10x10xf32>
|
|
|
|
// CHECK-NEXT: %7 = mulf %5, %6 : f32
|
|
|
|
// CHECK-NEXT: store %7, %arg0[%i0, %i1] : memref<10x10xf32>
|
|
|
|
// CHECK-NEXT: store %cst_0, %arg1[%i0, %i1] : memref<10x10xf32>
|
|
|
|
// CHECK-NEXT: %8 = affine.apply [[MAP0]](%i0, %i1, %i0, %i1)
|
|
|
|
// CHECK-NEXT: %9 = affine.apply [[MAP1]](%i0, %i1, %i0, %i1)
|
|
|
|
// CHECK-NEXT: %10 = load %0[%8, %9] : memref<1x1xf32>
|
|
|
|
// CHECK-NEXT: %11 = load %arg1[%i0, %i1] : memref<10x10xf32>
|
|
|
|
// CHECK-NEXT: %12 = addf %10, %11 : f32
|
|
|
|
// CHECK-NEXT: store %12, %arg1[%i0, %i1] : memref<10x10xf32>
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----
|
|
|
|
// CHECK-DAG: [[MAP2:#map[0-9]+]] = (d0, d1, d2) -> (d1)
|
|
|
|
// CHECK-DAG: [[MAP3:#map[0-9]+]] = (d0, d1, d2) -> (-d0 + d2)
|
|
|
|
|
|
|
|
func @two_matrix_vector_products() {
|
|
|
|
%in_matrix = alloc() : memref<10x10xf32>
|
|
|
|
%in_vec0 = alloc() : memref<10xf32>
|
|
|
|
%in_vec1 = alloc() : memref<10xf32>
|
|
|
|
%out_vec0 = alloc() : memref<10xf32>
|
|
|
|
%out_vec1 = alloc() : memref<10xf32>
|
|
|
|
%cf7 = constant 7.0 : f32
|
|
|
|
|
|
|
|
// Populate input matrix.
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 10 {
|
|
|
|
affine.for %i1 = 0 to 10 {
|
2019-03-02 03:50:25 +08:00
|
|
|
store %cf7, %in_matrix[%i0, %i1] : memref<10x10xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// out_vec0 = in_matrix x in_vec0
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i2 = 0 to 10 {
|
|
|
|
affine.for %i3 = 0 to 10 {
|
2019-03-02 03:50:25 +08:00
|
|
|
%v0 = load %in_matrix[%i2, %i3] : memref<10x10xf32>
|
|
|
|
%v1 = load %in_vec0[%i3] : memref<10xf32>
|
|
|
|
%v2 = mulf %v0, %v1 : f32
|
|
|
|
%v3 = load %out_vec0[%i3] : memref<10xf32>
|
|
|
|
%v4 = addf %v2, %v3 : f32
|
|
|
|
store %v4, %out_vec0[%i3] : memref<10xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// out_vec1 = in_matrix x in_vec1
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i4 = 0 to 10 {
|
|
|
|
affine.for %i5 = 0 to 10 {
|
2019-03-02 03:50:25 +08:00
|
|
|
%v5 = load %in_matrix[%i4, %i5] : memref<10x10xf32>
|
|
|
|
%v6 = load %in_vec1[%i5] : memref<10xf32>
|
|
|
|
%v7 = mulf %v5, %v6 : f32
|
|
|
|
%v8 = load %out_vec1[%i5] : memref<10xf32>
|
|
|
|
%v9 = addf %v7, %v8 : f32
|
|
|
|
store %v9, %out_vec1[%i5] : memref<10xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 10 {
|
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 10 {
|
2019-03-02 03:50:25 +08:00
|
|
|
// CHECK-NEXT: %5 = affine.apply [[MAP2]](%i0, %i1, %i0)
|
|
|
|
// CHECK-NEXT: %6 = affine.apply [[MAP3]](%i0, %i1, %i0)
|
|
|
|
// CHECK-NEXT: store %cst, %0[%5, %6] : memref<10x1xf32>
|
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i2 = 0 to 10 {
|
2019-03-02 03:50:25 +08:00
|
|
|
// CHECK-NEXT: %7 = affine.apply [[MAP2]](%i0, %i2, %i0)
|
|
|
|
// CHECK-NEXT: %8 = affine.apply [[MAP3]](%i0, %i2, %i0)
|
|
|
|
// CHECK-NEXT: %9 = load %0[%7, %8] : memref<10x1xf32>
|
|
|
|
// CHECK-NEXT: %10 = load %1[%i0] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: %11 = mulf %9, %10 : f32
|
|
|
|
// CHECK-NEXT: %12 = load %3[%i0] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: %13 = addf %11, %12 : f32
|
|
|
|
// CHECK-NEXT: store %13, %3[%i0] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i3 = 0 to 10 {
|
2019-03-02 03:50:25 +08:00
|
|
|
// CHECK-NEXT: %14 = affine.apply [[MAP2]](%i0, %i3, %i0)
|
|
|
|
// CHECK-NEXT: %15 = affine.apply [[MAP3]](%i0, %i3, %i0)
|
|
|
|
// CHECK-NEXT: %16 = load %0[%14, %15] : memref<10x1xf32>
|
|
|
|
// CHECK-NEXT: %17 = load %2[%i0] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: %18 = mulf %16, %17 : f32
|
|
|
|
// CHECK-NEXT: %19 = load %4[%i0] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: %20 = addf %18, %19 : f32
|
|
|
|
// CHECK-NEXT: store %20, %4[%i0] : memref<10xf32>
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
return
|
2019-03-05 06:58:59 +08:00
|
|
|
}
|
2019-03-05 07:14:12 +08:00
|
|
|
|
|
|
|
// -----
|
|
|
|
// CHECK-DAG: [[MAP3:#map[0-9]+]] = (d0, d1, d2) -> (-d0 + d1)
|
|
|
|
// CHECK-DAG: [[MAP4:#map[0-9]+]] = (d0, d1, d2) -> (d2)
|
|
|
|
|
|
|
|
func @should_not_slice_past_slice_barrier() {
|
|
|
|
%0 = alloc() : memref<100x16xf32>
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 100 {
|
|
|
|
affine.for %i1 = 0 to 16 {
|
2019-03-05 07:14:12 +08:00
|
|
|
%1 = "op1"() : () -> f32
|
|
|
|
store %1, %0[%i0, %i1] : memref<100x16xf32>
|
2019-06-26 10:06:06 +08:00
|
|
|
} {slice_fusion_barrier = true}
|
2019-03-05 07:14:12 +08:00
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i2 = 0 to 100 {
|
|
|
|
affine.for %i3 = 0 to 16 {
|
2019-03-05 07:14:12 +08:00
|
|
|
%2 = load %0[%i2, %i3] : memref<100x16xf32>
|
|
|
|
"op2"(%2) : (f32) -> ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// The 'slice_fusion_barrier' attribute on '%i1' prevents slicing the
|
|
|
|
// iteration space of '%i1' and any enclosing loop nests.
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 0 to 100 {
|
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 16 {
|
2019-03-05 07:14:12 +08:00
|
|
|
// CHECK-NEXT: %1 = "op1"() : () -> f32
|
|
|
|
// CHECK-NEXT: %2 = affine.apply [[MAP3]](%i0, %i0, %i1)
|
|
|
|
// CHECK-NEXT: %3 = affine.apply [[MAP4]](%i0, %i0, %i1)
|
|
|
|
// CHECK-NEXT: store %1, %0[%2, %3] : memref<1x16xf32>
|
2019-06-26 10:06:06 +08:00
|
|
|
// CHECK-NEXT: } {slice_fusion_barrier = true}
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i2 = 0 to 16 {
|
2019-03-05 07:14:12 +08:00
|
|
|
// CHECK-NEXT: %4 = affine.apply [[MAP3]](%i0, %i0, %i2)
|
|
|
|
// CHECK-NEXT: %5 = affine.apply [[MAP4]](%i0, %i0, %i2)
|
|
|
|
// CHECK-NEXT: %6 = load %0[%4, %5] : memref<1x16xf32>
|
|
|
|
// CHECK-NEXT: "op2"(%6) : (f32) -> ()
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
return
|
|
|
|
}
|
2019-03-06 03:08:41 +08:00
|
|
|
|
|
|
|
// -----
|
|
|
|
|
|
|
|
#map0 = (d0, d1) -> (d0 * 16 + d1)
|
|
|
|
func @fuse_across_dim_mismatch(%arg0: memref<4x4x16x1xf32>, %arg1: memref<144x9xf32>, %arg2: memref<9xf32>) {
|
|
|
|
%1 = alloc() : memref<144x4xf32>
|
|
|
|
%2 = constant 0.0 : f32
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i2 = 0 to 9 {
|
|
|
|
affine.for %i3 = 0 to 4 {
|
|
|
|
affine.for %i5 = 0 to 16 {
|
2019-03-06 03:08:41 +08:00
|
|
|
%7 = affine.apply #map0(%i2, %i5)
|
|
|
|
store %2, %1[%7, %i3] : memref<144x4xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i6 = 0 to 9 {
|
|
|
|
affine.for %i7 = 0 to 9 {
|
|
|
|
affine.for %i8 = 0 to 4 {
|
|
|
|
affine.for %i10 = 0 to 16 {
|
2019-03-06 03:08:41 +08:00
|
|
|
%10 = affine.apply #map0(%i6, %i10)
|
|
|
|
%11 = load %1[%10, %i8] : memref<144x4xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2019-04-05 02:13:02 +08:00
|
|
|
// MAXIMAL: #map0 = (d0, d1) -> (d0 * 16 + d1)
|
|
|
|
// MAXIMAL-NEXT: #map1 = (d0, d1, d2, d3, d4) -> (d0 * -16 - d1 + d3)
|
|
|
|
// MAXIMAL-NEXT: #map2 = (d0, d1, d2, d3, d4) -> (-d2 + d4)
|
2019-03-06 03:08:41 +08:00
|
|
|
// MAXIMAL-LABEL: func @fuse_across_dim_mismatch
|
|
|
|
// MAXIMAL: %0 = alloc() : memref<1x1xf32>
|
2019-03-26 01:14:34 +08:00
|
|
|
// MAXIMAL: affine.for %i0 = 0 to 9 {
|
|
|
|
// MAXIMAL-NEXT: affine.for %i1 = 0 to 9 {
|
|
|
|
// MAXIMAL-NEXT: affine.for %i2 = 0 to 4 {
|
|
|
|
// MAXIMAL-NEXT: affine.for %i3 = 0 to 16 {
|
2019-04-05 02:13:02 +08:00
|
|
|
// MAXIMAL-NEXT: %1 = affine.apply #map0(%i0, %i3)
|
|
|
|
// MAXIMAL-NEXT: %2 = affine.apply #map1(%i0, %i3, %i2, %1, %i2)
|
|
|
|
// MAXIMAL-NEXT: %3 = affine.apply #map2(%i0, %i3, %i2, %1, %i2)
|
2019-03-06 03:08:41 +08:00
|
|
|
// MAXIMAL-NEXT: store %cst, %0[%2, %3] : memref<1x1xf32>
|
2019-04-05 02:13:02 +08:00
|
|
|
// MAXIMAL-NEXT: %4 = affine.apply #map0(%i0, %i3)
|
|
|
|
// MAXIMAL-NEXT: %5 = affine.apply #map1(%i0, %i3, %i2, %4, %i2)
|
|
|
|
// MAXIMAL-NEXT: %6 = affine.apply #map2(%i0, %i3, %i2, %4, %i2)
|
2019-03-06 03:08:41 +08:00
|
|
|
// MAXIMAL-NEXT: %7 = load %0[%5, %6] : memref<1x1xf32>
|
|
|
|
// MAXIMAL-NEXT: }
|
|
|
|
// MAXIMAL-NEXT: }
|
|
|
|
// MAXIMAL-NEXT: }
|
|
|
|
// MAXIMAL-NEXT: }
|
|
|
|
|
|
|
|
// -----
|
|
|
|
|
|
|
|
#map3 = (d0, d1) -> ((d0 * 72 + d1) floordiv 2304)
|
|
|
|
#map4 = (d0, d1) -> (((d0 * 72 + d1) mod 2304) floordiv 1152)
|
|
|
|
#map5 = (d0, d1) -> (((((d0 * 72 + d1) mod 2304) mod 1152) floordiv 9) floordiv 8)
|
|
|
|
#map6 = (d0, d1) -> (((((d0 * 72 + d1) mod 2304) mod 1152) mod 9) floordiv 3)
|
|
|
|
#map7 = (d0, d1) -> (((((d0 * 72 + d1) mod 2304) mod 1152) mod 9) mod 3)
|
|
|
|
#map10 = (d0, d1) -> (d0 * 16 + d1)
|
|
|
|
#map11 = (d0, d1) -> (d0 * 16 + d1)
|
|
|
|
#map12 = (d0, d1) -> (d0 * 16 - d1 + 15)
|
|
|
|
func @fuse_across_varying_dims_complex() {
|
|
|
|
%c0 = constant 0 : index
|
|
|
|
%0 = alloc() : memref<2x2x3x3x16x1xf32>
|
|
|
|
%1 = alloc() : memref<64x9xf32>
|
|
|
|
%2 = alloc() : memref<144x4xf32>
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 64 {
|
|
|
|
affine.for %i1 = 0 to 9 {
|
2019-03-06 03:08:41 +08:00
|
|
|
%4 = affine.apply #map3(%i0, %i1)
|
|
|
|
%5 = affine.apply #map4(%i0, %i1)
|
|
|
|
%6 = affine.apply #map5(%i0, %i1)
|
|
|
|
%7 = affine.apply #map6(%i0, %i1)
|
|
|
|
%8 = affine.apply #map7(%i0, %i1)
|
|
|
|
%9 = load %0[%4, %5, %7, %8, %6, %c0] : memref<2x2x3x3x16x1xf32>
|
|
|
|
store %9, %1[%i0, %i1] : memref<64x9xf32>
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i2 = 0 to 9 {
|
|
|
|
affine.for %i3 = 0 to 4 {
|
|
|
|
affine.for %i4 = 0 to 16 {
|
2019-03-06 03:08:41 +08:00
|
|
|
%10 = affine.apply #map10(%i3, %i4)
|
|
|
|
%11 = load %1[%10, %i2] : memref<64x9xf32>
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i5 = 0 to 16 {
|
2019-03-06 03:08:41 +08:00
|
|
|
%13 = "bar"() : () -> f32
|
|
|
|
%14 = affine.apply #map11(%i2, %i5)
|
|
|
|
store %13, %2[%14, %i3] : memref<144x4xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i6 = 0 to 9 {
|
|
|
|
affine.for %i7 = 0 to 9 {
|
|
|
|
affine.for %i8 = 0 to 4 {
|
|
|
|
affine.for %i9 = 0 to 16 {
|
2019-03-06 03:08:41 +08:00
|
|
|
%15 = affine.apply #map12(%i8, %i9)
|
|
|
|
%16 = load %1[%15, %i7] : memref<64x9xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2019-04-05 02:13:02 +08:00
|
|
|
// MAXIMAL-DAG: [[MAP0:#map[0-9]+]] = (d0, d1) -> ((d0 * 72 + d1) floordiv 2304)
|
|
|
|
// MAXIMAL-DAG: [[MAP1:#map[0-9]+]] = (d0, d1) -> (((d0 * 72 + d1) mod 2304) floordiv 1152)
|
|
|
|
// MAXIMAL-DAG: [[MAP2:#map[0-9]+]] = (d0, d1) -> (((((d0 * 72 + d1) mod 2304) mod 1152) floordiv 9) floordiv 8)
|
|
|
|
// MAXIMAL-DAG: [[MAP3:#map[0-9]+]] = (d0, d1) -> (((((d0 * 72 + d1) mod 2304) mod 1152) mod 9) floordiv 3)
|
|
|
|
// MAXIMAL-DAG: [[MAP4:#map[0-9]+]] = (d0, d1) -> (((((d0 * 72 + d1) mod 2304) mod 1152) mod 9) mod 3)
|
|
|
|
// MAXIMAL-DAG: [[MAP5:#map[0-9]+]] = (d0, d1, d2) -> (d1)
|
|
|
|
// MAXIMAL-DAG: [[MAP6:#map[0-9]+]] = (d0, d1, d2) -> (-d0 + d2)
|
|
|
|
// MAXIMAL-DAG: [[MAP7:#map[0-9]+]] = (d0, d1) -> (d0 * 16 + d1)
|
|
|
|
// MAXIMAL-DAG: [[MAP8:#map[0-9]+]] = (d0, d1) -> (d0 * 16 - d1 + 15)
|
2019-03-06 08:54:34 +08:00
|
|
|
// MAXIMAL-LABEL: func @fuse_across_varying_dims_complex
|
2019-03-06 03:08:41 +08:00
|
|
|
// MAXIMAL-NEXT: %0 = alloc() : memref<64x1xf32>
|
|
|
|
// MAXIMAL-NEXT: %c0 = constant 0 : index
|
|
|
|
// MAXIMAL-NEXT: %1 = alloc() : memref<2x2x3x3x16x1xf32>
|
|
|
|
// MAXIMAL-NEXT: %2 = alloc() : memref<144x4xf32>
|
2019-03-26 01:14:34 +08:00
|
|
|
// MAXIMAL-NEXT: affine.for %i0 = 0 to 9 {
|
|
|
|
// MAXIMAL-NEXT: affine.for %i1 = 0 to 9 {
|
|
|
|
// MAXIMAL-NEXT: affine.for %i2 = 0 to 4 {
|
|
|
|
// MAXIMAL-NEXT: affine.for %i3 = 0 to 16 {
|
|
|
|
// MAXIMAL-NEXT: affine.for %i4 = 0 to 64 {
|
2019-04-05 02:13:02 +08:00
|
|
|
// MAXIMAL-NEXT: %3 = affine.apply [[MAP0]](%i4, %i0)
|
|
|
|
// MAXIMAL-NEXT: %4 = affine.apply [[MAP1]](%i4, %i0)
|
|
|
|
// MAXIMAL-NEXT: %5 = affine.apply [[MAP2]](%i4, %i0)
|
|
|
|
// MAXIMAL-NEXT: %6 = affine.apply [[MAP3]](%i4, %i0)
|
|
|
|
// MAXIMAL-NEXT: %7 = affine.apply [[MAP4]](%i4, %i0)
|
2019-03-06 03:08:41 +08:00
|
|
|
// MAXIMAL-NEXT: %8 = load %1[%3, %4, %6, %7, %5, %c0] : memref<2x2x3x3x16x1xf32>
|
2019-04-05 02:13:02 +08:00
|
|
|
// MAXIMAL-NEXT: %9 = affine.apply [[MAP5]](%i0, %i4, %i0)
|
|
|
|
// MAXIMAL-NEXT: %10 = affine.apply [[MAP6]](%i0, %i4, %i0)
|
2019-03-06 03:08:41 +08:00
|
|
|
// MAXIMAL-NEXT: store %8, %0[%9, %10] : memref<64x1xf32>
|
|
|
|
// MAXIMAL-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// MAXIMAL-NEXT: affine.for %i5 = 0 to 4 {
|
|
|
|
// MAXIMAL-NEXT: affine.for %i6 = 0 to 16 {
|
2019-04-05 02:13:02 +08:00
|
|
|
// MAXIMAL-NEXT: %11 = affine.apply [[MAP7]](%i5, %i6)
|
|
|
|
// MAXIMAL-NEXT: %12 = affine.apply [[MAP5]](%i0, %11, %i0)
|
|
|
|
// MAXIMAL-NEXT: %13 = affine.apply [[MAP6]](%i0, %11, %i0)
|
2019-03-06 03:08:41 +08:00
|
|
|
// MAXIMAL-NEXT: %14 = load %0[%12, %13] : memref<64x1xf32>
|
|
|
|
// MAXIMAL-NEXT: }
|
2019-03-26 01:14:34 +08:00
|
|
|
// MAXIMAL-NEXT: affine.for %i7 = 0 to 16 {
|
2019-03-06 03:08:41 +08:00
|
|
|
// MAXIMAL-NEXT: %15 = "bar"() : () -> f32
|
2019-04-05 02:13:02 +08:00
|
|
|
// MAXIMAL-NEXT: %16 = affine.apply [[MAP7]](%i0, %i7)
|
2019-03-06 03:08:41 +08:00
|
|
|
// MAXIMAL-NEXT: store %15, %2[%16, %i5] : memref<144x4xf32>
|
|
|
|
// MAXIMAL-NEXT: }
|
|
|
|
// MAXIMAL-NEXT: }
|
2019-04-05 02:13:02 +08:00
|
|
|
// MAXIMAL-NEXT: %17 = affine.apply [[MAP8]](%i2, %i3)
|
|
|
|
// MAXIMAL-NEXT: %18 = affine.apply [[MAP5]](%i0, %17, %i0)
|
|
|
|
// MAXIMAL-NEXT: %19 = affine.apply [[MAP6]](%i0, %17, %i0)
|
2019-03-06 03:08:41 +08:00
|
|
|
// MAXIMAL-NEXT: %20 = load %0[%18, %19] : memref<64x1xf32>
|
|
|
|
// MAXIMAL-NEXT: }
|
|
|
|
// MAXIMAL-NEXT: }
|
|
|
|
// MAXIMAL-NEXT: }
|
|
|
|
// MAXIMAL-NEXT: }
|
2019-03-06 12:33:30 +08:00
|
|
|
|
|
|
|
// -----
|
|
|
|
// CHECK-DAG: [[MAP3:#map[0-9]+]] = (d0) -> (d0 - 10)
|
|
|
|
|
|
|
|
func @should_fuse_with_slice_union() {
|
|
|
|
%a = alloc() : memref<100xf32>
|
|
|
|
%c0 = constant 0 : index
|
|
|
|
%cf0 = constant 0.0 : f32
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i0 = 0 to 100 {
|
2019-03-06 12:33:30 +08:00
|
|
|
store %cf0, %a[%i0]: memref<100xf32>
|
|
|
|
}
|
|
|
|
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i1 = 10 to 20 {
|
2019-03-06 12:33:30 +08:00
|
|
|
%v0 = load %a[%i1]: memref<100xf32>
|
2019-03-26 01:14:34 +08:00
|
|
|
affine.for %i2 = 15 to 25 {
|
2019-03-06 12:33:30 +08:00
|
|
|
%v1 = load %a[%i2]: memref<100xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// The union of two slice bounds (calculated between the store and each of
|
|
|
|
// the loads) is computed and used in the fusion cost calculation, index
|
|
|
|
// remapping, and private memref size. The result is that the temporary
|
|
|
|
// memref is reduced from 100xf32 to 15xf32 and properly indexed by
|
|
|
|
// the fused loops based on the union calculation.
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK: affine.for %i0 = 10 to 20 {
|
|
|
|
// CHECK-NEXT: affine.for %i1 = 10 to 25 {
|
2019-03-06 12:33:30 +08:00
|
|
|
// CHECK-NEXT: %1 = affine.apply [[MAP3]](%i1)
|
|
|
|
// CHECK-NEXT: store %cst, %0[%1] : memref<15xf32>
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: %2 = affine.apply [[MAP3]](%i0)
|
|
|
|
// CHECK-NEXT: %3 = load %0[%2] : memref<15xf32>
|
2019-03-26 01:14:34 +08:00
|
|
|
// CHECK-NEXT: affine.for %i2 = 15 to 25 {
|
2019-03-06 12:33:30 +08:00
|
|
|
// CHECK-NEXT: %4 = affine.apply [[MAP3]](%i2)
|
|
|
|
// CHECK-NEXT: %5 = load %0[%4] : memref<15xf32>
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: return
|
|
|
|
return
|
|
|
|
}
|
2019-03-29 05:54:49 +08:00
|
|
|
|
|
|
|
// -----
|
|
|
|
|
|
|
|
func @affine_add_mm_fused(%arg0: memref<1024x1024xf32>, %arg1: memref<1024x1024xf32>, %arg2: memref<1024x1024xf32>, %arg3: memref<1024x1024xf32>) {
|
|
|
|
affine.for %i2 = 0 to 1024 {
|
|
|
|
affine.for %i3 = 0 to 1024 {
|
|
|
|
%0 = load %arg3[%i2, %i3] : memref<1024x1024xf32>
|
|
|
|
%1 = load %arg2[%i2, %i3] : memref<1024x1024xf32>
|
|
|
|
%2 = addf %1, %0 : f32
|
|
|
|
store %2, %arg2[%i2, %i3] : memref<1024x1024xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
affine.for %i4 = 0 to 1024 {
|
|
|
|
affine.for %i5 = 0 to 1024 {
|
|
|
|
affine.for %i6 = 0 to 1024 {
|
|
|
|
%3 = load %arg1[%i6, %i5] : memref<1024x1024xf32>
|
|
|
|
%4 = load %arg0[%i4, %i6] : memref<1024x1024xf32>
|
|
|
|
%5 = mulf %4, %3 : f32
|
|
|
|
%6 = load %arg2[%i4, %i5] : memref<1024x1024xf32>
|
|
|
|
%7 = addf %6, %5 : f32
|
|
|
|
store %7, %arg2[%i4, %i5] : memref<1024x1024xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Should fuse elementwise add loop at loop depth 2, above loop-carried
|
|
|
|
// dependence between load/store on '%arg2', carried on reduction loop %i6.
|
|
|
|
// CHECK: affine.for %i0 = 0 to 1024 {
|
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 1024 {
|
|
|
|
// CHECK-NEXT: %0 = load %arg3[%i0, %i1] : memref<1024x1024xf32>
|
|
|
|
// CHECK-NEXT: %1 = load %arg2[%i0, %i1] : memref<1024x1024xf32>
|
|
|
|
// CHECK-NEXT: %2 = addf %1, %0 : f32
|
|
|
|
// CHECK-NEXT: store %2, %arg2[%i0, %i1] : memref<1024x1024xf32>
|
|
|
|
// CHECK-NEXT: affine.for %i2 = 0 to 1024 {
|
|
|
|
// CHECK-NEXT: %3 = load %arg1[%i2, %i1] : memref<1024x1024xf32>
|
|
|
|
// CHECK-NEXT: %4 = load %arg0[%i0, %i2] : memref<1024x1024xf32>
|
|
|
|
// CHECK-NEXT: %5 = mulf %4, %3 : f32
|
|
|
|
// CHECK-NEXT: %6 = load %arg2[%i0, %i1] : memref<1024x1024xf32>
|
|
|
|
// CHECK-NEXT: %7 = addf %6, %5 : f32
|
|
|
|
// CHECK-NEXT: store %7, %arg2[%i0, %i1] : memref<1024x1024xf32>
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
return
|
|
|
|
}
|
2019-03-29 23:06:25 +08:00
|
|
|
|
|
|
|
// -----
|
|
|
|
|
|
|
|
func @affine_2mm_fused(%arg0: memref<1024x1024xf32>, %arg1: memref<1024x1024xf32>, %arg2: memref<1024x1024xf32>, %arg3: memref<1024x1024xf32>, %arg4: memref<1024x1024xf32>) {
|
|
|
|
%cst = constant 0.000000e+00 : f32
|
|
|
|
affine.for %i0 = 0 to 1024 {
|
|
|
|
affine.for %i1 = 0 to 1024 {
|
|
|
|
store %cst, %arg2[%i0, %i1] : memref<1024x1024xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
affine.for %i2 = 0 to 1024 {
|
|
|
|
affine.for %i3 = 0 to 1024 {
|
|
|
|
store %cst, %arg4[%i2, %i3] : memref<1024x1024xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
affine.for %i4 = 0 to 1024 {
|
|
|
|
affine.for %i5 = 0 to 1024 {
|
|
|
|
affine.for %i6 = 0 to 1024 {
|
|
|
|
%0 = load %arg1[%i6, %i5] : memref<1024x1024xf32>
|
|
|
|
%1 = load %arg0[%i4, %i6] : memref<1024x1024xf32>
|
|
|
|
%2 = mulf %1, %0 : f32
|
|
|
|
%3 = load %arg2[%i4, %i5] : memref<1024x1024xf32>
|
|
|
|
%4 = addf %3, %2 : f32
|
|
|
|
store %4, %arg2[%i4, %i5] : memref<1024x1024xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
affine.for %i7 = 0 to 1024 {
|
|
|
|
affine.for %i8 = 0 to 1024 {
|
|
|
|
affine.for %i9 = 0 to 1024 {
|
|
|
|
%5 = load %arg1[%i9, %i8] : memref<1024x1024xf32>
|
|
|
|
%6 = load %arg0[%i7, %i9] : memref<1024x1024xf32>
|
|
|
|
%7 = mulf %6, %5 : f32
|
|
|
|
%8 = load %arg4[%i7, %i8] : memref<1024x1024xf32>
|
|
|
|
%9 = addf %8, %7 : f32
|
|
|
|
store %9, %arg4[%i7, %i8] : memref<1024x1024xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should fuse MM intialization loops into their consumers, then fuse the
|
|
|
|
// two matmul loops together for input reuse on '%arg0/%arg1'.
|
|
|
|
|
|
|
|
// CHECK: affine.for %i0 = 0 to 1024 {
|
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 1024 {
|
|
|
|
// CHECK-NEXT: store %cst, %arg4[%i0, %i1] : memref<1024x1024xf32>
|
|
|
|
// CHECK-NEXT: affine.for %i2 = 0 to 1024 {
|
|
|
|
// CHECK-NEXT: %0 = load %arg1[%i2, %i1] : memref<1024x1024xf32>
|
|
|
|
// CHECK-NEXT: %1 = load %arg0[%i0, %i2] : memref<1024x1024xf32>
|
|
|
|
// CHECK-NEXT: %2 = mulf %1, %0 : f32
|
|
|
|
// CHECK-NEXT: %3 = load %arg4[%i0, %i1] : memref<1024x1024xf32>
|
|
|
|
// CHECK-NEXT: %4 = addf %3, %2 : f32
|
|
|
|
// CHECK-NEXT: store %4, %arg4[%i0, %i1] : memref<1024x1024xf32>
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: affine.for %i3 = 0 to 1024 {
|
|
|
|
// CHECK-NEXT: store %cst, %arg2[%i0, %i3] : memref<1024x1024xf32>
|
|
|
|
// CHECK-NEXT: affine.for %i4 = 0 to 1024 {
|
|
|
|
// CHECK-NEXT: %5 = load %arg1[%i4, %i3] : memref<1024x1024xf32>
|
|
|
|
// CHECK-NEXT: %6 = load %arg0[%i0, %i4] : memref<1024x1024xf32>
|
|
|
|
// CHECK-NEXT: %7 = mulf %6, %5 : f32
|
|
|
|
// CHECK-NEXT: %8 = load %arg2[%i0, %i3] : memref<1024x1024xf32>
|
|
|
|
// CHECK-NEXT: %9 = addf %8, %7 : f32
|
|
|
|
// CHECK-NEXT: store %9, %arg2[%i0, %i3] : memref<1024x1024xf32>
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2019-04-02 21:37:40 +08:00
|
|
|
|
|
|
|
// -----
|
|
|
|
|
|
|
|
func @affine_2_dependent_mm_fused(%arg0: memref<1024x1024xf32>, %arg1: memref<1024x1024xf32>, %arg2: memref<1024x1024xf32>, %arg3: memref<1024x1024xf32>, %arg4: memref<1024x1024xf32>) {
|
|
|
|
affine.for %i0 = 0 to 1024 {
|
|
|
|
affine.for %i1 = 0 to 1024 {
|
|
|
|
affine.for %i2 = 0 to 1024 {
|
|
|
|
%0 = load %arg1[%i2, %i1] : memref<1024x1024xf32>
|
|
|
|
%1 = load %arg0[%i0, %i2] : memref<1024x1024xf32>
|
|
|
|
%2 = mulf %1, %0 : f32
|
|
|
|
%3 = load %arg2[%i0, %i1] : memref<1024x1024xf32>
|
|
|
|
%4 = addf %3, %2 : f32
|
|
|
|
store %4, %arg2[%i0, %i1] : memref<1024x1024xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
affine.for %i3 = 0 to 1024 {
|
|
|
|
affine.for %i4 = 0 to 1024 {
|
|
|
|
affine.for %i5 = 0 to 1024 {
|
|
|
|
%5 = load %arg3[%i5, %i4] : memref<1024x1024xf32>
|
|
|
|
%6 = load %arg2[%i3, %i5] : memref<1024x1024xf32>
|
|
|
|
%7 = mulf %6, %5 : f32
|
|
|
|
%8 = load %arg4[%i3, %i4] : memref<1024x1024xf32>
|
|
|
|
%9 = addf %8, %7 : f32
|
|
|
|
store %9, %arg4[%i3, %i4] : memref<1024x1024xf32>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CHECK: affine.for %i0 = 0 to 1024 {
|
|
|
|
// CHECK-NEXT: affine.for %i1 = 0 to 1024 {
|
|
|
|
// CHECK-NEXT: affine.for %i2 = 0 to 1024 {
|
|
|
|
// CHECK-NEXT: %0 = load %arg1[%i2, %i1] : memref<1024x1024xf32>
|
|
|
|
// CHECK-NEXT: %1 = load %arg0[%i0, %i2] : memref<1024x1024xf32>
|
|
|
|
// CHECK-NEXT: %2 = mulf %1, %0 : f32
|
|
|
|
// CHECK-NEXT: %3 = load %arg2[%i0, %i1] : memref<1024x1024xf32>
|
|
|
|
// CHECK-NEXT: %4 = addf %3, %2 : f32
|
|
|
|
// CHECK-NEXT: store %4, %arg2[%i0, %i1] : memref<1024x1024xf32>
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: affine.for %i3 = 0 to 1024 {
|
|
|
|
// CHECK-NEXT: affine.for %i4 = 0 to 1024 {
|
|
|
|
// CHECK-NEXT: %5 = load %arg3[%i4, %i3] : memref<1024x1024xf32>
|
|
|
|
// CHECK-NEXT: %6 = load %arg2[%i0, %i4] : memref<1024x1024xf32>
|
|
|
|
// CHECK-NEXT: %7 = mulf %6, %5 : f32
|
|
|
|
// CHECK-NEXT: %8 = load %arg4[%i0, %i3] : memref<1024x1024xf32>
|
|
|
|
// CHECK-NEXT: %9 = addf %8, %7 : f32
|
|
|
|
// CHECK-NEXT: store %9, %arg4[%i0, %i3] : memref<1024x1024xf32>
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
// CHECK-NEXT: }
|
|
|
|
return
|
|
|
|
}
|