Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix optimizations using CompilationEngine #960

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Neo.Compiler.CSharp/CompilationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Neo.Cryptography.ECC;
using Neo.IO;
using Neo.Json;
using Neo.Optimizer;
using Neo.SmartContract;
using Neo.SmartContract.Manifest;
using System;
Expand Down Expand Up @@ -136,6 +137,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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, bool> coveredMap = FindCoveredInstructions(nef, manifest, debugInfo);
Script oldScript = nef.Script;
Expand Down
17 changes: 2 additions & 15 deletions src/Neo.Compiler.CSharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CompilationContext> contexts)
Expand Down Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void EnsureArtifactsUpToDate()
var result = new CompilationEngine(new CompilationOptions()
{
Debug = true,
NoOptimize = false,
Nullable = Microsoft.CodeAnalysis.NullableContextOptions.Disable
})
.CompileSources(
Expand Down Expand Up @@ -66,9 +67,8 @@ public void EnsureArtifactsUpToDate()

private static (string, NeoDebugInfo) CreateArtifact<T>(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);
}
Expand Down
Loading