Skip to content

Commit

Permalink
1.0.10
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueAmulet committed Sep 15, 2024
1 parent 688aed9 commit 6653fa5
Show file tree
Hide file tree
Showing 22 changed files with 681 additions and 394 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections.Generic;
using UdonSharp.Compiler.Assembly;

namespace UdonSharpOptimizer.Optimizations
{
internal interface IBaseOptimization
{
bool Enabled();

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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Collections.Generic;
using UdonSharp.Compiler.Assembly;
using UdonSharp.Compiler.Assembly.Instructions;

namespace UdonSharpOptimizer.Optimizations
{
internal class OPTCopyLoad : IBaseOptimization
{
public bool Enabled()
{
return OptimizerSettings.Instance.CopyAndLoad;
}

public void ProcessInstruction(Optimizer optimizer, List<AssemblyInstruction> instrs, int i)
{
// Remove Copy: Copy + JumpIf
if (instrs[i] is CopyInstruction cInst && instrs[i + 1] is PushInstruction pInst)
{
if (Optimizer.IsPrivate(cInst.TargetValue) && cInst.TargetValue.UniqueID == pInst.PushValue.UniqueID && !optimizer.HasJump(pInst) && !optimizer.ReadScan(n => n == i + 1, cInst.TargetValue))
{
instrs[i] = optimizer.TransferInstr(Optimizer.CopyComment("OPTCopyLoad", cInst), cInst);
instrs[i + 1] = optimizer.TransferInstr(new PushInstruction(cInst.SourceValue), pInst);
optimizer.removedInsts += 3; // PUSH, PUSH, COPY
}
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Collections.Generic;
using UdonSharp.Compiler.Assembly;
using UdonSharp.Compiler.Assembly.Instructions;

namespace UdonSharpOptimizer.Optimizations
{
internal class OPTCopyTest : IBaseOptimization
{
public bool Enabled()
{
return OptimizerSettings.Instance.CopyAndTest;
}

public void ProcessInstruction(Optimizer optimizer, List<AssemblyInstruction> instrs, int i)
{
// Remove Copy: Copy + JumpIf
if (instrs[i] is CopyInstruction cInst && i < instrs.Count - 1 && instrs[i + 1] is JumpIfFalseInstruction jifInst)
{
if (Optimizer.IsPrivate(cInst.TargetValue) && cInst.TargetValue.UniqueID == jifInst.ConditionValue.UniqueID && !optimizer.HasJump(jifInst) && !optimizer.ReadScan(n => n == i + 1, cInst.TargetValue))
{
instrs[i] = optimizer.TransferInstr(Optimizer.CopyComment("OPTCopyTest", cInst), cInst);
instrs[i + 1] = optimizer.TransferInstr(new JumpIfFalseInstruction(jifInst.JumpTarget, cInst.SourceValue), jifInst);
optimizer.removedInsts += 3; // PUSH, PUSH, COPY
}
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Collections.Generic;
using UdonSharp.Compiler.Assembly;
using UdonSharp.Compiler.Assembly.Instructions;

namespace UdonSharpOptimizer.Optimizations
{
internal class OPTDoubleCopy : IBaseOptimization
{
public bool Enabled()
{
return OptimizerSettings.Instance.DoubleCopy;
}

public void ProcessInstruction(Optimizer optimizer, List<AssemblyInstruction> instrs, int i)
{
// Remove Copy: Copy + Copy
if (instrs[i] is CopyInstruction cInst1 && i < instrs.Count - 1 && instrs[i + 1] is CopyInstruction cInst2)
{
if (Optimizer.IsPrivate(cInst1.TargetValue) && cInst1.TargetValue.UniqueID == cInst2.SourceValue.UniqueID && !optimizer.HasJump(cInst2) && !optimizer.ReadScan(n => n == i + 1, cInst1.TargetValue))
{
instrs[i] = optimizer.TransferInstr(Optimizer.CopyComment("OPTDoubleCopy", cInst1), cInst1);
instrs[i + 1] = optimizer.TransferInstr(new CopyInstruction(cInst1.SourceValue, cInst2.TargetValue), cInst2);
optimizer.removedInsts += 3; // PUSH, PUSH, COPY
}
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Collections.Generic;
using UdonSharp.Compiler.Assembly;
using UdonSharp.Compiler.Assembly.Instructions;

namespace UdonSharpOptimizer.Optimizations
{
internal class OPTStoreCopy : IBaseOptimization
{
public bool Enabled()
{
return OptimizerSettings.Instance.StoreAndCopy;
}

public void ProcessInstruction(Optimizer optimizer, List<AssemblyInstruction> instrs, int i)
{
// Remove Copy: Extern + Copy
if (instrs[i] is PushInstruction pInst && i < instrs.Count - 2 && Optimizer.IsExternWrite(instrs[i + 1]) && instrs[i + 2] is CopyInstruction cInst)
{
if (Optimizer.IsPrivate(pInst.PushValue) && pInst.PushValue.UniqueID == cInst.SourceValue.UniqueID && !optimizer.HasJump(i + 1, i + 2) && !optimizer.ReadScan(n => n == i || n == i + 2, pInst.PushValue))
{
instrs[i] = optimizer.TransferInstr(new PushInstruction(cInst.TargetValue), pInst);
instrs[i + 2] = optimizer.TransferInstr(Optimizer.CopyComment("OPTStoreCopy", cInst), cInst);
optimizer.removedInsts += 3; // PUSH, PUSH, COPY
}
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Collections.Generic;
using UdonSharp.Compiler.Assembly;
using UdonSharp.Compiler.Assembly.Instructions;
using UdonSharp.Compiler.Emit;

namespace UdonSharpOptimizer.Optimizations
{
internal class OPTTailCall : IBaseOptimization
{
public bool Enabled()
{
return OptimizerSettings.Instance.EnableTCO;
}

public void ProcessInstruction(Optimizer optimizer, List<AssemblyInstruction> instrs, int i)
{
// Tail call optimization
// TODO: Properly verify this jump is to a method
if (instrs[i] is JumpInstruction && instrs[i + 1] is RetInstruction rInst && !optimizer.HasJump(rInst) && instrs[i - 1] is Comment cInst && cInst.Comment.StartsWith("Calling "))
{
// Locate the corresponding push above
int pushIdx = -1;
for (int j = i - 1; j >= 0; j--)
{
if (instrs[j] is PushInstruction pInst && pInst.PushValue.Flags == Value.ValueFlags.InternalGlobal && pInst.PushValue.DefaultValue is uint val && pInst.PushValue.UniqueID.StartsWith("__gintnl_RetAddress_") && val == rInst.InstructionAddress)
{
pushIdx = j;
break;
}
else if (optimizer.HasJump(j))
{
break;
}
}
if (pushIdx != -1)
{
PushInstruction pInst = (PushInstruction)instrs[pushIdx];
instrs[pushIdx] = optimizer.TransferInstr(new Comment($"OPTTailCall: Tail call optimization, removed PUSH {pInst.PushValue.UniqueID}"), instrs[pushIdx]);
instrs[i + 1] = optimizer.TransferInstr(new Comment($"OPTTailCall: Tail call optimization, removed RET {rInst.RetValRef.UniqueID}"), rInst);
optimizer.removedInsts += 4; // PUSH & PUSH + COPY + JUMP_INDIRECT
}
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Collections.Generic;
using UdonSharp.Compiler.Assembly;
using UdonSharp.Compiler.Assembly.Instructions;

namespace UdonSharpOptimizer.Optimizations
{
internal class OPTUnreadCopy : IBaseOptimization
{
public bool Enabled()
{
return OptimizerSettings.Instance.CleanUnreadCopy;
}

public void ProcessInstruction(Optimizer optimizer, List<AssemblyInstruction> instrs, int i)
{
// Remove Copy: Unread target (Cleans up Cow dirty)
if (instrs[i] is CopyInstruction cInst)
{
if (Optimizer.IsPrivate(cInst.TargetValue) && !optimizer.ReadScan(_ => false, cInst.TargetValue))
{
instrs[i] = optimizer.TransferInstr(Optimizer.CopyComment("OPTUnreadCopy", cInst), cInst);
optimizer.removedInsts += 3; // PUSH, PUSH, COPY
}
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6653fa5

Please sign in to comment.