|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from abc import ABC, abstractmethod |
| 4 | +from dataclasses import dataclass, field |
| 5 | + |
| 6 | +from xdsl.ir import Operation, Region, SSAValue |
| 7 | +from xdsl.traits import OpTrait |
| 8 | + |
| 9 | + |
| 10 | +@dataclass(frozen=True) |
| 11 | +class IntegerRange: |
| 12 | + """ |
| 13 | + The range of an integer value, defined by its lower and upper bounds (inclusive). |
| 14 | + If either bound is None, it means that the bound is not known. |
| 15 | + """ |
| 16 | + |
| 17 | + lower_bound: int | None |
| 18 | + upper_bound: int | None |
| 19 | + |
| 20 | + def is_empty(self) -> bool: |
| 21 | + """Check if the range is empty.""" |
| 22 | + if self.lower_bound is None or self.upper_bound is None: |
| 23 | + return False |
| 24 | + return self.lower_bound > self.upper_bound |
| 25 | + |
| 26 | + def get_as_constant(self) -> int | None: |
| 27 | + """ |
| 28 | + Get the range as a constant integer value if it is a single value range. |
| 29 | + If the range is not a single value, returns None. |
| 30 | + """ |
| 31 | + if self.lower_bound is not None and self.upper_bound is not None: |
| 32 | + if self.lower_bound == self.upper_bound: |
| 33 | + return self.lower_bound |
| 34 | + return None |
| 35 | + |
| 36 | + @staticmethod |
| 37 | + def top() -> IntegerRange: |
| 38 | + """ |
| 39 | + Get the top range, which is the range that covers all possible integer values. |
| 40 | + This is used when no specific range is known for a value. |
| 41 | + """ |
| 42 | + return IntegerRange(None, None) |
| 43 | + |
| 44 | + @staticmethod |
| 45 | + def bottom() -> IntegerRange: |
| 46 | + """ |
| 47 | + Get the bottom range, which is the empty range. |
| 48 | + This is used when a value is known to be outside of any possible integer range. |
| 49 | + """ |
| 50 | + return IntegerRange(1, -1) |
| 51 | + |
| 52 | + def __contains__(self, value: int) -> bool: |
| 53 | + """Check if a value is within the range.""" |
| 54 | + if self.lower_bound is not None: |
| 55 | + if value < self.lower_bound: |
| 56 | + return False |
| 57 | + if self.upper_bound is not None: |
| 58 | + if value > self.upper_bound: |
| 59 | + return False |
| 60 | + return True |
| 61 | + |
| 62 | + def __or__(self, other: IntegerRange) -> IntegerRange: |
| 63 | + """Combine two integer ranges into one that covers both ranges.""" |
| 64 | + if self.lower_bound is None: |
| 65 | + lower_bound = other.lower_bound |
| 66 | + elif other.lower_bound is None: |
| 67 | + lower_bound = self.lower_bound |
| 68 | + else: |
| 69 | + lower_bound = min(self.lower_bound, other.lower_bound) |
| 70 | + |
| 71 | + if self.upper_bound is None: |
| 72 | + upper_bound = other.upper_bound |
| 73 | + elif other.upper_bound is None: |
| 74 | + upper_bound = self.upper_bound |
| 75 | + else: |
| 76 | + upper_bound = max(self.upper_bound, other.upper_bound) |
| 77 | + |
| 78 | + return IntegerRange(lower_bound, upper_bound) |
| 79 | + |
| 80 | + def __and__(self, other: IntegerRange) -> IntegerRange: |
| 81 | + """Intersect two integer ranges into one that covers the intersection.""" |
| 82 | + if self.lower_bound is None: |
| 83 | + lower_bound = other.lower_bound |
| 84 | + elif other.lower_bound is None: |
| 85 | + lower_bound = self.lower_bound |
| 86 | + else: |
| 87 | + lower_bound = max(self.lower_bound, other.lower_bound) |
| 88 | + |
| 89 | + if self.upper_bound is None: |
| 90 | + upper_bound = other.upper_bound |
| 91 | + elif other.upper_bound is None: |
| 92 | + upper_bound = self.upper_bound |
| 93 | + else: |
| 94 | + upper_bound = min(self.upper_bound, other.upper_bound) |
| 95 | + |
| 96 | + return IntegerRange(lower_bound, upper_bound) |
| 97 | + |
| 98 | + |
| 99 | +@dataclass |
| 100 | +class IntegerRangeAnalysis: |
| 101 | + """An analysis that contains the integer ranges of integer values.""" |
| 102 | + |
| 103 | + ranges: dict[SSAValue, IntegerRange] = field( |
| 104 | + default_factory=dict[SSAValue, IntegerRange] |
| 105 | + ) |
| 106 | + |
| 107 | + def get_range(self, value: SSAValue) -> IntegerRange: |
| 108 | + """ |
| 109 | + Get the integer range of a value. |
| 110 | + If the value is not an integer type, returns None. |
| 111 | + """ |
| 112 | + return self.ranges.get(value, IntegerRange.top()) |
| 113 | + |
| 114 | + def set_range(self, value: SSAValue, integer_range: IntegerRange) -> None: |
| 115 | + """ |
| 116 | + Set the integer range of a value. |
| 117 | + If the value is not an integer type, this will raise an error. |
| 118 | + """ |
| 119 | + # No need to set the top range, as it is the default |
| 120 | + if integer_range == IntegerRange.top(): |
| 121 | + return |
| 122 | + self.ranges[value] = integer_range |
| 123 | + |
| 124 | + def compute_operation_analysis(self, op: Operation) -> None: |
| 125 | + """ |
| 126 | + Compute the integer ranges of the operation's results. |
| 127 | + This function will call the IntegerRangeTrait.compute_analysis method |
| 128 | + if the operation has the IntegerRangeTrait trait. |
| 129 | + """ |
| 130 | + trait = op.get_trait(IntegerRangeTrait) |
| 131 | + if trait is None: |
| 132 | + return |
| 133 | + |
| 134 | + trait.compute_analysis(op, self) |
| 135 | + |
| 136 | + def compute_single_block_region_analysis(self, region: Region) -> None: |
| 137 | + """ |
| 138 | + Compute the integer ranges of all the SSA values in an SSACFG region |
| 139 | + with a single block. |
| 140 | + """ |
| 141 | + assert len(region.blocks) == 1, "Region must have a single block" |
| 142 | + block = region.block |
| 143 | + |
| 144 | + for op in block.ops: |
| 145 | + self.compute_operation_analysis(op) |
| 146 | + |
| 147 | + |
| 148 | +class IntegerRangeTrait(OpTrait, ABC): |
| 149 | + """ |
| 150 | + A trait that indicates that an operation can be used for an integer range analysis. |
| 151 | + In practice, this means that we can compute the lower and upper bounds of the |
| 152 | + operation's integer results based on its operands and attributes ranges. |
| 153 | + """ |
| 154 | + |
| 155 | + @staticmethod |
| 156 | + @abstractmethod |
| 157 | + def compute_analysis(op: Operation, analysis: IntegerRangeAnalysis): |
| 158 | + """ |
| 159 | + Compute the integer ranges of the operation's results based on the |
| 160 | + integer ranges of its operands and attributes. |
| 161 | + """ |
| 162 | + ... |
0 commit comments