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

Fix bad folding #54722

Merged
merged 2 commits into from
Jul 13, 2021
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
12 changes: 4 additions & 8 deletions src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14864,19 +14864,15 @@ GenTree* Compiler::gtFoldExprConst(GenTree* tree)
JITDUMP("\nFolding operator with constant nodes into a constant:\n");
DISPTREE(tree);

#ifdef TARGET_64BIT
// Some operations are performed as 64 bit instead of 32 bit so the upper 32 bits
// need to be discarded. Since constant values are stored as ssize_t and the node
// has TYP_INT the result needs to be sign extended rather than zero extended.
i1 = INT32(i1);
#endif // TARGET_64BIT

// Also all conditional folding jumps here since the node hanging from
// GT_JTRUE has to be a GT_CNS_INT - value 0 or 1.

tree->ChangeOperConst(GT_CNS_INT);
tree->ChangeType(TYP_INT);
tree->AsIntCon()->SetIconValue(i1);
// Some operations are performed as 64 bit instead of 32 bit so the upper 32 bits
// need to be discarded. Since constant values are stored as ssize_t and the node
// has TYP_INT the result needs to be sign extended rather than zero extended.
tree->AsIntCon()->SetIconValue(static_cast<int>(i1));
tree->AsIntCon()->gtFieldSeq = fieldSeq;
if (vnStore != nullptr)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
public class FoldingExtendsInt32On64BitHostsTest
{
// On 64 bit hosts, 32 bit constants are stored as 64 bit signed values.
// gtFoldExpr failed to properly truncate the folded value to 32 bits when
// the host was 64 bit and the target - 32 bit. Thus local assertion prop
// got the "poisoned" value, which lead to silent bad codegen.

public static int Main()
{
var r1 = 31;
// "Poisoned" value.
var s1 = 0b11 << r1;

if (s1 == 0b11 << 31)
{
return 100;
}

// Just so that Roslyn actually uses locals.
Use(s1);
Use(r1);

return -1;
}

private static void Use(int a) { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<Optimize>True</Optimize>
<DebugType>None</DebugType>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
</Project>