Skip to content

Convert managed ilasm to use ANTLR actions instead of visiting the parse tree - #132346

Open
jkoritzinsky wants to merge 16 commits into
ilasm-pseudoattributesfrom
ilasm-actions
Open

Convert managed ilasm to use ANTLR actions instead of visiting the parse tree#132346
jkoritzinsky wants to merge 16 commits into
ilasm-pseudoattributesfrom
ilasm-actions

Conversation

@jkoritzinsky

Copy link
Copy Markdown
Member

Currently, managed ilasm builds a parse tree for the whole input .il file. This causes massive memory allocations (and OOMs on x86) for some of our larger test assemblies (HardwareIntrinsics AVX512 and HugeArray) that are on the scale of CoreLib. It also causes a massive slowdown due to the allocations.

This PR converts our parse tree visitor to instead be called as separate "actions" during grammar parsing, similar to how native ilasm does in the YACC file. Unlike native ilasm, all of the real logic is in C# files, and the logic in the CIL.g4 file is minimal (function calls into C#).

Because ANTLR4 only emits public types, we limit the exposed public API surface for (expected) internal consumers of the ILAssembler library (such as Roslyn for their test tree) to only the expected public API surface.


Stack created with GitHub Stacks CLIGive Feedback 💬

jkoritzinsky and others added 16 commits August 14, 2026 15:34
Use an unbuffered token stream and disable whole-document parse-tree
construction. Retain only bounded declaration and member subtrees while
semantic actions compile them, and stream byte arrays directly.

Split the compiler actions by IL feature to keep the implementation
maintainable and add error-recovery and large-input coverage.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c986c103-8383-4db4-9b11-9be49dc68a05
Emit common instruction and method-body forms directly from thin parser
actions without constructing temporary parse subtrees. Keep complex
operands in isolated bounded subtrees for subsequent stack layers.

Preserve instruction diagnostics and deterministic output while reducing
large-method assembly time.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c986c103-8383-4db4-9b11-9be49dc68a05
Synthesize floating literals and composed strings directly from parser
rules, and stream switch labels into an action-owned accumulator. Remove
the corresponding bounded instruction subtrees while preserving output
and error recovery.

Also fix odd-length ANSI string padding to match native ilasm.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c986c103-8383-4db4-9b11-9be49dc68a05
Remove the generic instruction parse-tree island and emit reference,
token, type, and calli instructions directly after their operand rules.
Each complex operand now retains only its own bounded subtree, preparing
the type and signature rules for independent synthesis.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c986c103-8383-4db4-9b11-9be49dc68a05
Convert type, signature, class-name, and member-reference rules to compact
semantic values so reference instructions no longer require parse-tree
subtrees. Preserve internal entity types behind object-valued generated
context slots and materialize them through strongly typed action helpers.

Keep marshalling as the final bounded signature island.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c986c103-8383-4db4-9b11-9be49dc68a05
Convert native marshalling, SAFEARRAY variants, IID parameters, and raw
marshal blobs to compact semantic values. Remove the final signature-layer
parse subtree while preserving descriptor bytes and recursive native-type
ordering.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c986c103-8383-4db4-9b11-9be49dc68a05
Replace the generic method-declaration and SEH subtrees with direct
method directive actions and synthesized exception-region descriptors.
Keep nested scopes and handlers tree-free while preserving catch type and
label allocation order.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c986c103-8383-4db4-9b11-9be49dc68a05
Replace class-member and method-header subtrees with synthesized method,
field, property, event, generic, and P/Invoke values. Resolve class-level
method overrides at type close so forward method definitions bind like
native ilasm.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c986c103-8383-4db4-9b11-9be49dc68a05
Replace the top-level declaration, namespace-header, and class-header
subtrees with direct dispatch and synthesized type headers. Preserve
class attribute, generic constraint, inheritance, interface, and nested
scope ordering while leaving shared directives in minimal islands.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c986c103-8383-4db4-9b11-9be49dc68a05
Convert custom attribute descriptors, named arguments, serialized values,
arrays, object sequences, and field/parameter initializers to semantic
values. Remove their parse-tree islands while preserving pseudo-attribute
lowering and owner binding.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c986c103-8383-4db4-9b11-9be49dc68a05
Stream data declaration items and synthesize declarative security, source
mapping, and language directives. Remove their shared parse-tree islands
while preserving label fixups, parent ownership, and PDB state.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c986c103-8383-4db4-9b11-9be49dc68a05
Synthesize assembly definitions and references, files, exported types,
resources, vtable fixups, and typedefs. Remove the final parse-tree
islands so BuildParseTree remains disabled for the complete parse.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c986c103-8383-4db4-9b11-9be49dc68a05
Synthesize assembly definitions and references, files, exported types,
resources, vtable fixups, and typedefs. Remove the final parse-tree
islands so BuildParseTree remains disabled for the complete parse.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c986c103-8383-4db4-9b11-9be49dc68a05
Disable visitor generation and remove the generated visitor interfaces,
parser Accept overrides, explicit forwarding methods, and visitor-only dead
code. The parser action pipeline now owns all semantic traversal.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c986c103-8383-4db4-9b11-9be49dc68a05
Replace the remaining GrammarResult wrappers with direct semantic return
values and remove the parse-tree mode stack, which became redundant once
tree construction stayed disabled for the entire parse. Inject the action
object directly through the generated parser.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c986c103-8383-4db4-9b11-9be49dc68a05
Add an explicit ILAssembler reference contract so generated parser and
implementation-only types do not define the supported API. Replace
object-typed grammar returns and parser accumulation stacks with concrete
CILParser semantic values and rule-local builders.

Harden every typed return against ANTLR error recovery and add malformed
input mutation coverage.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c986c103-8383-4db4-9b11-9be49dc68a05
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
11 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, @dotnet/jit-contrib
See info in area-owners.md if you want to be subscribed.

Comment thread src/tools/ilasm/README.md

```
./dotnet.sh build src/tools/ilasm/src/ILAssembler/gen
./dotnet.sh build src/tools/ilasm/src/ILAssembler

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it intentional that gen was dropped here?

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

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants