Registry Mirror
Skip to content

Conversation

@siegfriedpammer

Copy link
Copy Markdown
Member

Summary

  • Keep aggressive SROA from propagating a display-class field to its source variable when the field is assigned again after initialization.
  • Re-enable the existing AggressiveScalarReplacementOfAggregates Test9 regression case.

Fixes #2032

Testing

  • OPENSSL_ENABLE_SHA1_SIGNATURES=1 dotnet test --project ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj --filter 'FullyQualifiedName~UglyTestRunner.AggressiveScalarReplacementOfAggregates' --report-trx

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 this and scalar-replaced display-class variables).
  • Add a small helper (IsReadOnlyOrInitializerUse) to distinguish plain reads / recorded initializer stores from later mutations.
  • Re-enable the prior Test9 regression case and add Test10 covering an address-escape mutation via Interlocked.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 christophwille left a comment

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.

Reviewed both commits end to end, including a local run of the test suite.

Summary

  1. The ValidateUse guard cancels propagation when a propagatable field is mutated after initialization (second store, or address escape like Interlocked.Exchange), but only when the propagation source is this or another display class. Non-this parameters keep propagating, which is sound because ResolveVariableToPropagate only allows them with LoadCount == 1, so the mutation store can be redirected to the parameter.
  2. The VisitStLoc initializer-inlining change correctly skips initializer stores of still-propagated fields (previously they would re-emit as a self-assignment; for this that is invalid C#), and the insertionIndex bookkeeping keeps inserted stores contiguous. context.EndStep(null) is reachable when all stores are skipped, but EndStep is 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
@siegfriedpammer
siegfriedpammer force-pushed the fix-2032-tdcu-copy-propagation branch from 5a11150 to 5484d29 Compare July 31, 2026 10:36
@siegfriedpammer
siegfriedpammer merged commit 541bdec into master Jul 31, 2026
15 checks passed
@siegfriedpammer
siegfriedpammer deleted the fix-2032-tdcu-copy-propagation branch July 31, 2026 11:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Follow up on #1981 - TDCU copy propagation correctness

3 participants