Skip to content

Commit c827c27

Browse files
github-actions[bot]tannergoodingjeffschwMSFT
authored
Ensure that AdvSimd.Insert doesn't zero out the upper bits (#107089)
Co-authored-by: Tanner Gooding <tagoo@outlook.com> Co-authored-by: Jeff Schwartz <jeffschw@microsoft.com>
1 parent 6c8b1ed commit c827c27

File tree

4 files changed

+53
-35
lines changed

4 files changed

+53
-35
lines changed

src/coreclr/jit/hwintrinsiccodegenarm64.cpp

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,44 +1383,38 @@ void CodeGen::genHWIntrinsic(GenTreeHWIntrinsic* node)
13831383
break;
13841384

13851385
case NI_AdvSimd_Insert:
1386+
{
13861387
assert(isRMW);
13871388

13881389
GetEmitter()->emitIns_Mov(INS_mov, emitTypeSize(node), targetReg, op1Reg, /* canSkip */ true);
13891390

1390-
if (intrin.op3->isContainedFltOrDblImmed())
1391-
{
1392-
assert(intrin.op2->isContainedIntOrIImmed());
1393-
assert(intrin.op2->AsIntCon()->gtIconVal == 0);
1391+
// fmov (scalar) zeros the upper bits and is not safe to use
1392+
assert(!intrin.op3->isContainedFltOrDblImmed());
13941393

1395-
const double dataValue = intrin.op3->AsDblCon()->DconValue();
1396-
GetEmitter()->emitIns_R_F(INS_fmov, emitSize, targetReg, dataValue, opt);
1397-
}
1398-
else
1399-
{
1400-
assert(targetReg != op3Reg);
1394+
assert(targetReg != op3Reg);
14011395

1402-
HWIntrinsicImmOpHelper helper(this, intrin.op2, node);
1396+
HWIntrinsicImmOpHelper helper(this, intrin.op2, node);
14031397

1404-
if (varTypeIsFloating(intrin.baseType))
1398+
if (varTypeIsFloating(intrin.baseType))
1399+
{
1400+
for (helper.EmitBegin(); !helper.Done(); helper.EmitCaseEnd())
14051401
{
1406-
for (helper.EmitBegin(); !helper.Done(); helper.EmitCaseEnd())
1407-
{
1408-
const int elementIndex = helper.ImmValue();
1402+
const int elementIndex = helper.ImmValue();
14091403

1410-
GetEmitter()->emitIns_R_R_I_I(ins, emitSize, targetReg, op3Reg, elementIndex, 0, opt);
1411-
}
1404+
GetEmitter()->emitIns_R_R_I_I(ins, emitSize, targetReg, op3Reg, elementIndex, 0, opt);
14121405
}
1413-
else
1406+
}
1407+
else
1408+
{
1409+
for (helper.EmitBegin(); !helper.Done(); helper.EmitCaseEnd())
14141410
{
1415-
for (helper.EmitBegin(); !helper.Done(); helper.EmitCaseEnd())
1416-
{
1417-
const int elementIndex = helper.ImmValue();
1411+
const int elementIndex = helper.ImmValue();
14181412

1419-
GetEmitter()->emitIns_R_R_I(ins, emitSize, targetReg, op3Reg, elementIndex, opt);
1420-
}
1413+
GetEmitter()->emitIns_R_R_I(ins, emitSize, targetReg, op3Reg, elementIndex, opt);
14211414
}
14221415
}
14231416
break;
1417+
}
14241418

14251419
case NI_AdvSimd_InsertScalar:
14261420
{

src/coreclr/jit/lowerarmarch.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3771,18 +3771,6 @@ void Lowering::ContainCheckHWIntrinsic(GenTreeHWIntrinsic* node)
37713771
if (intrin.op2->IsCnsIntOrI())
37723772
{
37733773
MakeSrcContained(node, intrin.op2);
3774-
3775-
if ((intrin.op2->AsIntCon()->gtIconVal == 0) && intrin.op3->IsCnsFltOrDbl())
3776-
{
3777-
assert(varTypeIsFloating(intrin.baseType));
3778-
3779-
const double dataValue = intrin.op3->AsDblCon()->DconValue();
3780-
3781-
if (comp->GetEmitter()->emitIns_valid_imm_for_fmov(dataValue))
3782-
{
3783-
MakeSrcContained(node, intrin.op3);
3784-
}
3785-
}
37863774
}
37873775
break;
37883776

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+
4+
using System;
5+
using System.Runtime.CompilerServices;
6+
using System.Numerics;
7+
using System.Runtime.Intrinsics;
8+
using System.Runtime.Intrinsics.Arm;
9+
using Xunit;
10+
11+
// Generated by Fuzzlyn v2.2 on 2024-08-06 15:11:52
12+
// Run on Arm64 MacOS
13+
// Seed: 289142602786847481-vectort,vector64,vector128,armadvsimd,armadvsimdarm64,armaes,armarmbase,armarmbasearm64,armcrc32,armcrc32arm64,armdp,armrdm,armrdmarm64,armsha1,armsha256
14+
// Reduced from 87.5 KiB to 0.4 KiB in 00:00:36
15+
// Debug: Outputs <4607182418800017408, 13871573557235963454>
16+
// Release: Outputs <4607182418800017408, 0>
17+
18+
public class Runtime_106079
19+
{
20+
private static Vector128<double> s_38 = Vector128.Create(0, -567.3319449449843d);
21+
22+
public static void TestEntryPoint()
23+
{
24+
double vr4 = 1;
25+
s_38 = AdvSimd.Insert(s_38, 0, vr4);
26+
Assert.Equal(Vector128.Create(4607182418800017408UL, 13871573557235963454UL), s_38.AsUInt64());
27+
}
28+
}
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)