Support enum values in substitution XML - #131663
Conversation
Allow ILLink to parse numeric enum values and NativeAOT to resolve names for int-backed enums. Add shared coverage for fields and stubbed method returns. Assisted-by: GitHub Copilot:gpt-5.6-sol code-review Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 77531f73-7423-457e-891c-705990f7e7af
Assisted-by: GitHub Copilot:gpt-5.6-sol [code-review] Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 77531f73-7423-457e-891c-705990f7e7af
Assisted-by: GitHub Copilot:gpt-5.6-sol [code-review] Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 77531f73-7423-457e-891c-705990f7e7af
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
Tagging subscribers to this area: @agocke, @dotnet/illink |
There was a problem hiding this comment.
Pull request overview
This PR makes enum-valued substitutions in ILLink.Substitutions.xml more consistent between ILLink (trimmer) and NativeAOT (ILCompiler) by accepting additional enum value forms, and adds regression coverage in both test suites.
Changes:
- ILLink: when substituting an enum value, fall back to parsing numeric strings using the enum’s underlying type.
- NativeAOT: for
int-backed enums, allow substitution values to be specified by enum member name (resolved to the member’s constant). - Tests: add a new substitutions test case and enable it in the NativeAOT trimming substitutions suite.
Show a summary per file
| File | Description |
|---|---|
| src/tools/illink/test/Mono.Linker.Tests.Cases/Substitutions/EnumSubstitutions.xml | New substitution XML covering enum member names and numeric values for fields and stubbed methods. |
| src/tools/illink/test/Mono.Linker.Tests.Cases/Substitutions/EnumSubstitutions.cs | New linker test case asserting stubbed method IL for enum substitutions by name and number. |
| src/tools/illink/src/linker/Linker.Steps/ProcessLinkerXmlBase.cs | Extends enum substitution parsing to accept numeric values via underlying-type parsing. |
| src/coreclr/tools/aot/ILCompiler.Trimming.Tests/TestCases/TestSuites.cs | Enables the new test case in the NativeAOT trimming substitutions suite. |
| src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/BodySubstitutionParser.cs | Adds enum-member-name resolution for int-backed enums when creating substitutions. |
Copilot's findings
- Files reviewed: 5/5 changed files
- Comments generated: 1
Assisted-by: GitHub Copilot:gpt-5.6-sol [code-review] Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 77531f73-7423-457e-891c-705990f7e7af
There was a problem hiding this comment.
Copilot's findings
Suppressed comments (1)
src/tools/illink/src/linker/Linker.Steps/ProcessLinkerXmlBase.cs:656
- The enum fallback currently only attempts numeric parsing when the underlying type is Int32. That doesn't match the stated goal of parsing numeric values using the enum's underlying type, and it means numeric substitutions will still fail for enums backed by byte/short/long/uint/etc (even though TryConvertValue already supports those primitive metadata types).
var underlyingType = typeDefinition.GetEnumUnderlyingType();
if (underlyingType.MetadataType == MetadataType.Int32)
return TryConvertValue(value, underlyingType, out result);
- Files reviewed: 5/5 changed files
- Comments generated: 0 new
agocke
left a comment
There was a problem hiding this comment.
LGTM, I think there are some ILTrim failures
Assisted-by: GitHub Copilot:gpt-5.6-sol [ci-investigation] Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 77531f73-7423-457e-891c-705990f7e7af
There was a problem hiding this comment.
Copilot's findings
Suppressed comments (3)
src/tools/illink/src/linker/Linker.Steps/ProcessLinkerXmlBase.cs:656
- Enum value parsing only falls back to numeric parsing when the enum underlying type is Int32. This means numeric substitutions (e.g. value="2") still won’t work for enums backed by byte/short/long/uint/etc even though TryConvertValue already supports those underlying metadata types. Since GetEnumUnderlyingType() returns the actual underlying primitive, the fallback should attempt conversion against that type unconditionally.
var underlyingType = typeDefinition.GetEnumUnderlyingType();
if (underlyingType.MetadataType == MetadataType.Int32)
return TryConvertValue(value, underlyingType, out result);
src/tools/illink/test/Mono.Linker.Tests.Cases/Substitutions/EnumSubstitutions.cs:72
- Same issue as FieldByName: the optimizer rewrites substituted static field reads (ldsfld) into constants, so the expected IL should use an ldc.i4 constant rather than ldsfld.
"ldsfld Mono.Linker.Tests.Cases.Substitutions.EnumSubstitutions/SubstitutionValue Mono.Linker.Tests.Cases.Substitutions.EnumSubstitutions::FieldByNumber",
src/tools/illink/test/Mono.Linker.Tests.Cases/Substitutions/EnumSubstitutions.cs:49
- This ExpectedInstructionSequence still expects the original enum field load (ldsfld). However, UnreachableBlocksOptimizer rewrites substituted static field reads to constants (see UnreachableBlocksOptimizer.cs handling of Code.Ldsfld + TryGetFieldUserValue), so the linked IL is expected to start with an ldc.i4 constant instead of ldsfld.
This issue also appears on line 72 of the same file.
"ldsfld Mono.Linker.Tests.Cases.Substitutions.EnumSubstitutions/SubstitutionValue Mono.Linker.Tests.Cases.Substitutions.EnumSubstitutions::FieldByName",
- Files reviewed: 6/6 changed files
- Comments generated: 0 new
Fixes #131601
int-backed enums to use member names.Tests:
Mono.Linker.Tests.TestCases.All.SubstitutionsTestsMono.Linker.Tests.TestCases.All.Substitutions(NativeAOT)Note
This pull request was created with assistance from GitHub Copilot.