Skip to content

[Experiment] Fold NativeAOT relocatable constants with small offsets#131107

Draft
EgorBo wants to merge 1 commit into
dotnet:mainfrom
EgorBo:egorbo/nativeaot-reloc-addend
Draft

[Experiment] Fold NativeAOT relocatable constants with small offsets#131107
EgorBo wants to merge 1 commit into
dotnet:mainfrom
EgorBo:egorbo/nativeaot-reloc-addend

Conversation

@EgorBo

@EgorBo EgorBo commented Jul 20, 2026

Copy link
Copy Markdown
Member

Just an AI experiment for relocatable constants + small offsets in NativeAOT.

using System.Runtime.CompilerServices;

Test();

class TestClass
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    static ReadOnlySpan<char> Test() => "Hello".AsSpan();
}
-       lea      rax, gword ptr [(reloc ...)]      ; '"Hello"'
-       add      rax, 12
+       lea      rax, bword ptr [(reloc ...)]
        mov      bword ptr [rcx], rax
        mov      dword ptr [rcx+0x08], 5
        mov      rax, rcx
        ret

Copilot AI review requested due to automatic review settings July 20, 2026 21:40
@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 Jul 20, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
11 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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR extends the x64 JIT NativeAOT pipeline to fold OBJ_HDL + small_const address computations into a single relocatable constant with an explicit “addend”, enabling codegen to avoid emitting a separate add reg, imm in common patterns (e.g., string literal span construction).

Changes:

  • Teach Lowering::LowerAdd to fold GTF_ICON_OBJ_HDL + int32 offset into a new GTF_ICON_RELOC_ADDR constant (AMD64 + NativeAOT only), carrying the addend separately.
  • Introduce GTF_ICON_RELOC_ADDR and add GenTreeIntCon accessors to store/retrieve the relocation addend.
  • Extend the xarch emitter/codegen so lea reg, [reloc] can record a relocation with an extra addend (via a new emitIns_R_AI parameter and emitOutputAM handling).

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/coreclr/jit/lower.cpp Adds the NativeAOT AMD64 folding path from OBJ_HDL + small offset into a relocatable constant with addend.
src/coreclr/jit/handlekinds.h Adds the new handle kind GTF_ICON_RELOC_ADDR.
src/coreclr/jit/gentree.h Documents and implements addend storage/access (GetRelocOffset/SetRelocOffset) on GenTreeIntCon.
src/coreclr/jit/gentree.cpp Prevents address containment for GTF_ICON_RELOC_ADDR to avoid losing the addend.
src/coreclr/jit/emitxarch.h Extends emitIns_R_AI signature with an optional relocation addend parameter.
src/coreclr/jit/emitxarch.cpp Stores the addend in the instruction descriptor and applies it to RIP-rel relocation emission (emitOutputAM).
src/coreclr/jit/emitinl.h Adds emitGetInsAmdRelocOffset helper to retrieve the stored addend.
src/coreclr/jit/emit.h Declares emitGetInsAmdRelocOffset on the emitter.
src/coreclr/jit/codegenxarch.cpp Emits lea with relocation addend for GTF_ICON_RELOC_ADDR (NativeAOT AMD64), with fallback to lea/mov + add.

@EgorBo
EgorBo marked this pull request as draft July 21, 2026 00:00
On x64 NativeAOT, ADD(frozen_object_handle, small_offset) was emitted as
`lea reg, [reloc]` followed by `add reg, offset`. Fold it into a single
`lea reg, [reloc + offset]` by carrying the offset as a relocation addend
via a new GTF_ICON_RELOC_ADDR handle kind, so the relocation still targets
the exact base handle while the addend is applied by the emitter.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 61836295-249d-497a-8fec-f45402076cf7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants