Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid optimizing away ref array access #74553

Merged
merged 6 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Compilers/CSharp/Portable/BoundTree/BoundNodes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,15 @@
<Field Name="Indices" Type="ImmutableArray&lt;BoundExpression&gt;"/>
</Node>

<!--
Represents an array access that comes from the RHS of an unused ref (non-readonly) assignment.
Needed so we can emit with correct side effects (runtime element type check).
jjonescz marked this conversation as resolved.
Show resolved Hide resolved
-->
<Node Name="BoundRefArrayAccess" Base="BoundExpression">
<Field Name="Type" Type="TypeSymbol?" Override="true" Null="always"/>
<Field Name="ArrayAccess" Type="BoundArrayAccess" Null="disallow"/>
jjonescz marked this conversation as resolved.
Show resolved Hide resolved
</Node>

<!--
Represents an operation that is special in both IL and Expression trees -
getting length of a one-dimensional 0-based array (vector)
Expand Down
15 changes: 15 additions & 0 deletions src/Compilers/CSharp/Portable/CodeGen/EmitExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ private void EmitExpressionCore(BoundExpression expression, bool used)
EmitArrayElementLoad((BoundArrayAccess)expression, used);
break;

case BoundKind.RefArrayAccess:
EmitArrayElementRefLoad((BoundRefArrayAccess)expression, used);
break;

case BoundKind.ArrayLength:
EmitArrayLength((BoundArrayLength)expression, used);
break;
Expand Down Expand Up @@ -1099,6 +1103,17 @@ private void EmitArrayElementLoad(BoundArrayAccess arrayAccess, bool used)
EmitPopIfUnused(used);
}

private void EmitArrayElementRefLoad(BoundRefArrayAccess refArrayAccess, bool used)
{
if (used)
{
throw ExceptionUtilities.Unreachable();
}

EmitArrayElementAddress(refArrayAccess.ArrayAccess, AddressKind.Writeable);
_builder.EmitOpCode(ILOpCode.Pop);
}

private void EmitFieldLoad(BoundFieldAccess fieldAccess, bool used)
{
var field = fieldAccess.FieldSymbol;
Expand Down
10 changes: 10 additions & 0 deletions src/Compilers/CSharp/Portable/CodeGen/Optimizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2186,6 +2186,16 @@ public override BoundNode VisitAssignmentOperator(BoundAssignmentOperator node)

if (isLast)
{
if (node.IsRef &&
!node.WasCompilerGenerated &&
left.LocalSymbol.RefKind == RefKind.Ref &&
right is BoundArrayAccess arrayAccess &&
// Value types do not need runtime element type checks.
!arrayAccess.Type.IsValueType)
{
return new BoundRefArrayAccess(arrayAccess.Syntax, arrayAccess);
}

// assigned local is not used later => just emit the Right
return right;
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading