Skip to content

Commit 1b68755

Browse files
authored
Merge pull request #164 from nevillegrech/tight_packing_changes
Simplify tight packing logic
2 parents 4eb2da5 + e4845e5 commit 1b68755

File tree

2 files changed

+69
-12
lines changed

2 files changed

+69
-12
lines changed

clientlib/storage_modeling/data_structures.dl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -498,23 +498,25 @@ StorageStmtToIndexAndConstruct(stmt, "ACCESS", index, $Variable(cons)):-
498498
StorageAccessOp(stmt, var),
499499
ProcessedStorageVariable($Variable(cons), $Variable(cons)).
500500

501-
StorageStmtToIndexAndConstruct(stmt, "ACCESS", index, $TightlyPackedVariable(cons, low, high)):-
501+
StorageStmtToIndexAndConstruct(stmt, "ACCESS", index, $TightlyPackedVariable(cons, actualLow, actualHigh)):-
502502
LikelyVariableLoadingStorageIndex(index),
503503
StorageIndex_StorageConstruct(index, cons),
504504
Variable_StorageIndex(indexVar, index),
505505
StorageAccessOp(srcLoad, indexVar),
506506
VarHoldsBytesOfStorVarFinal(_, _, $Variable(cons), low, high),
507-
VarHoldsBytesOfStorVar(var, srcLoad, $Variable(cons), low, high),
507+
NormalizeStorageVarByteLimits($Variable(cons), low, high, actualLow, actualHigh),
508+
VarHoldsBytesOfStorVar(var, srcLoad, $Variable(cons), actualLow, actualHigh),
508509
Statement_Defines(stmt, var, _),
509-
ProcessedStorageVariable($Variable(cons), $TightlyPackedVariable(cons, low, high)).
510+
ProcessedStorageVariable($Variable(cons), $TightlyPackedVariable(cons, actualLow, actualHigh)).
510511

511-
StorageStmtToIndexAndConstruct(store, "ACCESS", index, $TightlyPackedVariable(cons, low, high)):-
512+
StorageStmtToIndexAndConstruct(store, "ACCESS", index, $TightlyPackedVariable(cons, actualLow, actualHigh)):-
512513
LikelyVariableLoadingStorageIndex(index),
513514
StorageIndex_StorageConstruct(index, cons),
514515
Variable_StorageIndex(indexVar, index),
515516
StorageAccessOp(store, indexVar),
516517
(VarWrittenToBytesOfStorVarFinal(_, store, $Variable(cons), low, high); ConstWrittenToBytesOfStorVarProcessed(_, _, store, _, $Variable(cons), low, high); DeleteOfStructSlot(store, $Variable(cons))),
517-
ProcessedStorageVariable($Variable(cons), $TightlyPackedVariable(cons, low, high)).
518+
NormalizeStorageVarByteLimits($Variable(cons), low, high, actualLow, actualHigh),
519+
ProcessedStorageVariable($Variable(cons), $TightlyPackedVariable(cons, actualLow, actualHigh)).
518520

519521
.decl StorageOffset_Type(offset: Value, type: symbol)
520522

clientlib/storage_modeling/tight_packing.dl

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,15 @@ VarHoldsBytesOfStorVarPlus(to, load, storVar, startByte, newLow, high):-
161161
newLow = maxKeptByte - startByte,
162162
HighBytesMaskOp(var, to, maskLen).
163163

164+
// Note: This is usually an intermediary case, to restore it to some storage bytes.
165+
// It could potentially lead to FPs, but not having it would introduce FNs, to revisit
166+
// That's the reason it's more restrictive (it will keep the bytes of the previous var is they are all kept)
167+
VarHoldsBytesOfStorVarPlus(to, load, storVar, startByte, low, high):-
168+
VarHoldsBytesOfStorVarPlus(var, load, storVar, startByte, low, high),
169+
width = high - low,
170+
ByteMaskOpKeepRange(var, to, [keepLow, keepHigh]),
171+
keepLow <= startByte, keepHigh >= startByte + width.
172+
164173
VarHoldsBytesOfStorVarFinal(var, load, storVar, low, high):-
165174
VarHoldsBytesOfStorVar(var, load, storVar, low, high),
166175
!VarFlowsFromSLOADToSSTORE(var, storVar), // Could potentially be restrictive for rare cases in optimized code
@@ -227,13 +236,34 @@ ConstWrittenToBytesOfStorVar(var, val, store, store, construct, 0, 31):-
227236
val != "0x0".
228237
// !SLOADOfConstruct(_, construct, _).
229238

