Skip to content

Implement computed goto dispatch for the CoreCLR interpreter#129216

Open
BrzVlad wants to merge 2 commits into
dotnet:mainfrom
BrzVlad:feature-clrinterp-computed-goto
Open

Implement computed goto dispatch for the CoreCLR interpreter#129216
BrzVlad wants to merge 2 commits into
dotnet:mainfrom
BrzVlad:feature-clrinterp-computed-goto

Conversation

@BrzVlad

@BrzVlad BrzVlad commented Jun 10, 2026

Copy link
Copy Markdown
Member

Instead of making each opcode dispatch to the loop start where we switch on the opcode, we transform every switch case into a label, we let the compiler statically populate the s_dispatchTable which maps each opcode to the label address. This makes opcode dispatch a simple load + branch to the label address from this table. This makes the interpreter 25% faster.

It is unclear whether this has any impact on wasm. Likely not, because wasm has control flow limitations that make random branches impossible.

As an additional optimization, we avoid saving pFrame->ip for each opcode. Removing this can improve execution speed by around 3%. Instead we explicitly save pFrame->ip in opcodes that can trigger GC or throw exception. Add InterpThrow helper to ensure pFrame->ip is set before all throws.

BrzVlad added 2 commits June 10, 2026 08:28
Instead of making each opcode dispatch to the loop start where we switch on the opcode, we transform every switch case into a label, we let the compiler statically populate the s_dispatchTable which maps each opcode to the label address. This makes opcode dispatch a simple load + branch to the label address from this table. This makes the interpreter 25% faster.

It is unclear whether this has any impact on wasm. Likely not, because wasm has control flow limitations that make random branches impossible.
Removing this can improve execution speed by around 3%. Instead we explicitly save pFrame->ip in opcodes that can trigger GC or throw exception. Add InterpThrow helper to ensure pFrame->ip is set before all throws.
Copilot AI review requested due to automatic review settings June 10, 2026 05:43
@BrzVlad BrzVlad requested review from janvorli and kg as code owners June 10, 2026 05:43

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.

Copilot wasn't able to review any files in this pull request.

@BrzVlad

BrzVlad commented Jun 10, 2026

Copy link
Copy Markdown
Member Author

/azp run runtime-interpreter

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@BrzVlad

BrzVlad commented Jun 10, 2026

Copy link
Copy Markdown
Member Author

/azp run runtime-libraries-interpreter

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@jkotas jkotas added the tenet-performance Performance related issue label Jun 10, 2026
@BrzVlad

BrzVlad commented Jun 11, 2026

Copy link
Copy Markdown
Member Author

Assembly before

// INTOP_NEXT
0x105260a0c: ldr    w23, [x28, #0xc]!   // x28 = ip, load the next opcode
0x105260a10: cmp    w23, #0x15f         // check for the default branch
0x105260a14: b.ls   0x10525e034
0x105260a18: b      0x10526295c

// Loop start
0x10525e034: mov    w8, w23
0x10525e038: adrp   x11, 414            // rather long switch dispatch
0x10525e03c: add    x11, x11, #0x624
0x10525e040: adr    x9, 0x10525e008
0x10525e044: ldrh   w10, [x11, x8, lsl #1]
0x10525e048: add    x9, x9, x10, lsl #2
0x10525e04c: br     x9

Assembly after

// INTOP_NEXT
0x1051f63e0: ldr    w23, [x27, #0x8]!       // x27 = ip, load the next opcode
0x1051f63e4: ldr    x8, [x22, x23, lsl #3]  // x22 = s_dispatchTable, load label of next
0x1051f63e8: br     x8                      // dispatch

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants