Registry Mirror
Skip to content

Conversation

@NinoFloris

@NinoFloris NinoFloris commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Today the arm64 JIT often takes three instructions to put a handle address in a register, then a fourth to load from it. When the handle is nearby, adrp gets us to the page and the load can encode the offset within it. For an entry-point cell, the call still goes through the same cell and saves 8 bytes:

; Before                     ; After
movz x1, ...                 adrp x1, cell
movk x1, ..., lsl #16        ldr  x1, [x1, #:lo12:cell]
movk x1, ..., lsl #32        blr  x1
ldr  x1, [x1]
blr  x1

The same addressing mode works for integer loads and stores at other handle slots. Lowering checks alignment against the access width, and LSRA reserves a temporary for stores so the value being stored stays in its register. Reference loads are eligible when the indirection is invariant.

When the JIT needs the address itself, it uses adrp and add if the absolute move sequence would take at least three instructions. This also works for direct frozen-object references, including string literals and typeof results. The GC starts tracking the register after add completes the object address.

The VM offers the relocation when the target page is reachable from every page in the preferred code range. It checks the actual displacement once code is allocated and retries the method with absolute addressing if it overflows. DOTNET_JitEnableOptionalRelocs=0 disables the optimization.

@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Nov 24, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 24, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 6 pipeline(s).
9 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

@NinoFloris
NinoFloris force-pushed the jit/arm64-pcrel-call-cells branch from 2c49510 to ec94eba Compare November 24, 2026 14:50
@NinoFloris NinoFloris changed the title ARM64: Use page-relative loads for nearby arm64 call cells ARM64: Use page-relative loads for nearby call cells Nov 24, 2026
@NinoFloris

Copy link
Copy Markdown
Contributor Author

@MihuBot -arm64 -jitutils-repo NinoFloris/jitutils -jitutils-branch arm64-pmi-optional-relocs

@NinoFloris

NinoFloris commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Casual 8.5% size reduction

Locally I also found a small perf improvement for calls, likely due to fewer instructions and less work on address generation.

175,013 changed method listings; 2,000 examples across 177 assemblies.

Found 258 files with textual diffs.

Summary of Code Size diffs:
(Lower is better)

Total bytes of base: 55974024
Total bytes of diff: 51181140
Total bytes of delta: -4792884 (-8.56 % of base)
Total relative delta: Infinity
    diff is an improvement.
    relative diff is a regression.


Total byte diff includes 0 bytes from reconciling methods
	Base had    1 unique methods,        0 unique bytes
	Diff had    0 unique methods,        0 unique bytes

Top file improvements (bytes):
     -792028 : System.Private.CoreLib.dasm (-8.14 % of base)
     -425432 : System.Private.Xml.dasm (-8.47 % of base)
     -251572 : System.Collections.Immutable.dasm (-9.09 % of base)
     -211480 : System.Linq.AsyncEnumerable.dasm (-6.75 % of base)
     -183556 : System.Text.Json.dasm (-9.30 % of base)
...

177 total files with Code Size differences (177 improved, 0 regressed), 83 unchanged.

Top method regressions (bytes):
          52 (Infinity of base) : System.Private.CoreLib.dasm - System.Threading.Tasks.ConcurrentExclusiveSchedulerPair+ConcurrentExclusiveTaskScheduler+<>c:.cctor() (FullOpts)

