From 02d8551f73133f98be681ecf6b1ce64b867bf603 Mon Sep 17 00:00:00 2001 From: Fernando Diaz Toledano Date: Mon, 26 Feb 2024 18:15:13 +0100 Subject: [PATCH 1/2] Full optimize --- src/Neo.Compiler.CSharp/CompilationContext.cs | 25 +++++++++++++++++-- .../Optimizer/Strategies/Reachability.cs | 2 +- src/Neo.Compiler.CSharp/Program.cs | 17 ++----------- .../templates/TestCleanup.cs | 6 ++--- 4 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/Neo.Compiler.CSharp/CompilationContext.cs b/src/Neo.Compiler.CSharp/CompilationContext.cs index c3b58ed13..d4ded7eb8 100644 --- a/src/Neo.Compiler.CSharp/CompilationContext.cs +++ b/src/Neo.Compiler.CSharp/CompilationContext.cs @@ -9,7 +9,6 @@ // modifications are permitted. extern alias scfx; - using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -17,8 +16,10 @@ using Neo.Cryptography.ECC; using Neo.IO; using Neo.Json; +using Neo.Optimizer; using Neo.SmartContract; using Neo.SmartContract.Manifest; +using scfx::Neo.SmartContract.Framework.Attributes; using System; using System.Collections.Generic; using System.ComponentModel; @@ -27,7 +28,6 @@ using System.Reflection; using System.Runtime.CompilerServices; using System.Text; -using scfx::Neo.SmartContract.Framework.Attributes; using Diagnostic = Microsoft.CodeAnalysis.Diagnostic; namespace Neo.Compiler @@ -136,6 +136,27 @@ internal void Compile() } } + public (NefFile nef, ContractManifest manifest, JObject debugInfo) CreateResults(string folder) + { + NefFile nef = CreateExecutable(); + ContractManifest manifest = CreateManifest(); + JObject debugInfo = CreateDebugInformation(folder); + + if (!Options.NoOptimize) + { + try + { + (nef, manifest, debugInfo) = Reachability.RemoveUncoveredInstructions(nef, manifest, (JObject)debugInfo.Clone()); + } + catch (Exception ex) + { + Console.Error.WriteLine($"Failed to optimize: {ex}"); + } + } + + return (nef, manifest, debugInfo); + } + public NefFile CreateExecutable() { Assembly assembly = Assembly.GetExecutingAssembly(); diff --git a/src/Neo.Compiler.CSharp/Optimizer/Strategies/Reachability.cs b/src/Neo.Compiler.CSharp/Optimizer/Strategies/Reachability.cs index 17e0043ff..81164b32d 100644 --- a/src/Neo.Compiler.CSharp/Optimizer/Strategies/Reachability.cs +++ b/src/Neo.Compiler.CSharp/Optimizer/Strategies/Reachability.cs @@ -39,7 +39,7 @@ public enum BranchType } [Strategy(Priority = int.MaxValue)] - public static (NefFile, ContractManifest, JToken) RemoveUncoveredInstructions(NefFile nef, ContractManifest manifest, JToken debugInfo) + public static (NefFile, ContractManifest, JObject) RemoveUncoveredInstructions(NefFile nef, ContractManifest manifest, JObject debugInfo) { Dictionary coveredMap = FindCoveredInstructions(nef, manifest, debugInfo); Script oldScript = nef.Script; diff --git a/src/Neo.Compiler.CSharp/Program.cs b/src/Neo.Compiler.CSharp/Program.cs index 645aaa871..2fedeb759 100644 --- a/src/Neo.Compiler.CSharp/Program.cs +++ b/src/Neo.Compiler.CSharp/Program.cs @@ -133,7 +133,7 @@ private static int ProcessCsproj(Options options, string path) private static int ProcessSources(Options options, string folder, string[] sourceFiles) { - return ProcessOutputs(options, folder, new CompilationEngine(options).CompileSources(null, sourceFiles)); + return ProcessOutputs(options, folder, new CompilationEngine(options).CompileSources(sourceFiles)); } private static int ProcessOutputs(Options options, string folder, List contexts) @@ -171,20 +171,7 @@ private static int ProcessOutput(Options options, string folder, CompilationCont string path = outputFolder; string baseName = options.BaseName ?? context.ContractName!; - NefFile nef = context.CreateExecutable(); - ContractManifest manifest = context.CreateManifest(); - JToken debugInfo = context.CreateDebugInformation(folder); - if (!options.NoOptimize) - { - try - { - (nef, manifest, debugInfo) = Reachability.RemoveUncoveredInstructions(nef, manifest, debugInfo.Clone()); - } - catch (Exception ex) - { - Console.Error.WriteLine($"Failed to optimize: {ex}"); - } - } + (NefFile nef, ContractManifest manifest, JToken debugInfo) = context.CreateResults(folder); try { diff --git a/tests/Neo.SmartContract.Template.UnitTests/templates/TestCleanup.cs b/tests/Neo.SmartContract.Template.UnitTests/templates/TestCleanup.cs index 5223a1e94..e4d5c6fb8 100644 --- a/tests/Neo.SmartContract.Template.UnitTests/templates/TestCleanup.cs +++ b/tests/Neo.SmartContract.Template.UnitTests/templates/TestCleanup.cs @@ -33,6 +33,7 @@ public void EnsureArtifactsUpToDate() var result = new CompilationEngine(new CompilationOptions() { Debug = true, + NoOptimize = false, Nullable = Microsoft.CodeAnalysis.NullableContextOptions.Disable }) .CompileSources( @@ -66,9 +67,8 @@ public void EnsureArtifactsUpToDate() private static (string, NeoDebugInfo) CreateArtifact(CompilationContext context, string rootDebug) { - var manifest = context.CreateManifest(); - var nef = context.CreateExecutable(); - var debug = NeoDebugInfo.FromDebugInfoJson(context.CreateDebugInformation(rootDebug)); + (var nef, var manifest, var debugInfo) = context.CreateResults(rootDebug); + var debug = NeoDebugInfo.FromDebugInfoJson(debugInfo); return (manifest.GetArtifactsSource(typeof(T).Name, nef, generateProperties: true), debug); } From 2bd495d58e515d0728e3aa5d7a9265323e4c8bea Mon Sep 17 00:00:00 2001 From: Fernando Diaz Toledano Date: Mon, 26 Feb 2024 18:16:33 +0100 Subject: [PATCH 2/2] clean using --- src/Neo.Compiler.CSharp/CompilationContext.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Neo.Compiler.CSharp/CompilationContext.cs b/src/Neo.Compiler.CSharp/CompilationContext.cs index d4ded7eb8..5433b8866 100644 --- a/src/Neo.Compiler.CSharp/CompilationContext.cs +++ b/src/Neo.Compiler.CSharp/CompilationContext.cs @@ -9,6 +9,7 @@ // modifications are permitted. extern alias scfx; + using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -19,7 +20,6 @@ using Neo.Optimizer; using Neo.SmartContract; using Neo.SmartContract.Manifest; -using scfx::Neo.SmartContract.Framework.Attributes; using System; using System.Collections.Generic; using System.ComponentModel; @@ -28,6 +28,7 @@ using System.Reflection; using System.Runtime.CompilerServices; using System.Text; +using scfx::Neo.SmartContract.Framework.Attributes; using Diagnostic = Microsoft.CodeAnalysis.Diagnostic; namespace Neo.Compiler