xref: /llvm-project/mlir/test/Transforms/test-canonicalize.mlir (revision a506279e5c5d5668e66b0749c26a93d8d373931a)
1// RUN: mlir-opt %s -pass-pipeline='builtin.module(func.func(canonicalize))' | FileCheck %s
2// RUN: mlir-opt %s -pass-pipeline='builtin.module(func.func(canonicalize{region-simplify=disabled}))' | FileCheck %s --check-prefixes=CHECK,NO-RS
3
4// CHECK-LABEL: func @remove_op_with_inner_ops_pattern
5func.func @remove_op_with_inner_ops_pattern() {
6  // CHECK-NEXT: return
7  "test.op_with_region_pattern"() ({
8    "test.op_with_region_terminator"() : () -> ()
9  }) : () -> ()
10  return
11}
12
13// CHECK-LABEL: func @remove_op_with_inner_ops_fold_no_side_effect
14func.func @remove_op_with_inner_ops_fold_no_side_effect() {
15  // CHECK-NEXT: return
16  "test.op_with_region_fold_no_side_effect"() ({
17    "test.op_with_region_terminator"() : () -> ()
18  }) : () -> ()
19  return
20}
21
22// CHECK-LABEL: func @remove_op_with_inner_ops_fold
23// CHECK-SAME: (%[[ARG_0:[a-z0-9]*]]: i32)
24func.func @remove_op_with_inner_ops_fold(%arg0 : i32) -> (i32) {
25  // CHECK-NEXT: return %[[ARG_0]]
26  %0 = "test.op_with_region_fold"(%arg0) ({
27    "test.op_with_region_terminator"() : () -> ()
28  }) : (i32) -> (i32)
29  return %0 : i32
30}
31
32// CHECK-LABEL: func @remove_op_with_variadic_results_and_folder
33// CHECK-SAME: (%[[ARG_0:[a-z0-9]*]]: i32, %[[ARG_1:[a-z0-9]*]]: i32)
34func.func @remove_op_with_variadic_results_and_folder(%arg0 : i32, %arg1 : i32) -> (i32, i32) {
35  // CHECK-NEXT: return %[[ARG_0]], %[[ARG_1]]
36  %0, %1 = "test.op_with_variadic_results_and_folder"(%arg0, %arg1) : (i32, i32) -> (i32, i32)
37  return %0, %1 : i32, i32
38}
39
40// CHECK-LABEL: func @test_commutative_multi
41// CHECK-SAME: (%[[ARG_0:[a-z0-9]*]]: i32, %[[ARG_1:[a-z0-9]*]]: i32)
42func.func @test_commutative_multi(%arg0: i32, %arg1: i32) -> (i32, i32) {
43  // CHECK-DAG: %[[C42:.*]] = arith.constant 42 : i32
44  %c42_i32 = arith.constant 42 : i32
45  // CHECK-DAG: %[[C43:.*]] = arith.constant 43 : i32
46  %c43_i32 = arith.constant 43 : i32
47  // CHECK-NEXT: %[[O0:.*]] = "test.op_commutative"(%[[ARG_0]], %[[ARG_1]], %[[C42]], %[[C43]]) : (i32, i32, i32, i32) -> i32
48  %y = "test.op_commutative"(%c42_i32, %arg0, %arg1, %c43_i32) : (i32, i32, i32, i32) -> i32
49
50  // CHECK-NEXT: %[[O1:.*]] = "test.op_commutative"(%[[ARG_0]], %[[ARG_1]], %[[C42]], %[[C43]]) : (i32, i32, i32, i32) -> i32
51  %z = "test.op_commutative"(%arg0, %c42_i32, %c43_i32, %arg1): (i32, i32, i32, i32) -> i32
52  // CHECK-NEXT: return %[[O0]], %[[O1]]
53  return %y, %z: i32, i32
54}
55
56
57// CHECK-LABEL: func @test_commutative_multi_cst
58func.func @test_commutative_multi_cst(%arg0: i32, %arg1: i32) -> (i32, i32) {
59  // CHECK-NEXT: %c42_i32 = arith.constant 42 : i32
60  %c42_i32 = arith.constant 42 : i32
61  %c42_i32_2 = arith.constant 42 : i32
62  // CHECK-NEXT: %[[O0:.*]] = "test.op_commutative"(%arg0, %arg1, %c42_i32, %c42_i32) : (i32, i32, i32, i32) -> i32
63  %y = "test.op_commutative"(%c42_i32, %arg0, %arg1, %c42_i32_2) : (i32, i32, i32, i32) -> i32
64
65  %c42_i32_3 = arith.constant 42 : i32
66
67  // CHECK-NEXT: %[[O1:.*]] = "test.op_commutative"(%arg0, %arg1, %c42_i32, %c42_i32) : (i32, i32, i32, i32) -> i32
68  %z = "test.op_commutative"(%arg0, %c42_i32_3, %c42_i32_2, %arg1): (i32, i32, i32, i32) -> i32
69  // CHECK-NEXT: return %[[O0]], %[[O1]]
70  return %y, %z: i32, i32
71}
72
73// CHECK-LABEL: test_dialect_canonicalizer
74func.func @test_dialect_canonicalizer() -> (i32) {
75  %0 = "test.dialect_canonicalizable"() : () -> (i32)
76  // CHECK: %[[CST:.*]] = arith.constant 42 : i32
77  // CHECK: return %[[CST]]
78  return %0 : i32
79}
80
81// Check that the option to control region simplification actually works
82// CHECK-LABEL: test_region_simplify
83func.func @test_region_simplify() {
84  // CHECK-NEXT:   return
85  // NO-RS-NEXT: ^bb1
86  // NO-RS-NEXT:   return
87  // CHECK-NEXT: }
88  return
89^bb1:
90  return
91}
92