Skip to content

Commit

Permalink
Bring up hot-cold splitting on NativeAOT
Browse files Browse the repository at this point in the history
  • Loading branch information
cshung committed Sep 11, 2022
1 parent 9a3feae commit f153d83
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 30 deletions.
34 changes: 5 additions & 29 deletions src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ private void PublishCode()
, isFoldable: (_compilation._compilationOptions & RyuJitCompilationOptions.MethodBodyFolding) != 0
#endif
);
#if READYTORUN

if (_methodColdCodeNode != null)
{
var relocs2 = _coldCodeRelocs.ToArray();
Expand All @@ -484,9 +484,10 @@ private void PublishCode()
alignment,
new ISymbolDefinitionNode[] { _methodColdCodeNode });
_methodColdCodeNode.SetCode(coldObjectData);
#if READYTORUN
_methodCodeNode.SetColdCodeNode(_methodColdCodeNode);
}
#endif
}

_methodCodeNode.InitializeFrameInfos(_frameInfos);
#if READYTORUN
Expand Down Expand Up @@ -631,9 +632,8 @@ private void CompileMethodCleanup()
}

_methodCodeNode = null;
#if READYTORUN
_methodColdCodeNode = null;
#endif

_code = null;
_coldCode = null;

Expand All @@ -642,18 +642,15 @@ private void CompileMethodCleanup()

_codeRelocs = new ArrayBuilder<Relocation>();
_roDataRelocs = new ArrayBuilder<Relocation>();
#if READYTORUN
_coldCodeRelocs = new ArrayBuilder<Relocation>();
#endif

_numFrameInfos = 0;
_usedFrameInfos = 0;
_frameInfos = null;

#if READYTORUN
_numColdFrameInfos = 0;
_usedColdFrameInfos = 0;
_coldFrameInfos = null;
#endif

_gcInfo = null;
_ehClauses = null;
Expand Down Expand Up @@ -3313,11 +3310,9 @@ private bool getTailCallHelpers(ref CORINFO_RESOLVED_TOKEN callToken, CORINFO_SI
private int _usedFrameInfos;
private FrameInfo[] _frameInfos;

#if READYTORUN
private int _numColdFrameInfos;
private int _usedColdFrameInfos;
private FrameInfo[] _coldFrameInfos;
#endif

private byte[] _gcInfo;
private CORINFO_EH_CLAUSE[] _ehClauses;
Expand All @@ -3329,10 +3324,7 @@ private void allocMem(ref AllocMemArgs args)

if (args.coldCodeSize != 0)
{

#if READYTORUN
this._methodColdCodeNode = new MethodColdCodeNode(MethodBeingCompiled);
#endif
args.coldCodeBlock = (void*)GetPin(_coldCode = new byte[args.coldCodeSize]);
args.coldCodeBlockRW = args.coldCodeBlock;
}
Expand Down Expand Up @@ -3377,23 +3369,19 @@ private void allocMem(ref AllocMemArgs args)
_frameInfos = new FrameInfo[_numFrameInfos];
}

#if READYTORUN
if (_numColdFrameInfos > 0)
{
_coldFrameInfos = new FrameInfo[_numColdFrameInfos];
}
#endif
}

private void reserveUnwindInfo(bool isFunclet, bool isColdCode, uint unwindSize)
{
#if READYTORUN
if (isColdCode)
{
_numColdFrameInfos++;
}
else
#endif
{
_numFrameInfos++;
}
Expand Down Expand Up @@ -3431,18 +3419,14 @@ private void allocUnwindInfo(byte* pHotCode, byte* pColdCode, uint startOffset,
blobData = CompressARM64CFI(blobData);
}
#endif
#if READYTORUN
if (pColdCode == null)
#endif
{
_frameInfos[_usedFrameInfos++] = new FrameInfo(flags, (int)startOffset, (int)endOffset, blobData);
}
#if READYTORUN
else
{
_coldFrameInfos[_usedColdFrameInfos++] = new FrameInfo(flags, (int)startOffset, (int)endOffset, blobData);
}
#endif
}

