Skip to content

Commit c0bb699

Browse files
committed
1.0.11
Add per optimization instruction breakdown Fix Unity 2019 Improve tail call optimization
1 parent 46061d2 commit c0bb699

20 files changed

Lines changed: 281 additions & 95 deletions
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Collections.Generic;
2+
using UdonSharp.Compiler.Assembly;
3+
using UnityEditor;
4+
5+
namespace UdonSharpOptimizer.Optimizations
6+
{
7+
abstract class BaseOptimization : IBaseOptimization
8+
{
9+
private int removedInstructions;
10+
protected abstract string GUILabel { get; }
11+
public abstract bool Enabled { get; }
12+
13+
public abstract void ProcessInstruction(Optimizer optimizer, List<AssemblyInstruction> instrs, int i);
14+
15+
public void ResetStats()
16+
{
17+
removedInstructions = 0;
18+
}
19+
20+
public void OnGUI()
21+
{
22+
OptimizerEditorWindow.AlignedText(GUILabel, removedInstructions.ToString(), EditorStyles.label);
23+
}
24+
25+
protected void CountRemoved(Optimizer optimizer, int count)
26+
{
27+
optimizer.removedInsts += count;
28+
removedInstructions += count;
29+
}
30+
}
31+
}

Packages/blueamulet.udonsharpoptimizer/Editor/Optimizations/BaseOptimization.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/blueamulet.udonsharpoptimizer/Editor/Optimizations/IBaseOptimization.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ namespace UdonSharpOptimizer.Optimizations
55
{
66
internal interface IBaseOptimization
77
{
8-
bool Enabled();
8+
bool Enabled { get; }
9+
10+
void ResetStats();
11+
12+
void OnGUI();
913

1014
void ProcessInstruction(Optimizer optimizer, List<AssemblyInstruction> instrs, int i);
1115
}

Packages/blueamulet.udonsharpoptimizer/Editor/Optimizations/OPTCopyLoad.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44

55
namespace UdonSharpOptimizer.Optimizations
66
{
7-
internal class OPTCopyLoad : IBaseOptimization
7+
internal class OPTCopyLoad : BaseOptimization
88
{
9-
public bool Enabled()
10-
{
11-
return OptimizerSettings.Instance.CopyAndLoad;
12-
}
9+
protected override string GUILabel => "Copy Load";
10+
11+
public override bool Enabled => OptimizerSettings.Instance.CopyAndLoad;
1312

14-
public void ProcessInstruction(Optimizer optimizer, List<AssemblyInstruction> instrs, int i)
13+
public override void ProcessInstruction(Optimizer optimizer, List<AssemblyInstruction> instrs, int i)
1514
{
1615
// Remove Copy: Copy + Push
1716
if (instrs[i] is CopyInstruction cInst && i < instrs.Count - 1 && instrs[i + 1] is PushInstruction pInst)
@@ -20,7 +19,7 @@ public void ProcessInstruction(Optimizer optimizer, List<AssemblyInstruction> in
2019
{
2120
instrs[i] = optimizer.TransferInstr(Optimizer.CopyComment("OPTCopyLoad", cInst), cInst);
2221
instrs[i + 1] = optimizer.TransferInstr(new PushInstruction(cInst.SourceValue), pInst);
23-
optimizer.removedInsts += 3; // PUSH, PUSH, COPY
22+
CountRemoved(optimizer, 3); // PUSH, PUSH, COPY
2423
}
2524
}
2625
}

Packages/blueamulet.udonsharpoptimizer/Editor/Optimizations/OPTCopyTest.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44

55
namespace UdonSharpOptimizer.Optimizations
66
{
7-
internal class OPTCopyTest : IBaseOptimization
7+
internal class OPTCopyTest : BaseOptimization
88
{
9-
public bool Enabled()
10-
{
11-
return OptimizerSettings.Instance.CopyAndTest;
12-
}
9+
protected override string GUILabel => "Copy Test";
10+
11+
public override bool Enabled => OptimizerSettings.Instance.CopyAndTest;
1312

14-
public void ProcessInstruction(Optimizer optimizer, List<AssemblyInstruction> instrs, int i)
13+
public override void ProcessInstruction(Optimizer optimizer, List<AssemblyInstruction> instrs, int i)
1514
{
1615
// Remove Copy: Copy + JumpIf
1716
if (instrs[i] is CopyInstruction cInst && i < instrs.Count - 1 && instrs[i + 1] is JumpIfFalseInstruction jifInst)
@@ -20,7 +19,7 @@ public void ProcessInstruction(Optimizer optimizer, List<AssemblyInstruction> in
2019
{
2120
instrs[i] = optimizer.TransferInstr(Optimizer.CopyComment("OPTCopyTest", cInst), cInst);
2221
instrs[i + 1] = optimizer.TransferInstr(new JumpIfFalseInstruction(jifInst.JumpTarget, cInst.SourceValue), jifInst);
23-
optimizer.removedInsts += 3; // PUSH, PUSH, COPY
22+
CountRemoved(optimizer, 3); // PUSH, PUSH, COPY
2423
}
2524
}
2625
}

Packages/blueamulet.udonsharpoptimizer/Editor/Optimizations/OPTDoubleCopy.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44

55
namespace UdonSharpOptimizer.Optimizations
66
{
7-
internal class OPTDoubleCopy : IBaseOptimization
7+
internal class OPTDoubleCopy : BaseOptimization
88
{
9-
public bool Enabled()
10-
{
11-
return OptimizerSettings.Instance.DoubleCopy;
12-
}
9+
protected override string GUILabel => "Double Copy";
10+
11+
public override bool Enabled => OptimizerSettings.Instance.DoubleCopy;
1312

14-
public void ProcessInstruction(Optimizer optimizer, List<AssemblyInstruction> instrs, int i)
13+
public override void ProcessInstruction(Optimizer optimizer, List<AssemblyInstruction> instrs, int i)
1514
{
1615
// Remove Copy: Copy + Copy
1716
if (instrs[i] is CopyInstruction cInst1 && i < instrs.Count - 1 && instrs[i + 1] is CopyInstruction cInst2)
@@ -20,7 +19,7 @@ public void ProcessInstruction(Optimizer optimizer, List<AssemblyInstruction> in
2019
{
2120
instrs[i] = optimizer.TransferInstr(Optimizer.CopyComment("OPTDoubleCopy", cInst1), cInst1);
2221
instrs[i + 1] = optimizer.TransferInstr(new CopyInstruction(cInst1.SourceValue, cInst2.TargetValue), cInst2);
23-
optimizer.removedInsts += 3; // PUSH, PUSH, COPY
22+
CountRemoved(optimizer, 3); // PUSH, PUSH, COPY
2423
}
2524
}
2625
}

Packages/blueamulet.udonsharpoptimizer/Editor/Optimizations/OPTStoreCopy.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44

55
namespace UdonSharpOptimizer.Optimizations
66
{
7-
internal class OPTStoreCopy : IBaseOptimization
7+
internal class OPTStoreCopy : BaseOptimization
88
{
9-
public bool Enabled()
10-
{
11-
return OptimizerSettings.Instance.StoreAndCopy;
12-
}
9+
protected override string GUILabel => "Store Copy";
10+
11+
public override bool Enabled => OptimizerSettings.Instance.StoreAndCopy;
1312

14-
public void ProcessInstruction(Optimizer optimizer, List<AssemblyInstruction> instrs, int i)
13+
public override void ProcessInstruction(Optimizer optimizer, List<AssemblyInstruction> instrs, int i)
1514
{
1615
// Remove Copy: Extern + Copy
1716
if (instrs[i] is PushInstruction pInst && i < instrs.Count - 2 && Optimizer.IsExternWrite(instrs[i + 1]) && instrs[i + 2] is CopyInstruction cInst)
@@ -20,7 +19,7 @@ public void ProcessInstruction(Optimizer optimizer, List<AssemblyInstruction> in
2019
{
2120
instrs[i] = optimizer.TransferInstr(new PushInstruction(cInst.TargetValue), pInst);
2221
instrs[i + 2] = optimizer.TransferInstr(Optimizer.CopyComment("OPTStoreCopy", cInst), cInst);
23-
optimizer.removedInsts += 3; // PUSH, PUSH, COPY
22+
CountRemoved(optimizer, 3); // PUSH, PUSH, COPY
2423
}
2524
}
2625
}

Packages/blueamulet.udonsharpoptimizer/Editor/Optimizations/OPTTailCall.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@
55

66
namespace UdonSharpOptimizer.Optimizations
77
{
8-
internal class OPTTailCall : IBaseOptimization
8+
internal class OPTTailCall : BaseOptimization
99
{
10-
public bool Enabled()
11-
{
12-
return OptimizerSettings.Instance.EnableTCO;
13-
}
10+
protected override string GUILabel => "Tail Calls";
1411

15-
public void ProcessInstruction(Optimizer optimizer, List<AssemblyInstruction> instrs, int i)
12+
public override bool Enabled => OptimizerSettings.Instance.EnableTCO;
13+
14+
public override void ProcessInstruction(Optimizer optimizer, List<AssemblyInstruction> instrs, int i)
1615
{
1716
// Tail call optimization
1817
// TODO: Properly verify this jump is to a method
19-
if (instrs[i] is JumpInstruction && instrs[i + 1] is RetInstruction rInst && !optimizer.HasJump(rInst) && instrs[i - 1] is Comment cInst && cInst.Comment.StartsWith("Calling "))
18+
if (instrs[i] is JumpInstruction && instrs[i + 1] is RetInstruction rInst && instrs[i - 1] is Comment cInst && cInst.Comment.StartsWith("Calling "))
2019
{
2120
// Locate the corresponding push above
2221
int pushIdx = -1;
@@ -36,8 +35,15 @@ public void ProcessInstruction(Optimizer optimizer, List<AssemblyInstruction> in
3635
{
3736
PushInstruction pInst = (PushInstruction)instrs[pushIdx];
3837
instrs[pushIdx] = optimizer.TransferInstr(new Comment($"OPTTailCall: Tail call optimization, removed PUSH {pInst.PushValue.UniqueID}"), instrs[pushIdx]);
39-
instrs[i + 1] = optimizer.TransferInstr(new Comment($"OPTTailCall: Tail call optimization, removed RET {rInst.RetValRef.UniqueID}"), rInst);
40-
optimizer.removedInsts += 4; // PUSH & PUSH + COPY + JUMP_INDIRECT
38+
if (!optimizer.HasJump(rInst))
39+
{
40+
instrs[i + 1] = optimizer.TransferInstr(new Comment($"OPTTailCall: Tail call optimization, removed RET {rInst.RetValRef.UniqueID}"), rInst);
41+
CountRemoved(optimizer, 4); // PUSH & PUSH + COPY + JUMP_INDIRECT
42+
}
43+
else
44+
{
45+
CountRemoved(optimizer, 1); // PUSH
46+
}
4147
}
4248
}
4349
}

Packages/blueamulet.udonsharpoptimizer/Editor/Optimizations/OPTUnreadCopy.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,21 @@
44

55
namespace UdonSharpOptimizer.Optimizations
66
{
7-
internal class OPTUnreadCopy : IBaseOptimization
7+
internal class OPTUnreadCopy : BaseOptimization
88
{
9-
public bool Enabled()
10-
{
11-
return OptimizerSettings.Instance.CleanUnreadCopy;
12-
}
9+
protected override string GUILabel => "Unread Copy";
10+
11+
public override bool Enabled => OptimizerSettings.Instance.CleanUnreadCopy;
1312

14-
public void ProcessInstruction(Optimizer optimizer, List<AssemblyInstruction> instrs, int i)
13+
public override void ProcessInstruction(Optimizer optimizer, List<AssemblyInstruction> instrs, int i)
1514
{
1615
// Remove Copy: Unread target (Cleans up Cow dirty)
1716
if (instrs[i] is CopyInstruction cInst)
1817
{
1918
if (Optimizer.IsPrivate(cInst.TargetValue) && !optimizer.ReadScan(_ => false, cInst.TargetValue))
2019
{
2120
instrs[i] = optimizer.TransferInstr(Optimizer.CopyComment("OPTUnreadCopy", cInst), cInst);
22-
optimizer.removedInsts += 3; // PUSH, PUSH, COPY
21+
CountRemoved(optimizer, 3); // PUSH, PUSH, COPY
2322
}
2423
}
2524
}

Packages/blueamulet.udonsharpoptimizer/Editor/Optimizer.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Unofficial UdonSharp Optimizer
33
* The Optimizer.
4-
* Version 1.0.10
4+
* Version 1.0.11
55
* Written by BlueAmulet
66
*/
77

@@ -43,7 +43,7 @@ internal class Optimizer
4343
public static int RemovedThisTotal => _removedThisTotal;
4444

4545
// Optimizations
46-
readonly IBaseOptimization[] _optimizations = new IBaseOptimization[]
46+
private static readonly IBaseOptimization[] _optimizations = new IBaseOptimization[]
4747
{
4848
new OPTCopyLoad(),
4949
new OPTCopyTest(),
@@ -70,13 +70,25 @@ internal static void ResetGlobalCounters()
7070
_removedInstructions = 0;
7171
_removedVariables = 0;
7272
_removedThisTotal = 0;
73+
foreach (IBaseOptimization optimization in _optimizations)
74+
{
75+
optimization.ResetStats();
76+
}
7377
}
7478

7579
internal static void LogGlobalCounters()
7680
{
7781
Debug.Log($"[Optimizer] Removed {_removedInstructions} instructions, {_removedVariables} variables, and {_removedThisTotal} extra __this total");
7882
}
7983

84+
internal static void OnGUI()
85+
{
86+
foreach (IBaseOptimization optimization in _optimizations)
87+
{
88+
optimization.OnGUI();
89+
}
90+
}
91+
8092
/* The Optimizer */
8193
internal static Comment CopyComment(string code, CopyInstruction cInst)
8294
{
@@ -242,7 +254,7 @@ internal void OptimizeProgram()
242254
List<IBaseOptimization> activeOptList = new List<IBaseOptimization>();
243255
foreach (IBaseOptimization optimization in _optimizations)
244256
{
245-
if (optimization.Enabled())
257+
if (optimization.Enabled)
246258
{
247259
activeOptList.Add(optimization);
248260
}

0 commit comments

Comments
 (0)