Conversation
|
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. |
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
2c49510 to
ec94eba
Compare
|
@MihuBot -arm64 -jitutils-repo NinoFloris/jitutils -jitutils-branch arm64-pmi-optional-relocs |
|
Locally I also found a small perf improvement for calls, likely due to fewer instructions and less work on address generation. |
5429114 to
9cced94
Compare
|
@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;
} |
|
@MihuBot -arm64 -jitutils-repo NinoFloris/jitutils -jitutils-branch arm64-pmi-optional-relocs |
|
With the added changes the size reduction moves to 11.33% from 8.56% before. |
774446c to
be41888
Compare
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.
4d40462 to
d285327
Compare
Check that only the first far method retries and that near methods can use ADRP again.
2158e0e to
896c0ee
Compare
|
@EgorBot -orchard -azure_linux_cobalt200 -dotnet-counters |
|
@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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
We do have important customers with very large apps that JIT gigabytes of code. I suspect these recompilations may be a problem for them.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
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:
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.