xref: /llvm-project/mlir/test/Dialect/Transform/test-loop-transforms.mlir (revision 2798b72ae7e5caad793169b77cbac47fe2362d0f)
1// RUN: mlir-opt %s --transform-interpreter --split-input-file \
2// RUN:     --verify-diagnostics | FileCheck %s
3
4// UNSUPPORTED: target=aarch64-pc-windows-msvc
5
6// CHECK-LABEL: func @test_loop_invariant_subset_hoisting(
7//  CHECK-SAME:     %[[arg:.*]]: tensor<?xf32>
8func.func @test_loop_invariant_subset_hoisting(%arg: tensor<?xf32>) -> tensor<?xf32> {
9  %lb = "test.foo"() : () -> (index)
10  %ub = "test.foo"() : () -> (index)
11  %step = "test.foo"() : () -> (index)
12  // CHECK: %[[extract:.*]] = tensor.extract_slice %[[arg]]
13  // CHECK: %[[for:.*]]:2 = scf.for {{.*}} iter_args(%[[t:.*]] = %[[arg]], %[[hoisted:.*]] = %[[extract]])
14  // expected-remark @below{{new loop op}}
15  %0 = scf.for %iv = %lb to %ub step %step iter_args(%t = %arg) -> (tensor<?xf32>) {
16    %1 = tensor.extract_slice %t[0][5][1] : tensor<?xf32> to tensor<5xf32>
17    // CHECK: %[[foo:.*]] = "test.foo"(%[[hoisted]])
18    %2 = "test.foo"(%1) : (tensor<5xf32>) -> (tensor<5xf32>)
19    // Obfuscate the IR by inserting at offset %sub instead of 0; both of them
20    // have the same value.
21    %3 = tensor.insert_slice %2 into %t[0][5][1] : tensor<5xf32> into tensor<?xf32>
22    // CHECK: scf.yield %[[t]], %[[foo]]
23    scf.yield %3 : tensor<?xf32>
24  }
25  // CHECK: %[[insert:.*]] = tensor.insert_slice %[[for]]#1 into %[[for]]#0
26  // CHECK: return %[[insert]]
27  return %0 : tensor<?xf32>
28}
29
30module attributes {transform.with_named_sequence} {
31  transform.named_sequence @__transform_main(%arg0: !transform.any_op {transform.readonly}) {
32    %0 = transform.structured.match ops{["scf.for"]} in %arg0 : (!transform.any_op) -> !transform.any_op
33    %1 = transform.structured.match ops{["tensor.extract_slice"]} in %arg0 : (!transform.any_op) -> !transform.any_op
34    %2 = transform.structured.match ops{["tensor.insert_slice"]} in %arg0 : (!transform.any_op) -> !transform.any_op
35
36    transform.loop.hoist_loop_invariant_subsets %0 : !transform.any_op
37    // Make sure that the handles are still valid (and were updated in case of
38    // the loop).
39
40    %p = transform.num_associations %0 : (!transform.any_op) -> !transform.param<i64>
41    // expected-remark @below{{1}}
42    transform.debug.emit_param_as_remark %p : !transform.param<i64>
43    transform.debug.emit_remark_at %0, "new loop op" : !transform.any_op
44    %p2 = transform.num_associations %1 : (!transform.any_op) -> !transform.param<i64>
45    // expected-remark @below{{1}}
46    transform.debug.emit_param_as_remark %p2 : !transform.param<i64>
47    %p3 = transform.num_associations %2 : (!transform.any_op) -> !transform.param<i64>
48    // expected-remark @below{{1}}
49    transform.debug.emit_param_as_remark %p3 : !transform.param<i64>
50
51    transform.yield
52  }
53}
54
55// -----
56
57// Checks that transform ops from LoopExtensionOps and SCFTransformOps can be
58// used together.
59
60// CHECK-LABEL: func @test_mixed_loop_extension_scf_transform(
61func.func @test_mixed_loop_extension_scf_transform(%arg: tensor<?xf32>) -> tensor<?xf32> {
62  %lb = "test.foo"() : () -> (index)
63  %ub = "test.foo"() : () -> (index)
64  %step = "test.foo"() : () -> (index)
65  // CHECK: scf.for
66  // CHECK: scf.for
67  %0 = scf.for %iv = %lb to %ub step %step iter_args(%t = %arg) -> (tensor<?xf32>) {
68    %1 = "test.foo"(%t) : (tensor<?xf32>) -> (tensor<?xf32>)
69    scf.yield %1 : tensor<?xf32>
70  }
71  return %0 : tensor<?xf32>
72}
73
74module attributes {transform.with_named_sequence} {
75  transform.named_sequence @__transform_main(%arg0: !transform.any_op {transform.readonly}) {
76    %0 = transform.structured.match ops{["scf.for"]} in %arg0 : (!transform.any_op) -> !transform.any_op
77    transform.loop.hoist_loop_invariant_subsets %0 : !transform.any_op
78    transform.loop.unroll %0 { factor = 4 } : !transform.any_op
79    transform.yield
80  }
81}
82