Skip to content

Commit 62ef9b8

Browse files
Ensure that AdvSimd.Insert doesn't zero out the upper bits (#107091)
Co-authored-by: Tanner Gooding <[email protected]>
1 parent 578af59 commit 62ef9b8

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

612612
case NI_AdvSimd_Insert:
613+
{
613614
assert(isRMW);
614615

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

617-
if (intrin.op3->isContainedFltOrDblImmed())
618-
{
619-
assert(intrin.op2->isContainedIntOrIImmed());
620-
assert(intrin.op2->AsIntCon()->gtIconVal == 0);
618+
// fmov (scalar) zeros the upper bits and is not safe to use
619+
assert(!intrin.op3->isContainedFltOrDblImmed());
621620

622-
const double dataValue = intrin.op3->AsDblCon()->DconValue();
623-
GetEmitter()->emitIns_R_F(INS_fmov, emitSize, targetReg, dataValue, opt);
624-
}
625-
else
626-
{
627-
assert(targetReg != op3Reg);
621+
assert(targetReg != op3Reg);
628622

629-
HWIntrinsicImmOpHelper helper(this, intrin.op2, node);
623+
HWIntrinsicImmOpHelper helper(this, intrin.op2, node);
630624

631-
if (varTypeIsFloating(intrin.baseType))
625+
if (varTypeIsFloating(intrin.baseType))
626+
{
627+
for (helper.EmitBegin(); !helper.Done(); helper.EmitCaseEnd())
632628
{
633-
for (helper.EmitBegin(); !helper.Done(); helper.EmitCaseEnd())
634-
{
635-
const int elementIndex = helper.ImmValue();
629+
const int elementIndex = helper.ImmValue();
636630

637-
GetEmitter()->emitIns_R_R_I_I(ins, emitSize, targetReg, op3Reg, elementIndex, 0, opt);
638-
}
631+
GetEmitter()->emitIns_R_R_I_I(ins, emitSize, targetReg, op3Reg, elementIndex, 0, opt);
639632
}
640-
else
633+
}
634+
else
635+
{
636+
for (helper.EmitBegin(); !helper.Done(); helper.EmitCaseEnd())
641637
{
642-
for (helper.EmitBegin(); !helper.Done(); helper.EmitCaseEnd())
643-
{
644-
const int elementIndex = helper.ImmValue();
638+
const int elementIndex = helper.ImmValue();
645639

646-
GetEmitter()->emitIns_R_R_I(ins, emitSize, targetReg, op3Reg, elementIndex, opt);
647-
}
640+
GetEmitter()->emitIns_R_R_I(ins, emitSize, targetReg, op3Reg, elementIndex, opt);
648641
}
649642
}
650643
break;
644+
}
651645

652646
case NI_AdvSimd_InsertScalar:
653647
{

src/coreclr/jit/lowerarmarch.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2819,18 +2819,6 @@ void Lowering::ContainCheckHWIntrinsic(GenTreeHWIntrinsic* node)
28192819
if (intrin.op2->IsCnsIntOrI())
28202820
{
28212821
MakeSrcContained(node, intrin.op2);
2822-
2823-
if ((intrin.op2->AsIntCon()->gtIconVal == 0) && intrin.op3->IsCnsFltOrDbl())
2824-
{
2825-
assert(varTypeIsFloating(intrin.baseType));
2826-
2827-
const double dataValue = intrin.op3->AsDblCon()->DconValue();
2828-
2829-
if (comp->GetEmitter()->emitIns_valid_imm_for_fmov(dataValue))
2830-
{
2831-
MakeSrcContained(node, intrin.op3);
2832-
}
2833-
}
28342822
}
28352823
break;
28362824

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)