File: int-range-inference.mlir

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (66 lines) | stat: -rw-r--r-- 2,353 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// RUN: mlir-opt -test-int-range-inference -canonicalize %s | FileCheck %s

// Most operations are covered by the `arith` tests, which use the same code
// Here, we add a few tests to ensure the "index can be 32- or 64-bit" handling
// code is operating as expected.

// CHECK-LABEL: func @add_same_for_both
// CHECK: %[[true:.*]] = index.bool.constant true
// CHECK: return %[[true]]
func.func @add_same_for_both(%arg0 : index) -> i1 {
  %c1 = index.constant 1
  %calmostBig = index.constant 0xfffffffe
  %0 = index.minu %arg0, %calmostBig
  %1 = index.add %0, %c1
  %2 = index.cmp uge(%1, %c1)
  func.return %2 : i1
}

// CHECK-LABEL: func @add_unsigned_ov
// CHECK: %[[uge:.*]] = index.cmp uge
// CHECK: return %[[uge]]
func.func @add_unsigned_ov(%arg0 : index) -> i1 {
  %c1 = index.constant 1
  %cu32_max = index.constant 0xffffffff
  %0 = index.minu %arg0, %cu32_max
  %1 = index.add %0, %c1
  // On 32-bit, the add could wrap, so the result doesn't have to be >= 1
  %2 = index.cmp uge(%1, %c1)
  func.return %2 : i1
}

// CHECK-LABEL: func @add_signed_ov
// CHECK: %[[sge:.*]] = index.cmp sge
// CHECK: return %[[sge]]
func.func @add_signed_ov(%arg0 : index) -> i1 {
  %c0 = index.constant 0
  %c1 = index.constant 1
  %ci32_max = index.constant 0x7fffffff
  %0 = index.minu %arg0, %ci32_max
  %1 = index.add %0, %c1
  // On 32-bit, the add could wrap, so the result doesn't have to be positive
  %2 = index.cmp sge(%1, %c0)
  func.return %2 : i1
}

// CHECK-LABEL: func @add_big
// CHECK: %[[true:.*]] = index.bool.constant true
// CHECK: return %[[true]]
func.func @add_big(%arg0 : index) -> i1 {
  %c1 = index.constant 1
  %cmin = index.constant 0x300000000
  %cmax = index.constant 0x30000ffff
  // Note: the order of the clamps matters.
  // If you go max, then min, you infer the ranges [0x300...0, 0xff..ff]
  // and then [0x30...0000, 0x30...ffff]
  // If you switch the order of the below operations, you instead first infer
  // the range [0,0x3...ffff]. Then, the min inference can't constraint
  // this intermediate, since in the 32-bit case we could have, for example
  // trunc(%arg0 = 0x2ffffffff) = 0xffffffff > trunc(0x30000ffff) = 0x0000ffff
  // which means we can't do any inference.
  %0 = index.maxu %arg0, %cmin
  %1 = index.minu %0, %cmax
  %2 = index.add %1, %c1
  %3 = index.cmp uge(%1, %cmin)
  func.return %3 : i1
}