Support /OPTIMIZE and raw method-body emission in managed ilasm - #135136
Open
jkoritzinsky wants to merge 2 commits into
Open
jkoritzinsky wants to merge 2 commits into
jkoritzinsky wants to merge 2 commits into
Conversation
Preserve authored instruction forms by default and select compact encodings only with /OPTIMIZE. Use a local method-body writer for branch fixups and raw exception clauses, retaining malformed offsets in error-tolerant output. Correct long variable operands and cover instruction, layout, diagnostic, and PDB behavior with exact-output tests. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Contributor
|
Tagging subscribers to this area: @JulieLeeMSFT, @dotnet/jit-contrib |
Resolve the visitor deletion by integrating raw instruction emission and method finalization with upstream ANTLR actions. Preserve native-compatible fat-header handling, assign non-conflicting diagnostic IDs, and retain upstream tests while adapting malformed-EH and source-span assertions. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Member
|
There are still unresolved conflicts |
Contributor
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Zero-code methods still bypass body emission and drop authored malformed exception regions.
Review effort: Balanced
Findings: 1
What changed in this PR
Adds byte-faithful managed ilasm emission, optional instruction optimization, branch fixups, and malformed EH preservation.
Changes:
- Introduces a custom method-body writer with branch, switch, token, and EH fixups.
- Implements
/OPTIMIZEinstruction selection and new diagnostics. - Adds extensive encoding, EH, and PDB tests.
| File | Description |
|---|---|
SourceDirectiveTests.cs |
Tests optimized PDB offsets. |
InstructionTests.cs |
Tests instruction encodings and fixups. |
ExceptionHandlingTests.cs |
Tests raw and optimized EH emission. |
DocumentCompilerTestHelpers.cs |
Adds compilation and IL helpers. |
Options.cs |
Documents optimization behavior. |
MethodBodyWriter.cs |
Implements raw method-body emission. |
InstructionEncoderExtensions.cs |
Removes private SRM accessor. |
EntityRegistry.cs |
Integrates the new writer. |
Diagnostic.cs |
Adds branch and EH diagnostics. |
GrammarActions.MethodBodies.ExceptionHandling.cs |
Records EH locations and labels. |
GrammarActions.MethodBodies.cs |
Completes fixups at method end. |
GrammarActions.Instructions.cs |
Routes instruction emission through the writer. |
GrammarActions.Conversions.cs |
Marks new diagnostics recoverable. |
KNOWN-ISSUES.md |
Removes the obsolete optimization limitation. |
Comment on lines
+370
to
+371
| bodyOffset = methodDef.MethodBody.WriteTo(bodyStreamEncoder, methodDef.MaxStack, | ||
| localsSigHandle, bodyAttributes, methodDef.ExceptionRegions, hasDynamicStackAllocation: true); |
jkotas
reviewed
Oct 3, 2026
| if (optimize && _labels[target.Id - 1] is int targetOffset) | ||
| { | ||
| long distance = (long)targetOffset - Offset; | ||
| // Native ilasm uses the worst-case long-instruction size when shortening a known target. |
Member
There was a problem hiding this comment.
Can this comment be rephrased without referring to native ilasm?
This branch has not been deployed
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.

Motivation
Managed ilasm currently ignores
/OPTIMIZE: integer-constant helpers compact instructions regardless of the option, while SRM's control-flow encoders cannot preserve some intentionally malformed offsets. This prevents byte-faithful assembly and limits the invalid IL that error-tolerant runtime tests can generate.Approach
/OPTIMIZE, select compact constant and argument/local encodings and apply native ilasm's numeric/known-target branch policy./ERROR, with source-located diagnostics, rather than dropping exception tables. Numeric branch/switch operands remain verbatim even when they target bytes outside the code.Forward long branches are not relaxed. Explicit
.sbranches are never widened; overflowing operands are diagnosed, and error-tolerant output retains their truncated bytes. Raw numeric EH bounds remain absolute and are not relocated when optimization changes instruction sizes.Validation
Windows x64:
dotnet.cmd build src\tools\ilasm\ilasm.slnxpassed, including NativeAOT publication.Assembler overhead measurements
Baseline and changed Release libraries were measured with the same branch-heavy input using BenchmarkDotNet 0.15.8 on Windows x64/.NET 11 RC, with three warmups and eight measured iterations.
These in-process measurements on a Hyper-V VM are exploratory, not a throughput-improvement claim. No mean throughput regression was observed; the large case allocates about 1.5% more for source-located fixup bookkeeping.
Note
This change and pull request description were prepared with GitHub Copilot.