Top method improvements (bytes):
       -5632 (-15.56 % of base) : System.Net.Mail.dasm - System.Net.Mime.MediaTypeMap:.cctor() (FullOpts)
       -3416 (-17.19 % of base) : System.Data.Common.dasm - System.Data.BinaryNode:EvalBinaryOp(int,System.Data.ExpressionNode,System.Data.ExpressionNode,System.Data.DataRow,int,int[]):System.Object:this (FullOpts)
       -2840 (-12.40 % of base) : System.Private.CoreLib.dasm - System.TimeZoneInfo:CacheTransitionsForYear(int):System.ValueTuple`2[int,int]:this (FullOpts)
       -2200 (-15.36 % of base) : System.ComponentModel.TypeConverter.dasm - System.ComponentModel.CultureInfoConverter+CultureInfoMapper:CreateMap():System.Collections.Generic.Dictionary`2[System.String,System.String] (FullOpts)
       -2088 (-10.68 % of base) : System.Private.Xml.dasm - System.Xml.Xsl.IlGen.XmlILMethods:.cctor() (FullOpts)
       -2064 (-11.67 % of base) : System.Text.RegularExpressions.dasm - System.Text.RegularExpressions.RegexWriter:EmitFragment(byte,System.Text.RegularExpressions.RegexNode,int):this (FullOpts) (2 methods)
       -2048 (-15.32 % of base) : System.Private.CoreLib.dasm - System.Globalization.CultureData:get_RegionNames():System.Collections.Generic.Dictionary`2[System.String,System.String] (FullOpts)
       -1880 (-12.30 % of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlReflectionImporter:ImportAccessorMapping(System.Xml.Serialization.MemberMapping,System.Xml.Serialization.FieldModel,System.Xml.Serialization.XmlAttributes,System.String,System.Type,bool,bool,System.Xml.Serialization.RecursionLimiter):this (FullOpts)
       -1816 (-14.87 % of base) : System.Text.RegularExpressions.dasm - System.Text.RegularExpressions.RegexCompiler:EmitMatchCharacterClass(System.String):this (FullOpts)
       -1744 (-15.69 % of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlSerializationReaderILGen:WriteLiteralStructMethod(System.Xml.Serialization.StructMapping):this (FullOpts)
       -1672 (-16.75 % of base) : System.Private.DataContractSerialization.dasm - System.Xml.XmlBinaryReader:ReadNode():bool:this (FullOpts)
       -1616 (-14.61 % of base) : System.Data.Common.dasm - System.Data.Common.SqlConvert:ChangeTypeForXML(System.Object,System.Type):System.Object (FullOpts)
       -1552 (-13.36 % of base) : System.Console.dasm - System.TerminalFormatStrings:.ctor(System.TermInfo+Database):this (FullOpts)
       -1504 (-15.26 % of base) : System.Linq.Expressions.dasm - System.Runtime.CompilerServices.CallSite`1[System.__Canon]:CreateCustomUpdateDelegate(System.Reflection.MethodInfo):System.__Canon:this (FullOpts)
       -1496 (-10.51 % of base) : System.Net.Quic.dasm - Microsoft.Quic.QUIC_CONNECTION_EVENT:ToString():System.String:this (FullOpts)
       -1408 (-16.85 % of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlSerializationReader:ReadTypedPrimitive(System.Xml.XmlQualifiedName,bool):System.Object:this (FullOpts)
       -1380 (-13.98 % of base) : ILCompiler.Reflection.ReadyToRun.dasm - ILCompiler.Reflection.ReadyToRun.SignatureDecoder:ParseSignature(int,System.Text.StringBuilder):ILCompiler.Reflection.ReadyToRun.ReadyToRunSignature:this (FullOpts)
       -1296 (-23.23 % of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Operators:XorObject(System.Object,System.Object):System.Object (FullOpts)
       -1296 (-13.69 % of base) : System.Private.CoreLib.dasm - System.Diagnostics.Tracing.ManifestBuilder:CreateManifestString():System.String:this (FullOpts)
       -1264 (-18.82 % of base) : System.Text.RegularExpressions.dasm - System.Text.RegularExpressions.RegexCompiler:<EmitTryFindNextPossibleStartingPosition>g__EmitFixedSet_LeftToRight|326_3(byref):this (FullOpts)
       -1240 (-16.08 % of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlSerializationWriterILGen:WriteStructMethod(System.Xml.Serialization.StructMapping):this (FullOpts)
       -1232 (-14.33 % of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlSerializationWriterCodeGen:WriteValue(System.Object):this (FullOpts)
       -1224 (-21.56 % of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Operators:CompareObject2(System.Object,System.Object,bool):int (FullOpts)
       -1208 (-10.85 % of base) : System.Net.Http.dasm - System.Net.Http.HttpConnection:SendAsync(System.Net.Http.HttpRequestMessage,bool,System.Threading.CancellationToken):System.Net.Http.HttpResponseMessage:this (FullOpts)
       -1200 (-8.72 % of base) : System.Data.Common.dasm - System.Data.XmlTreeGen:HandleTable(System.Data.DataTable,System.Xml.XmlDocument,System.Xml.XmlElement,bool):System.Xml.XmlElement:this (FullOpts)
       -1184 (-10.43 % of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlSerializationWriterCodeGen:WriteElements(System.String,System.String,System.Xml.Serialization.ElementAccessor[],System.Xml.Serialization.TextAccessor,System.Xml.Serialization.ChoiceIdentifierAccessor,System.String,bool,bool):this (FullOpts)
       -1176 (-12.93 % of base) : System.Data.Common.dasm - System.Data.DataTable:SerializeTableSchema(System.Runtime.Serialization.SerializationInfo,bool):this (FullOpts)
       -1176 (-25.54 % of base) : System.Text.RegularExpressions.dasm - System.Text.RegularExpressions.RegexCompiler:<EmitTryFindNextPossibleStartingPosition>g__EmitAnchors|326_0(byref):bool:this (FullOpts)
       -1152 (-17.11 % of base) : System.Private.DataContractSerialization.dasm - System.Runtime.Serialization.XmlFormatReaderGenerator+CriticalHelper:ReadCollection(System.Runtime.Serialization.DataContracts.CollectionDataContract):this (FullOpts)
       -1144 (-17.24 % of base) : System.Private.Xml.dasm - System.Xml.Xsl.XsltOld.HtmlElementProps:CreatePropsTable():System.Collections.Hashtable (FullOpts)
       -1136 (-11.82 % of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlSerializationWriterCodeGen:WriteElement(System.String,System.Xml.Serialization.ElementAccessor,System.String,bool):this (FullOpts)
       -1128 (-20.94 % of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Operators:AddObject(System.Object,System.Object):System.Object (FullOpts)
       -1128 (-18.00 % of base) : System.Text.Encoding.CodePages.dasm - System.Text.EncodingNLS:GetLocalizedEncodingNameResource(int):System.String (FullOpts)
       -1128 (-11.05 % of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlSerializationWriterCodeGen:WriteStructMethod(System.Xml.Serialization.StructMapping):this (FullOpts)
       -1112 (-15.27 % of base) : System.Linq.Expressions.dasm - System.Linq.Expressions.Interpreter.LightCompiler:CompileBinaryExpression(System.Linq.Expressions.Expression):this (FullOpts)
       -1096 (-12.07 % of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlSerializationReaderCodeGen:WriteElement(System.String,System.String,System.String,System.Xml.Serialization.ElementAccessor,System.Xml.Serialization.ChoiceIdentifierAccessor,System.String,bool,bool,int,int):this (FullOpts)
       -1072 (-14.09 % of base) : System.Private.CoreLib.dasm - System.Reflection.FieldAccessor:SetValue(System.Object,System.Object,int,System.Reflection.Binder,System.Globalization.CultureInfo):this (FullOpts)
       -1072 (-14.81 % of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlSerializationReaderILGen:WriteElement(System.String,System.String,System.String,System.Xml.Serialization.ElementAccessor,System.Xml.Serialization.ChoiceIdentifierAccessor,System.String,bool,bool,int,int):this (FullOpts)
       -1048 (-11.63 % of base) : System.Text.Json.dasm - System.Text.Json.Schema.JsonSchemaExporter:MapJsonSchemaCore(byref,System.Text.Json.Serialization.Metadata.JsonTypeInfo,System.Text.Json.Serialization.Metadata.JsonPropertyInfo,System.Text.Json.Serialization.JsonConverter,System.Nullable`1[int],System.Text.Json.Serialization.Metadata.JsonTypeInfo,bool,bool,System.Nullable`1[System.Collections.Generic.KeyValuePair`2[System.String,System.Text.Json.Schema.JsonSchema]],bool):System.Text.Json.Schema.JsonSchema (FullOpts)
       -1040 (-21.29 % of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Operators:SubtractObject(System.Object,System.Object):System.Object (FullOpts)
       -1032 (-10.92 % of base) : System.Security.Cryptography.dasm - System.Security.Cryptography.X509Certificates.X509Certificate2:ToString(bool):System.String:this (FullOpts)
       -1024 (-4.45 % of base) : ILCompiler.Reflection.ReadyToRun.dasm - Microsoft.Diagnostics.DataContractReader.Contracts.GCInfoHelpers.X86.InfoHdr:.cctor() (FullOpts)
      
... truncated for GitHub; see the full report in the diff browser ...

@NinoFloris
NinoFloris marked this pull request as ready for review November 24, 2026 16:44
@NinoFloris
NinoFloris force-pushed the jit/arm64-pcrel-call-cells branch from 5429114 to 9cced94 Compare November 26, 2026 22:54
@NinoFloris NinoFloris changed the title ARM64: Use page-relative loads for nearby call cells ARM64: Use page-relative addressing for nearby handles Nov 26, 2026
@NinoFloris

Copy link
Copy Markdown
Contributor Author

@EgorBot -azure_linux_cobalt200 --envVars DOTNET_JitEnableOptionalRelocs:1 DOTNET_JitDisasm:Shape

using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;

public unsafe class AdrpBench
{
    public class Base { public int X; }
    public sealed class Derived : Base { public int Y = 1; }

    static Base s_b = new Derived();
    static object s_gcStatic = "gc";
    static int s_int;
    static byte s_byte;
    static delegate* managed<int, int> s_fp = &AddOneShape;

    [MethodImpl(MethodImplOptions.NoInlining)]
    static int AddOneShape(int x) => x + 1;

    // Entry-point cell for a known target.
    [Benchmark, MethodImpl(MethodImplOptions.NoInlining)]
    public int CellShape() => AddOneShape(41);

    // Function pointer loaded from a mutable static slot.
    [Benchmark, MethodImpl(MethodImplOptions.NoInlining)]
    public int FptrShape() => s_fp(41);

    // MethodTable address used by the type check.
    [Benchmark, MethodImpl(MethodImplOptions.NoInlining)]
    public int CastShape() => ((Derived)s_b).Y;

    [Benchmark, MethodImpl(MethodImplOptions.NoInlining)]
    public bool IsShape() => s_b is Derived;

    // Separate loads and stores so each can fold its page offset.
    [Benchmark, MethodImpl(MethodImplOptions.NoInlining)]
    public int IntLoadShape() => s_int;

    [Benchmark, MethodImpl(MethodImplOptions.NoInlining)]
    public void IntStoreShape() => s_int = 41;

    [Benchmark, MethodImpl(MethodImplOptions.NoInlining)]
    public byte ByteLoadShape() => s_byte;

    [Benchmark, MethodImpl(MethodImplOptions.NoInlining)]
    public void ByteStoreShape() => s_byte = 1;

    // Direct frozen-object address, eligible when in range.
    [Benchmark, MethodImpl(MethodImplOptions.NoInlining)]
    public string FrozenShape() => "frozen";

    // Control for a mutable static reference load.
    [Benchmark, MethodImpl(MethodImplOptions.NoInlining)]
    public object GcStaticShape() => s_gcStatic;
}

@NinoFloris

Copy link
Copy Markdown
Contributor Author

@MihuBot -arm64 -jitutils-repo NinoFloris/jitutils -jitutils-branch arm64-pmi-optional-relocs

@NinoFloris

Copy link
Copy Markdown
Contributor Author

With the added changes the size reduction moves to 11.33% from 8.56% before.

199,569 changed method listings; 2,000 examples across 177 assemblies.

Found 258 files with textual diffs.

Summary of Code Size diffs:
(Lower is better)

Total bytes of base: 55969636
Total bytes of diff: 49627844
Total bytes of delta: -6341792 (-11.33 % of base)
Total relative delta: -28332.47
    diff is an improvement.
    relative diff is an improvement.


Top file improvements (bytes):
    -1016352 : System.Private.CoreLib.dasm (-10.45 % of base)
     -571104 : System.Private.Xml.dasm (-11.38 % of base)
     -336284 : System.Collections.Immutable.dasm (-12.12 % of base)
     -320504 : System.Linq.AsyncEnumerable.dasm (-10.24 % of base)
     -237028 : System.Data.Common.dasm (-11.66 % of base)
     -235884 : System.Security.Cryptography.dasm (-13.41 % of base)
     -232204 : System.Text.Json.dasm (-11.75 % of base)
     -221888 : System.Linq.Parallel.dasm (-9.11 % of base)
     -197052 : System.Threading.Tasks.Dataflow.dasm (-12.91 % of base)
     -163372 : System.Private.DataContractSerialization.dasm (-13.07 % of base)
     -156604 : System.Numerics.Tensors.dasm (-10.07 % of base)
     -134484 : System.Linq.dasm (-7.44 % of base)
     -133524 : System.Linq.Expressions.dasm (-11.82 % of base)
     -127396 : System.Text.RegularExpressions.dasm (-10.21 % of base)
     -119856 : System.Net.Http.dasm (-11.59 % of base)
     -101280 : System.Collections.dasm (-11.21 % of base)
...

177 total files with Code Size differences (177 improved, 0 regressed), 83 unchanged.

Top method improvements (bytes):
      -10956 (-30.28 % of base) : System.Net.Mail.dasm - System.Net.Mime.MediaTypeMap:.cctor() (FullOpts)
       -4092 (-30.60 % of base) : System.Private.CoreLib.dasm - System.Globalization.CultureData:get_RegionNames():System.Collections.Generic.Dictionary`2[System.String,System.String] (FullOpts)
       -4024 (-20.24 % of base) : System.Data.Common.dasm - System.Data.BinaryNode:EvalBinaryOp(int,System.Data.ExpressionNode,System.Data.ExpressionNode,System.Data.DataRow,int,int[]):System.Object:this (FullOpts)
       -4008 (-16.93 % of base) : System.Text.RegularExpressions.dasm - System.Text.RegularExpressions.RegexCharClass:.cctor() (FullOpts) (2 methods)
       -3412 (-17.45 % of base) : System.Private.Xml.dasm - System.Xml.Xsl.IlGen.XmlILMethods:.cctor() (FullOpts)
       -2848 (-12.45 % of base) : System.Private.CoreLib.dasm - System.TimeZoneInfo:CacheTransitionsForYear(int):System.ValueTuple`2[int,int]:this (FullOpts)
       -2480 (-16.23 % of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlReflectionImporter:ImportAccessorMapping(System.Xml.Serialization.MemberMapping,System.Xml.Serialization.FieldModel,System.Xml.Serialization.XmlAttributes,System.String,System.Type,bool,bool,System.Xml.Serialization.RecursionLimiter):this (FullOpts)
       -2236 (-20.22 % of base) : System.Data.Common.dasm - System.Data.Common.SqlConvert:ChangeTypeForXML(System.Object,System.Type):System.Object (FullOpts)
       -2204 (-15.39 % of base) : System.ComponentModel.TypeConverter.dasm - System.ComponentModel.CultureInfoConverter+CultureInfoMapper:CreateMap():System.Collections.Generic.Dictionary`2[System.String,System.String] (FullOpts)
       -2196 (-19.77 % of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlSerializationReaderILGen:WriteLiteralStructMethod(System.Xml.Serialization.StructMapping):this (FullOpts)
       -2084 (-17.07 % of base) : System.Text.RegularExpressions.dasm - System.Text.RegularExpressions.RegexCompiler:EmitMatchCharacterClass(System.String):this (FullOpts)
       -2080 (-11.76 % of base) : System.Text.RegularExpressions.dasm - System.Text.RegularExpressions.RegexWriter:EmitFragment(byte,System.Text.RegularExpressions.RegexNode,int):this (FullOpts) (2 methods)
       -1936 (-13.59 % of base) : System.Net.Quic.dasm - Microsoft.Quic.QUIC_CONNECTION_EVENT:ToString():System.String:this (FullOpts)
       -1908 (-19.38 % of base) : System.Linq.Expressions.dasm - System.Runtime.CompilerServices.CallSite`1[System.__Canon]:CreateCustomUpdateDelegate(System.Reflection.MethodInfo):System.__Canon:this (FullOpts)
       -1804 (-13.10 % of base) : System.Data.Common.dasm - System.Data.XmlTreeGen:HandleTable(System.Data.DataTable,System.Xml.XmlDocument,System.Xml.XmlElement,bool):System.Xml.XmlElement:this (FullOpts)
       -1744 (-17.47 % of base) : System.Private.DataContractSerialization.dasm - System.Xml.XmlBinaryReader:ReadNode():bool:this (FullOpts)
       -1732 (-14.35 % of base) : System.Net.Http.dasm - System.Net.Http.Headers.KnownHeaders:.cctor() (FullOpts)
       -1700 (-30.29 % of base) : System.Security.Cryptography.dasm - System.Security.Cryptography.OidLookup:InitializeLookupDictionaries() (FullOpts)
       -1684 (-18.52 % of base) : System.Data.Common.dasm - System.Data.DataTable:SerializeTableSchema(System.Runtime.Serialization.SerializationInfo,bool):this (FullOpts)
       -1604 (-13.81 % of base) : System.Console.dasm - System.TerminalFormatStrings:.ctor(System.TermInfo+Database):this (FullOpts)
       -1600 (-20.75 % of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlSerializationWriterILGen:WriteStructMethod(System.Xml.Serialization.StructMapping):this (FullOpts)
       -1584 (-18.78 % of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlSerializationWriterCodeGen:WriteValue(System.Object):this (FullOpts)
       -1552 (-18.57 % of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlSerializationReader:ReadTypedPrimitive(System.Xml.XmlQualifiedName,bool):System.Object:this (FullOpts)
       -1520 (-15.82 % of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlSerializationWriterCodeGen:WriteElement(System.String,System.Xml.Serialization.ElementAccessor,System.String,bool):this (FullOpts)
       -1516 (-13.36 % of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlSerializationWriterCodeGen:WriteElements(System.String,System.String,System.Xml.Serialization.ElementAccessor[],System.Xml.Serialization.TextAccessor,System.Xml.Serialization.ChoiceIdentifierAccessor,System.String,bool,bool):this (FullOpts)
       -1504 (-16.56 % of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlSerializationReaderCodeGen:WriteElement(System.String,System.String,System.String,System.Xml.Serialization.ElementAccessor,System.Xml.Serialization.ChoiceIdentifierAccessor,System.String,bool,bool,int,int):this (FullOpts)
       -1496 (-14.65 % of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlSerializationWriterCodeGen:WriteStructMethod(System.Xml.Serialization.StructMapping):this (FullOpts)
       -1448 (-19.19 % of base) : System.Data.Common.dasm - System.Data.Common.SqlConvert:ChangeType2(System.Object,int,System.Type,System.IFormatProvider):System.Object (FullOpts)
       -1432 (-21.58 % of base) : System.Private.Xml.dasm - System.Xml.Xsl.XsltOld.HtmlElementProps:CreatePropsTable():System.Collections.Hashtable (FullOpts)
       -1424 (-20.72 % of base) : System.Net.Http.dasm - System.Net.Http.QPack.H3StaticTable:.cctor() (FullOpts)
       -1412 (-14.30 % of base) : ILCompiler.Reflection.ReadyToRun.dasm - ILCompiler.Reflection.ReadyToRun.SignatureDecoder:ParseSignature(int,System.Text.StringBuilder):ILCompiler.Reflection.ReadyToRun.ReadyToRunSignature:this (FullOpts)
       -1404 (-12.61 % of base) : System.Net.Http.dasm - System.Net.Http.HttpConnection:SendAsync(System.Net.Http.HttpRequestMessage,bool,System.Threading.CancellationToken):System.Net.Http.HttpResponseMessage:this (FullOpts)
       -1400 (-25.09 % of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Operators:XorObject(System.Object,System.Object):System.Object (FullOpts)
       -1400 (-13.03 % of base) : System.Configuration.ConfigurationManager.dasm - System.Configuration.ConfigurationElement:DeserializeElement(System.Xml.XmlReader,bool):this (FullOpts)
       -1392 (-14.81 % of base) : System.Private.CoreLib.dasm - System.Diagnostics.Tracing.ManifestBuilder:CreateManifestString():System.String:this (FullOpts)
       -1388 (-19.18 % of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlSerializationReaderILGen:WriteElement(System.String,System.String,System.String,System.Xml.Serialization.ElementAccessor,System.Xml.Serialization.ChoiceIdentifierAccessor,System.String,bool,bool,int,int):this (FullOpts)
       -1348 (-15.05 % of base) : Microsoft.Extensions.Logging.Console.dasm - Microsoft.Extensions.Configuration.Binder.SourceGeneration.<BindingExtensions_g>FAC8695D4937EA8DE8932CEFC0AFAC460CE97D28FF1F625D2A58A1A116CF82AE5__BindingExtensions:BindCore(Microsoft.Extensions.Configuration.IConfiguration,byref,bool,Microsoft.Extensions.Configuration.BinderOptions) (FullOpts) (6 methods)
       -1296 (-14.39 % of base) : System.Text.Json.dasm - System.Text.Json.Schema.JsonSchemaExporter:MapJsonSchemaCore(byref,System.Text.Json.Serialization.Metadata.JsonTypeInfo,System.Text.Json.Serialization.Metadata.JsonPropertyInfo,System.Text.Json.Serialization.JsonConverter,System.Nullable`1[int],System.Text.Json.Serialization.Metadata.JsonTypeInfo,bool,bool,System.Nullable`1[System.Collections.Generic.KeyValuePair`2[System.String,System.Text.Json.Schema.JsonSchema]],bool):System.Text.Json.Schema.JsonSchema (FullOpts)
       -1296 (-12.59 % of base) : System.Private.Xml.dasm - System.Xml.Schema.SchemaNames:.ctor(System.Xml.XmlNameTable):this (FullOpts)
       -1288 (-29.22 % of base) : System.Security.Cryptography.dasm - System.Security.Cryptography.CryptoConfig:get_DefaultNameHT():System.Collections.Generic.Dictionary`2[System.String,System.Object] (FullOpts)
       -1288 (-16.10 % of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlSerializationReaderILGen:GenerateLiteralMembersElement(System.Xml.Serialization.XmlMembersMapping):System.String:this (FullOpts)
       -1280 (-19.06 % of base) : System.Text.RegularExpressions.dasm - System.Text.RegularExpressions.RegexCompiler:<EmitTryFindNextPossibleStartingPosition>g__EmitFixedSet_LeftToRight|326_3(byref):this (FullOpts)
       -1256 (-22.41 % of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Operators:CompareObject2(System.Object,System.Object,bool):int (FullOpts)
       -1256 (-16.51 % of base) : System.Private.CoreLib.dasm - System.Reflection.FieldAccessor:SetValue(System.Object,System.Object,int,System.Reflection.Binder,System.Globalization.CultureInfo):this (FullOpts
... truncated for GitHub; see the full report in the diff browser ...

Check ADRP reach after code allocation and retry with absolute addressing if the call cell is too far away.
Use the pair when materializing the absolute address would take at least three instructions.
Only the scaled LDR needs an eight-byte-aligned offset.
An invariant reference load can use the same fold if the JIT reports the GC reference at the LDR.
Report the GC reference after the ADD completes the object address.
Fail fast before encoding a truncated page displacement.
Mark the handle relocatable so both paths use instGen_Set_Reg_To_Imm.
Use AddrNeedsReloc for handle constants and FitsInAddrBase for handle slot accesses.
Only the final ADD or LDR can produce a GC reference.
Keep counting far branches after a page miss so the absolute-address retry can reserve stub space.
@NinoFloris
NinoFloris force-pushed the jit/arm64-pcrel-call-cells branch from 4d40462 to d285327 Compare November 27, 2026 13:26
Check that only the first far method retries and that near methods can use ADRP again.
@NinoFloris
NinoFloris force-pushed the jit/arm64-pcrel-call-cells branch from 2158e0e to 896c0ee Compare November 27, 2026 14:36
@EgorBo

EgorBo commented Nov 27, 2026

Copy link
Copy Markdown
Member

@EgorBot -orchard -azure_linux_cobalt200 -dotnet-counters

@EgorBo

EgorBo commented Nov 27, 2026

Copy link
Copy Markdown
Member

@EgorBot -orchard -azure_linux_cobalt200 -azure_linux_cobalt100 -dotnet-counters

var start = new ProcessStartInfo(Environment.ProcessPath!) { RedirectStandardOutput = true, UseShellExecute = false };
start.ArgumentList.Add(Assembly.GetExecutingAssembly().Location);
start.ArgumentList.Add("child");
start.Environment["DOTNET_JitStressCodePlacement"] = nameof(ProbeFar);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this test and test switches be replaced by improving ForceRelocs stress mode?

The reloc bugs tend to show up when the stars align in specific way. It is why we have ForceRelocs stress mode and regularly running all tests with it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the extra switches and extended ForceRelocs to produce mixed near/far placement.

I kept the directed test for its original purpose of catching excessive recompilation, it now uses ForceRelocs for this. For a batch of methods, compiled sequentially out of ADRP range of the target, only the first method may request a page-relocation retry. Returning to the preferred code range should re-enable ADRP. Alternating near/far placement can disable and re-enable ADRP repeatedly, so the test reports that scenario separately.

ADRP can reach 4 GB back and forwards, compared with 2 GB on x64. Regardless, tracking code placement per loader may still be useful on x64. Today it disables optional relative addressing process-wide after the first out-of-range reloc, which is why the JIT is selective about which handle kinds participate (thanks Jakob, for pointing me to #60712 and #60721).

With tracking, all handle kinds can participate freely, but if code placement does oscillate, users could pay for repeated recompilations. This worst case is clear to me, but I don’t know how often large apps could actually see sustained near/far oscillation, or how additional JIT CPU use from the retries would be the main problem for them.

@jkotas jkotas Nov 28, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do have important customers with very large apps that JIT gigabytes of code. I suspect these recompilations may be a problem for them.

@NinoFloris NinoFloris Nov 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do have important customers with very large apps that JIT gigabytes of code. I suspect these recompilations may be a problem for them.

It really depends on whether it oscillates, not just that it's a lot of code. By which I mean that I haven't really put thought into how such apps can be simulated faithfully.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still, x64 just turns relocs off completely, process-wide. This PR seems to potentially do double compilation on each run (the retry cost is unbounded). Technically, we can use ADRP for more than just function pointers, for pretty much everything (e.g. from the code heap to libcoreclr.so in memory) so may run into this a lot more often, 4gb of codeheap won't be a requirement.

Maybe we should just assume we won't use relocs, and at the emit stage do a bait-and-switch to relocs when possible, i.e. when we know for certain the distance between the currently emitted instruction and the target.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still, x64 just turns relocs off completely, process-wide

We can do that of course, we'd just have to be selective like x64 on handle kinds.

Maybe we should just assume we won't use relocs, and at the emit stage do a bait-and-switch to relocs when possible, i.e. when we know for certain the distance between the currently emitted instruction and the target.

Doesn't this mean you overreserve?

In your PR, IsInPreferredPageRelativeRange handles only 2gb, not 4gb. (on windows-arm64).

Right, I meant ADRP's reach. As we don’t know where we’ll land the target has to be reachable from both ends. On windows that leaves 2 GB from the middle, on unix where it's just 1 GB we get 3.5 GB (there is no guarantee we get another 1 GB afterwards next to coreclr though, see dotnet/coreclr#2205).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch-arm64 area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI community-contribution Indicates that the PR has been added by a community member tenet-performance Performance related issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants