• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// RUN: mlir-opt -allow-unregistered-dialect %s -pass-pipeline="func(sccp)" -split-input-file | FileCheck %s
2
3/// Check that a constant is properly propagated when only one edge is taken.
4
5// CHECK-LABEL: func @simple(
6func @simple(%arg0 : i32) -> i32 {
7  // CHECK: %[[CST:.*]] = constant 1 : i32
8  // CHECK-NOT: scf.if
9  // CHECK: return %[[CST]] : i32
10
11  %cond = constant true
12  %res = scf.if %cond -> (i32) {
13    %1 = constant 1 : i32
14    scf.yield %1 : i32
15  } else {
16    scf.yield %arg0 : i32
17  }
18  return %res : i32
19}
20
21/// Check that a constant is properly propagated when both edges produce the
22/// same value.
23
24// CHECK-LABEL: func @simple_both_same(
25func @simple_both_same(%cond : i1) -> i32 {
26  // CHECK: %[[CST:.*]] = constant 1 : i32
27  // CHECK-NOT: scf.if
28  // CHECK: return %[[CST]] : i32
29
30  %res = scf.if %cond -> (i32) {
31    %1 = constant 1 : i32
32    scf.yield %1 : i32
33  } else {
34    %2 = constant 1 : i32
35    scf.yield %2 : i32
36  }
37  return %res : i32
38}
39
40/// Check that the arguments go to overdefined if the branch cannot detect when
41/// a specific successor is taken.
42
43// CHECK-LABEL: func @overdefined_unknown_condition(
44func @overdefined_unknown_condition(%cond : i1, %arg0 : i32) -> i32 {
45  // CHECK: %[[RES:.*]] = scf.if
46  // CHECK: return %[[RES]] : i32
47
48  %res = scf.if %cond -> (i32) {
49    %1 = constant 1 : i32
50    scf.yield %1 : i32
51  } else {
52    scf.yield %arg0 : i32
53  }
54  return %res : i32
55}
56
57/// Check that the arguments go to overdefined if there are conflicting
58/// constants.
59
60// CHECK-LABEL: func @overdefined_different_constants(
61func @overdefined_different_constants(%cond : i1) -> i32 {
62  // CHECK: %[[RES:.*]] = scf.if
63  // CHECK: return %[[RES]] : i32
64
65  %res = scf.if %cond -> (i32) {
66    %1 = constant 1 : i32
67    scf.yield %1 : i32
68  } else {
69    %2 = constant 2 : i32
70    scf.yield %2 : i32
71  }
72  return %res : i32
73}
74
75/// Check that arguments are properly merged across loop-like control flow.
76
77// CHECK-LABEL: func @simple_loop(
78func @simple_loop(%arg0 : index, %arg1 : index, %arg2 : index) -> i32 {
79  // CHECK: %[[CST:.*]] = constant 0 : i32
80  // CHECK-NOT: scf.for
81  // CHECK: return %[[CST]] : i32
82
83  %s0 = constant 0 : i32
84  %result = scf.for %i0 = %arg0 to %arg1 step %arg2 iter_args(%si = %s0) -> (i32) {
85    %sn = addi %si, %si : i32
86    scf.yield %sn : i32
87  }
88  return %result : i32
89}
90
91/// Check that arguments go to overdefined when loop backedges produce a
92/// conflicting value.
93
94// CHECK-LABEL: func @loop_overdefined(
95func @loop_overdefined(%arg0 : index, %arg1 : index, %arg2 : index) -> i32 {
96  // CHECK: %[[RES:.*]] = scf.for
97  // CHECK: return %[[RES]] : i32
98
99  %s0 = constant 1 : i32
100  %result = scf.for %i0 = %arg0 to %arg1 step %arg2 iter_args(%si = %s0) -> (i32) {
101    %sn = addi %si, %si : i32
102    scf.yield %sn : i32
103  }
104  return %result : i32
105}
106
107/// Test that we can properly propagate within inner control, and in situations
108/// where the executable edges within the CFG are sensitive to the current state
109/// of the analysis.
110
111// CHECK-LABEL: func @loop_inner_control_flow(
112func @loop_inner_control_flow(%arg0 : index, %arg1 : index, %arg2 : index) -> i32 {
113  // CHECK: %[[CST:.*]] = constant 1 : i32
114  // CHECK-NOT: scf.for
115  // CHECK-NOT: scf.if
116  // CHECK: return %[[CST]] : i32
117
118  %cst_1 = constant 1 : i32
119  %result = scf.for %i0 = %arg0 to %arg1 step %arg2 iter_args(%si = %cst_1) -> (i32) {
120    %cst_20 = constant 20 : i32
121    %cond = cmpi "ult", %si, %cst_20 : i32
122    %inner_res = scf.if %cond -> (i32) {
123      %1 = constant 1 : i32
124      scf.yield %1 : i32
125    } else {
126      %si_inc = addi %si, %cst_1 : i32
127      scf.yield %si_inc : i32
128    }
129    scf.yield %inner_res : i32
130  }
131  return %result : i32
132}
133