Optimize branch instructions to short form where branch distance fits in sbyte#529
Draft
Optimize branch instructions to short form where branch distance fits in sbyte#529
Conversation
Agent-Logs-Url: https://github.com/dadhi/FastExpressionCompiler/sessions/12b84d82-7d59-4139-8f30-ddc49bf21766 Co-authored-by: dadhi <39516+dadhi@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dadhi/FastExpressionCompiler/sessions/12b84d82-7d59-4139-8f30-ddc49bf21766 Co-authored-by: dadhi <39516+dadhi@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dadhi/FastExpressionCompiler/sessions/12b84d82-7d59-4139-8f30-ddc49bf21766 Co-authored-by: dadhi <39516+dadhi@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Optimize branches to small branches
Optimize branch instructions to short form where branch distance fits in sbyte
Apr 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Branch instructions like
Br,Brtrue,Brfalseencode a 4-byte offset by default, but have short-form variants (Br_S,Brtrue_S,Brfalse_S) using a 1-byte signed offset when the target is within ±127 bytes. Previously, a blanket replacement broke large switch statements. This PR adds proper distance-checking before selecting the shorter form.Changes
General backward-branch optimizer (
ILGeneratorTools)_ilLabelPositions(ConditionalWeakTable<ILGenerator, Dictionary<Label, int>>) — keyed byLabelstruct directly to avoid any hash collision ambiguityDmarkLabelrecordsil.ILOffsetwhen marking a labelDemit(ILGenerator, OpCode, Label)usesTryGetShortFormOpCodeForBackwardBranchto automatically emit short form for backward branches (label already marked) whenlabelOffset - (ILOffset + 2)fits in[-128, 127]GetShortFormBranchOpCodemaps all 13 long-form branch opcodes to their short-form counterpartsILGeneratoris reused (RentPooledOrNewILGenerator), preventing false short-form selection after pool reuseSpecific forward-branch optimizations (always safe, hardcoded)
Patterns where the skipped IL is provably tiny:
IsFalseunaryBrfalse→Brfalse_S,Br→Br_SBr→Br_SCastclass)isLiftedToNullcomparisonBrfalse→Brfalse_S,Brtrue→Brtrue_SlabelDoneBr→Br_SLdc_I4_0/1)Tests updated
Three existing opcode-assertion tests updated to expect the new short-form opcodes where the optimizer now applies (
Issue498,Issue346,Issue159).