Skip to content

Commit 2fb4f61

Browse files
authored
HOTFIX: avoid bitRangeInt function for branching (#switchInt) (#732)
The [`bitRangeInt` equation in `domains.md`](https://github.com/runtimeverification/k/blob/c376de7d91e5f14cff13b7e32e1c997983a2859b/k-distribution/include/kframework/builtin/domains.md?plain=1#L1438) uses partial bit-shift symbols and is therefore not applied by booster. A better alternative is to use the `truncate` function locally defined in `mir-semantics`. Expressions are equivalent: ``` bitRangeInt(VAL, 0, WIDTH) (def) == (VAL >>Int 0) modInt (1 <<Int WIDTH) (>> 0) == VAL modInt (1 <<Int WIDTH) (mod via mask) == VAL &Int ((1<<Int WIDTH) -Int 1) == truncate(VAL, WIDTH, Unsigned) ``` Also marking some relevant simplifications as smt-lemmas. Altogether, this avoids a number of observed fall-backs in p-token execution.
1 parent d401761 commit 2fb4f61

File tree

3 files changed

+13
-17
lines changed

3 files changed

+13
-17
lines changed

kmir/src/kmir/kdist/mir-semantics/kmir.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ will be `129`.
324324
325325
rule #switchMatch(0, BoolVal(B) ) => notBool B
326326
rule #switchMatch(1, BoolVal(B) ) => B
327-
rule #switchMatch(I, Integer(I2, WIDTH, _)) => I ==Int bitRangeInt(I2, 0, WIDTH)
327+
rule #switchMatch(I, Integer(I2, WIDTH, _)) => I ==Int truncate(I2, WIDTH, Unsigned)
328328
```
329329

330330
`Return` simply returns from a function call, using the information

kmir/src/kmir/kdist/mir-semantics/lemmas/kmir-lemmas.md

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,16 @@ Therefore, its value range should be simplified for symbolic input asserted to b
8181

8282
```k
8383
rule truncate(VAL, WIDTH, Unsigned) => VAL
84-
requires VAL <Int (1 <<Int WIDTH)
84+
requires 0 <Int WIDTH
85+
andBool VAL <Int (1 <<Int WIDTH)
8586
andBool 0 <=Int VAL
86-
[simplification]
87+
[simplification, preserves-definedness] // , smt-lemma], but `Unsigned` needs to be smtlib
8788
8889
rule truncate(VAL, WIDTH, Signed) => VAL
89-
requires VAL <Int (1 <<Int (WIDTH -Int 1))
90+
requires 0 <Int WIDTH
91+
andBool VAL <Int (1 <<Int (WIDTH -Int 1))
9092
andBool 0 -Int (1 <<Int (WIDTH -Int 1)) <=Int VAL
91-
[simplification]
93+
[simplification, preserves-definedness] // , smt-lemma], but `Signed` needs to be smtlib
9294
```
9395

9496
However, `truncate` gets evaluated and is therefore not present any more for this simplification.
@@ -97,17 +99,6 @@ The following simplification rules operate on the expression created by evaluati
9799
power of two but the semantics will always operate with these particular ones.
98100

99101
```k
100-
rule VAL &Int MASK => VAL
101-
requires 0 <=Int VAL
102-
andBool VAL <=Int MASK
103-
andBool ( MASK ==Int bitmask8
104-
orBool MASK ==Int bitmask16
105-
orBool MASK ==Int bitmask32
106-
orBool MASK ==Int bitmask64
107-
orBool MASK ==Int bitmask128
108-
)
109-
[simplification, preserves-definedness]
110-
111102
syntax Int ::= "bitmask8" [macro]
112103
| "bitmask16" [macro]
113104
| "bitmask32" [macro]
@@ -120,6 +111,11 @@ power of two but the semantics will always operate with these particular ones.
120111
rule bitmask64 => ( 1 <<Int 64 ) -Int 1
121112
rule bitmask128 => ( 1 <<Int 128) -Int 1
122113
114+
rule VAL &Int bitmask8 => VAL requires 0 <=Int VAL andBool VAL <=Int bitmask8 [simplification, preserves-definedness, smt-lemma]
115+
rule VAL &Int bitmask16 => VAL requires 0 <=Int VAL andBool VAL <=Int bitmask16 [simplification, preserves-definedness, smt-lemma]
116+
rule VAL &Int bitmask32 => VAL requires 0 <=Int VAL andBool VAL <=Int bitmask32 [simplification, preserves-definedness, smt-lemma]
117+
rule VAL &Int bitmask64 => VAL requires 0 <=Int VAL andBool VAL <=Int bitmask64 [simplification, preserves-definedness, smt-lemma]
118+
rule VAL &Int bitmask128 => VAL requires 0 <=Int VAL andBool VAL <=Int bitmask128 [simplification, preserves-definedness, smt-lemma]
123119
```
124120

125121

kmir/src/kmir/kdist/mir-semantics/rt/numbers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ This truncation function is instrumental in the implementation of Integer arithm
6868

6969
```k
7070
// helper function to truncate int values
71-
syntax Int ::= truncate(Int, Int, Signedness) [function, total]
71+
syntax Int ::= truncate(Int, Int, Signedness) [function, total, smtlib(smt_truncate)]
7272
// -------------------------------------------------------------
7373
rule truncate(_, WIDTH, _) => 0
7474
requires WIDTH <=Int 0

0 commit comments

Comments
 (0)