Conversation
d4a1818 to
3e956e0
Compare
There was a problem hiding this comment.
Pull request overview
This PR tightens the correctness conditions for aggressive SROA in TransformDisplayClassUsage by preventing propagation of display-class fields back to certain source variables when the field is mutated again (additional stores or escaping address), and updates the Ugly SROA regression tests to cover the fixed behavior.
Changes:
- Detect post-initialization mutations/escapes of captured fields and disable propagation when it cannot be safely redirected (notably for
thisand scalar-replaced display-class variables). - Add a small helper (
IsReadOnlyOrInitializerUse) to distinguish plain reads / recorded initializer stores from later mutations. - Re-enable the prior
Test9regression case and addTest10covering an address-escape mutation viaInterlocked.Exchange.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| ICSharpCode.Decompiler/IL/Transforms/TransformDisplayClassUsage.cs | Adds a guard that disables unsafe propagation when a captured field is mutated after initialization, plus a helper to classify field uses. |
| ICSharpCode.Decompiler.Tests/TestCases/Ugly/AggressiveScalarReplacementOfAggregates.Expected.cs | Updates expected decompiler output: re-enables Test9 and adds Test10 (Interlocked-based mutation case). |
| ICSharpCode.Decompiler.Tests/TestCases/Ugly/AggressiveScalarReplacementOfAggregates.cs | Updates input test case source: re-enables Test9 and adds Test10 with Interlocked.Exchange. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
christophwille
left a comment
There was a problem hiding this comment.
Reviewed both commits end to end, including a local run of the test suite.
Summary
- The
ValidateUseguard cancels propagation when a propagatable field is mutated after initialization (second store, or address escape likeInterlocked.Exchange), but only when the propagation source isthisor another display class. Non-this parameters keep propagating, which is sound becauseResolveVariableToPropagateonly allows them withLoadCount == 1, so the mutation store can be redirected to the parameter. - The
VisitStLocinitializer-inlining change correctly skips initializer stores of still-propagated fields (previously they would re-emit as a self-assignment; forthisthat is invalid C#), and theinsertionIndexbookkeeping keeps inserted stores contiguous.context.EndStep(null)is reachable when all stores are skipped, butEndStepis null-safe, so that is fine.
Details I checked: IsReadOnlyOrInitializerUse classifies all use shapes conservatively (nested LdFlda, ldflda as store value, call arguments all count as mutation); Initializers is non-empty whenever CanPropagate is true (both set together in AddVariable); HandleInitBlock breaks on duplicate fields so a second store in the init region is treated as mutation; the guard composes symmetrically with the existing post-pass in AnalyzeFunction that restricts surviving propagation to parameters and display classes; mutations inside nested lambdas are caught because LoadInstructions spans nested functions.
Verification
Ran UglyTestRunner.AggressiveScalarReplacementOfAggregates on this branch locally: all 4 UseRoslyn4_14_0 configs pass. The 4 UseRoslynLatest configs failed in my environment, but an unrelated control test (NoNewOfT) fails identically (csc cannot load System.Runtime.CompilerServices.Unsafe 6.0.3.0), so that is a local toolset issue with the Roslyn 5.9.0 transport-feed compiler, not this PR.
Verdict
Looks good to me. The fix is conservative in the right places and well-commented. Inline comments cover one theoretical validation-order edge case and two optional test suggestions; none are blockers.
This review was AI-assisted (Claude Code, claude-fable-5), driven and checked by a human.
Aggressive scalar replacement propagated a display-class field to its source variable even when the field is mutated after initialization, aliasing two distinct source-level variables (Test9: thisField and this). Propagation is now cancelled when the field sees a second store or its address escapes, but only for propagation targets that cannot absorb the store: 'this' and variables that are themselves scalar-replaced display classes. Parameters continue to propagate, because their remaining uses are already restricted by ResolveVariableToPropagate and a captured parameter mutated inside a lambda (DelegateConstruction's Bug951) must keep mapping to the parameter. Checking CanPropagate first also keeps the guard away from Mono state-machine fields, whose VariableToDeclare is pre-bound to a state-machine variable that Propagate(null) would discard. Re-enables Test9 and adds Test10 covering the escaping-address variant (Interlocked.Exchange(ref displayClass.thisField, ...)). Assisted-by: OpenCode:openai/gpt-5.5:OpenCode Assisted-by: Claude:claude-fable-5:Claude Code
A field that is propagated to the variable it was initialized from is
replaced by that variable, so re-emitting its initializer assigns the
variable to itself. Where the field was initialized from 'this' the
result does not even compile ('this = this'). The store is dropped
instead, which is what VisitStObj already does for initializer stores
that are not part of an object-initializer block; the insertion position
has to be tracked separately from the loop index, because skipping a
store would otherwise push the following ones past the end of the block.
Assisted-by: Claude:claude-fable-5:Claude Code
Review follow-up. A display-class field initialized from a non-this parameter is now the only shape where propagation and a later mutation coexist; it stays sound only because ResolveVariableToPropagate accepts a parameter with LoadCount == 1, so the mutation can be redirected to it. Nothing covered that, so Test12 pins it, and Test13 records the neighbouring shape where the mutation happens inside a lambda - there capturing the display class keeps it materialized and propagation never arises. The guard predicate is renamed to say what it matches, since 'ReadOnly' reads like the C# keyword rather than 'a plain read'. Assisted-by: Claude:claude-fable-5:Claude Code
5a11150 to
5484d29
Compare
Summary
Fixes #2032
Testing