xref: /llvm-project/mlir/test/Dialect/Index/int-range-inference.mlir (revision 472291111d9135961305afebe4e283e3e4e7eebc)
1// RUN: mlir-opt -int-range-optimizations -canonicalize %s | FileCheck %s
2
3// Most operations are covered by the `arith` tests, which use the same code
4// Here, we add a few tests to ensure the "index can be 32- or 64-bit" handling
5// code is operating as expected.
6
7// CHECK-LABEL: func @add_same_for_both
8// CHECK: %[[true:.*]] = index.bool.constant true
9// CHECK: return %[[true]]
10func.func @add_same_for_both(%arg0 : index) -> i1 {
11  %c1 = index.constant 1
12  %calmostBig = index.constant 0xfffffffe
13  %0 = index.minu %arg0, %calmostBig
14  %1 = index.add %0, %c1
15  %2 = index.cmp uge(%1, %c1)
16  func.return %2 : i1
17}
18
19// CHECK-LABEL: func @add_unsigned_ov
20// CHECK: %[[uge:.*]] = index.cmp uge
21// CHECK: return %[[uge]]
22func.func @add_unsigned_ov(%arg0 : index) -> i1 {
23  %c1 = index.constant 1
24  %cu32_max = index.constant 0xffffffff
25  %0 = index.minu %arg0, %cu32_max
26  %1 = index.add %0, %c1
27  // On 32-bit, the add could wrap, so the result doesn't have to be >= 1
28  %2 = index.cmp uge(%1, %c1)
29  func.return %2 : i1
30}
31
32// CHECK-LABEL: func @add_signed_ov
33// CHECK: %[[sge:.*]] = index.cmp sge
34// CHECK: return %[[sge]]
35func.func @add_signed_ov(%arg0 : index) -> i1 {
36  %c0 = index.constant 0
37  %c1 = index.constant 1
38  %ci32_max = index.constant 0x7fffffff
39  %0 = index.minu %arg0, %ci32_max
40  %1 = index.add %0, %c1
41  // On 32-bit, the add could wrap, so the result doesn't have to be positive
42  %2 = index.cmp sge(%1, %c0)
43  func.return %2 : i1
44}
45
46// CHECK-LABEL: func @add_big
47// CHECK: %[[true:.*]] = index.bool.constant true
48// CHECK: return %[[true]]
49func.func @add_big(%arg0 : index) -> i1 {
50  %c1 = index.constant 1
51  %cmin = index.constant 0x300000000
52  %cmax = index.constant 0x30000ffff
53  // Note: the order of the clamps matters.
54  // If you go max, then min, you infer the ranges [0x300...0, 0xff..ff]
55  // and then [0x30...0000, 0x30...ffff]
56  // If you switch the order of the below operations, you instead first infer
57  // the range [0,0x3...ffff]. Then, the min inference can't constraint
58  // this intermediate, since in the 32-bit case we could have, for example
59  // trunc(%arg0 = 0x2ffffffff) = 0xffffffff > trunc(0x30000ffff) = 0x0000ffff
60  // which means we can't do any inference.
61  %0 = index.maxu %arg0, %cmin
62  %1 = index.minu %0, %cmax
63  %2 = index.add %1, %c1
64  %3 = index.cmp uge(%1, %cmin)
65  func.return %3 : i1
66}
67