187 lines · plain
1// RUN: mlir-opt -int-range-optimizations %s | FileCheck %s2 3// CHECK-LABEL: func @constant4// CHECK: %[[cst:.*]] = "test.constant"() <{value = 3 : index}5// CHECK: return %[[cst]]6func.func @constant() -> index {7 %0 = test.with_bounds { umin = 3 : index, umax = 3 : index,8 smin = 3 : index, smax = 3 : index} : index9 func.return %0 : index10}11 12// CHECK-LABEL: func @increment13// CHECK: %[[cst:.*]] = "test.constant"() <{value = 4 : index}14// CHECK: return %[[cst]]15func.func @increment() -> index {16 %0 = test.with_bounds { umin = 3 : index, umax = 3 : index, smin = 0 : index, smax = 0x7fffffffffffffff : index } : index17 %1 = test.increment %0 : index18 func.return %1 : index19}20 21// CHECK-LABEL: func @maybe_increment22// CHECK: test.reflect_bounds {smax = 4 : index, smin = 3 : index, umax = 4 : index, umin = 3 : index}23func.func @maybe_increment(%arg0 : i1) -> index {24 %0 = test.with_bounds { umin = 3 : index, umax = 3 : index,25 smin = 3 : index, smax = 3 : index} : index26 %1 = scf.if %arg0 -> index {27 scf.yield %0 : index28 } else {29 %2 = test.increment %0 : index30 scf.yield %2 : index31 }32 %3 = test.reflect_bounds %1 : index33 func.return %3 : index34}35 36// CHECK-LABEL: func @maybe_increment_br37// CHECK: test.reflect_bounds {smax = 4 : index, smin = 3 : index, umax = 4 : index, umin = 3 : index}38func.func @maybe_increment_br(%arg0 : i1) -> index {39 %0 = test.with_bounds { umin = 3 : index, umax = 3 : index,40 smin = 3 : index, smax = 3 : index} : index41 cf.cond_br %arg0, ^bb0, ^bb142^bb0:43 %1 = test.increment %0 : index44 cf.br ^bb2(%1 : index)45^bb1:46 cf.br ^bb2(%0 : index)47^bb2(%2 : index):48 %3 = test.reflect_bounds %2 : index49 func.return %3 : index50}51 52// CHECK-LABEL: func @for_bounds53// CHECK: test.reflect_bounds {smax = 1 : index, smin = 0 : index, umax = 1 : index, umin = 0 : index}54func.func @for_bounds() -> index {55 %c0 = test.with_bounds { umin = 0 : index, umax = 0 : index,56 smin = 0 : index, smax = 0 : index} : index57 %c1 = test.with_bounds { umin = 1 : index, umax = 1 : index,58 smin = 1 : index, smax = 1 : index} : index59 %c2 = test.with_bounds { umin = 2 : index, umax = 2 : index,60 smin = 2 : index, smax = 2 : index} : index61 62 %0 = scf.for %arg0 = %c0 to %c2 step %c1 iter_args(%arg2 = %c0) -> index {63 scf.yield %arg0 : index64 }65 %1 = test.reflect_bounds %0 : index66 func.return %1 : index67}68 69// CHECK-LABEL: func @no_analysis_of_loop_variants70// CHECK: test.reflect_bounds {smax = 9223372036854775807 : index, smin = -9223372036854775808 : index, umax = -1 : index, umin = 0 : index}71func.func @no_analysis_of_loop_variants() -> index {72 %c0 = test.with_bounds { umin = 0 : index, umax = 0 : index,73 smin = 0 : index, smax = 0 : index} : index74 %c1 = test.with_bounds { umin = 1 : index, umax = 1 : index,75 smin = 1 : index, smax = 1 : index} : index76 %c2 = test.with_bounds { umin = 2 : index, umax = 2 : index,77 smin = 2 : index, smax = 2 : index} : index78 79 %0 = scf.for %arg0 = %c0 to %c2 step %c1 iter_args(%arg2 = %c0) -> index {80 %1 = test.increment %arg2 : index81 scf.yield %1 : index82 }83 %2 = test.reflect_bounds %0 : index84 func.return %2 : index85}86 87// CHECK-LABEL: func @region_args88// CHECK: test.reflect_bounds {smax = 4 : index, smin = 3 : index, umax = 4 : index, umin = 3 : index}89func.func @region_args() {90 test.with_bounds_region { umin = 3 : index, umax = 4 : index,91 smin = 3 : index, smax = 4 : index } %arg0 : index {92 %0 = test.reflect_bounds %arg0 : index93 }94 func.return95}96 97// CHECK-LABEL: func @func_args_unbound98// CHECK: test.reflect_bounds {smax = 9223372036854775807 : index, smin = -9223372036854775808 : index, umax = -1 : index, umin = 0 : index}99func.func @func_args_unbound(%arg0 : index) -> index {100 %0 = test.reflect_bounds %arg0 : index101 func.return %0 : index102}103 104// CHECK-LABEL: func @propagate_across_while_loop_false()105func.func @propagate_across_while_loop_false() -> index {106 // CHECK: %[[C1:.*]] = "test.constant"() <{value = 1107 %0 = test.with_bounds { umin = 0 : index, umax = 0 : index,108 smin = 0 : index, smax = 0 : index } : index109 %1 = scf.while : () -> index {110 %false = arith.constant false111 scf.condition(%false) %0 : index112 } do {113 ^bb0(%i1: index):114 scf.yield115 }116 // CHECK: return %[[C1]]117 %2 = test.increment %1 : index118 return %2 : index119}120 121// CHECK-LABEL: func @propagate_across_while_loop122func.func @propagate_across_while_loop(%arg0 : i1) -> index {123 // CHECK: %[[C1:.*]] = "test.constant"() <{value = 1124 %0 = test.with_bounds { umin = 0 : index, umax = 0 : index,125 smin = 0 : index, smax = 0 : index } : index126 %1 = scf.while : () -> index {127 scf.condition(%arg0) %0 : index128 } do {129 ^bb0(%i1: index):130 scf.yield131 }132 // CHECK: return %[[C1]]133 %2 = test.increment %1 : index134 return %2 : index135}136 137// CHECK-LABEL: func @dont_propagate_across_infinite_loop()138func.func @dont_propagate_across_infinite_loop() -> index {139 // CHECK: %[[C0:.*]] = "test.constant"() <{value = 0140 %0 = test.with_bounds { umin = 0 : index, umax = 0 : index,141 smin = 0 : index, smax = 0 : index } : index142 // CHECK: %[[loopRes:.*]] = scf.while143 %1 = scf.while : () -> index {144 %true = arith.constant true145 // CHECK: scf.condition(%{{.*}}) %[[C0]]146 scf.condition(%true) %0 : index147 } do {148 ^bb0(%i1: index):149 scf.yield150 }151 // CHECK: %[[ret:.*]] = test.reflect_bounds %[[loopRes]] : index152 %2 = test.reflect_bounds %1 : index153 // CHECK: return %[[ret]]154 return %2 : index155}156 157// CHECK-LABEL: @propagate_from_block_to_iterarg158func.func @propagate_from_block_to_iterarg(%arg0: index, %arg1: i1) {159 %c0 = arith.constant 0 : index160 %c1 = arith.constant 1 : index161 %0 = scf.if %arg1 -> (index) {162 %1 = scf.if %arg1 -> (index) {163 scf.yield %arg0 : index164 } else {165 scf.yield %arg0 : index166 }167 scf.yield %1 : index168 } else {169 scf.yield %c1 : index170 }171 scf.for %arg2 = %c0 to %arg0 step %c1 {172 scf.if %arg1 {173 %1 = arith.subi %0, %c1 : index174 %2 = arith.muli %0, %1 : index175 %3 = arith.addi %2, %c1 : index176 scf.for %arg3 = %c0 to %3 step %c1 {177 %4 = arith.cmpi uge, %arg3, %c1 : index178 // CHECK-NOT: scf.if %false179 scf.if %4 {180 "test.foo"() : () -> ()181 }182 }183 }184 }185 return186}187