|
1 | 1 | // RUN: mlir-opt --arith-int-range-narrowing="int-bitwidths-supported=1,8,16,24,32" %s | FileCheck %s |
2 | 2 |
|
| 3 | +//===----------------------------------------------------------------------===// |
| 4 | +// Some basic tests |
| 5 | +//===----------------------------------------------------------------------===// |
| 6 | + |
3 | 7 | // Do not truncate negative values |
4 | 8 | // CHECK-LABEL: func @test_addi_neg |
5 | 9 | // CHECK: %[[RES:.*]] = arith.addi %{{.*}}, %{{.*}} : index |
@@ -55,3 +59,141 @@ func.func @test_cmpi() -> i1 { |
55 | 59 | %2 = arith.cmpi slt, %0, %1 : index |
56 | 60 | return %2 : i1 |
57 | 61 | } |
| 62 | + |
| 63 | +//===----------------------------------------------------------------------===// |
| 64 | +// arith.addi |
| 65 | +//===----------------------------------------------------------------------===// |
| 66 | + |
| 67 | +// CHECK-LABEL: func.func @addi_extui_i8 |
| 68 | +// CHECK-SAME: (%[[ARG0:.+]]: i8, %[[ARG1:.+]]: i8) |
| 69 | +// CHECK-NEXT: %[[EXT0:.+]] = arith.extui %[[ARG0]] : i8 to i32 |
| 70 | +// CHECK-NEXT: %[[EXT1:.+]] = arith.extui %[[ARG1]] : i8 to i32 |
| 71 | +// CHECK-NEXT: %[[LHS:.+]] = arith.trunci %[[EXT0]] : i32 to i16 |
| 72 | +// CHECK-NEXT: %[[RHS:.+]] = arith.trunci %[[EXT1]] : i32 to i16 |
| 73 | +// CHECK-NEXT: %[[ADD:.+]] = arith.addi %[[LHS]], %[[RHS]] : i16 |
| 74 | +// CHECK-NEXT: %[[RET:.+]] = arith.extui %[[ADD]] : i16 to i32 |
| 75 | +// CHECK-NEXT: return %[[RET]] : i32 |
| 76 | +func.func @addi_extui_i8(%lhs: i8, %rhs: i8) -> i32 { |
| 77 | + %a = arith.extui %lhs : i8 to i32 |
| 78 | + %b = arith.extui %rhs : i8 to i32 |
| 79 | + %r = arith.addi %a, %b : i32 |
| 80 | + return %r : i32 |
| 81 | +} |
| 82 | + |
| 83 | +// This case should not get optimized because of mixed extensions. |
| 84 | +// |
| 85 | +// CHECK-LABEL: func.func @addi_mixed_ext_i8 |
| 86 | +// CHECK-SAME: (%[[ARG0:.+]]: i8, %[[ARG1:.+]]: i8) |
| 87 | +// CHECK-NEXT: %[[EXT0:.+]] = arith.extsi %[[ARG0]] : i8 to i32 |
| 88 | +// CHECK-NEXT: %[[EXT1:.+]] = arith.extui %[[ARG1]] : i8 to i32 |
| 89 | +// CHECK-NEXT: %[[ADD:.+]] = arith.addi %[[EXT0]], %[[EXT1]] : i32 |
| 90 | +// CHECK-NEXT: return %[[ADD]] : i32 |
| 91 | +func.func @addi_mixed_ext_i8(%lhs: i8, %rhs: i8) -> i32 { |
| 92 | + %a = arith.extsi %lhs : i8 to i32 |
| 93 | + %b = arith.extui %rhs : i8 to i32 |
| 94 | + %r = arith.addi %a, %b : i32 |
| 95 | + return %r : i32 |
| 96 | +} |
| 97 | + |
| 98 | +// This case should not get optimized because we cannot reduce the bitwidth |
| 99 | +// below i16, given the pass options set. |
| 100 | +// |
| 101 | +// CHECK-LABEL: func.func @addi_extsi_i16 |
| 102 | +// CHECK-SAME: (%[[ARG0:.+]]: i8, %[[ARG1:.+]]: i8) |
| 103 | +// CHECK-NEXT: %[[EXT0:.+]] = arith.extsi %[[ARG0]] : i8 to i16 |
| 104 | +// CHECK-NEXT: %[[EXT1:.+]] = arith.extsi %[[ARG1]] : i8 to i16 |
| 105 | +// CHECK-NEXT: %[[ADD:.+]] = arith.addi %[[EXT0]], %[[EXT1]] : i16 |
| 106 | +// CHECK-NEXT: return %[[ADD]] : i16 |
| 107 | +func.func @addi_extsi_i16(%lhs: i8, %rhs: i8) -> i16 { |
| 108 | + %a = arith.extsi %lhs : i8 to i16 |
| 109 | + %b = arith.extsi %rhs : i8 to i16 |
| 110 | + %r = arith.addi %a, %b : i16 |
| 111 | + return %r : i16 |
| 112 | +} |
| 113 | + |
| 114 | +//===----------------------------------------------------------------------===// |
| 115 | +// arith.subi |
| 116 | +//===----------------------------------------------------------------------===// |
| 117 | + |
| 118 | +// This patterns should only apply to `arith.subi` ops with sign-extended |
| 119 | +// arguments. |
| 120 | +// |
| 121 | +// CHECK-LABEL: func.func @subi_extui_i8 |
| 122 | +// CHECK-SAME: (%[[ARG0:.+]]: i8, %[[ARG1:.+]]: i8) |
| 123 | +// CHECK-NEXT: %[[EXT0:.+]] = arith.extui %[[ARG0]] : i8 to i32 |
| 124 | +// CHECK-NEXT: %[[EXT1:.+]] = arith.extui %[[ARG1]] : i8 to i32 |
| 125 | +// CHECK-NEXT: %[[SUB:.+]] = arith.subi %[[EXT0]], %[[EXT1]] : i32 |
| 126 | +// CHECK-NEXT: return %[[SUB]] : i32 |
| 127 | +func.func @subi_extui_i8(%lhs: i8, %rhs: i8) -> i32 { |
| 128 | + %a = arith.extui %lhs : i8 to i32 |
| 129 | + %b = arith.extui %rhs : i8 to i32 |
| 130 | + %r = arith.subi %a, %b : i32 |
| 131 | + return %r : i32 |
| 132 | +} |
| 133 | + |
| 134 | +// This case should not get optimized because of mixed extensions. |
| 135 | +// |
| 136 | +// CHECK-LABEL: func.func @subi_mixed_ext_i8 |
| 137 | +// CHECK-SAME: (%[[ARG0:.+]]: i8, %[[ARG1:.+]]: i8) |
| 138 | +// CHECK-NEXT: %[[EXT0:.+]] = arith.extsi %[[ARG0]] : i8 to i32 |
| 139 | +// CHECK-NEXT: %[[EXT1:.+]] = arith.extui %[[ARG1]] : i8 to i32 |
| 140 | +// CHECK-NEXT: %[[ADD:.+]] = arith.subi %[[EXT0]], %[[EXT1]] : i32 |
| 141 | +// CHECK-NEXT: return %[[ADD]] : i32 |
| 142 | +func.func @subi_mixed_ext_i8(%lhs: i8, %rhs: i8) -> i32 { |
| 143 | + %a = arith.extsi %lhs : i8 to i32 |
| 144 | + %b = arith.extui %rhs : i8 to i32 |
| 145 | + %r = arith.subi %a, %b : i32 |
| 146 | + return %r : i32 |
| 147 | +} |
| 148 | + |
| 149 | +//===----------------------------------------------------------------------===// |
| 150 | +// arith.muli |
| 151 | +//===----------------------------------------------------------------------===// |
| 152 | + |
| 153 | +// TODO: This should be optimized into i16 |
| 154 | +// CHECK-LABEL: func.func @muli_extui_i8 |
| 155 | +// CHECK-SAME: (%[[ARG0:.+]]: i8, %[[ARG1:.+]]: i8) |
| 156 | +// CHECK-NEXT: %[[EXT0:.+]] = arith.extui %[[ARG0]] : i8 to i32 |
| 157 | +// CHECK-NEXT: %[[EXT1:.+]] = arith.extui %[[ARG1]] : i8 to i32 |
| 158 | +// CHECK-NEXT: %[[LHS:.+]] = arith.trunci %[[EXT0]] : i32 to i24 |
| 159 | +// CHECK-NEXT: %[[RHS:.+]] = arith.trunci %[[EXT1]] : i32 to i24 |
| 160 | +// CHECK-NEXT: %[[MUL:.+]] = arith.muli %[[LHS]], %[[RHS]] : i24 |
| 161 | +// CHECK-NEXT: %[[RET:.+]] = arith.extui %[[MUL]] : i24 to i32 |
| 162 | +// CHECK-NEXT: return %[[RET]] : i32 |
| 163 | +func.func @muli_extui_i8(%lhs: i8, %rhs: i8) -> i32 { |
| 164 | + %a = arith.extui %lhs : i8 to i32 |
| 165 | + %b = arith.extui %rhs : i8 to i32 |
| 166 | + %r = arith.muli %a, %b : i32 |
| 167 | + return %r : i32 |
| 168 | +} |
| 169 | + |
| 170 | +// We do not expect this case to be optimized because given n-bit operands, |
| 171 | +// arith.muli produces 2n bits of result. |
| 172 | +// |
| 173 | +// CHECK-LABEL: func.func @muli_extsi_i32 |
| 174 | +// CHECK-SAME: (%[[ARG0:.+]]: i16, %[[ARG1:.+]]: i16) |
| 175 | +// CHECK-NEXT: %[[LHS:.+]] = arith.extsi %[[ARG0]] : i16 to i32 |
| 176 | +// CHECK-NEXT: %[[RHS:.+]] = arith.extsi %[[ARG1]] : i16 to i32 |
| 177 | +// CHECK-NEXT: %[[RET:.+]] = arith.muli %[[LHS]], %[[RHS]] : i32 |
| 178 | +// CHECK-NEXT: return %[[RET]] : i32 |
| 179 | +func.func @muli_extsi_i32(%lhs: i16, %rhs: i16) -> i32 { |
| 180 | + %a = arith.extsi %lhs : i16 to i32 |
| 181 | + %b = arith.extsi %rhs : i16 to i32 |
| 182 | + %r = arith.muli %a, %b : i32 |
| 183 | + return %r : i32 |
| 184 | +} |
| 185 | + |
| 186 | +// This case should not get optimized because of mixed extensions. |
| 187 | +// |
| 188 | +// CHECK-LABEL: func.func @muli_mixed_ext_i8 |
| 189 | +// CHECK-SAME: (%[[ARG0:.+]]: i8, %[[ARG1:.+]]: i8) |
| 190 | +// CHECK-NEXT: %[[EXT0:.+]] = arith.extsi %[[ARG0]] : i8 to i32 |
| 191 | +// CHECK-NEXT: %[[EXT1:.+]] = arith.extui %[[ARG1]] : i8 to i32 |
| 192 | +// CHECK-NEXT: %[[MUL:.+]] = arith.muli %[[EXT0]], %[[EXT1]] : i32 |
| 193 | +// CHECK-NEXT: return %[[MUL]] : i32 |
| 194 | +func.func @muli_mixed_ext_i8(%lhs: i8, %rhs: i8) -> i32 { |
| 195 | + %a = arith.extsi %lhs : i8 to i32 |
| 196 | + %b = arith.extui %rhs : i8 to i32 |
| 197 | + %r = arith.muli %a, %b : i32 |
| 198 | + return %r : i32 |
| 199 | +} |
0 commit comments