239+
// FailedMergedStorageModelingReason(storVar, stmt, stmt2, [low, high], [otherLow, otherHigh]),
240+
// FailedMergedStorageModeling(storVar):-
241+
// AnyLoadStoreStorVarBytes(stmt, storVar, low, high),
242+
// AnyLoadStoreStorVarBytes(stmt2, storVar, otherLow, otherHigh), otherLow = otherLow, otherHigh = otherHigh, // NOWARN
243+
// (low != otherLow ; high != otherHigh),
244+
// ( (low < otherLow , otherLow < high) ; (low < otherHigh, otherHigh < high) ).
245+
230246
FailedMergedStorageModelingReason(storVar, stmt, stmt2, [low, high], [otherLow, otherHigh]),
231247
FailedMergedStorageModeling(storVar):-
232248
AnyLoadStoreStorVarBytes(stmt, storVar, low, high),
233-
AnyLoadStoreStorVarBytes(stmt2, storVar, otherLow, otherHigh), otherLow = otherLow, otherHigh = otherHigh, // NOWARN
234-
(low != otherLow ; high != otherHigh),
249+
AnyLoadStoreStorVarBytes(stmt2, storVar, otherLow, otherHigh),
250+
low != otherLow, high != otherHigh,
235251
( (low < otherLow , otherLow < high) ; (low < otherHigh, otherHigh < high) ).
236252

253+
FailedMergedStorageModelingReason(storVar, stmt, stmt2, [low, high], [otherLow, high]),
254+
FailedMergedStorageModeling(storVar):-
255+
AnyLoadStoreStorVarBytes(stmt, storVar, low, high),
256+
AnyLoadStoreStorVarBytes(stmt2, storVar, otherLow, high),
257+
low != otherLow,
258+
low < otherLow , otherLow < high.
259+
260+
// FailedMergedStorageModelingReason(storVar, stmt, stmt2, [low, high], [otherLow, otherHigh]),
261+
// FailedMergedStorageModeling(storVar):-
262+
// AnyLoadStoreStorVarBytes(stmt, storVar, low, high),
263+
// AnyLoadStoreStorVarBytes(stmt2, storVar, otherLow, otherHigh), otherLow = otherLow, otherHigh = otherHigh, // NOWARN
264+
// (low != otherLow ; high != otherHigh),
265+
// ( (low < otherLow , otherLow < high) ; (low < otherHigh, otherHigh < high) ).
266+
237267
LoadOrStoreToStoreVar(stmt, construct):-
238268
InitialStorageStmtToIndexAndConstruct(stmt, "ACCESS", _, construct).
239269

@@ -556,6 +586,15 @@ VarHoldsBytesOfStorVar(to, store, storVar, 0, 0):-
556586
Statement_Uses(store, storedVar, 1),
557587
BooleanCast(storedVar, to).
558588

589+
590+
.decl NormalizeStorageVarByteLimits(storvVar: StorageConstruct, inByteLow: number, inByteHigh: number, actualByteLow: number, actualByteHigh: number)
591+
DEBUG_OUTPUT(NormalizeStorageVarByteLimits)
592+
593+
NormalizeStorageVarByteLimits(storVar, low, high, low, actualHigh):-
594+
AnyLoadStoreStorVarBytes(_, storVar, low, high),
595+
actualHigh = max otherHigh :AnyLoadStoreStorVarBytes(_, storVar, low, otherHigh).
596+
597+
559598
/**
560599
Top-Level Global Variables
561600
*/
@@ -604,7 +643,9 @@ LoadGlobalVariable(stmt, v, var):-
604643
SuccessfulMergedStorageModeling($Variable($Constant(storVar))),
605644
SLOADOfConst(_, storVar, _), // ensure it's a global variable
606645
VarHoldsBytesOfStorVarFinal(_, _, $Variable($Constant(storVar)), low, high),
607-
VarHoldsBytesOfStorVar(var, _, $Variable($Constant(storVar)), low, high),
646+
// perhaps we need to add new instruction here
647+
NormalizeStorageVarByteLimits($Variable($Constant(storVar)), low, high, actualLow, actualHigh),
648+
VarHoldsBytesOfStorVar(var, _, $Variable($Constant(storVar)), actualLow, actualHigh),
608649
Statement_Defines(stmt, var, 0),
609650
(low != 0 ; high != 31),
610651
v = MERGED_STORAGE_VAR(storVar, low, high).
@@ -622,13 +663,24 @@ StoreGlobalVariable(store, v, writtenVar):-
622663
SuccessfulMergedStorageModeling($Variable($Constant(storVar))),
623664
SSTOREToConst(_, storVar, _), // ensure it's a global variable
624665
VarWrittenToBytesOfStorVarFinal(writtenVar, store, $Variable($Constant(storVar)), byteLow, byteHigh),
625-
v = MERGED_STORAGE_VAR(storVar, byteLow, byteHigh).
666+
NormalizeStorageVarByteLimits($Variable($Constant(storVar)), byteLow, byteHigh, actualLow, actualHigh),
667+
ProcessedStorageVariable($Variable($Constant(storVar)), $TightlyPackedVariable($Constant(storVar), actualLow, actualHigh)),
668+
v = MERGED_STORAGE_VAR(storVar, actualLow, actualHigh).
669+
670+
StoreGlobalVariable(store, storVar, writtenVar):-
671+
SuccessfulMergedStorageModeling($Variable($Constant(storVar))),
672+
SSTOREToConst(_, storVar, _), // ensure it's a global variable
673+
VarWrittenToBytesOfStorVarFinal(writtenVar, store, $Variable($Constant(storVar)), byteLow, byteHigh),
674+
NormalizeStorageVarByteLimits($Variable($Constant(storVar)), byteLow, byteHigh, 0, 31),
675+
ProcessedStorageVariable($Variable($Constant(storVar)), $Variable($Constant(storVar))).
626676

