-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
optimizer removes redundant DUP and DROP; throw when optimizer fails (#…
…1168) * optimizer removes redundant DUP and DROP * fix DUP reference from jumps * cancel cloning debugInfo
- Loading branch information
Showing
63 changed files
with
483 additions
and
282 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright (C) 2015-2024 The Neo Project. | ||
// | ||
// Reachability.cs file belongs to the neo project and is free | ||
// software distributed under the MIT software license, see the | ||
// accompanying file LICENSE in the main directory of the | ||
// repository or http://www.opensource.org/licenses/mit-license.php | ||
// for more details. | ||
// | ||
// Redistribution and use in source and binary forms with or without | ||
// modifications are permitted. | ||
|
||
using Neo.Json; | ||
using Neo.SmartContract; | ||
using Neo.SmartContract.Manifest; | ||
using Neo.VM; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Neo.Optimizer | ||
{ | ||
public static class Peephole | ||
{ | ||
public static HashSet<OpCode> RemoveDupDropOpCodes = new() { OpCode.REVERSEITEMS, OpCode.CLEARITEMS, OpCode.DUP, OpCode.DROP, OpCode.ABORTMSG }; | ||
|
||
/// <summary> | ||
/// DUP SOMEOP DROP | ||
/// delete DUP and DROP when they are meaningless, optimizing to SOMPOP only | ||
/// This is mainly used for simple assignments like `a=1`, which is compiled to | ||
/// PUSH1 DUP STLOC:$index_of_a DROP | ||
/// This is correct compilation, because the expression `a=1` has return value 1 | ||
/// The return value of assignment expression is used in continuous assignments like `a=b=1` | ||
/// But at runtime we just need PUSH1 STLOC:$index_of_a | ||
/// TODO in the future: use symbolic VM to judge multiple instructions between DUP and DROP | ||
/// </summary> | ||
/// <param name="nef"></param> | ||
/// <param name="manifest"></param> | ||
/// <param name="debugInfo"></param> | ||
/// <returns></returns> | ||
[Strategy(Priority = 1 << 10)] | ||
public static (NefFile, ContractManifest, JObject?) RemoveDupDrop(NefFile nef, ContractManifest manifest, JObject? debugInfo = null) | ||
{ | ||
ContractInBasicBlocks contractInBasicBlocks = new(nef, manifest, debugInfo); | ||
InstructionCoverage oldContractCoverage = contractInBasicBlocks.coverage; | ||
Dictionary<int, Instruction> oldAddressToInstruction = new(); | ||
foreach ((int a, Instruction i) in oldContractCoverage.addressAndInstructions) | ||
oldAddressToInstruction.Add(a, i); | ||
(Dictionary<Instruction, Instruction> jumpSourceToTargets, | ||
Dictionary<Instruction, (Instruction, Instruction)> trySourceToTargets, | ||
Dictionary<Instruction, HashSet<Instruction>> jumpTargetToSources) = | ||
(oldContractCoverage.jumpInstructionSourceToTargets, | ||
oldContractCoverage.tryInstructionSourceToTargets, | ||
oldContractCoverage.jumpTargetToSources); | ||
System.Collections.Specialized.OrderedDictionary simplifiedInstructionsToAddress = new(); | ||
int currentAddress = 0; | ||
foreach (List<Instruction> basicBlock in contractInBasicBlocks.sortedBasicBlocks.Select(i => i.block)) | ||
{ | ||
for (int index = 0; index < basicBlock.Count; index++) | ||
{ | ||
if (index + 2 < basicBlock.Count | ||
&& basicBlock[index].OpCode == OpCode.DUP | ||
&& basicBlock[index + 2].OpCode == OpCode.DROP) | ||
{ | ||
Instruction currentDup = basicBlock[index]; | ||
Instruction nextInstruction = basicBlock[index + 1]; | ||
OpCode opAfterDup = nextInstruction.OpCode; | ||
if (OpCodeTypes.storeArguments.Contains(opAfterDup) | ||
|| OpCodeTypes.storeStaticFields.Contains(opAfterDup) | ||
|| OpCodeTypes.storeLocalVariables.Contains(opAfterDup) | ||
|| RemoveDupDropOpCodes.Contains(opAfterDup)) | ||
{ | ||
// Include only the instruction between DUP and DROP | ||
simplifiedInstructionsToAddress.Add(nextInstruction, currentAddress); | ||
currentAddress += nextInstruction.Size; | ||
index += 2; | ||
|
||
// If the old DUP is target of jump, re-target to the next instruction | ||
OptimizedScriptBuilder.RetargetJump(currentDup, nextInstruction, | ||
jumpSourceToTargets, jumpTargetToSources, trySourceToTargets); | ||
continue; | ||
} | ||
} | ||
simplifiedInstructionsToAddress.Add(basicBlock[index], currentAddress); | ||
currentAddress += basicBlock[index].Size; | ||
} | ||
} | ||
return AssetBuilder.BuildOptimizedAssets(nef, manifest, debugInfo, | ||
simplifiedInstructionsToAddress, | ||
jumpSourceToTargets, trySourceToTargets, | ||
oldAddressToInstruction); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
tests/Neo.Compiler.CSharp.TestContracts/Contract_Assignment.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Neo.SmartContract.Framework; | ||
|
||
namespace Neo.Compiler.CSharp.TestContracts | ||
{ | ||
public class Contract_Assignment : SmartContract.Framework.SmartContract | ||
{ | ||
public static void TestAssignment() | ||
{ | ||
int a = 1; | ||
ExecutionEngine.Assert(a == 1); | ||
int b; | ||
a = b = 2; | ||
ExecutionEngine.Assert(a == 2); | ||
ExecutionEngine.Assert(b == 2); | ||
} | ||
|
||
public static void TestCoalesceAssignment() | ||
{ | ||
int? a = null; | ||
a ??= 1; | ||
ExecutionEngine.Assert(a == 1); | ||
a ??= 2; | ||
ExecutionEngine.Assert(a == 1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.