Skip to content

Commit b54c8de

Browse files
Ensure that AdvSimd.Insert doesn't zero out the upper bits (#106981)
1 parent 8d24ba3 commit b54c8de

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
@@ -1349,44 +1349,38 @@ void CodeGen::genHWIntrinsic(GenTreeHWIntrinsic* node)
13491349
break;
13501350

13511351
case NI_AdvSimd_Insert:
1352+
{
13521353
assert(isRMW);
13531354

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

1356-
if (intrin.op3->isContainedFltOrDblImmed())
1357-
{
1358-
assert(intrin.op2->isContainedIntOrIImmed());
1359-
assert(intrin.op2->AsIntCon()->gtIconVal == 0);
1357+
// fmov (scalar) zeros the upper bits and is not safe to use
1358+
assert(!intrin.op3->isContainedFltOrDblImmed());
13601359

1361-
const double dataValue = intrin.op3->AsDblCon()->DconValue();
1362-
GetEmitter()->emitIns_R_F(INS_fmov, emitSize, targetReg, dataValue, opt);
1363-
}
1364-
else
1365-
{
1366-
assert(targetReg != op3Reg);
1360+
assert(targetReg != op3Reg);
13671361

1368-
HWIntrinsicImmOpHelper helper(this, intrin.op2, node);
1362+
HWIntrinsicImmOpHelper helper(this, intrin.op2, node);
13691363

1370-
if (varTypeIsFloating(intrin.baseType))
1364+
if (varTypeIsFloating(intrin.baseType))
1365+
{
1366+
for (helper.EmitBegin(); !helper.Done(); helper.EmitCaseEnd())
13711367
{
1372-
for (helper.EmitBegin(); !helper.Done(); helper.EmitCaseEnd())
1373-
{
1374-
const int elementIndex = helper.ImmValue();
1368+
const int elementIndex = helper.ImmValue();
13751369

1376-
GetEmitter()->emitIns_R_R_I_I(ins, emitSize, targetReg, op3Reg, elementIndex, 0, opt);
1377-
}
1370+
GetEmitter()->emitIns_R_R_I_I(ins, emitSize, targetReg, op3Reg, elementIndex, 0, opt);
13781371
}
1379-
else
1372+
}
1373+
else
1374+
{
1375+
for (helper.EmitBegin(); !helper.Done(); helper.EmitCaseEnd())
13801376
{
1381-
for (helper.EmitBegin(); !helper.Done(); helper.EmitCaseEnd())
1382-
{
1383-
const int elementIndex = helper.ImmValue();
1377+
const int elementIndex = helper.ImmValue();
13841378

1385-
GetEmitter()->emitIns_R_R_I(ins, emitSize, targetReg, op3Reg, elementIndex, opt);
1386-
}
1379+
GetEmitter()->emitIns_R_R_I(ins, emitSize, targetReg, op3Reg, elementIndex, opt);
13871380
}
13881381
}
13891382
break;
1383+
}
13901384

13911385
case NI_AdvSimd_InsertScalar:
13921386
{

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)