|
| 1 | +// RUN: mlir-opt %s -test-wrap-scf-while-loop-in-zero-trip-check -split-input-file | FileCheck %s |
| 2 | +// RUN: mlir-opt %s -test-wrap-scf-while-loop-in-zero-trip-check='force-create-check=true' -split-input-file | FileCheck %s --check-prefix FORCE-CREATE-CHECK |
| 3 | + |
| 4 | +func.func @wrap_while_loop_in_zero_trip_check(%bound : i32) -> i32 { |
| 5 | + %cst0 = arith.constant 0 : i32 |
| 6 | + %cst5 = arith.constant 5 : i32 |
| 7 | + %res:2 = scf.while (%iter = %cst0) : (i32) -> (i32, i32) { |
| 8 | + %cond = arith.cmpi slt, %iter, %bound : i32 |
| 9 | + %inv = arith.addi %bound, %cst5 : i32 |
| 10 | + scf.condition(%cond) %iter, %inv : i32, i32 |
| 11 | + } do { |
| 12 | + ^bb0(%arg1: i32, %arg2: i32): |
| 13 | + %next = arith.addi %arg1, %arg2 : i32 |
| 14 | + scf.yield %next : i32 |
| 15 | + } |
| 16 | + return %res#0 : i32 |
| 17 | +} |
| 18 | + |
| 19 | +// CHECK-LABEL: func.func @wrap_while_loop_in_zero_trip_check( |
| 20 | +// CHECK-SAME: %[[BOUND:.*]]: i32) -> i32 { |
| 21 | +// CHECK-DAG: %[[C0:.*]] = arith.constant 0 : i32 |
| 22 | +// CHECK-DAG: %[[C5:.*]] = arith.constant 5 : i32 |
| 23 | +// CHECK-DAG: %[[PRE_COND:.*]] = arith.cmpi slt, %[[C0]], %[[BOUND]] : i32 |
| 24 | +// CHECK-DAG: %[[PRE_INV:.*]] = arith.addi %[[BOUND]], %[[C5]] : i32 |
| 25 | +// CHECK: %[[IF:.*]]:2 = scf.if %[[PRE_COND]] -> (i32, i32) { |
| 26 | +// CHECK: %[[WHILE:.*]]:2 = scf.while ( |
| 27 | +// CHECK-SAME: %[[ARG1:.*]] = %[[C0]], %[[ARG2:.*]] = %[[PRE_INV]] |
| 28 | +// CHECK-SAME: ) : (i32, i32) -> (i32, i32) { |
| 29 | +// CHECK: %[[NEXT:.*]] = arith.addi %[[ARG1]], %[[ARG2]] : i32 |
| 30 | +// CHECK: %[[COND:.*]] = arith.cmpi slt, %[[NEXT]], %[[BOUND]] : i32 |
| 31 | +// CHECK: %[[INV:.*]] = arith.addi %[[BOUND]], %[[C5]] : i32 |
| 32 | +// CHECK: scf.condition(%[[COND]]) %[[NEXT]], %[[INV]] : i32, i32 |
| 33 | +// CHECK: } do { |
| 34 | +// CHECK: ^bb0(%[[ARG3:.*]]: i32, %[[ARG4:.*]]: i32): |
| 35 | +// CHECK: scf.yield %[[ARG3]], %[[ARG4]] : i32, i32 |
| 36 | +// CHECK: } |
| 37 | +// CHECK: scf.yield %[[WHILE]]#0, %[[WHILE]]#1 : i32, i32 |
| 38 | +// CHECK: } else { |
| 39 | +// CHECK: scf.yield %[[C0]], %[[PRE_INV]] : i32, i32 |
| 40 | +// CHECK: } |
| 41 | +// CHECK: return %[[IF]]#0 : i32 |
| 42 | + |
| 43 | +// ----- |
| 44 | + |
| 45 | +func.func @wrap_while_loop_with_minimal_before_block(%bound : i32) -> i32 { |
| 46 | + %cst0 = arith.constant 0 : i32 |
| 47 | + %true = arith.constant true |
| 48 | + %cst5 = arith.constant 5 : i32 |
| 49 | + %res = scf.while (%iter = %cst0, %arg0 = %true) : (i32, i1) -> i32 { |
| 50 | + scf.condition(%arg0) %iter : i32 |
| 51 | + } do { |
| 52 | + ^bb0(%arg1: i32): |
| 53 | + %next = arith.addi %arg1, %cst5 : i32 |
| 54 | + %cond = arith.cmpi slt, %next, %bound : i32 |
| 55 | + scf.yield %next, %cond : i32, i1 |
| 56 | + } |
| 57 | + return %res : i32 |
| 58 | +} |
| 59 | + |
| 60 | +// CHECK-LABEL: func.func @wrap_while_loop_with_minimal_before_block( |
| 61 | +// CHECK-SAME: %[[BOUND:.*]]: i32) -> i32 { |
| 62 | +// CHECK-DAG: %[[C0:.*]] = arith.constant 0 : i32 |
| 63 | +// CHECK-DAG: %[[TRUE:.*]] = arith.constant true |
| 64 | +// CHECK-DAG: %[[C5:.*]] = arith.constant 5 : i32 |
| 65 | +// CHECK: %[[IF:.*]] = scf.if %[[TRUE]] -> (i32) { |
| 66 | +// CHECK: %[[WHILE:.*]] = scf.while (%[[ARG1:.*]] = %[[C0]]) : (i32) -> i32 { |
| 67 | +// CHECK: %[[NEXT:.*]] = arith.addi %[[ARG1]], %[[C5]] : i32 |
| 68 | +// CHECK: %[[COND:.*]] = arith.cmpi slt, %[[NEXT]], %[[BOUND]] : i32 |
| 69 | +// CHECK: scf.condition(%[[COND]]) %[[NEXT]] : i32 |
| 70 | +// CHECK: } do { |
| 71 | +// CHECK: ^bb0(%[[ARG2:.*]]: i32): |
| 72 | +// CHECK: scf.yield %[[ARG2]] : i32 |
| 73 | +// CHECK: } |
| 74 | +// CHECK: scf.yield %[[WHILE]] : i32 |
| 75 | +// CHECK: } else { |
| 76 | +// CHECK: scf.yield %[[C0]] : i32 |
| 77 | +// CHECK: } |
| 78 | +// CHECK: return %[[IF]] : i32 |
| 79 | + |
| 80 | +// ----- |
| 81 | + |
| 82 | +func.func @wrap_do_while_loop_in_zero_trip_check(%bound : i32) -> i32 { |
| 83 | + %cst0 = arith.constant 0 : i32 |
| 84 | + %cst5 = arith.constant 5 : i32 |
| 85 | + %res = scf.while (%iter = %cst0) : (i32) -> i32 { |
| 86 | + %next = arith.addi %iter, %cst5 : i32 |
| 87 | + %cond = arith.cmpi slt, %next, %bound : i32 |
| 88 | + scf.condition(%cond) %next : i32 |
| 89 | + } do { |
| 90 | + ^bb0(%arg1: i32): |
| 91 | + scf.yield %arg1 : i32 |
| 92 | + } |
| 93 | + return %res : i32 |
| 94 | +} |
| 95 | + |
| 96 | +// CHECK-LABEL: func.func @wrap_do_while_loop_in_zero_trip_check( |
| 97 | +// CHECK-SAME: %[[BOUND:.*]]: i32) -> i32 { |
| 98 | +// CHECK-DAG: %[[C0:.*]] = arith.constant 0 : i32 |
| 99 | +// CHECK-DAG: %[[C5:.*]] = arith.constant 5 : i32 |
| 100 | +// CHECK-NOT: scf.if |
| 101 | +// CHECK: %[[WHILE:.*]] = scf.while (%[[ARG1:.*]] = %[[C0]]) : (i32) -> i32 { |
| 102 | +// CHECK: %[[NEXT:.*]] = arith.addi %[[ARG1]], %[[C5]] : i32 |
| 103 | +// CHECK: %[[COND:.*]] = arith.cmpi slt, %[[NEXT]], %[[BOUND]] : i32 |
| 104 | +// CHECK: scf.condition(%[[COND]]) %[[NEXT]] : i32 |
| 105 | +// CHECK: } do { |
| 106 | +// CHECK: ^bb0(%[[ARG2:.*]]: i32): |
| 107 | +// CHECK: scf.yield %[[ARG2]] : i32 |
| 108 | +// CHECK: } |
| 109 | +// CHECK: return %[[WHILE]] : i32 |
| 110 | + |
| 111 | +// FORCE-CREATE-CHECK-LABEL: func.func @wrap_do_while_loop_in_zero_trip_check( |
| 112 | +// FORCE-CREATE-CHECK-SAME: %[[BOUND:.*]]: i32) -> i32 { |
| 113 | +// FORCE-CREATE-CHECK-DAG: %[[C0:.*]] = arith.constant 0 : i32 |
| 114 | +// FORCE-CREATE-CHECK-DAG: %[[C5:.*]] = arith.constant 5 : i32 |
| 115 | +// FORCE-CREATE-CHECK: %[[PRE_NEXT:.*]] = arith.addi %[[C0]], %[[C5]] : i32 |
| 116 | +// FORCE-CREATE-CHECK: %[[PRE_COND:.*]] = arith.cmpi slt, %[[PRE_NEXT]], %[[BOUND]] : i32 |
| 117 | +// FORCE-CREATE-CHECK: %[[IF:.*]] = scf.if %[[PRE_COND]] -> (i32) { |
| 118 | +// FORCE-CREATE-CHECK: %[[WHILE:.*]] = scf.while (%[[ARG1:.*]] = %[[PRE_NEXT]]) : (i32) -> i32 { |
| 119 | +// FORCE-CREATE-CHECK: %[[NEXT:.*]] = arith.addi %[[ARG1]], %[[C5]] : i32 |
| 120 | +// FORCE-CREATE-CHECK: %[[COND:.*]] = arith.cmpi slt, %[[NEXT]], %[[BOUND]] : i32 |
| 121 | +// FORCE-CREATE-CHECK: scf.condition(%[[COND]]) %[[NEXT]] : i32 |
| 122 | +// FORCE-CREATE-CHECK: } do { |
| 123 | +// FORCE-CREATE-CHECK: ^bb0(%[[ARG2:.*]]: i32): |
| 124 | +// FORCE-CREATE-CHECK: scf.yield %[[ARG2]] : i32 |
| 125 | +// FORCE-CREATE-CHECK: } |
| 126 | +// FORCE-CREATE-CHECK: scf.yield %[[WHILE]] : i32 |
| 127 | +// FORCE-CREATE-CHECK: } else { |
| 128 | +// FORCE-CREATE-CHECK: scf.yield %[[PRE_NEXT]] : i32 |
| 129 | +// FORCE-CREATE-CHECK: } |
| 130 | +// FORCE-CREATE-CHECK: return %[[IF]] : i32 |
0 commit comments