627677
StoreGlobalVariable(store, v, constVar):-
628678
SuccessfulMergedStorageModeling($Variable($Constant(storVar))),
629679
SSTOREToConst(_, storVar, _), // ensure it's a global variable
630680
ConstWrittenToBytesOfStorVarProcessed(constVar, _, store, _, $Variable($Constant(storVar)), byteLow, byteHigh),
631-
v = MERGED_STORAGE_VAR(storVar, byteLow, byteHigh).
681+
NormalizeStorageVarByteLimits($Variable($Constant(storVar)), byteLow, byteHigh, actualLow, actualHigh),
682+
ProcessedStorageVariable($Variable($Constant(storVar)), $TightlyPackedVariable($Constant(storVar), actualLow, actualHigh)),
683+
v = MERGED_STORAGE_VAR(storVar, actualLow, actualHigh).
632684

633685

634686
StorageVariableInfo(storVar, storVar, 0, 31):-
@@ -649,15 +701,17 @@ ProcessedStorageVariable(construct, construct):-
649701
VarHoldsBytesOfStorVarFinal(_, _, construct, 0, 31)
650702
).
651703

652-
ProcessedStorageVariable(storageVar, $TightlyPackedVariable(parentCons, byteLow, byteHigh)):-
704+
ProcessedStorageVariable(storageVar, $TightlyPackedVariable(parentCons, actualLow, actualHigh)):-
653705
SuccessfulMergedStorageModeling(storageVar),
654706
storageVar = $Variable(parentCons),
655707
(
656708
ConstWrittenToUnreadBytesOfStorVar(_, _, _, storageVar, byteLow, byteHigh);
657709
VarWrittenToBytesOfStorVarFinal(_, _, storageVar, byteLow, byteHigh);
658710
VarHoldsBytesOfStorVarFinal(_, _, storageVar, byteLow, byteHigh)
659711
),
660-
byteHigh - byteLow != 31.
712+
NormalizeStorageVarByteLimits(storageVar, byteLow, byteHigh, actualLow, actualHigh),
713+
byteHigh - byteLow != 31,
714+
actualHigh - actualLow != 31.
661715

662716
// If an array element is never loaded or stored directly (i.e. it is only copied via loops)
663717
// We consider it non merged. Maybe this isn't the right aproach, review.
@@ -670,6 +724,7 @@ StorageVariablePacksNVars(storageVar, numberOfVars):-
670724
numberOfVars = count : ProcessedStorageVariable(storageVar, _),
671725
numberOfVars > 1.
672726

727+
// Does this need to be fixed for the (cons, cons) case
673728
ConstWrittenToBytesOfStorVarProcessed(constVar, const, store, load, $Variable(parentCons), byteLow, byteHigh):-
674729
ConstWrittenToBytesOfStorVar(constVar, const, store, load, $Variable(parentCons), byteLow, byteHigh),
675730
ProcessedStorageVariable($Variable(parentCons), $TightlyPackedVariable(parentCons, byteLow, byteHigh)).

0 commit comments

Comments
 (0)