[llvm-branch-commits] [llvm] [Dexter] Add ability to check float values within a range (PR #204161)

Stephen Tozer via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Jun 24 06:00:59 PDT 2026


================
@@ -518,3 +519,110 @@ def representer(dumper, data: "Label"):
     def register_yaml(loader):
         yaml.add_constructor("!label", Label.constructor, loader)
         yaml.add_representer(Label, Label.representer)
+
+
+class Float:
+    """Used to match against float values that may have an approximate range.
+    There are four possible representations for a !float node, with/without a list of values and with/without a range:
+    - `!float <value>` - checks for an exact match using floating point equality, e.g. !float 10 will match 10.0.
+    - `!float <value>+-<range>` - checks for a match within the given range, e.g. !float 10 +- 0.2 will match any
+                                  value in the range [9.8, 10.2].
+    - `!float [<value>...] - checks for exact matches against any of the given values, e.g. !float [10, 11, 12] will
+                             match 10.0, 11.0, or 12.0. This is effectively a shorthand for using a list of single float
+                             values, e.g. `[!float 10, !float 11, !float 12]`.
+    - `!float {values: [<value>...], range: <range>} - checks for matches against any of the given values, each of which
+                                                       will match a range of +-<range>. As with normal lists, this is a
+                                                       shorthand, e.g. !float{values: [1, 2], range: 0.1} is equivalent
+                                                       to [!float 1 +- 0.1, !float 2 +- 0.1].
+    """
+
+    def __init__(self, values, range):
+        try:
+            if isinstance(values, list):
+                values = [float(v) for v in values]
+                range = float(range) if range is not None else None
+            elif isinstance(values, str) and "+-" in values:
+                assert (
+                    range is None
+                ), "Float has both an explicit range and a string-embedded range?"
+                values, range = (float(n) for n in values.split("+-", maxsplit=1))
+            else:
+                assert range is None, "Explicit range passed with single float value?"
+                values = float(values)
+        except ValueError as err:
+            raise DexterNodeError(self, f"!float received non-float value: {err}")
+        self.values: Union[float, List[float]] = values
----------------
SLTozer wrote:

>From a matching point of view, yes; in general though we try to make each node have a clean parse/print roundtrip (modulo YAML formatting), which means keeping track of the difference between writing `!value x: [<float>]` and `!value x: <float>`. Not a critically important property to maintain, but the added complexity to maintain it is fairly low imo.

https://github.com/llvm/llvm-project/pull/204161


More information about the llvm-branch-commits mailing list