private void* allocGCInfo(UIntPtr size)
Expand Down Expand Up @@ -3477,9 +3461,7 @@ private void recordCallSite(uint instrOffset, CORINFO_SIG_INFO* callSig, CORINFO

private ArrayBuilder<Relocation> _codeRelocs;
private ArrayBuilder<Relocation> _roDataRelocs;
#if READYTORUN
private ArrayBuilder<Relocation> _coldCodeRelocs;
#endif

/// <summary>
/// Various type of block.
Expand Down Expand Up @@ -3557,11 +3539,9 @@ private ref ArrayBuilder<Relocation> findRelocBlock(BlockType blockType, out int
case BlockType.ROData:
length = _roData.Length;
return ref _roDataRelocs;
#if READYTORUN
case BlockType.ColdCode:
length = _coldCode.Length;
return ref _coldCodeRelocs;
#endif
default:
throw new NotImplementedException("Arbitrary relocs");
}
Expand Down Expand Up @@ -3635,13 +3615,9 @@ private void recordRelocation(void* location, void* locationRW, void* target, us
break;

case BlockType.ColdCode:
#if READYTORUN
Debug.Assert(_methodColdCodeNode != null);
relocTarget = _methodColdCodeNode;
break;
#else
throw new NotImplementedException("ColdCode relocs");
#endif

case BlockType.ROData:
relocTarget = _roDataBlob;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Internal.Text;
using Internal.TypeSystem;
using System.Diagnostics;
using System;

namespace ILCompiler.DependencyAnalysis
{
public class MethodColdCodeNode : ObjectNode, ISymbolDefinitionNode
{
private ObjectData _methodColdCode;
private MethodDesc _owningMethod;

public MethodColdCodeNode(MethodDesc owningMethod)
{
_owningMethod = owningMethod;
}

public int Offset => 0;

public override ObjectNodeSection Section
{
get
{
return _owningMethod.Context.Target.IsWindows ? ObjectNodeSection.ManagedCodeWindowsContentSection : ObjectNodeSection.ManagedCodeUnixContentSection;

}
}

public override bool IsShareable => false;

public override int ClassCode => 788492408;

public override bool StaticDependenciesAreComputed => _methodColdCode != null;

public void AppendMangledName(NameMangler nameMangler, Utf8StringBuilder sb)
{
sb.Append("__coldcode_" + nameMangler.GetMangledMethodName(_owningMethod));
}

public override int CompareToImpl(ISortableNode other, CompilerComparer comparer)
{
MethodColdCodeNode otherNode = (MethodColdCodeNode)other;
return comparer.Compare(_owningMethod, otherNode._owningMethod);
}

public override ObjectData GetData(NodeFactory factory, bool relocsOnly = false) => _methodColdCode;

protected override string GetName(NodeFactory context) => throw new NotImplementedException();

public void SetCode(ObjectData data)
{
Debug.Assert(_methodColdCode == null);
_methodColdCode = data;
}

public int GetColdCodeSize()
{
return _methodColdCode.Data.Length;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ protected override void ComputeDependencyNodeDependencies(List<DependencyNodeCor
}
else
{
CompileMultiThreaded(methodsToCompile);
CompileSingleThreaded(methodsToCompile);
// CompileMultiThreaded(methodsToCompile);
}
}
private void CompileMultiThreaded(List<MethodCodeNode> methodsToCompile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ public override ICompilation ToCompilation()
if (_resilient)
options |= RyuJitCompilationOptions.UseResilience;

// TODO: Andrew Au, Command line option
jitFlagBuilder.Add(CorJitFlag.CORJIT_FLAG_PROCSPLIT);

var factory = new RyuJitNodeFactory(_context, _compilationGroup, _metadataManager, _interopStubManager, _nameMangler, _vtableSliceProvider, _dictionaryLayoutProvider, GetPreinitializationManager());

JitConfigProvider.Initialize(_context.Target, jitFlagBuilder.ToArray(), _ryujitOptions, _jitPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

<ItemGroup>
<Compile Include="Compiler\DependencyAnalysis\MethodCodeNode.cs" />
<Compile Include="Compiler\DependencyAnalysis\MethodColdCodeNode.cs" />
<Compile Include="Compiler\DependencyAnalysis\RyuJitNodeFactory.cs" />
<Compile Include="Compiler\ProfileDataManager.cs" />
<Compile Include="Compiler\RyuJitCompilation.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ unsafe partial class CorInfoImpl
private RyuJitCompilation _compilation;
private MethodDebugInformation _debugInfo;
private MethodCodeNode _methodCodeNode;
private MethodColdCodeNode _methodColdCodeNode;
private DebugLocInfo[] _debugLocInfos;
private DebugVarInfo[] _debugVarInfos;
private readonly UnboxingMethodDescFactory _unboxingThunkFactory = new UnboxingMethodDescFactory();
Expand Down

0 comments on commit f153d83

Please sign in to comment.