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

SpillSequenceSpiller – preserve ref BoundConditionalOperator in presence of spilling in its either branch #74310

Merged
merged 3 commits into from
Jul 16, 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
29 changes: 28 additions & 1 deletion src/Compilers/CSharp/Portable/Lowering/SpillSequenceSpiller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,7 @@ public override BoundNode VisitConditionalOperator(BoundConditionalOperator node

return conditionBuilder.Update(_F.Default(node.Type));
}
else
else if (!node.IsRef)
{
var tmp = _F.SynthesizedLocal(node.Type, kind: SynthesizedLocalKind.Spill, syntax: _F.Syntax);

Expand All @@ -1142,6 +1142,33 @@ public override BoundNode VisitConditionalOperator(BoundConditionalOperator node

return conditionBuilder.Update(_F.Local(tmp));
}
else
{
Debug.Assert(condition.Type.SpecialType == SpecialType.System_Boolean);

// 1. Capture the boolean value (the condition) in a temp
var tmp = _F.SynthesizedLocal(condition.Type, kind: SynthesizedLocalKind.Spill, syntax: _F.Syntax);

conditionBuilder.AddLocal(tmp);
conditionBuilder.AddStatement(_F.Assignment(_F.Local(tmp), condition));
condition = _F.Local(tmp);

// 2. Conditionally execute side-effects from the builders based on the temp
conditionBuilder.AddLocals(consequenceBuilder.GetLocals());
conditionBuilder.AddLocals(alternativeBuilder.GetLocals());

conditionBuilder.AddStatement(
_F.If(condition,
_F.StatementList(consequenceBuilder.GetStatements()),
_F.StatementList(alternativeBuilder.GetStatements())));

consequenceBuilder.Free();
alternativeBuilder.Free();

// 3. Use updated conditional operator as the result. Note, we are using the captured temp as its condition,
Copy link
Member

Choose a reason for hiding this comment

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

Use updated conditional operator as the result

nit: consider calling out that we want to use the original node to maintain proper refness

// plus rewritten consequence and alternative.
return conditionBuilder.Update(node.Update(node.IsRef, condition, consequence, alternative, node.ConstantValueOpt, node.NaturalTypeOpt, node.WasTargetTyped, node.Type));
}
}

public override BoundNode VisitConversion(BoundConversion node)
Expand Down
Loading
Loading