dotnet / dotnet/runtime

[clr-interp] Move InterpByteCodeStart header into negative space in front of the code pointer

Open
#128,081 1 comment 0 reactions 0 assignees View on GitHub
area-CodeGen-Interpreter-coreclr
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

## Background

`InterpByteCodeStart` is a small header struct (currently a single `InterpMethod*` pointer) placed at the start of every interpreter method allocation, immediately before the bytecode itself. The bytecode pointer that the running interpreter uses points past this header.

Because the header sits in the same allocation as the bytecode, every place that converts between a "code pointer" and an offset (or stores a "native offset" derived from one) needs to add or subtract `sizeof(InterpByteCodeStart)`. Examples:

- `compiler.cpp:903` — `ConvertOffset` adds `sizeof(void*)` to interpreter-local offsets.
- `compiler.cpp:939` — `DiagnosticIP` is stored as `sizeof(InterpByteCodeStart) + ((size_t)startIp - (size_t)m_pMethodCode)`.
- `compiler.cpp:946` — `suspensionPointIPOffsets[i]` adds the same.
- `interpexec.cpp` — runtime resume math relies on the same convention.

Each new feature that exposes interp data to the runtime or the debugger picks up another offset-math site that needs to know the header is there.

## Proposed fix

Move `InterpByteCodeStart` into negative space relative to the code pointer:

- The interpreter's "method code pointer" continues to point at the first bytecode opcode.
- The `InterpMethod*` (and any other header data) lives at `*(InterpByteCodeStart**)((uint8_t*)code - sizeof(InterpByteCodeStart))`, accessed only when needed.
- All "offset from code pointer" math drops the `+ sizeof(InterpByteCodeStart)` corrections.

This matches the JIT's pattern.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.