|
| 1 | +from lit.BooleanExpression import BooleanExpression |
| 2 | + |
| 3 | + |
| 4 | +class E2EExpr(BooleanExpression): |
| 5 | + build_specific_features = { |
| 6 | + "build-and-run-mode", |
| 7 | + "target-spir", |
| 8 | + "target-nvidia", |
| 9 | + "target-amd", |
| 10 | + "target-native_cpu", |
| 11 | + "any-target-is-spir", |
| 12 | + "any-target-is-nvidia", |
| 13 | + "any-target-is-amd", |
| 14 | + "any-target-is-native_cpu", |
| 15 | + "linux", |
| 16 | + "system-linux", |
| 17 | + "windows", |
| 18 | + "system-windows", |
| 19 | + "enable-perf-tests", |
| 20 | + "preview-breaking-changes-supported", |
| 21 | + "has_ndebug", |
| 22 | + "ocloc", |
| 23 | + "opencl-aot", |
| 24 | + "opencl_icd", |
| 25 | + "cm-compiler", |
| 26 | + "xptifw", |
| 27 | + "level_zero_dev_kit", |
| 28 | + "cuda_dev_kit", |
| 29 | + "zstd", |
| 30 | + "vulkan", |
| 31 | + "true", |
| 32 | + "false", |
| 33 | + } |
| 34 | + |
| 35 | + def __init__(self, string, variables, build_only_mode, final_unknown_value): |
| 36 | + BooleanExpression.__init__(self, string, variables) |
| 37 | + self.build_only_mode = build_only_mode |
| 38 | + self.unknown = False |
| 39 | + self.final_unknown_value = final_unknown_value |
| 40 | + |
| 41 | + @staticmethod |
| 42 | + def evaluate(string, variables, build_only_mode, final_unknown_value=True): |
| 43 | + """ |
| 44 | + string: Expression to evaluate |
| 45 | + variables: variables that evaluate to true |
| 46 | + build_only_mode: if true enables unknown values |
| 47 | + final_unknown_value: final boolean result if evaluation results in `unknown` |
| 48 | + """ |
| 49 | + try: |
| 50 | + parser = E2EExpr( |
| 51 | + string, set(variables), build_only_mode, final_unknown_value |
| 52 | + ) |
| 53 | + return parser.parseAll() |
| 54 | + except ValueError as e: |
| 55 | + raise ValueError(str(e) + ("\nin expression: %r" % string)) |
| 56 | + |
| 57 | + def parseMATCH(self): |
| 58 | + token = self.token |
| 59 | + BooleanExpression.parseMATCH(self) |
| 60 | + if token not in self.build_specific_features and self.build_only_mode: |
| 61 | + self.unknown = True |
| 62 | + else: |
| 63 | + self.unknown = False |
| 64 | + if self.value and self.unknown: |
| 65 | + raise ValueError( |
| 66 | + 'Runtime feature "' + token + '" evaluated to True in build-only' |
| 67 | + ) |
| 68 | + |
| 69 | + def parseAND(self): |
| 70 | + self.parseNOT() |
| 71 | + while self.accept("&&"): |
| 72 | + left = self.value |
| 73 | + left_unknown = self.unknown |
| 74 | + self.parseNOT() |
| 75 | + right = self.value |
| 76 | + right_unknown = self.unknown |
| 77 | + self.value = left and right |
| 78 | + # Unknown if both are unknown or if one is true and the other is unknown |
| 79 | + self.unknown = ( |
| 80 | + (left_unknown and right_unknown) |
| 81 | + or (left_unknown and right) |
| 82 | + or (left and right_unknown) |
| 83 | + ) |
| 84 | + |
| 85 | + def parseOR(self): |
| 86 | + self.parseAND() |
| 87 | + while self.accept("||"): |
| 88 | + left = self.value |
| 89 | + left_unknown = self.unknown |
| 90 | + self.parseAND() |
| 91 | + right = self.value |
| 92 | + right_unknown = self.unknown |
| 93 | + self.value = left or right |
| 94 | + # Unknown if both are unknown or if one is false and the other is unknown |
| 95 | + self.unknown = ( |
| 96 | + (left_unknown and right_unknown) |
| 97 | + or (left_unknown and not right) |
| 98 | + or (not left and right_unknown) |
| 99 | + ) |
| 100 | + |
| 101 | + def parseAll(self): |
| 102 | + self.token = next(self.tokens) |
| 103 | + self.parseOR() |
| 104 | + self.expect(BooleanExpression.END) |
| 105 | + return self.final_unknown_value if self.unknown else self.value |
| 106 | + |
| 107 | + |
| 108 | +import unittest |
| 109 | + |
| 110 | + |
| 111 | +class TestE2EExpr(unittest.TestCase): |
| 112 | + def test_basic(self): |
| 113 | + BuildOnly = True |
| 114 | + BuildAndRun = False |
| 115 | + RequiresDirective = True |
| 116 | + UnsupportedDirective = False |
| 117 | + RegularEval = lambda expr, features: E2EExpr.evaluate( |
| 118 | + expr, features, BuildAndRun |
| 119 | + ) |
| 120 | + RequiresBuildEval = lambda expr, features: E2EExpr.evaluate( |
| 121 | + expr, features, BuildOnly, RequiresDirective |
| 122 | + ) |
| 123 | + UnsupportedBuildEval = lambda expr, features: E2EExpr.evaluate( |
| 124 | + expr, features, BuildOnly, UnsupportedDirective |
| 125 | + ) |
| 126 | + # Non build-only expressions should work the same |
| 127 | + self.assertTrue(RegularEval("linux", {"linux", "rt_feature"})) |
| 128 | + self.assertTrue(RegularEval("rt_feature", {"linux", "rt_feature"})) |
| 129 | + self.assertFalse( |
| 130 | + RegularEval("rt_feature1 && rt_feature2", {"linux", "rt_feature1"}) |
| 131 | + ) |
| 132 | + # build-only expressions with no unknowns should work the same |
| 133 | + self.assertTrue(UnsupportedBuildEval("linux", {"linux"})) |
| 134 | + self.assertFalse(RequiresBuildEval("linux && windows", {"linux"})) |
| 135 | + self.assertTrue(UnsupportedBuildEval("!(windows || zstd)", {"linux"})) |
| 136 | + # build-only expressions where unknown affects the resulting value |
| 137 | + self.assertTrue(RequiresBuildEval("rt_feature", {})) |
| 138 | + self.assertFalse(UnsupportedBuildEval("rt_feature", {})) |
| 139 | + self.assertFalse(UnsupportedBuildEval("!rt_feature", {})) |
| 140 | + self.assertTrue(RequiresBuildEval("windows || rt_feature", {"linux"})) |
| 141 | + self.assertFalse(UnsupportedBuildEval("windows || rt_feature", {"linux"})) |
| 142 | + self.assertTrue(RequiresBuildEval("linux && rt_feature", {"linux"})) |
| 143 | + self.assertFalse(UnsupportedBuildEval("linux && rt_feature", {"linux"})) |
| 144 | + self.assertTrue(RequiresBuildEval("linux && !(zstd || rt_feature)", {"linux"})) |
| 145 | + self.assertFalse( |
| 146 | + UnsupportedBuildEval("linux && !(zstd || rt_feature)", {"linux"}) |
| 147 | + ) |
| 148 | + # build-only expressions where unknown does not affect the resulting value |
| 149 | + self.assertTrue(RequiresBuildEval("linux || rt_feature", {"linux"})) |
| 150 | + self.assertTrue(UnsupportedBuildEval("linux || rt_feature", {"linux"})) |
| 151 | + self.assertFalse(RequiresBuildEval("windows && rt_feature", {"linux"})) |
| 152 | + self.assertFalse(UnsupportedBuildEval("windows && rt_feature", {"linux"})) |
| 153 | + self.assertFalse( |
| 154 | + RequiresBuildEval("linux && (vulkan && rt_feature)", {"linux"}) |
| 155 | + ) |
| 156 | + self.assertFalse( |
| 157 | + UnsupportedBuildEval("linux && (vulkan && rt_feature)", {"linux"}) |
| 158 | + ) |
| 159 | + # runtime feature is present in build-only |
| 160 | + with self.assertRaises(ValueError): |
| 161 | + RequiresBuildEval("rt_feature", {"rt_feature"}) |
| 162 | + with self.assertRaises(ValueError): |
| 163 | + UnsupportedBuildEval("rt_feature", {"rt_feature"}) |
| 164 | + |
| 165 | + |
| 166 | +if __name__ == "__main__": |
| 167 | + unittest.main() |
0 commit comments