Reduce JIT GC allocations (#2515)
* Turn `MemoryOperand` into a struct * Remove `IntrinsicOperation` * Remove `PhiNode` * Remove `Node` * Turn `Operand` into a struct * Turn `Operation` into a struct * Clean up pool management methods * Add `Arena` allocator * Move `OperationHelper` to `Operation.Factory` * Move `OperandHelper` to `Operand.Factory` * Optimize `Operation` a bit * Fix `Arena` initialization * Rename `NativeList<T>` to `ArenaList<T>` * Reduce `Operand` size from 88 to 56 bytes * Reduce `Operation` size from 56 to 40 bytes * Add optimistic interning of Register & Constant operands * Optimize `RegisterUsage` pass a bit * Optimize `RemoveUnusedNodes` pass a bit Iterating in reverse-order allows killing dependency chains in a single pass. * Fix PPTC symbols * Optimize `BasicBlock` a bit Reduce allocations from `_successor` & `DominanceFrontiers` * Fix `Operation` resize * Make `Arena` expandable Change the arena allocator to be expandable by allocating in pages, with some of them being pooled. Currently 32 pages are pooled. An LRU removal mechanism should probably be added to it. Apparently MHR can allocate bitmaps large enough to exceed the 16MB limit for the type. * Move `Arena` & `ArenaList` to `Common` * Remove `ThreadStaticPool` & co * Add `PhiOperation` * Reduce `Operand` size from 56 from 48 bytes * Add linear-probing to `Operand` intern table * Optimize `HybridAllocator` a bit * Add `Allocators` class * Tune `ArenaAllocator` sizes * Add page removal mechanism to `ArenaAllocator` Remove pages which have not been used for more than 5s after each reset. I am on fence if this would be better using a Gen2 callback object like the one in System.Buffers.ArrayPool<T>, to trim the pool. Because right now if a large translation happens, the pages will be freed only after a reset. This reset may not happen for a while because no new translation is hit, but the arena base sizes are rather small. * Fix `OOM` when allocating larger than page size in `ArenaAllocator` Tweak resizing mechanism for Operand.Uses and Assignemnts. * Optimize `Optimizer` a bit * Optimize `Operand.Add<T>/Remove<T>` a bit * Clean up `PreAllocator` * Fix phi insertion order Reduce codegen diffs. * Fix code alignment * Use new heuristics for degree of parallelism * Suppress warnings * Address gdkchan's feedback Renamed `GetValue()` to `GetValueUnsafe()` to make it more clear that `Operand.Value` should usually not be modified directly. * Add fast path to `ArenaAllocator` * Assembly for `ArenaAllocator.Allocate(ulong)`: .L0: mov rax, [rcx+0x18] lea r8, [rax+rdx] cmp r8, [rcx+0x10] ja short .L2 .L1: mov rdx, [rcx+8] add rax, [rdx+8] mov [rcx+0x18], r8 ret .L2: jmp ArenaAllocator.AllocateSlow(UInt64) A few variable/field had to be changed to ulong so that RyuJIT avoids emitting zero-extends. * Implement a new heuristic to free pooled pages. If an arena is used often, it is more likely that its pages will be needed, so the pages are kept for longer (e.g: during PPTC rebuild or burst sof compilations). If is not used often, then it is more likely that its pages will not be needed (e.g: after PPTC rebuild or bursts of compilations). * Address riperiperi's feedback * Use `EqualityComparer<T>` in `IntrusiveList<T>` Avoids a potential GC hole in `Equals(T, T)`.
This commit is contained in:
parent
cd4530f29c
commit
22b2cb39af
91 changed files with 2354 additions and 2142 deletions
|
@ -29,11 +29,11 @@ namespace ARMeilleure.CodeGen.RegisterAllocators
|
|||
|
||||
private LiveInterval[] _parentIntervals;
|
||||
|
||||
private List<(IntrusiveList<Node>, Node)> _operationNodes;
|
||||
private List<(IntrusiveList<Operation>, Operation)> _operationNodes;
|
||||
|
||||
private int _operationsCount;
|
||||
|
||||
private class AllocationContext : IDisposable
|
||||
private class AllocationContext
|
||||
{
|
||||
public RegisterMasks Masks { get; }
|
||||
|
||||
|
@ -50,10 +50,8 @@ namespace ARMeilleure.CodeGen.RegisterAllocators
|
|||
StackAlloc = stackAlloc;
|
||||
Masks = masks;
|
||||
|
||||
BitMapPool.PrepareBitMapPool();
|
||||
|
||||
Active = BitMapPool.Allocate(intervalsCount);
|
||||
Inactive = BitMapPool.Allocate(intervalsCount);
|
||||
Active = new BitMap(Allocators.Default, intervalsCount);
|
||||
Inactive = new BitMap(Allocators.Default, intervalsCount);
|
||||
}
|
||||
|
||||
public void MoveActiveToInactive(int bit)
|
||||
|
@ -72,11 +70,6 @@ namespace ARMeilleure.CodeGen.RegisterAllocators
|
|||
|
||||
dest.Set(bit);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
BitMapPool.ResetBitMapPool();
|
||||
}
|
||||
}
|
||||
|
||||
public AllocationResult RunPass(
|
||||
|
@ -86,7 +79,7 @@ namespace ARMeilleure.CodeGen.RegisterAllocators
|
|||
{
|
||||
NumberLocals(cfg);
|
||||
|
||||
using AllocationContext context = new AllocationContext(stackAlloc, regMasks, _intervals.Count);
|
||||
var context = new AllocationContext(stackAlloc, regMasks, _intervals.Count);
|
||||
|
||||
BuildIntervals(cfg, context);
|
||||
|
||||
|
@ -588,7 +581,7 @@ namespace ARMeilleure.CodeGen.RegisterAllocators
|
|||
|
||||
int splitPosition = kv.Key;
|
||||
|
||||
(IntrusiveList<Node> nodes, Node node) = GetOperationNode(splitPosition);
|
||||
(IntrusiveList<Operation> nodes, Operation node) = GetOperationNode(splitPosition);
|
||||
|
||||
Operation[] sequence = copyResolver.Sequence();
|
||||
|
||||
|
@ -621,9 +614,9 @@ namespace ARMeilleure.CodeGen.RegisterAllocators
|
|||
continue;
|
||||
}
|
||||
|
||||
bool hasSingleOrNoSuccessor = block.SuccessorCount <= 1;
|
||||
bool hasSingleOrNoSuccessor = block.SuccessorsCount <= 1;
|
||||
|
||||
for (int i = 0; i < block.SuccessorCount; i++)
|
||||
for (int i = 0; i < block.SuccessorsCount; i++)
|
||||
{
|
||||
BasicBlock successor = block.GetSuccessor(i);
|
||||
|
||||
|
@ -677,7 +670,7 @@ namespace ARMeilleure.CodeGen.RegisterAllocators
|
|||
{
|
||||
successor.Operations.AddFirst(sequence[0]);
|
||||
|
||||
Node prependNode = sequence[0];
|
||||
Operation prependNode = sequence[0];
|
||||
|
||||
for (int index = 1; index < sequence.Length; index++)
|
||||
{
|
||||
|
@ -710,7 +703,7 @@ namespace ARMeilleure.CodeGen.RegisterAllocators
|
|||
for (int i = usePositions.Count - 1; i >= 0; i--)
|
||||
{
|
||||
int usePosition = -usePositions[i];
|
||||
(_, Node operation) = GetOperationNode(usePosition);
|
||||
(_, Operation operation) = GetOperationNode(usePosition);
|
||||
|
||||
for (int index = 0; index < operation.SourcesCount; index++)
|
||||
{
|
||||
|
@ -722,7 +715,7 @@ namespace ARMeilleure.CodeGen.RegisterAllocators
|
|||
}
|
||||
else if (source.Kind == OperandKind.Memory)
|
||||
{
|
||||
MemoryOperand memOp = (MemoryOperand)source;
|
||||
MemoryOperand memOp = source.GetMemory();
|
||||
|
||||
if (memOp.BaseAddress == current.Local)
|
||||
{
|
||||
|
@ -752,20 +745,20 @@ namespace ARMeilleure.CodeGen.RegisterAllocators
|
|||
{
|
||||
Debug.Assert(!interval.IsSpilled, "Spilled intervals are not allowed.");
|
||||
|
||||
return OperandHelper.Register(
|
||||
return Operand.Factory.Register(
|
||||
interval.Register.Index,
|
||||
interval.Register.Type,
|
||||
interval.Local.Type);
|
||||
}
|
||||
|
||||
private (IntrusiveList<Node>, Node) GetOperationNode(int position)
|
||||
private (IntrusiveList<Operation>, Operation) GetOperationNode(int position)
|
||||
{
|
||||
return _operationNodes[position / InstructionGap];
|
||||
}
|
||||
|
||||
private void NumberLocals(ControlFlowGraph cfg)
|
||||
{
|
||||
_operationNodes = new List<(IntrusiveList<Node>, Node)>();
|
||||
_operationNodes = new List<(IntrusiveList<Operation>, Operation)>();
|
||||
|
||||
_intervals = new List<LiveInterval>();
|
||||
|
||||
|
@ -783,13 +776,14 @@ namespace ARMeilleure.CodeGen.RegisterAllocators
|
|||
{
|
||||
BasicBlock block = cfg.PostOrderBlocks[index];
|
||||
|
||||
for (Node node = block.Operations.First; node != null; node = node.ListNext)
|
||||
for (Operation node = block.Operations.First; node != default; node = node.ListNext)
|
||||
{
|
||||
_operationNodes.Add((block.Operations, node));
|
||||
|
||||
for (int i = 0; i < node.DestinationsCount; i++)
|
||||
{
|
||||
Operand dest = node.GetDestination(i);
|
||||
|
||||
if (dest.Kind == OperandKind.LocalVariable && visited.Add(dest))
|
||||
{
|
||||
dest.NumberLocal(_intervals.Count);
|
||||
|
@ -804,7 +798,7 @@ namespace ARMeilleure.CodeGen.RegisterAllocators
|
|||
if (block.Operations.Count == 0)
|
||||
{
|
||||
// Pretend we have a dummy instruction on the empty block.
|
||||
_operationNodes.Add((null, null));
|
||||
_operationNodes.Add((default, default));
|
||||
|
||||
_operationsCount += InstructionGap;
|
||||
}
|
||||
|
@ -825,10 +819,10 @@ namespace ARMeilleure.CodeGen.RegisterAllocators
|
|||
// Compute local live sets.
|
||||
for (BasicBlock block = cfg.Blocks.First; block != null; block = block.ListNext)
|
||||
{
|
||||
BitMap liveGen = BitMapPool.Allocate(mapSize);
|
||||
BitMap liveKill = BitMapPool.Allocate(mapSize);
|
||||
BitMap liveGen = new BitMap(Allocators.Default, mapSize);
|
||||
BitMap liveKill = new BitMap(Allocators.Default, mapSize);
|
||||
|
||||
for (Node node = block.Operations.First; node != null; node = node.ListNext)
|
||||
for (Operation node = block.Operations.First; node != default; node = node.ListNext)
|
||||
{
|
||||
Sources(node, (source) =>
|
||||
{
|
||||
|
@ -857,8 +851,8 @@ namespace ARMeilleure.CodeGen.RegisterAllocators
|
|||
|
||||
for (int index = 0; index < cfg.Blocks.Count; index++)
|
||||
{
|
||||
blkLiveIn [index] = BitMapPool.Allocate(mapSize);
|
||||
blkLiveOut[index] = BitMapPool.Allocate(mapSize);
|
||||
blkLiveIn [index] = new BitMap(Allocators.Default, mapSize);
|
||||
blkLiveOut[index] = new BitMap(Allocators.Default, mapSize);
|
||||
}
|
||||
|
||||
bool modified;
|
||||
|
@ -873,7 +867,7 @@ namespace ARMeilleure.CodeGen.RegisterAllocators
|
|||
|
||||
BitMap liveOut = blkLiveOut[block.Index];
|
||||
|
||||
for (int i = 0; i < block.SuccessorCount; i++)
|
||||
for (int i = 0; i < block.SuccessorsCount; i++)
|
||||
{
|
||||
BasicBlock succ = block.GetSuccessor(i);
|
||||
|
||||
|
@ -926,7 +920,7 @@ namespace ARMeilleure.CodeGen.RegisterAllocators
|
|||
continue;
|
||||
}
|
||||
|
||||
foreach (Node node in BottomOperations(block))
|
||||
foreach (Operation node in BottomOperations(block))
|
||||
{
|
||||
operationPos -= InstructionGap;
|
||||
|
||||
|
@ -947,7 +941,7 @@ namespace ARMeilleure.CodeGen.RegisterAllocators
|
|||
interval.AddUsePosition(operationPos);
|
||||
});
|
||||
|
||||
if (node is Operation operation && operation.Instruction == Instruction.Call)
|
||||
if (node.Instruction == Instruction.Call)
|
||||
{
|
||||
AddIntervalCallerSavedReg(context.Masks.IntCallerSavedRegisters, operationPos, RegisterType.Integer);
|
||||
AddIntervalCallerSavedReg(context.Masks.VecCallerSavedRegisters, operationPos, RegisterType.Vector);
|
||||
|
@ -993,11 +987,11 @@ namespace ARMeilleure.CodeGen.RegisterAllocators
|
|||
return (register.Index << 1) | (register.Type == RegisterType.Vector ? 1 : 0);
|
||||
}
|
||||
|
||||
private static IEnumerable<Node> BottomOperations(BasicBlock block)
|
||||
private static IEnumerable<Operation> BottomOperations(BasicBlock block)
|
||||
{
|
||||
Node node = block.Operations.Last;
|
||||
Operation node = block.Operations.Last;
|
||||
|
||||
while (node != null && !(node is PhiNode))
|
||||
while (node != default)
|
||||
{
|
||||
yield return node;
|
||||
|
||||
|
@ -1005,7 +999,7 @@ namespace ARMeilleure.CodeGen.RegisterAllocators
|
|||
}
|
||||
}
|
||||
|
||||
private static void Sources(Node node, Action<Operand> action)
|
||||
private static void Sources(Operation node, Action<Operand> action)
|
||||
{
|
||||
for (int index = 0; index < node.SourcesCount; index++)
|
||||
{
|
||||
|
@ -1017,14 +1011,14 @@ namespace ARMeilleure.CodeGen.RegisterAllocators
|
|||
}
|
||||
else if (source.Kind == OperandKind.Memory)
|
||||
{
|
||||
MemoryOperand memOp = (MemoryOperand)source;
|
||||
MemoryOperand memOp = source.GetMemory();
|
||||
|
||||
if (memOp.BaseAddress != null)
|
||||
if (memOp.BaseAddress != default)
|
||||
{
|
||||
action(memOp.BaseAddress);
|
||||
}
|
||||
|
||||
if (memOp.Index != null)
|
||||
if (memOp.Index != default)
|
||||
{
|
||||
action(memOp.Index);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue