Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit b5268f3

Browse files
AndyAyersMSRussKeldorph
authored andcommitted
Port fix value numbering when selecting a constant to release/2.1
Master PR #18627 When applying selectors, constants were special-cased to not require any type casts. However this is wrong if a narrowing needs to be performed. Fix #18235
1 parent a104aa2 commit b5268f3

File tree

7 files changed

+179
-37
lines changed

7 files changed

+179
-37
lines changed

src/jit/valuenum.cpp

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2543,49 +2543,41 @@ ValueNum ValueNumStore::VNApplySelectorsTypeCheck(ValueNum elem, var_types indTy
25432543

25442544
if (indType != elemTyp)
25452545
{
2546-
bool isConstant = IsVNConstant(elem);
2547-
if (isConstant && (elemTyp == genActualType(indType)))
2546+
// We are trying to read from an 'elem' of type 'elemType' using 'indType' read
2547+
2548+
size_t elemTypSize = (elemTyp == TYP_STRUCT) ? elemStructSize : genTypeSize(elemTyp);
2549+
size_t indTypeSize = genTypeSize(indType);
2550+
2551+
if ((indType == TYP_REF) && (varTypeIsStruct(elemTyp)))
25482552
{
2549-
// (i.e. We recorded a constant of TYP_INT for a TYP_BYTE field)
2553+
// indType is TYP_REF and elemTyp is TYP_STRUCT
2554+
//
2555+
// We have a pointer to a static that is a Boxed Struct
2556+
//
2557+
return elem;
25502558
}
2551-
else
2559+
else if (indTypeSize > elemTypSize)
25522560
{
2553-
// We are trying to read from an 'elem' of type 'elemType' using 'indType' read
2554-
2555-
size_t elemTypSize = (elemTyp == TYP_STRUCT) ? elemStructSize : genTypeSize(elemTyp);
2556-
size_t indTypeSize = genTypeSize(indType);
2557-
2558-
if ((indType == TYP_REF) && (varTypeIsStruct(elemTyp)))
2559-
{
2560-
// indType is TYP_REF and elemTyp is TYP_STRUCT
2561-
//
2562-
// We have a pointer to a static that is a Boxed Struct
2563-
//
2564-
return elem;
2565-
}
2566-
else if (indTypeSize > elemTypSize)
2567-
{
2568-
// Reading beyong the end of 'elem'
2561+
// Reading beyong the end of 'elem'
25692562

2570-
// return a new unique value number
2571-
elem = VNForExpr(nullptr, indType);
2572-
JITDUMP(" *** Mismatched types in VNApplySelectorsTypeCheck (reading beyond the end)\n");
2573-
}
2574-
else if (varTypeIsStruct(indType))
2575-
{
2576-
// indType is TYP_STRUCT
2563+
// return a new unique value number
2564+
elem = VNForExpr(nullptr, indType);
2565+
JITDUMP(" *** Mismatched types in VNApplySelectorsTypeCheck (reading beyond the end)\n");
2566+
}
2567+
else if (varTypeIsStruct(indType))
2568+
{
2569+
// indType is TYP_STRUCT
25772570

2578-
// return a new unique value number
2579-
elem = VNForExpr(nullptr, indType);
2580-
JITDUMP(" *** Mismatched types in VNApplySelectorsTypeCheck (indType is TYP_STRUCT)\n");
2581-
}
2582-
else
2583-
{
2584-
// We are trying to read an 'elem' of type 'elemType' using 'indType' read
2571+
// return a new unique value number
2572+
elem = VNForExpr(nullptr, indType);
2573+
JITDUMP(" *** Mismatched types in VNApplySelectorsTypeCheck (indType is TYP_STRUCT)\n");
2574+
}
2575+
else
2576+
{
2577+
// We are trying to read an 'elem' of type 'elemType' using 'indType' read
25852578

2586-
// insert a cast of elem to 'indType'
2587-
elem = VNForCast(elem, indType, elemTyp);
2588-
}
2579+
// insert a cast of elem to 'indType'
2580+
elem = VNForCast(elem, indType, elemTyp);
25892581
}
25902582
}
25912583
return elem;

tests/arm/Tests.lst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90787,3 +90787,19 @@ Expected=0
9078790787
MaxAllowedDurationSeconds=600
9078890788
Categories=EXPECTED_PASS
9078990789
HostStyle=0
90790+
90791+
[GitHub_18235_1.cmd_11403]
90792+
RelativePath=JIT\Regression\JitBlue\GitHub_18235\GitHub_18235_1\GitHub_18235_1.cmd
90793+
WorkingDir=JIT\Regression\JitBlue\GitHub_18235\GitHub_18235_1
90794+
Expected=0
90795+
MaxAllowedDurationSeconds=600
90796+
Categories=EXPECTED_PASS
90797+
HostStyle=0
90798+
90799+
[GitHub_18235_2.cmd_11404]
90800+
RelativePath=JIT\Regression\JitBlue\GitHub_18235\GitHub_18235_2\GitHub_18235_2.cmd
90801+
WorkingDir=JIT\Regression\JitBlue\GitHub_18235\GitHub_18235_2
90802+
Expected=0
90803+
MaxAllowedDurationSeconds=600
90804+
Categories=EXPECTED_PASS
90805+
HostStyle=0

tests/arm64/Tests.lst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90787,3 +90787,19 @@ Expected=0
9078790787
MaxAllowedDurationSeconds=600
9078890788
Categories=EXPECTED_PASS
9078990789
HostStyle=0
90790+
90791+
[GitHub_18235_1.cmd_11721]
90792+
RelativePath=JIT\Regression\JitBlue\GitHub_18235\GitHub_18235_1\GitHub_18235_1.cmd
90793+
WorkingDir=JIT\Regression\JitBlue\GitHub_18235\GitHub_18235_1
90794+
Expected=0
90795+
MaxAllowedDurationSeconds=600
90796+
Categories=EXPECTED_PASS
90797+
HostStyle=0
90798+
90799+
[GitHub_18235_2.cmd_11722]
90800+
RelativePath=JIT\Regression\JitBlue\GitHub_18235\GitHub_18235_2\GitHub_18235_2.cmd
90801+
WorkingDir=JIT\Regression\JitBlue\GitHub_18235\GitHub_18235_2
90802+
Expected=0
90803+
MaxAllowedDurationSeconds=600
90804+
Categories=EXPECTED_PASS
90805+
HostStyle=0
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
7+
class C0
8+
{
9+
public sbyte F;
10+
}
11+
12+
public class Program
13+
{
14+
public static int Main()
15+
{
16+
C0 var0 = new C0 { F = -1 };
17+
// The JIT was giving (byte)var0.F the same value number as the -1 assigned
18+
// above, which was causing the OR below to be discarded.
19+
ulong var1 = (ulong)(1000 | (byte)var0.F);
20+
return var1 == 1023 ? 100 : 0;
21+
}
22+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<AssemblyName>$(MSBuildProjectName)</AssemblyName>
8+
<SchemaVersion>2.0</SchemaVersion>
9+
<ProjectGuid>{95DFC527-4DC1-495E-97D7-E94EE1F7140D}</ProjectGuid>
10+
<OutputType>Exe</OutputType>
11+
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
12+
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
13+
</PropertyGroup>
14+
<!-- Default configurations to help VS understand the configurations -->
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "></PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "></PropertyGroup>
17+
<ItemGroup>
18+
<CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
19+
<Visible>False</Visible>
20+
</CodeAnalysisDependentAssemblyPaths>
21+
</ItemGroup>
22+
<PropertyGroup>
23+
<DebugType></DebugType>
24+
<Optimize>True</Optimize>
25+
</PropertyGroup>
26+
<ItemGroup>
27+
<Compile Include="$(MSBuildProjectName).cs" />
28+
</ItemGroup>
29+
<ItemGroup>
30+
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
31+
</ItemGroup>
32+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
33+
<PropertyGroup Condition=" '$(MsBuildProjectDirOverride)' != '' "></PropertyGroup>
34+
</Project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
struct S0
6+
{
7+
public uint F0;
8+
public byte F2;
9+
public int F3;
10+
public sbyte F5;
11+
public long F8;
12+
}
13+
14+
public class Program
15+
{
16+
static uint s_0;
17+
public static int Main()
18+
{
19+
S0 vr3 = new S0();
20+
vr3.F0 = 0x10001;
21+
// The JIT was giving the RHS below the same value-number as
22+
// 0x10001 above, which was then constant propagated to
23+
// usages of vr4, even though it should be narrowed.
24+
uint vr4 = (ushort)vr3.F0;
25+
s_0 = vr4;
26+
return vr4 == 1 ? 100 : 0;
27+
}
28+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<AssemblyName>$(MSBuildProjectName)</AssemblyName>
8+
<SchemaVersion>2.0</SchemaVersion>
9+
<ProjectGuid>{95DFC527-4DC1-495E-97D7-E94EE1F7140D}</ProjectGuid>
10+
<OutputType>Exe</OutputType>
11+
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
12+
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
13+
</PropertyGroup>
14+
<!-- Default configurations to help VS understand the configurations -->
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "></PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "></PropertyGroup>
17+
<ItemGroup>
18+
<CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
19+
<Visible>False</Visible>
20+
</CodeAnalysisDependentAssemblyPaths>
21+
</ItemGroup>
22+
<PropertyGroup>
23+
<DebugType></DebugType>
24+
<Optimize>True</Optimize>
25+
</PropertyGroup>
26+
<ItemGroup>
27+
<Compile Include="$(MSBuildProjectName).cs" />
28+
</ItemGroup>
29+
<ItemGroup>
30+
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
31+
</ItemGroup>
32+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
33+
<PropertyGroup Condition=" '$(MsBuildProjectDirOverride)' != '' "></PropertyGroup>
34+
</Project>

0 commit comments

Comments
 (0)