Skip to content

JIT: Do not fold a relocatable constant into a block store address mode on Arm, LoongArch64 and RISC-V - #137128

Closed
SHAZAM-Analytics wants to merge 1 commit into
dotnet:mainfrom
SHAZAM-Analytics:fix/jit-blockstore-reloc
Closed

SHAZAM-Analytics wants to merge 1 commit into
dotnet:mainfrom
SHAZAM-Analytics:fix/jit-blockstore-reloc

Conversation

@SHAZAM-Analytics

Copy link
Copy Markdown
Contributor

Fixes #136687

Problem

Lowering::ContainBlockStoreAddress for arm/arm64 (and its loongarch64 and riscv64 copies) turns ADD(base, CNS_INT) into a contained LEA(base, offset) whenever the constant fits the offset range. It does not check whether the constant needs a relocation. When the constant is a handle, such as a frozen object's address under NativeAOT, the handle becomes a plain address-mode offset. Codegen then materializes it with movz/movk and no relocation, so at run time the value is ILC's placeholder rather than an address and the block copy faults.

The xarch version builds its address mode through TryCreateAddrMode, which only folds constants for which ImmedValCanBeFolded is true, so x86 and x64 are not affected.

Minimal case: a loop over a preinitialized static readonly array of structs that copies each element to a local (an unrolled block copy):

struct Element { public int A, B, C, D, E, F, G, H, I, J, K, L, M; /* 52 bytes */ }

static readonly Element[] s_elements = new Element[] { new Element(0), /* ... */ };

static int SumElements()
{
    int sum = 0, index = 0;
    foreach (Element e in s_elements)
        sum += Checksum(e, index++);   // Checksum is NoInlining and takes Element by value
    return sum;
}

SumElements compiled for win-arm64 by the 10.0.14 ILCompiler:

movz    x0, #0x20B0
movk    x0, #66 LSL #16        ; the frozen array's handle, with no relocation
add     x0, x21, x0
ldp     q16, q17, [x0, #0x20]

With this change:

adrp    x0, [HIGH RELOC #0x421B58]
add     x0, x0, [LOW RELOC #0x421B58]
add     x0, x21, x0

Fix

ContainBlockStoreAddress returns early when the offset constant ImmedValNeedsReloc, so the ADD stays uncontained and the constant is emitted through the normal relocatable path. The same check is added in the loongarch64 and riscv64 implementations, which have the same shape.

Testing

Adds TestFrozenStructArrayElementCopy to the NativeAOT Preinitialization smoke test, which runs on the osx-arm64, linux-arm64 and windows-arm64 NativeAOT legs. It asserts the class is preinitialized and checks a checksum over every field of every element. Without the fix it faults at the first element copy on arm64; on x64 it passes either way.

I confirmed the codegen difference above by cross-compiling the test (in the release/10.0 copy of Preinitialization.cs) for win-arm64 with the 10.0.14 ILCompiler, once with its shipped arm64 JIT and once with the release/10.0 JIT rebuilt with this change. I have not run it on main's JIT, whose JIT-EE interface does not match a shipped ILCompiler.

ContainBlockStoreAddress on arm/arm64, loongarch64 and riscv64 turned
ADD(base, CNS_INT) into a contained LEA whenever the constant fit the
offset range, without checking whether the constant needs a relocation.
A frozen object handle under NativeAOT became a plain offset that codegen
materialized with movz/movk and no relocation, so the copy faulted.

Leave the ADD uncontained when the constant needs a relocation, matching
what TryCreateAddrMode does on xarch.

Fixes #136687
@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 7, 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 7, 2026
@azure-pipelines

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

@SHAZAM-Analytics

Copy link
Copy Markdown
Contributor Author

This fix is needed in servicing too: shipped .NET 10 and 11 NativeAOT apps hit it on arm64 today. Once it merges, could an area owner backport it to release/10.0 and release/11.0? I opened backport PRs earlier by mistake and they were rightly closed.

@SHAZAM-Analytics SHAZAM-Analytics closed this by deleting the head repository Nov 15, 2026
@jkotas

jkotas commented Nov 15, 2026

Copy link
Copy Markdown
Member

@SHAZAM-Analytics Did you happen to close this by accident?

@SHAZAM-Analytics

Copy link
Copy Markdown
Contributor Author

@jkotas Yes, by accident, sorry. We deleted the fork this came from, which closed it, and GitHub cannot reopen a pull request once its fork is gone. I recreated the fork and opened the same commit as #137602.

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 community-contribution Indicates that the PR has been added by a community member

Projects

None yet

2 participants