Skip to content

Commit 70518f4

Browse files
amanasifkhalidjtschuster
authored andcommitted
JIT ARM64: Don't emit mov for zero case in jump tables for shift intrinsics (dotnet#107322)
1 parent 16494ec commit 70518f4

File tree

4 files changed

+65
-12
lines changed

4 files changed

+65
-12
lines changed

src/coreclr/jit/gentree.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20824,6 +20824,16 @@ GenTree* Compiler::gtNewSimdBinOpNode(
2082420824
if (op2->IsCnsIntOrI())
2082520825
{
2082620826
op2->AsIntCon()->gtIconVal &= shiftCountMask;
20827+
#ifdef TARGET_ARM64
20828+
// On ARM64, ShiftRight* intrinsics cannot encode a shift value of zero,
20829+
// so use the generic Shift* fallback intrinsic.
20830+
// GenTreeHWIntrinsic::GetHWIntrinsicIdForBinOp will see that the immediate node is not const,
20831+
// and return the correct fallback intrinsic.
20832+
if ((op != GT_LSH) && (op2->AsIntCon()->IconValue() == 0))
20833+
{
20834+
op2 = gtNewZeroConNode(type);
20835+
}
20836+
#endif // TARGET_ARM64
2082720837
}
2082820838
else
2082920839
{

src/coreclr/jit/hwintrinsiccodegenarm64.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -434,18 +434,8 @@ void CodeGen::genHWIntrinsic(GenTreeHWIntrinsic* node)
434434
for (helper.EmitBegin(); !helper.Done(); helper.EmitCaseEnd())
435435
{
436436
const int shiftAmount = helper.ImmValue();
437-
438-
if (shiftAmount == 0)
439-
{
440-
// TODO: Use emitIns_Mov instead.
441-
// We do not use it currently because it will still elide the 'mov'
442-
// even if 'canSkip' is false. We cannot elide the 'mov' here.
443-
GetEmitter()->emitIns_R_R_R(INS_mov, emitTypeSize(node), targetReg, reg, reg);
444-
}
445-
else
446-
{
447-
GetEmitter()->emitIns_R_R_I(ins, emitSize, targetReg, reg, shiftAmount, opt);
448-
}
437+
assert((shiftAmount != 0) || (intrin.category == HW_Category_ShiftLeftByImmediate));
438+
GetEmitter()->emitIns_R_R_I(ins, emitSize, targetReg, reg, shiftAmount, opt);
449439
}
450440
};
451441

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.4 on 2024-08-26 23:38:13
5+
// Run on Arm64 Linux
6+
// Seed: 8716802894387291290-vectort,vector64,vector128,armadvsimd,armadvsimdarm64,armaes,armarmbase,armarmbasearm64,armcrc32,armcrc32arm64,armdp,armrdm,armrdmarm64,armsha1,armsha256
7+
// Reduced from 19.5 KiB to 0.5 KiB in 00:00:27
8+
// Debug: Outputs <0, 0, 0, 0>
9+
// Release: Outputs <0, 0, 4457472, 0>
10+
using System;
11+
using System.Numerics;
12+
using System.Runtime.Intrinsics;
13+
using System.Runtime.Intrinsics.Arm;
14+
using Xunit;
15+
16+
public class C0
17+
{
18+
public ushort F2;
19+
public ushort F8;
20+
}
21+
22+
public class Runtime_107173
23+
{
24+
public static C0 s_8 = new C0();
25+
26+
[Fact]
27+
public static void TestLeftShift()
28+
{
29+
if (AdvSimd.IsSupported)
30+
{
31+
var vr6 = s_8.F8;
32+
var vr7 = s_8.F2;
33+
var vr8 = Vector64.Create(vr6, vr7, 0, 0);
34+
Vector128<uint> vr9 = AdvSimd.ShiftLeftLogicalWideningLower(vr8, 0);
35+
Assert.Equal(vr9, Vector128<uint>.Zero);
36+
}
37+
}
38+
39+
[Fact]
40+
public static void TestRightShift()
41+
{
42+
var result = Vector128<byte>.AllBitsSet >> 8;
43+
Assert.Equal(result, Vector128<byte>.AllBitsSet);
44+
}
45+
}
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)