From 726db6ef53f97407ae547c04c6584f8523d32611 Mon Sep 17 00:00:00 2001 From: Sifis Lagouvardos Date: Thu, 24 Oct 2024 17:18:38 -0700 Subject: [PATCH 1/4] Try different tight-packing heuristic --- clientlib/storage_modeling/tight_packing.dl | 60 ++++++++++++++++++--- 1 file changed, 53 insertions(+), 7 deletions(-) diff --git a/clientlib/storage_modeling/tight_packing.dl b/clientlib/storage_modeling/tight_packing.dl index 466331b8..6e1174db 100644 --- a/clientlib/storage_modeling/tight_packing.dl +++ b/clientlib/storage_modeling/tight_packing.dl @@ -227,13 +227,34 @@ ConstWrittenToBytesOfStorVar(var, val, store, store, construct, 0, 31):- val != "0x0". // !SLOADOfConstruct(_, construct, _). +// FailedMergedStorageModelingReason(storVar, stmt, stmt2, [low, high], [otherLow, otherHigh]), +// FailedMergedStorageModeling(storVar):- +// AnyLoadStoreStorVarBytes(stmt, storVar, low, high), +// AnyLoadStoreStorVarBytes(stmt2, storVar, otherLow, otherHigh), otherLow = otherLow, otherHigh = otherHigh, // NOWARN +// (low != otherLow ; high != otherHigh), +// ( (low < otherLow , otherLow < high) ; (low < otherHigh, otherHigh < high) ). + FailedMergedStorageModelingReason(storVar, stmt, stmt2, [low, high], [otherLow, otherHigh]), FailedMergedStorageModeling(storVar):- AnyLoadStoreStorVarBytes(stmt, storVar, low, high), - AnyLoadStoreStorVarBytes(stmt2, storVar, otherLow, otherHigh), otherLow = otherLow, otherHigh = otherHigh, // NOWARN - (low != otherLow ; high != otherHigh), + AnyLoadStoreStorVarBytes(stmt2, storVar, otherLow, otherHigh), + low != otherLow, high != otherHigh, ( (low < otherLow , otherLow < high) ; (low < otherHigh, otherHigh < high) ). +FailedMergedStorageModelingReason(storVar, stmt, stmt2, [low, high], [otherLow, high]), +FailedMergedStorageModeling(storVar):- + AnyLoadStoreStorVarBytes(stmt, storVar, low, high), + AnyLoadStoreStorVarBytes(stmt2, storVar, otherLow, high), + low != otherLow, + low < otherLow , otherLow < high. + +// FailedMergedStorageModelingReason(storVar, stmt, stmt2, [low, high], [otherLow, otherHigh]), +// FailedMergedStorageModeling(storVar):- +// AnyLoadStoreStorVarBytes(stmt, storVar, low, high), +// AnyLoadStoreStorVarBytes(stmt2, storVar, otherLow, otherHigh), otherLow = otherLow, otherHigh = otherHigh, // NOWARN +// (low != otherLow ; high != otherHigh), +// ( (low < otherLow , otherLow < high) ; (low < otherHigh, otherHigh < high) ). + LoadOrStoreToStoreVar(stmt, construct):- InitialStorageStmtToIndexAndConstruct(stmt, "ACCESS", _, construct). @@ -556,6 +577,15 @@ VarHoldsBytesOfStorVar(to, store, storVar, 0, 0):- Statement_Uses(store, storedVar, 1), BooleanCast(storedVar, to). + +.decl NormalizeStorageVarByteLimits(storvVar: StorageConstruct, inByteLow: number, inByteHigh: number, actualByteLow: number, actualByteHigh: number) +DEBUG_OUTPUT(NormalizeStorageVarByteLimits) + +NormalizeStorageVarByteLimits(storVar, low, high, low, actualHigh):- + AnyLoadStoreStorVarBytes(_, storVar, low, high), + actualHigh = max otherHigh :AnyLoadStoreStorVarBytes(_, storVar, low, otherHigh). + + /** Top-Level Global Variables */ @@ -604,7 +634,9 @@ LoadGlobalVariable(stmt, v, var):- SuccessfulMergedStorageModeling($Variable($Constant(storVar))), SLOADOfConst(_, storVar, _), // ensure it's a global variable VarHoldsBytesOfStorVarFinal(_, _, $Variable($Constant(storVar)), low, high), - VarHoldsBytesOfStorVar(var, _, $Variable($Constant(storVar)), low, high), + // perhaps we need to add new instruction here + NormalizeStorageVarByteLimits($Variable($Constant(storVar)), low, high, actualLow, actualHigh), + VarHoldsBytesOfStorVar(var, _, $Variable($Constant(storVar)), actualLow, actualHigh), Statement_Defines(stmt, var, 0), (low != 0 ; high != 31), v = MERGED_STORAGE_VAR(storVar, low, high). @@ -622,13 +654,24 @@ StoreGlobalVariable(store, v, writtenVar):- SuccessfulMergedStorageModeling($Variable($Constant(storVar))), SSTOREToConst(_, storVar, _), // ensure it's a global variable VarWrittenToBytesOfStorVarFinal(writtenVar, store, $Variable($Constant(storVar)), byteLow, byteHigh), - v = MERGED_STORAGE_VAR(storVar, byteLow, byteHigh). + NormalizeStorageVarByteLimits($Variable($Constant(storVar)), byteLow, byteHigh, actualLow, actualHigh), + ProcessedStorageVariable($Variable($Constant(storVar)), $TightlyPackedVariable($Constant(storVar), actualLow, actualHigh)), + v = MERGED_STORAGE_VAR(storVar, actualLow, actualHigh). + +StoreGlobalVariable(store, storVar, writtenVar):- + SuccessfulMergedStorageModeling($Variable($Constant(storVar))), + SSTOREToConst(_, storVar, _), // ensure it's a global variable + VarWrittenToBytesOfStorVarFinal(writtenVar, store, $Variable($Constant(storVar)), byteLow, byteHigh), + NormalizeStorageVarByteLimits($Variable($Constant(storVar)), byteLow, byteHigh, 0, 31), + ProcessedStorageVariable($Variable($Constant(storVar)), $Variable($Constant(storVar))). StoreGlobalVariable(store, v, constVar):- SuccessfulMergedStorageModeling($Variable($Constant(storVar))), SSTOREToConst(_, storVar, _), // ensure it's a global variable ConstWrittenToBytesOfStorVarProcessed(constVar, _, store, _, $Variable($Constant(storVar)), byteLow, byteHigh), - v = MERGED_STORAGE_VAR(storVar, byteLow, byteHigh). + NormalizeStorageVarByteLimits($Variable($Constant(storVar)), byteLow, byteHigh, actualLow, actualHigh), + ProcessedStorageVariable($Variable($Constant(storVar)), $TightlyPackedVariable($Constant(storVar), actualLow, actualHigh)), + v = MERGED_STORAGE_VAR(storVar, actualLow, actualHigh). StorageVariableInfo(storVar, storVar, 0, 31):- @@ -649,7 +692,7 @@ ProcessedStorageVariable(construct, construct):- VarHoldsBytesOfStorVarFinal(_, _, construct, 0, 31) ). -ProcessedStorageVariable(storageVar, $TightlyPackedVariable(parentCons, byteLow, byteHigh)):- +ProcessedStorageVariable(storageVar, $TightlyPackedVariable(parentCons, actualLow, actualHigh)):- SuccessfulMergedStorageModeling(storageVar), storageVar = $Variable(parentCons), ( @@ -657,7 +700,9 @@ ProcessedStorageVariable(storageVar, $TightlyPackedVariable(parentCons, byteLow, VarWrittenToBytesOfStorVarFinal(_, _, storageVar, byteLow, byteHigh); VarHoldsBytesOfStorVarFinal(_, _, storageVar, byteLow, byteHigh) ), - byteHigh - byteLow != 31. + NormalizeStorageVarByteLimits(storageVar, byteLow, byteHigh, actualLow, actualHigh), + byteHigh - byteLow != 31, + actualHigh - actualLow != 31. // If an array element is never loaded or stored directly (i.e. it is only copied via loops) // We consider it non merged. Maybe this isn't the right aproach, review. @@ -670,6 +715,7 @@ StorageVariablePacksNVars(storageVar, numberOfVars):- numberOfVars = count : ProcessedStorageVariable(storageVar, _), numberOfVars > 1. +// Does this need to be fixed for the (cons, cons) case ConstWrittenToBytesOfStorVarProcessed(constVar, const, store, load, $Variable(parentCons), byteLow, byteHigh):- ConstWrittenToBytesOfStorVar(constVar, const, store, load, $Variable(parentCons), byteLow, byteHigh), ProcessedStorageVariable($Variable(parentCons), $TightlyPackedVariable(parentCons, byteLow, byteHigh)). From 7dde8bacba6c82d4d736ffa5bd418376788808af Mon Sep 17 00:00:00 2001 From: Sifis Lagouvardos Date: Thu, 24 Oct 2024 18:08:42 -0700 Subject: [PATCH 2/4] work NormalizeStorageVarByteLimits into more places --- clientlib/storage_modeling/data_structures.dl | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/clientlib/storage_modeling/data_structures.dl b/clientlib/storage_modeling/data_structures.dl index ef25b790..7ffbce11 100644 --- a/clientlib/storage_modeling/data_structures.dl +++ b/clientlib/storage_modeling/data_structures.dl @@ -498,23 +498,25 @@ StorageStmtToIndexAndConstruct(stmt, "ACCESS", index, $Variable(cons)):- StorageAccessOp(stmt, var), ProcessedStorageVariable($Variable(cons), $Variable(cons)). -StorageStmtToIndexAndConstruct(stmt, "ACCESS", index, $TightlyPackedVariable(cons, low, high)):- +StorageStmtToIndexAndConstruct(stmt, "ACCESS", index, $TightlyPackedVariable(cons, actualLow, actualHigh)):- LikelyVariableLoadingStorageIndex(index), StorageIndex_StorageConstruct(index, cons), Variable_StorageIndex(indexVar, index), StorageAccessOp(srcLoad, indexVar), VarHoldsBytesOfStorVarFinal(_, _, $Variable(cons), low, high), - VarHoldsBytesOfStorVar(var, srcLoad, $Variable(cons), low, high), + NormalizeStorageVarByteLimits($Variable(cons), low, high, actualLow, actualHigh), + VarHoldsBytesOfStorVar(var, srcLoad, $Variable(cons), actualLow, actualHigh), Statement_Defines(stmt, var, _), - ProcessedStorageVariable($Variable(cons), $TightlyPackedVariable(cons, low, high)). + ProcessedStorageVariable($Variable(cons), $TightlyPackedVariable(cons, actualLow, actualHigh)). -StorageStmtToIndexAndConstruct(store, "ACCESS", index, $TightlyPackedVariable(cons, low, high)):- +StorageStmtToIndexAndConstruct(store, "ACCESS", index, $TightlyPackedVariable(cons, actualLow, actualHigh)):- LikelyVariableLoadingStorageIndex(index), StorageIndex_StorageConstruct(index, cons), Variable_StorageIndex(indexVar, index), StorageAccessOp(store, indexVar), (VarWrittenToBytesOfStorVarFinal(_, store, $Variable(cons), low, high); ConstWrittenToBytesOfStorVarProcessed(_, _, store, _, $Variable(cons), low, high); DeleteOfStructSlot(store, $Variable(cons))), - ProcessedStorageVariable($Variable(cons), $TightlyPackedVariable(cons, low, high)). + NormalizeStorageVarByteLimits($Variable(cons), low, high, actualLow, actualHigh), + ProcessedStorageVariable($Variable(cons), $TightlyPackedVariable(cons, actualLow, actualHigh)). .decl StorageOffset_Type(offset: Value, type: symbol) From 8d1723ffe2d22c6b397e38d1b0ba514dc6c7105b Mon Sep 17 00:00:00 2001 From: Sifis Lagouvardos Date: Fri, 25 Oct 2024 13:57:11 -0700 Subject: [PATCH 3/4] Increase completeness of VarHoldsBytesOfStorVarPlus --- clientlib/storage_modeling/tight_packing.dl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/clientlib/storage_modeling/tight_packing.dl b/clientlib/storage_modeling/tight_packing.dl index 6e1174db..7e643a53 100644 --- a/clientlib/storage_modeling/tight_packing.dl +++ b/clientlib/storage_modeling/tight_packing.dl @@ -161,6 +161,12 @@ VarHoldsBytesOfStorVarPlus(to, load, storVar, startByte, newLow, high):- newLow = maxKeptByte - startByte, HighBytesMaskOp(var, to, maskLen). +VarHoldsBytesOfStorVarPlus(to, load, storVar, startByte, low, high):- + VarHoldsBytesOfStorVarPlus(var, load, storVar, startByte, low, high), + width = high - low, + ByteMaskOpKeepRange(var, to, [keepLow, keepHigh]), + keepLow <= startByte, keepHigh >= startByte + width. + VarHoldsBytesOfStorVarFinal(var, load, storVar, low, high):- VarHoldsBytesOfStorVar(var, load, storVar, low, high), !VarFlowsFromSLOADToSSTORE(var, storVar), // Could potentially be restrictive for rare cases in optimized code From e4845e5caa68f224d7668a652bcd1dfb1f4c9eb4 Mon Sep 17 00:00:00 2001 From: Sifis Lagouvardos Date: Sun, 27 Oct 2024 07:06:07 -0700 Subject: [PATCH 4/4] add comment --- clientlib/storage_modeling/tight_packing.dl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/clientlib/storage_modeling/tight_packing.dl b/clientlib/storage_modeling/tight_packing.dl index 7e643a53..18f013cc 100644 --- a/clientlib/storage_modeling/tight_packing.dl +++ b/clientlib/storage_modeling/tight_packing.dl @@ -161,6 +161,9 @@ VarHoldsBytesOfStorVarPlus(to, load, storVar, startByte, newLow, high):- newLow = maxKeptByte - startByte, HighBytesMaskOp(var, to, maskLen). +// Note: This is usually an intermediary case, to restore it to some storage bytes. +// It could potentially lead to FPs, but not having it would introduce FNs, to revisit +// That's the reason it's more restrictive (it will keep the bytes of the previous var is they are all kept) VarHoldsBytesOfStorVarPlus(to, load, storVar, startByte, low, high):- VarHoldsBytesOfStorVarPlus(var, load, storVar, startByte, low, high), width = high - low,