Skip to content

Commit ce1978f

Browse files
saucecontroljkotas
andauthored
JIT: Don't allow retyping of casts containing a memory load (#114602)
* don't allow retyping of casts containing a memory load * fix NRE in test --------- Co-authored-by: Jan Kotas <[email protected]>
1 parent ed3081f commit ce1978f

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

src/coreclr/jit/lowerxarch.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4150,7 +4150,8 @@ GenTree* Lowering::LowerHWIntrinsicCreate(GenTreeHWIntrinsic* node)
41504150
// The most likely case is that op1 is a cast from int/long to the base type:
41514151
// * CAST int <- short <- int/long
41524152
// If the base type is signed, that cast will be sign-extending, but we need zero extension,
4153-
// so we can simply retype the cast to the unsigned type of the same size.
4153+
// so we may be able to simply retype the cast to the unsigned type of the same size.
4154+
// This is valid only if the cast is not checking overflow and is not containing a load.
41544155
//
41554156
// It's also possible we have a memory load of the base type:
41564157
// * IND short
@@ -4163,7 +4164,7 @@ GenTree* Lowering::LowerHWIntrinsicCreate(GenTreeHWIntrinsic* node)
41634164

41644165
var_types unsignedType = varTypeToUnsigned(simdBaseType);
41654166

4166-
if (op1->OperIs(GT_CAST) && !op1->gtOverflow() &&
4167+
if (op1->OperIs(GT_CAST) && !op1->gtOverflow() && !op1->AsCast()->CastOp()->isContained() &&
41674168
(genTypeSize(op1->CastToType()) == genTypeSize(simdBaseType)))
41684169
{
41694170
op1->AsCast()->gtCastType = unsignedType;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
4+
// Generated by Fuzzlyn v2.5 on 2025-04-11 20:04:48
5+
// Run on X64 Windows
6+
// Seed: 177501341222670481-vectort,vector128,vector256,x86aes,x86avx,x86avx2,x86avx512bw,x86avx512bwvl,x86avx512cd,x86avx512cdvl,x86avx512dq,x86avx512dqvl,x86avx512f,x86avx512fvl,x86avx512fx64,x86bmi1,x86bmi1x64,x86bmi2,x86bmi2x64,x86fma,x86lzcnt,x86lzcntx64,x86pclmulqdq,x86popcnt,x86popcntx64,x86sse,x86ssex64,x86sse2,x86sse2x64,x86sse3,x86sse41,x86sse41x64,x86sse42,x86sse42x64,x86ssse3,x86x86base
7+
// Reduced from 73.5 KiB to 0.6 KiB in 00:02:02
8+
// Hits JIT assert in Release:
9+
// Assertion failed 'varTypeIsUnsigned(srcLoadType) || (genTypeSize(srcLoadType) >= genTypeSize(castType))' in 'Program:Main(Fuzzlyn.ExecutionServer.IRuntime)' during 'Generate code' (IL size 35; hash 0xade6b36b; FullOpts)
10+
using System;
11+
using System.Runtime.Intrinsics;
12+
using System.Runtime.Intrinsics.X86;
13+
using Xunit;
14+
15+
public class Runtime_114573
16+
{
17+
public static IRuntime s_rt = new Runtime();
18+
public static sbyte s_1;
19+
20+
[Fact]
21+
public static void Problem()
22+
{
23+
var vr1 = (short)(65535 & s_1);
24+
Vector128<short> vr2 = Vector128.CreateScalar(vr1);
25+
s_rt.WriteLine(vr2);
26+
}
27+
}
28+
29+
public interface IRuntime
30+
{
31+
void WriteLine<T>(T value);
32+
}
33+
34+
public class Runtime : IRuntime
35+
{
36+
public void WriteLine<T>(T value) => System.Console.WriteLine(value);
37+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<Optimize>True</Optimize>
4+
</PropertyGroup>
5+
<ItemGroup>
6+
<Compile Include="$(MSBuildProjectName).cs" />
7+
</ItemGroup>
8+
</Project>

0 commit comments

Comments
 (0)