Skip to content

Commit

Permalink
Allow to choose the kind of optimization (#964)
Browse files Browse the repository at this point in the history
* Avoid NoX argument

* Fix

* Use all

* fix ut
  • Loading branch information
shargon authored Feb 27, 2024
1 parent 0412d7e commit 157a892
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 15 deletions.
7 changes: 5 additions & 2 deletions src/Neo.Compiler.CSharp/CompilationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ internal void Compile()
RemoveEmptyInitialize();
Instruction[] instructions = GetInstructions().ToArray();
instructions.RebuildOffsets();
if (!Options.NoOptimize) Optimizer.CompressJumps(instructions);
if (Options.Optimize.HasFlag(CompilationOptions.OptimizationType.Basic))
{
Optimizer.CompressJumps(instructions);
}
instructions.RebuildOperands();
}
}
Expand All @@ -143,7 +146,7 @@ internal void Compile()
ContractManifest manifest = CreateManifest();
JObject debugInfo = CreateDebugInformation(folder);

if (!Options.NoOptimize)
if (Options.Optimize.HasFlag(CompilationOptions.OptimizationType.Experimental))
{
try
{
Expand Down
13 changes: 12 additions & 1 deletion src/Neo.Compiler.CSharp/CompilationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,26 @@

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using System;
using System.Collections.Generic;

namespace Neo.Compiler
{
public class CompilationOptions
{
[Flags]
public enum OptimizationType : byte
{
None = 0,
Basic = 1,
Experimental = 2,

All = Basic | Experimental
}

public NullableContextOptions Nullable { get; set; }
public bool Debug { get; set; }
public bool NoOptimize { get; set; }
public OptimizationType Optimize { get; set; } = OptimizationType.Basic;
public bool Checked { get; set; }
public bool NoInline { get; set; }
public byte AddressVersion { get; set; }
Expand Down
6 changes: 3 additions & 3 deletions src/Neo.Compiler.CSharp/MethodConvert/MethodConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ private byte AddAnonymousVariable()

private void RemoveAnonymousVariable(byte index)
{
if (!_context.Options.NoOptimize)
if (_context.Options.Optimize.HasFlag(CompilationOptions.OptimizationType.Basic))
_anonymousVariables.Remove(index);
}

private void RemoveLocalVariable(ILocalSymbol symbol)
{
if (!_context.Options.NoOptimize)
if (_context.Options.Optimize.HasFlag(CompilationOptions.OptimizationType.Basic))
_localVariables.Remove(symbol);
}

Expand Down Expand Up @@ -226,7 +226,7 @@ public void Convert(SemanticModel model)
// it comes from modifier clean up
AddInstruction(OpCode.RET);
}
if (!_context.Options.NoOptimize)
if (_context.Options.Optimize.HasFlag(CompilationOptions.OptimizationType.Basic))
Optimizer.RemoveNops(_instructions);
_startTarget.Instruction = _instructions[0];
}
Expand Down
14 changes: 9 additions & 5 deletions src/Neo.Compiler.CSharp/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using System;

namespace Neo.Compiler
{
public class Options : CompilationOptions
{
public enum GenerateArtifactsKind
[Flags]
public enum GenerateArtifactsKind : byte
{
None,
Source,
Library,
All
None = 0,
Source = 1,
Library = 2,

All = Source | Library
}

public string? Output { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions src/Neo.Compiler.CSharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,14 @@ private static int ProcessOutput(Options options, string folder, CompilationCont
{
var artifact = manifest.GetArtifactsSource(baseName, nef);

if (options.GenerateArtifacts == Options.GenerateArtifactsKind.All || options.GenerateArtifacts == Options.GenerateArtifactsKind.Source)
if (options.GenerateArtifacts.HasFlag(Options.GenerateArtifactsKind.Source))
{
path = Path.Combine(outputFolder, $"{baseName}.artifacts.cs");
File.WriteAllText(path, artifact);
Console.WriteLine($"Created {path}");
}

if (options.GenerateArtifacts == Options.GenerateArtifactsKind.All || options.GenerateArtifacts == Options.GenerateArtifactsKind.Library)
if (options.GenerateArtifacts.HasFlag(Options.GenerateArtifactsKind.Library))
{
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void EnsureArtifactsUpToDate()
var result = new CompilationEngine(new CompilationOptions()
{
Debug = true,
NoOptimize = false,
Optimize = CompilationOptions.OptimizationType.All,
Nullable = Microsoft.CodeAnalysis.NullableContextOptions.Disable
})
.CompileSources(
Expand Down
2 changes: 1 addition & 1 deletion tests/Neo.SmartContract.TestEngine/TestEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public List<CompilationContext> AddEntryScripts(bool optimize = true, bool debug
{
AddressVersion = ProtocolSettings.Default.AddressVersion,
Debug = debug,
NoOptimize = !optimize
Optimize = optimize ? Compiler.CompilationOptions.OptimizationType.All : Compiler.CompilationOptions.OptimizationType.None
}).Compile(files, references);

if (contexts == null || contexts.Count == 0)
Expand Down

0 comments on commit 157a892

Please sign in to comment.