From c4b770cbdbd0123a82d779debf003629f3b59b37 Mon Sep 17 00:00:00 2001 From: Kunal Pathak Date: Fri, 12 Nov 2021 09:43:30 -0800 Subject: [PATCH 1/6] Trim the hot code size to the actual code length --- .../tools/Common/JitInterface/CorInfoImpl.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs b/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs index bbc0c99d4f597..ef17f7b202ea7 100644 --- a/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs +++ b/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs @@ -366,6 +366,21 @@ private void CompileMethodInternal(IMethodNode methodCodeNodeNeedingCode, Method #endif } + if (codeSize != allocatedHotCodeSize) + { + // If the generated code is smaller than allocatedHotCodeSize, then trim the codeBlock. + // + // Currently, hot/cold splitting is not done and hence `codeSize` just includes the size of + // hotCode. Once hot/cold splitting is done, need to trim respective `_code` or `_coldCode` + // accordingly. + if (_compilation.Logger.IsVerbose) + { + Log.WriteLine($"Trimming codeSize from {_code.Length} to {codeSize}, Savings: {allocatedHotCodeSize - codeSize}, Method: {_methodCodeNode.Method.Name}"); + } + Debug.Assert(codeSize != 0); + Debug.Assert(allocatedHotCodeSize > codeSize); + Array.Resize(ref _code, (int)codeSize); + } PublishCode(); PublishROData(); } @@ -3242,6 +3257,8 @@ private bool getTailCallHelpers(ref CORINFO_RESOLVED_TOKEN callToken, CORINFO_SI private byte[] _gcInfo; private CORINFO_EH_CLAUSE[] _ehClauses; + private long allocatedHotCodeSize = 0; + private void allocMem(ref AllocMemArgs args) { args.hotCodeBlock = (void*)GetPin(_code = new byte[args.hotCodeSize]); @@ -3252,6 +3269,7 @@ private void allocMem(ref AllocMemArgs args) args.coldCodeBlock = (void*)GetPin(_coldCode = new byte[args.coldCodeSize]); args.coldCodeBlockRW = args.coldCodeBlock; } + allocatedHotCodeSize = args.hotCodeSize; _codeAlignment = -1; if ((args.flag & CorJitAllocMemFlag.CORJIT_ALLOCMEM_FLG_32BYTE_ALIGN) != 0) From 9825f8809d95a7d59ef2547f12951805bbe75494 Mon Sep 17 00:00:00 2001 From: Kunal Pathak Date: Fri, 12 Nov 2021 10:48:39 -0800 Subject: [PATCH 2/6] Remove allocatedHotCodeSize field --- src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs b/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs index ef17f7b202ea7..7c4fead54cf1f 100644 --- a/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs +++ b/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs @@ -366,7 +366,7 @@ private void CompileMethodInternal(IMethodNode methodCodeNodeNeedingCode, Method #endif } - if (codeSize != allocatedHotCodeSize) + if (codeSize != _code.Length) { // If the generated code is smaller than allocatedHotCodeSize, then trim the codeBlock. // @@ -375,10 +375,10 @@ private void CompileMethodInternal(IMethodNode methodCodeNodeNeedingCode, Method // accordingly. if (_compilation.Logger.IsVerbose) { - Log.WriteLine($"Trimming codeSize from {_code.Length} to {codeSize}, Savings: {allocatedHotCodeSize - codeSize}, Method: {_methodCodeNode.Method.Name}"); + Log.WriteLine($"Trimming codeSize from {_code.Length} to {codeSize}, Savings: {_code.Length - codeSize}, Method: {_methodCodeNode.Method.Name}"); } Debug.Assert(codeSize != 0); - Debug.Assert(allocatedHotCodeSize > codeSize); + Debug.Assert(_code.Length > codeSize); Array.Resize(ref _code, (int)codeSize); } PublishCode(); @@ -3257,8 +3257,6 @@ private bool getTailCallHelpers(ref CORINFO_RESOLVED_TOKEN callToken, CORINFO_SI private byte[] _gcInfo; private CORINFO_EH_CLAUSE[] _ehClauses; - private long allocatedHotCodeSize = 0; - private void allocMem(ref AllocMemArgs args) { args.hotCodeBlock = (void*)GetPin(_code = new byte[args.hotCodeSize]); @@ -3269,7 +3267,6 @@ private void allocMem(ref AllocMemArgs args) args.coldCodeBlock = (void*)GetPin(_coldCode = new byte[args.coldCodeSize]); args.coldCodeBlockRW = args.coldCodeBlock; } - allocatedHotCodeSize = args.hotCodeSize; _codeAlignment = -1; if ((args.flag & CorJitAllocMemFlag.CORJIT_ALLOCMEM_FLG_32BYTE_ALIGN) != 0) From d17ee5fe9f27af3b1f8482d3c72b4d427cfb2c12 Mon Sep 17 00:00:00 2001 From: Kunal Pathak Date: Fri, 12 Nov 2021 11:17:40 -0800 Subject: [PATCH 3/6] Remove verbose logging --- src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs b/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs index 7c4fead54cf1f..bfa267f84ca4b 100644 --- a/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs +++ b/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs @@ -373,10 +373,7 @@ private void CompileMethodInternal(IMethodNode methodCodeNodeNeedingCode, Method // Currently, hot/cold splitting is not done and hence `codeSize` just includes the size of // hotCode. Once hot/cold splitting is done, need to trim respective `_code` or `_coldCode` // accordingly. - if (_compilation.Logger.IsVerbose) - { - Log.WriteLine($"Trimming codeSize from {_code.Length} to {codeSize}, Savings: {_code.Length - codeSize}, Method: {_methodCodeNode.Method.Name}"); - } + Debug.Assert(codeSize != 0); Debug.Assert(_code.Length > codeSize); Array.Resize(ref _code, (int)codeSize); From 0fe283c689769b1901cce53c2dc2351f9fede1c2 Mon Sep 17 00:00:00 2001 From: Kunal Pathak Date: Fri, 12 Nov 2021 14:31:06 -0800 Subject: [PATCH 4/6] Perform trimming only for xarch --- .../tools/Common/JitInterface/CorInfoImpl.cs | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs b/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs index bfa267f84ca4b..c1665a87446cd 100644 --- a/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs +++ b/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs @@ -366,17 +366,20 @@ private void CompileMethodInternal(IMethodNode methodCodeNodeNeedingCode, Method #endif } - if (codeSize != _code.Length) + if (codeSize < _code.Length) { - // If the generated code is smaller than allocatedHotCodeSize, then trim the codeBlock. - // - // Currently, hot/cold splitting is not done and hence `codeSize` just includes the size of - // hotCode. Once hot/cold splitting is done, need to trim respective `_code` or `_coldCode` - // accordingly. - - Debug.Assert(codeSize != 0); - Debug.Assert(_code.Length > codeSize); - Array.Resize(ref _code, (int)codeSize); + if (_compilation.TypeSystemContext.Target.Architecture == TargetArchitecture.X64 || + _compilation.TypeSystemContext.Target.Architecture == TargetArchitecture.X86) + { + // For xarch, the generated code is sometimes smaller than the memory allocated. + // In that case, trim the codeBlock to the actual value. + // + // Currently, hot/cold splitting is not done and hence `codeSize` just includes the size of + // hotCode. Once hot/cold splitting is done, need to trim respective `_code` or `_coldCode` + // accordingly. + Debug.Assert(codeSize != 0); + Array.Resize(ref _code, (int)codeSize); + } } PublishCode(); PublishROData(); From 83bf82f739c48742503adc05fa0ec469b70b0416 Mon Sep 17 00:00:00 2001 From: Kunal Pathak Date: Fri, 12 Nov 2021 14:40:38 -0800 Subject: [PATCH 5/6] Include more comment about armarch --- src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs b/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs index c1665a87446cd..8b36a85312504 100644 --- a/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs +++ b/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs @@ -374,6 +374,10 @@ private void CompileMethodInternal(IMethodNode methodCodeNodeNeedingCode, Method // For xarch, the generated code is sometimes smaller than the memory allocated. // In that case, trim the codeBlock to the actual value. // + // For armarch, the allocation request of `hotCodeSize` also includes the roData size + // while the `codeSize` returned just contains the size of the native code. As such, + // there is guarantee that for armarch, (codeSize == _code.Length) is always true. + // // Currently, hot/cold splitting is not done and hence `codeSize` just includes the size of // hotCode. Once hot/cold splitting is done, need to trim respective `_code` or `_coldCode` // accordingly. From 5effeea070504033eb853c0bf5bb3b9e8c8fafa5 Mon Sep 17 00:00:00 2001 From: Kunal Pathak Date: Fri, 12 Nov 2021 15:20:23 -0800 Subject: [PATCH 6/6] Fix the condition for arm64 --- src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs b/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs index 8b36a85312504..b23de8abbd5bb 100644 --- a/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs +++ b/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs @@ -368,13 +368,12 @@ private void CompileMethodInternal(IMethodNode methodCodeNodeNeedingCode, Method if (codeSize < _code.Length) { - if (_compilation.TypeSystemContext.Target.Architecture == TargetArchitecture.X64 || - _compilation.TypeSystemContext.Target.Architecture == TargetArchitecture.X86) + if (_compilation.TypeSystemContext.Target.Architecture != TargetArchitecture.ARM64) { - // For xarch, the generated code is sometimes smaller than the memory allocated. + // For xarch/arm32, the generated code is sometimes smaller than the memory allocated. // In that case, trim the codeBlock to the actual value. // - // For armarch, the allocation request of `hotCodeSize` also includes the roData size + // For arm64, the allocation request of `hotCodeSize` also includes the roData size // while the `codeSize` returned just contains the size of the native code. As such, // there is guarantee that for armarch, (codeSize == _code.Length) is always true. //