xref: /llvm-project/mlir/test/Transforms/test-operation-folder.mlir (revision 09dfc5713d7e2342bea4c8447d1ed76c85eb8225)
1// RUN: mlir-opt -test-greedy-patterns='top-down=false' %s | FileCheck %s
2// RUN: mlir-opt -test-greedy-patterns='top-down=true' %s | FileCheck %s
3// RUN: mlir-opt -test-greedy-patterns='cse-constants=false' %s | FileCheck %s --check-prefix=NOCSE
4// RUN: mlir-opt -test-greedy-patterns='fold=false' %s | FileCheck %s --check-prefix=NOFOLD
5
6func.func @foo() -> i32 {
7  %c42 = arith.constant 42 : i32
8
9  // The new operation should be present in the output and contain an attribute
10  // with value "42" that results from folding.
11
12  // CHECK: "test.op_in_place_fold"(%{{.*}}) <{attr = 42 : i32}
13  %0 = "test.op_in_place_fold_anchor"(%c42) : (i32) -> (i32)
14  return %0 : i32
15}
16
17func.func @test_fold_before_previously_folded_op() -> (i32, i32) {
18  // When folding two constants will be generated and uniqued. Check that the
19  // uniqued constant properly dominates both uses.
20  // CHECK: %[[CST:.+]] = arith.constant true
21  // CHECK-NEXT: "test.cast"(%[[CST]]) : (i1) -> i32
22  // CHECK-NEXT: "test.cast"(%[[CST]]) : (i1) -> i32
23
24  %0 = "test.cast"() {test_fold_before_previously_folded_op} : () -> (i32)
25  %1 = "test.cast"() {test_fold_before_previously_folded_op} : () -> (i32)
26  return %0, %1 : i32, i32
27}
28
29func.func @test_dont_reorder_constants() -> (i32, i32, i32) {
30  // Test that we don't reorder existing constants during folding if it isn't
31  // necessary.
32  // CHECK: %[[CST:.+]] = arith.constant 1
33  // CHECK-NEXT: %[[CST:.+]] = arith.constant 2
34  // CHECK-NEXT: %[[CST:.+]] = arith.constant 3
35  %0 = arith.constant 1 : i32
36  %1 = arith.constant 2 : i32
37  %2 = arith.constant 3 : i32
38  return %0, %1, %2 : i32, i32, i32
39}
40
41// CHECK-LABEL: test_fold_nofold_nocse
42// NOCSE-LABEL: test_fold_nofold_nocse
43// NOFOLD-LABEL: test_fold_nofold_nocse
44func.func @test_fold_nofold_nocse() -> (i32, i32, i32, i32, i32, i32) {
45  // Test either not folding or deduping constants.
46
47  // Testing folding. There should be only 4 constants here.
48  // CHECK-NOT: arith.constant
49  // CHECK-DAG: %[[CST:.+]] = arith.constant 0
50  // CHECK-DAG: %[[CST:.+]] = arith.constant 1
51  // CHECK-DAG: %[[CST:.+]] = arith.constant 2
52  // CHECK-DAG: %[[CST:.+]] = arith.constant 3
53  // CHECK-NOT: arith.constant
54  // CHECK-NEXT: return
55
56  // Testing not-CSE'ing. In this case we have the 3 original constants and 3
57  // produced by folding.
58  // NOCSE-DAG: arith.constant 0 : i32
59  // NOCSE-DAG: arith.constant 1 : i32
60  // NOCSE-DAG: arith.constant 2 : i32
61  // NOCSE-DAG: arith.constant 1 : i32
62  // NOCSE-DAG: arith.constant 2 : i32
63  // NOCSE-DAG: arith.constant 3 : i32
64  // NOCSE-NEXT: return
65
66  // Testing not folding. In this case we just have the original constants.
67  // NOFOLD-DAG: %[[CST:.+]] = arith.constant 0
68  // NOFOLD-DAG: %[[CST:.+]] = arith.constant 1
69  // NOFOLD-DAG: %[[CST:.+]] = arith.constant 2
70  // NOFOLD: arith.addi
71  // NOFOLD: arith.addi
72  // NOFOLD: arith.addi
73
74  %c0 = arith.constant 0 : i32
75  %c1 = arith.constant 1 : i32
76  %c2 = arith.constant 2 : i32
77  %0 = arith.addi %c0, %c1 : i32
78  %1 = arith.addi %0, %c1 : i32
79  %2 = arith.addi %c2, %c1 : i32
80  return %0, %1, %2, %c0, %c1, %c2 : i32, i32, i32, i32, i32, i32
81}
82
83