mirror of
https://github.com/Ryujinx/Ryujinx.git
synced 2024-11-13 13:16:39 +00:00
d904706fc0
* Implement Jump Table for Native Calls NOTE: this slows down rejit considerably! Not recommended to be used without codegen optimisation or AOT. - Does not work on Linux - A32 needs an additional commit. * A32 Support (WIP) * Actually write Direct Call pointers to the table That would help. * Direct Calls: Rather than returning to the translator, attempt to keep within the native stack frame. A return to the translator can still happen, but only by exceptionally bubbling up to it. Also: - Always translate lowCq as a function. Faster interop with the direct jumps, and this will be useful in future if we want to do speculative translation. - Tail Call Detection: after the decoding stage, detect if we do a tail call, and avoid translating into it. Detected if a jump is made to an address outwith the contiguous sequence of blocks surrounding the entry point. The goal is to reduce code touched by jit and rejit. * A32 Support * Use smaller max function size for lowCq, fix exceptional returns When a return has an unexpected value and there is no code block following this one, we now return the value rather than continuing. * CompareAndSwap (buggy) * Ensure CompareAndSwap does not get optimized away. * Use CompareAndSwap to make the dynamic table thread safe. * Tail call for linux, throw on too many arguments. * Combine CompareAndSwap 128 and 32/64. They emit different IR instructions since their PreAllocator behaviour is different, but now they just have one function on EmitterContext. * Fix issues separating from optimisations. * Use a stub to find and execute missing functions. This allows us to skip doing many runtime comparisons and branches, and reduces the amount of code we need to emit significantly. For the indirect call table, this stub also does the work of moving in the highCq address to the table when one is found. * Make Jump Tables and Jit Cache dynmically resize Reserve virtual memory, commit as needed. * Move TailCallRemover to its own class. * Multithreaded Translation (based on heuristic) A poor one, at that. Need to get core count for a better one, which means a lot of OS specific garbage. * Better priority management for background threads. * Bound core limit a bit more Past a certain point the load is not paralellizable and starts stealing from the main thread. Likely due to GC, memory, heap allocation thread contention. Reduce by one core til optimisations come to improve the situation. * Fix memory management on linux. * Temporary solution to some sync problems. This will make sure threads exit correctly, most of the time. There is a potential race where setting the sync counter to 0 does nothing (counter stays at what it was before, thread could take too long to exit), but we need to find a better way to do this anyways. Synchronization frequency has been tightened as we never enter blockwise segments of code. Essentially this means, check every x functions or loop iterations, before lowcq blocks existed and were worth just as much. Ideally it should be done in a better way, since functions can be anywhere from 1 to 5000 instructions. (maybe based on host timer, or an interrupt flag from a scheduler thread) * Address feedback minus CompareAndSwap change. * Use default ReservedRegion granularity. * Merge CompareAndSwap with its V128 variant. * We already got the source, no need to do it again. * Make sure all background translation threads exit. * Fix CompareAndSwap128 Detection criteria was a bit scuffed. * Address Comments.
576 lines
No EOL
17 KiB
C#
576 lines
No EOL
17 KiB
C#
using ARMeilleure.IntermediateRepresentation;
|
|
using ARMeilleure.State;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Runtime.InteropServices;
|
|
|
|
using static ARMeilleure.IntermediateRepresentation.OperandHelper;
|
|
|
|
namespace ARMeilleure.Translation
|
|
{
|
|
class EmitterContext
|
|
{
|
|
private Dictionary<Operand, BasicBlock> _irLabels;
|
|
|
|
private IntrusiveList<BasicBlock> _irBlocks;
|
|
|
|
private BasicBlock _irBlock;
|
|
|
|
private bool _needsNewBlock;
|
|
|
|
public EmitterContext()
|
|
{
|
|
_irLabels = new Dictionary<Operand, BasicBlock>();
|
|
|
|
_irBlocks = new IntrusiveList<BasicBlock>();
|
|
|
|
_needsNewBlock = true;
|
|
}
|
|
|
|
public Operand Add(Operand op1, Operand op2)
|
|
{
|
|
return Add(Instruction.Add, Local(op1.Type), op1, op2);
|
|
}
|
|
|
|
public Operand BitwiseAnd(Operand op1, Operand op2)
|
|
{
|
|
return Add(Instruction.BitwiseAnd, Local(op1.Type), op1, op2);
|
|
}
|
|
|
|
public Operand BitwiseExclusiveOr(Operand op1, Operand op2)
|
|
{
|
|
return Add(Instruction.BitwiseExclusiveOr, Local(op1.Type), op1, op2);
|
|
}
|
|
|
|
public Operand BitwiseNot(Operand op1)
|
|
{
|
|
return Add(Instruction.BitwiseNot, Local(op1.Type), op1);
|
|
}
|
|
|
|
public Operand BitwiseOr(Operand op1, Operand op2)
|
|
{
|
|
return Add(Instruction.BitwiseOr, Local(op1.Type), op1, op2);
|
|
}
|
|
|
|
public void Branch(Operand label)
|
|
{
|
|
Add(Instruction.Branch, null);
|
|
|
|
BranchToLabel(label);
|
|
}
|
|
|
|
public void BranchIfFalse(Operand label, Operand op1)
|
|
{
|
|
Add(Instruction.BranchIfFalse, null, op1);
|
|
|
|
BranchToLabel(label);
|
|
}
|
|
|
|
public void BranchIfTrue(Operand label, Operand op1)
|
|
{
|
|
Add(Instruction.BranchIfTrue, null, op1);
|
|
|
|
BranchToLabel(label);
|
|
}
|
|
|
|
public Operand ByteSwap(Operand op1)
|
|
{
|
|
return Add(Instruction.ByteSwap, Local(op1.Type), op1);
|
|
}
|
|
|
|
public Operand Call(Delegate func, params Operand[] callArgs)
|
|
{
|
|
// Add the delegate to the cache to ensure it will not be garbage collected.
|
|
func = DelegateCache.GetOrAdd(func);
|
|
|
|
IntPtr ptr = Marshal.GetFunctionPointerForDelegate<Delegate>(func);
|
|
|
|
OperandType returnType = GetOperandType(func.Method.ReturnType);
|
|
|
|
return Call(Const(ptr.ToInt64()), returnType, callArgs);
|
|
}
|
|
|
|
private static Dictionary<TypeCode, OperandType> _typeCodeToOperandTypeMap =
|
|
new Dictionary<TypeCode, OperandType>()
|
|
{
|
|
{ TypeCode.Boolean, OperandType.I32 },
|
|
{ TypeCode.Byte, OperandType.I32 },
|
|
{ TypeCode.Char, OperandType.I32 },
|
|
{ TypeCode.Double, OperandType.FP64 },
|
|
{ TypeCode.Int16, OperandType.I32 },
|
|
{ TypeCode.Int32, OperandType.I32 },
|
|
{ TypeCode.Int64, OperandType.I64 },
|
|
{ TypeCode.SByte, OperandType.I32 },
|
|
{ TypeCode.Single, OperandType.FP32 },
|
|
{ TypeCode.UInt16, OperandType.I32 },
|
|
{ TypeCode.UInt32, OperandType.I32 },
|
|
{ TypeCode.UInt64, OperandType.I64 }
|
|
};
|
|
|
|
private static OperandType GetOperandType(Type type)
|
|
{
|
|
if (_typeCodeToOperandTypeMap.TryGetValue(Type.GetTypeCode(type), out OperandType ot))
|
|
{
|
|
return ot;
|
|
}
|
|
else if (type == typeof(V128))
|
|
{
|
|
return OperandType.V128;
|
|
}
|
|
else if (type == typeof(void))
|
|
{
|
|
return OperandType.None;
|
|
}
|
|
|
|
throw new ArgumentException($"Invalid type \"{type.Name}\".");
|
|
}
|
|
|
|
public Operand Call(Operand address, OperandType returnType, params Operand[] callArgs)
|
|
{
|
|
Operand[] args = new Operand[callArgs.Length + 1];
|
|
|
|
args[0] = address;
|
|
|
|
Array.Copy(callArgs, 0, args, 1, callArgs.Length);
|
|
|
|
if (returnType != OperandType.None)
|
|
{
|
|
return Add(Instruction.Call, Local(returnType), args);
|
|
}
|
|
else
|
|
{
|
|
return Add(Instruction.Call, null, args);
|
|
}
|
|
}
|
|
|
|
public void Tailcall(Operand address, params Operand[] callArgs)
|
|
{
|
|
Operand[] args = new Operand[callArgs.Length + 1];
|
|
|
|
args[0] = address;
|
|
|
|
Array.Copy(callArgs, 0, args, 1, callArgs.Length);
|
|
|
|
Add(Instruction.Tailcall, null, args);
|
|
|
|
_needsNewBlock = true;
|
|
}
|
|
|
|
public Operand CompareAndSwap(Operand address, Operand expected, Operand desired)
|
|
{
|
|
return Add(Instruction.CompareAndSwap, Local(desired.Type), address, expected, desired);
|
|
}
|
|
|
|
public Operand ConditionalSelect(Operand op1, Operand op2, Operand op3)
|
|
{
|
|
return Add(Instruction.ConditionalSelect, Local(op2.Type), op1, op2, op3);
|
|
}
|
|
|
|
public Operand ConvertI64ToI32(Operand op1)
|
|
{
|
|
if (op1.Type != OperandType.I64)
|
|
{
|
|
throw new ArgumentException($"Invalid operand type \"{op1.Type}\".");
|
|
}
|
|
|
|
return Add(Instruction.ConvertI64ToI32, Local(OperandType.I32), op1);
|
|
}
|
|
|
|
public Operand ConvertToFP(OperandType type, Operand op1)
|
|
{
|
|
return Add(Instruction.ConvertToFP, Local(type), op1);
|
|
}
|
|
|
|
public Operand ConvertToFPUI(OperandType type, Operand op1)
|
|
{
|
|
return Add(Instruction.ConvertToFPUI, Local(type), op1);
|
|
}
|
|
|
|
public Operand Copy(Operand op1)
|
|
{
|
|
return Add(Instruction.Copy, Local(op1.Type), op1);
|
|
}
|
|
|
|
public Operand Copy(Operand dest, Operand op1)
|
|
{
|
|
if (dest.Kind != OperandKind.Register)
|
|
{
|
|
throw new ArgumentException($"Invalid dest operand kind \"{dest.Kind}\".");
|
|
}
|
|
|
|
return Add(Instruction.Copy, dest, op1);
|
|
}
|
|
|
|
public Operand CountLeadingZeros(Operand op1)
|
|
{
|
|
return Add(Instruction.CountLeadingZeros, Local(op1.Type), op1);
|
|
}
|
|
|
|
internal Operand CpuId()
|
|
{
|
|
return Add(Instruction.CpuId, Local(OperandType.I64));
|
|
}
|
|
|
|
public Operand Divide(Operand op1, Operand op2)
|
|
{
|
|
return Add(Instruction.Divide, Local(op1.Type), op1, op2);
|
|
}
|
|
|
|
public Operand DivideUI(Operand op1, Operand op2)
|
|
{
|
|
return Add(Instruction.DivideUI, Local(op1.Type), op1, op2);
|
|
}
|
|
|
|
public Operand ICompareEqual(Operand op1, Operand op2)
|
|
{
|
|
return Add(Instruction.CompareEqual, Local(OperandType.I32), op1, op2);
|
|
}
|
|
|
|
public Operand ICompareGreater(Operand op1, Operand op2)
|
|
{
|
|
return Add(Instruction.CompareGreater, Local(OperandType.I32), op1, op2);
|
|
}
|
|
|
|
public Operand ICompareGreaterOrEqual(Operand op1, Operand op2)
|
|
{
|
|
return Add(Instruction.CompareGreaterOrEqual, Local(OperandType.I32), op1, op2);
|
|
}
|
|
|
|
public Operand ICompareGreaterOrEqualUI(Operand op1, Operand op2)
|
|
{
|
|
return Add(Instruction.CompareGreaterOrEqualUI, Local(OperandType.I32), op1, op2);
|
|
}
|
|
|
|
public Operand ICompareGreaterUI(Operand op1, Operand op2)
|
|
{
|
|
return Add(Instruction.CompareGreaterUI, Local(OperandType.I32), op1, op2);
|
|
}
|
|
|
|
public Operand ICompareLess(Operand op1, Operand op2)
|
|
{
|
|
return Add(Instruction.CompareLess, Local(OperandType.I32), op1, op2);
|
|
}
|
|
|
|
public Operand ICompareLessOrEqual(Operand op1, Operand op2)
|
|
{
|
|
return Add(Instruction.CompareLessOrEqual, Local(OperandType.I32), op1, op2);
|
|
}
|
|
|
|
public Operand ICompareLessOrEqualUI(Operand op1, Operand op2)
|
|
{
|
|
return Add(Instruction.CompareLessOrEqualUI, Local(OperandType.I32), op1, op2);
|
|
}
|
|
|
|
public Operand ICompareLessUI(Operand op1, Operand op2)
|
|
{
|
|
return Add(Instruction.CompareLessUI, Local(OperandType.I32), op1, op2);
|
|
}
|
|
|
|
public Operand ICompareNotEqual(Operand op1, Operand op2)
|
|
{
|
|
return Add(Instruction.CompareNotEqual, Local(OperandType.I32), op1, op2);
|
|
}
|
|
|
|
public Operand Load(OperandType type, Operand address)
|
|
{
|
|
return Add(Instruction.Load, Local(type), address);
|
|
}
|
|
|
|
public Operand Load16(Operand address)
|
|
{
|
|
return Add(Instruction.Load16, Local(OperandType.I32), address);
|
|
}
|
|
|
|
public Operand Load8(Operand address)
|
|
{
|
|
return Add(Instruction.Load8, Local(OperandType.I32), address);
|
|
}
|
|
|
|
public Operand LoadArgument(OperandType type, int index)
|
|
{
|
|
return Add(Instruction.LoadArgument, Local(type), Const(index));
|
|
}
|
|
|
|
public void LoadFromContext()
|
|
{
|
|
_needsNewBlock = true;
|
|
|
|
Add(Instruction.LoadFromContext);
|
|
}
|
|
|
|
public Operand Multiply(Operand op1, Operand op2)
|
|
{
|
|
return Add(Instruction.Multiply, Local(op1.Type), op1, op2);
|
|
}
|
|
|
|
public Operand Multiply64HighSI(Operand op1, Operand op2)
|
|
{
|
|
return Add(Instruction.Multiply64HighSI, Local(OperandType.I64), op1, op2);
|
|
}
|
|
|
|
public Operand Multiply64HighUI(Operand op1, Operand op2)
|
|
{
|
|
return Add(Instruction.Multiply64HighUI, Local(OperandType.I64), op1, op2);
|
|
}
|
|
|
|
public Operand Negate(Operand op1)
|
|
{
|
|
return Add(Instruction.Negate, Local(op1.Type), op1);
|
|
}
|
|
|
|
public void Return()
|
|
{
|
|
Add(Instruction.Return);
|
|
|
|
_needsNewBlock = true;
|
|
}
|
|
|
|
public void Return(Operand op1)
|
|
{
|
|
Add(Instruction.Return, null, op1);
|
|
|
|
_needsNewBlock = true;
|
|
}
|
|
|
|
public Operand RotateRight(Operand op1, Operand op2)
|
|
{
|
|
return Add(Instruction.RotateRight, Local(op1.Type), op1, op2);
|
|
}
|
|
|
|
public Operand ShiftLeft(Operand op1, Operand op2)
|
|
{
|
|
return Add(Instruction.ShiftLeft, Local(op1.Type), op1, op2);
|
|
}
|
|
|
|
public Operand ShiftRightSI(Operand op1, Operand op2)
|
|
{
|
|
return Add(Instruction.ShiftRightSI, Local(op1.Type), op1, op2);
|
|
}
|
|
|
|
public Operand ShiftRightUI(Operand op1, Operand op2)
|
|
{
|
|
return Add(Instruction.ShiftRightUI, Local(op1.Type), op1, op2);
|
|
}
|
|
|
|
public Operand SignExtend16(OperandType type, Operand op1)
|
|
{
|
|
return Add(Instruction.SignExtend16, Local(type), op1);
|
|
}
|
|
|
|
public Operand SignExtend32(OperandType type, Operand op1)
|
|
{
|
|
return Add(Instruction.SignExtend32, Local(type), op1);
|
|
}
|
|
|
|
public Operand SignExtend8(OperandType type, Operand op1)
|
|
{
|
|
return Add(Instruction.SignExtend8, Local(type), op1);
|
|
}
|
|
|
|
public void Store(Operand address, Operand value)
|
|
{
|
|
Add(Instruction.Store, null, address, value);
|
|
}
|
|
|
|
public void Store16(Operand address, Operand value)
|
|
{
|
|
Add(Instruction.Store16, null, address, value);
|
|
}
|
|
|
|
public void Store8(Operand address, Operand value)
|
|
{
|
|
Add(Instruction.Store8, null, address, value);
|
|
}
|
|
|
|
public void StoreToContext()
|
|
{
|
|
Add(Instruction.StoreToContext);
|
|
|
|
_needsNewBlock = true;
|
|
}
|
|
|
|
public Operand Subtract(Operand op1, Operand op2)
|
|
{
|
|
return Add(Instruction.Subtract, Local(op1.Type), op1, op2);
|
|
}
|
|
|
|
public Operand VectorCreateScalar(Operand value)
|
|
{
|
|
return Add(Instruction.VectorCreateScalar, Local(OperandType.V128), value);
|
|
}
|
|
|
|
public Operand VectorExtract(OperandType type, Operand vector, int index)
|
|
{
|
|
return Add(Instruction.VectorExtract, Local(type), vector, Const(index));
|
|
}
|
|
|
|
public Operand VectorExtract16(Operand vector, int index)
|
|
{
|
|
return Add(Instruction.VectorExtract16, Local(OperandType.I32), vector, Const(index));
|
|
}
|
|
|
|
public Operand VectorExtract8(Operand vector, int index)
|
|
{
|
|
return Add(Instruction.VectorExtract8, Local(OperandType.I32), vector, Const(index));
|
|
}
|
|
|
|
public Operand VectorInsert(Operand vector, Operand value, int index)
|
|
{
|
|
return Add(Instruction.VectorInsert, Local(OperandType.V128), vector, value, Const(index));
|
|
}
|
|
|
|
public Operand VectorInsert16(Operand vector, Operand value, int index)
|
|
{
|
|
return Add(Instruction.VectorInsert16, Local(OperandType.V128), vector, value, Const(index));
|
|
}
|
|
|
|
public Operand VectorInsert8(Operand vector, Operand value, int index)
|
|
{
|
|
return Add(Instruction.VectorInsert8, Local(OperandType.V128), vector, value, Const(index));
|
|
}
|
|
|
|
public Operand VectorZero()
|
|
{
|
|
return Add(Instruction.VectorZero, Local(OperandType.V128));
|
|
}
|
|
|
|
public Operand VectorZeroUpper64(Operand vector)
|
|
{
|
|
return Add(Instruction.VectorZeroUpper64, Local(OperandType.V128), vector);
|
|
}
|
|
|
|
public Operand VectorZeroUpper96(Operand vector)
|
|
{
|
|
return Add(Instruction.VectorZeroUpper96, Local(OperandType.V128), vector);
|
|
}
|
|
|
|
public Operand ZeroExtend16(OperandType type, Operand op1)
|
|
{
|
|
return Add(Instruction.ZeroExtend16, Local(type), op1);
|
|
}
|
|
|
|
public Operand ZeroExtend32(OperandType type, Operand op1)
|
|
{
|
|
return Add(Instruction.ZeroExtend32, Local(type), op1);
|
|
}
|
|
|
|
public Operand ZeroExtend8(OperandType type, Operand op1)
|
|
{
|
|
return Add(Instruction.ZeroExtend8, Local(type), op1);
|
|
}
|
|
|
|
private Operand Add(Instruction inst, Operand dest = null, params Operand[] sources)
|
|
{
|
|
if (_needsNewBlock)
|
|
{
|
|
NewNextBlock();
|
|
}
|
|
|
|
Operation operation = new Operation(inst, dest, sources);
|
|
|
|
_irBlock.Operations.AddLast(operation);
|
|
|
|
return dest;
|
|
}
|
|
|
|
public Operand AddIntrinsic(Intrinsic intrin, params Operand[] args)
|
|
{
|
|
return Add(intrin, Local(OperandType.V128), args);
|
|
}
|
|
|
|
public Operand AddIntrinsicInt(Intrinsic intrin, params Operand[] args)
|
|
{
|
|
return Add(intrin, Local(OperandType.I32), args);
|
|
}
|
|
|
|
public Operand AddIntrinsicLong(Intrinsic intrin, params Operand[] args)
|
|
{
|
|
return Add(intrin, Local(OperandType.I64), args);
|
|
}
|
|
|
|
private Operand Add(Intrinsic intrin, Operand dest, params Operand[] sources)
|
|
{
|
|
if (_needsNewBlock)
|
|
{
|
|
NewNextBlock();
|
|
}
|
|
|
|
IntrinsicOperation operation = new IntrinsicOperation(intrin, dest, sources);
|
|
|
|
_irBlock.Operations.AddLast(operation);
|
|
|
|
return dest;
|
|
}
|
|
|
|
private void BranchToLabel(Operand label)
|
|
{
|
|
if (!_irLabels.TryGetValue(label, out BasicBlock branchBlock))
|
|
{
|
|
branchBlock = new BasicBlock();
|
|
|
|
_irLabels.Add(label, branchBlock);
|
|
}
|
|
|
|
_irBlock.Branch = branchBlock;
|
|
|
|
_needsNewBlock = true;
|
|
}
|
|
|
|
public void MarkLabel(Operand label)
|
|
{
|
|
if (_irLabels.TryGetValue(label, out BasicBlock nextBlock))
|
|
{
|
|
nextBlock.Index = _irBlocks.Count;
|
|
|
|
_irBlocks.AddLast(nextBlock);
|
|
|
|
NextBlock(nextBlock);
|
|
}
|
|
else
|
|
{
|
|
NewNextBlock();
|
|
|
|
_irLabels.Add(label, _irBlock);
|
|
}
|
|
}
|
|
|
|
private void NewNextBlock()
|
|
{
|
|
BasicBlock block = new BasicBlock(_irBlocks.Count);
|
|
|
|
_irBlocks.AddLast(block);
|
|
|
|
NextBlock(block);
|
|
}
|
|
|
|
private void NextBlock(BasicBlock nextBlock)
|
|
{
|
|
if (_irBlock != null && !EndsWithUnconditional(_irBlock))
|
|
{
|
|
_irBlock.Next = nextBlock;
|
|
}
|
|
|
|
_irBlock = nextBlock;
|
|
|
|
_needsNewBlock = false;
|
|
}
|
|
|
|
private static bool EndsWithUnconditional(BasicBlock block)
|
|
{
|
|
Operation lastOp = block.GetLastOp() as Operation;
|
|
|
|
if (lastOp == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return lastOp.Instruction == Instruction.Branch ||
|
|
lastOp.Instruction == Instruction.Return;
|
|
}
|
|
|
|
public ControlFlowGraph GetControlFlowGraph()
|
|
{
|
|
return new ControlFlowGraph(_irBlocks.First, _irBlocks);
|
|
}
|
|
}
|
|
} |