Skip to content

Commit 050e42f

Browse files
authored
Updates from master (discriminant bit width sentinel) (#794)
Bitwidth zero for discriminants (#792)
2 parents fa28e3f + d6a8dac commit 050e42f

File tree

3 files changed

+19
-27
lines changed

3 files changed

+19
-27
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ will be `129`.
175175
176176
rule #switchMatch(0, BoolVal(B) ) => notBool B
177177
rule #switchMatch(1, BoolVal(B) ) => B
178-
rule #switchMatch(I, Integer(I2, WIDTH, _)) => I ==Int truncate(I2, WIDTH, Unsigned)
178+
rule #switchMatch(I, Integer(I2, WIDTH, _)) => I ==Int truncate(I2, WIDTH, Unsigned) requires 0 <Int WIDTH
179+
rule #switchMatch(I, Integer(I2, 0 , _)) => I ==Int I2
179180
```
180181

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

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

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,10 +1015,9 @@ The `getTyOf` helper applies the projections from the `Place` to determine the `
10151015
syntax Evaluation ::= #discriminant ( Evaluation , MaybeTy ) [strict(1)]
10161016
// ----------------------------------------------------------------
10171017
rule <k> #discriminant(Aggregate(IDX, _), TY:Ty)
1018-
=> Integer(#lookupDiscriminant(lookupTy(TY), IDX), #discriminantSize(lookupTy(TY)), false)
1018+
=> Integer(#lookupDiscriminant(lookupTy(TY), IDX), 0, false) // HACK: bit width 0 means "flexible"
10191019
...
10201020
</k>
1021-
requires #discriminantSize(lookupTy(TY)) =/=Int 0
10221021
10231022
syntax Int ::= #lookupDiscriminant ( TypeInfo , VariantIdx ) [function, total]
10241023
| #lookupDiscrAux ( Discriminants , Int ) [function]
@@ -1029,27 +1028,6 @@ The `getTyOf` helper applies the projections from the `Place` to determine the `
10291028
// --------------------------------------------------------------------
10301029
rule #lookupDiscrAux( discriminant(RESULT) _ , IDX) => RESULT requires IDX ==Int 0
10311030
rule #lookupDiscrAux( _:Discriminant MORE:Discriminants, IDX) => #lookupDiscrAux(MORE, IDX -Int 1) requires 0 <Int IDX [owise]
1032-
1033-
syntax Int ::= #discriminantSize ( TypeInfo ) [function, total]
1034-
| #discriminantSize ( LayoutShape ) [function, total]
1035-
| #discriminantSize ( Scalar ) [function, total]
1036-
| #intLength (IntegerLength) [function, total]
1037-
// ------------------------------------------------------------
1038-
rule #discriminantSize(typeInfoEnumType(_, _, _, _, someLayoutShape(LAYOUT))) => #discriminantSize(LAYOUT)
1039-
rule #discriminantSize(_OTHER_TYPE:TypeInfo) => 0 [owise]
1040-
1041-
rule #discriminantSize(layoutShape(_, variantsShapeMultiple(mk(TAG, _, _, _)), _, _, _)) => #discriminantSize(TAG)
1042-
rule #discriminantSize(_OTHER:LayoutShape) => 0 [owise]
1043-
1044-
rule #discriminantSize(scalarInitialized(mk(primitiveInt(mk(INTLENGTH, _SIGNED)), _VALIDRANGE))) => #intLength(INTLENGTH)
1045-
rule #discriminantSize(scalarInitialized(mk(primitivePointer(_) , _VALIDRANGE))) => 64 // pointer size assumed 64 bit
1046-
rule #discriminantSize(_OTHER:Scalar) => 0 [owise]
1047-
1048-
rule #intLength(integerLengthI8) => 8
1049-
rule #intLength(integerLengthI16) => 16
1050-
rule #intLength(integerLengthI32) => 32
1051-
rule #intLength(integerLengthI64) => 64
1052-
rule #intLength(integerLengthI128) => 128
10531031
```
10541032

10551033
```k
@@ -1422,7 +1400,7 @@ If none of the `enum` variants has any fields, the `Transmute` of a number to th
14221400
rule <k> #discriminant(
14231401
thunk(#cast (Integer(DATA, _, false), castKindTransmute, _, TY)),
14241402
TY
1425-
) => Integer(DATA, #discriminantSize(lookupTy(TY)), false)
1403+
) => Integer(DATA, 0, false) // HACK: bit width 0 means "flexible"
14261404
...
14271405
</k>
14281406
requires #isEnumWithoutFields(lookupTy(TY))
@@ -1705,14 +1683,27 @@ The argument types must be the same for all comparison operations, however this
17051683
rule #applyBinOp(OP, Integer(VAL1, WIDTH, SIGN), Integer(VAL2, WIDTH, SIGN), _)
17061684
=>
17071685
BoolVal(cmpOpInt(OP, VAL1, VAL2))
1686+
requires isComparison(OP)
1687+
andBool WIDTH =/=Int 0
1688+
[preserves-definedness] // OP known to be a comparison
1689+
1690+
// HACK: accept bit width 0 in comparisons (coming from discriminants)
1691+
rule #applyBinOp(OP, Integer(VAL1, 0, false), Integer(VAL2, _WIDTH, _SIGN), _)
1692+
=>
1693+
BoolVal(cmpOpInt(OP, VAL1, VAL2))
1694+
requires isComparison(OP)
1695+
[priority(55), preserves-definedness] // OP known to be a comparison
1696+
rule #applyBinOp(OP, Integer(VAL1, _WIDTH, _SIGN_), Integer(VAL2, 0, false), _)
1697+
=>
1698+
BoolVal(cmpOpInt(OP, VAL1, VAL2))
17081699
requires isComparison(OP)
17091700
[preserves-definedness] // OP known to be a comparison
17101701
17111702
rule #applyBinOp(OP, BoolVal(VAL1), BoolVal(VAL2), _)
17121703
=>
17131704
BoolVal(cmpOpBool(OP, VAL1, VAL2))
17141705
requires isComparison(OP)
1715-
[preserves-definedness] // OP known to be a comparison
1706+
[priority(60), preserves-definedness] // OP known to be a comparison
17161707
```
17171708

17181709
The `binOpCmp` operation returns `-1`, `0`, or `+1` (the behaviour of Rust's `std::cmp::Ordering as i8`), indicating `LE`, `EQ`, or `GT`.

kmir/src/tests/integration/data/exec-smir/enum/enum.state

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
ListItem ( newLocal ( ty ( 16 ) , mutabilityNot ) )
4848
ListItem ( newLocal ( ty ( 28 ) , mutabilityNot ) )
4949
ListItem ( typedValue ( Moved , ty ( 6 ) , mutabilityMut ) )
50-
ListItem ( typedValue ( Integer ( 9090 , 16 , false ) , ty ( 29 ) , mutabilityMut ) )
50+
ListItem ( typedValue ( Integer ( 9090 , 0 , false ) , ty ( 29 ) , mutabilityMut ) )
5151
ListItem ( newLocal ( ty ( 26 ) , mutabilityNot ) )
5252
ListItem ( newLocal ( ty ( 28 ) , mutabilityMut ) )
5353
</locals>

0 commit comments

Comments
 (0)