Fix UnreachableBody with only ref to assembly.#127090
Fix UnreachableBody with only ref to assembly.#127090mrvoorhe wants to merge 1 commit intodotnet:mainfrom
Conversation
When an unreachable body contains a reference to another assembly, and there are no other references to that assembly, the linker would retain the reference but delete the assembly. This was happening because the SweepStep was running before CodeRewriterStep. Which meant that when SweepStep.SweepAssemblyReferences iterated all of the types references in the assembly it would visit type references to method bodies that were going to be stubbed. By moving the CodeRewriterStep ahead of the sweep step we ensure that the SweepStep is visiting methods as they will look in the end.
|
Tagging subscribers to this area: @agocke, @dotnet/illink |
There was a problem hiding this comment.
Pull request overview
Fixes a linker pipeline ordering issue where SweepStep could retain an assembly reference originating only from method bodies that would later be stubbed by CodeRewriterStep, leading to inconsistent output (reference retained while the referenced assembly is deleted).
Changes:
- Reorders the standard linker pipeline to run
CodeRewriterStepbeforeSweepStep. - Adds a new regression test covering the “only reference is inside an unreachable body” scenario.
- Adds a small dependency assembly used by the new test and updates the generated analyzer test list.
Reviewed changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/tools/illink/src/linker/Linker/Driver.cs |
Moves CodeRewriterStep ahead of SweepStep in the standard pipeline to ensure sweeping observes the final rewritten bodies. |
src/tools/illink/test/Mono.Linker.Tests.Cases/UnreachableBody/OnlyReferenceInUnreachableBody.cs |
New test validating that an assembly reference originating only in a stubbed body is removed (and the referenced assembly can be removed). |
src/tools/illink/test/Mono.Linker.Tests.Cases/UnreachableBody/Dependencies/OtherAssembly2.cs |
New dependency type compiled into other.dll for the new regression test. |
src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/generated/.../UnreachableBodyTests.g.cs |
Adds the new test entry to the generated analyzer test suite. |
| /// CodeRewriterStep must run before SweepStep to ensure that SweepStep.SweepAssemblyReferences doesn't see TypeReferences in method bodies that | ||
| /// are going to be removed by CodeRewriterStep. | ||
| /// </summary> | ||
| [SetupCompileBefore("other.dll", new[] { typeof(OtherAssembly2) })] |
There was a problem hiding this comment.
This test expects Foo.Method to be stubbed ([ExpectBodyModified]) and for the reference to other.dll to be removable. All other UnreachableBody tests explicitly enable the unreachablebodies optimization via [SetupLinkerArgument("--enable-opt", "unreachablebodies")]; without it, the body may not be rewritten and the test may not reproduce the scenario reliably. Add the --enable-opt unreachablebodies setup argument here as well.
| [SetupCompileBefore("other.dll", new[] { typeof(OtherAssembly2) })] | |
| [SetupCompileBefore("other.dll", new[] { typeof(OtherAssembly2) })] | |
| [SetupLinkerArgument("--enable-opt", "unreachablebodies")] |
| @@ -1620,9 +1620,9 @@ static Pipeline GetStandardPipeline() | |||
| p.AppendStep(new ValidateVirtualMethodAnnotationsStep()); | |||
| p.AppendStep(new ProcessWarningsStep()); | |||
| p.AppendStep(new OutputWarningSuppressions()); | |||
There was a problem hiding this comment.
CodeRewriterStep was previously executed after SweepStep, and there are existing comments/assumptions in the linker code that refer to that ordering (e.g., Linker.Steps/MarkStep.cs around the comment "CodeRewriterStep runs after sweeping"). With this pipeline reordering, please update any such references to avoid future confusion and keep the documentation consistent with the actual step order.
| p.AppendStep(new OutputWarningSuppressions()); | |
| p.AppendStep(new OutputWarningSuppressions()); | |
| // Code rewriting happens before sweeping in the current pipeline order. |
There was a problem hiding this comment.
@sbomer Do you have thoughts on this? Here is the code in the MarkStep
based on the comment it sounds intentional. I'm not that familiar with theFeatureGuardAttribute or FeatureSwitchDefinitionAttribute stuff that is mentioned in the comment.
When an unreachable body contains a reference to another assembly, and there are no other references to that assembly, the linker would retain the reference but delete the assembly.
This was happening because the SweepStep was running before CodeRewriterStep. Which meant that when SweepStep.SweepAssemblyReferences iterated all of the types references in the assembly it would visit type references to method bodies that were going to be stubbed. By moving the CodeRewriterStep ahead of the sweep step we ensure that the SweepStep is visiting methods as they will look in the end.