10000 Fix a couple issues with Vector.WithElement by tannergooding · Pull Request #115648 · dotnet/runtime · GitHub
[go: up one dir, main page]

Skip to content

Fix a couple issues with Vector.WithElement #115648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27867,14 +27867,8 @@ GenTree* Compiler::gtNewSimdWithElementNode(
var_types simdBaseType = JitType2PreciseVarType(simdBaseJitType);

assert(varTypeIsArithmetic(simdBaseType));
assert(op2->IsCnsIntOrI());
assert(varTypeIsArithmetic(op3));

ssize_t imm8 = op2->AsIntCon()->IconValue();
ssize_t count = simdSize / genTypeSize(simdBaseType);

assert((0 <= imm8) && (imm8 < count));

#if defined(TARGET_XARCH)
switch (simdBaseType)
{
Expand Down Expand Up @@ -27940,6 +27934,20 @@ GenTree* Compiler::gtNewSimdWithElementNode(
#error Unsupported platform
#endif // !TARGET_XARCH && !TARGET_ARM64

int immUpperBound = getSIMDVectorLength(simdSize, simdBaseType) - 1;
bool rangeCheckNeeded = !op2->OperIsConst();

if (!rangeCheckNeeded)
{
ssize_t imm8 = op2->AsIntCon()->IconValue();
rangeCheckNeeded = (imm8 < 0) || (imm8 > immUpperBound);
}

if (rangeCheckNeeded)
{
op2 = addRangeCheckForHWIntrinsic(op2, 0, immUpperBound);
}

return gtNewSimdHWIntrinsicNode(type, op1, op2, op3, hwIntrinsicID, simdBaseJitType, simdSize);
}

Expand Down
53 changes: 53 additions & 0 deletions src/coreclr/jit/hwintrinsiccodegenxarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1832,6 +1832,7 @@ void CodeGen::genBaseIntrinsic(GenTreeHWIntrinsic* node, insOpts instOptions)

GenTree* op1 = (node->GetOperandCount() >= 1) ? node->Op(1) : nullptr;
GenTree* op2 = (node->GetOperandCount() >= 2) ? node->Op(2) : nullptr;
GenTree* op3 = (node->GetOperandCount() >= 3) ? node->Op(3) : nullptr;

genConsumeMultiOpOperands(node);
regNumber op1Reg = (op1 == nullptr) ? REG_NA : op1->GetRegNum();
Expand Down Expand Up @@ -1968,6 +1969,58 @@ void CodeGen::genBaseIntrinsic(GenTreeHWIntrinsic* node, insOpts instOptions)
break;
}

case NI_Vector128_WithElement:
case NI_Vector256_WithElement:
case NI_Vector512_WithElement:
{
// Optimize the case where op2 is not a constant.

assert(!op1->isContained());
assert(!op2->OperIsConst());

// We don't have an instruction to implement this intrinsic if the index is not a constant.
// So we will use the SIMD temp location to store the vector, set the value and then reload it.
// The range check will already have been performed, so at this point we know we have an index
// within the bounds of the vector.

unsigned simdInitTempVarNum = compiler->lvaSIMDInitTempVarNum;
noway_assert(simdInitTempVarNum != BAD_VAR_NUM);

bool isEBPbased;
unsigned offs = compiler->lvaFrameAddress(simdInitTempVarNum, &isEBPbased);

#if !FEATURE_FIXED_OUT_ARGS
if (!isEBPbased)
{
// Adjust the offset by the amount currently pushed on the CPU stack
offs += genStackLevel;
}
#else
assert(genStackLevel == 0);
#endif // !FEATURE_FIXED_OUT_ARGS

regNumber indexReg = op2->GetRegNum();
regNumber valueReg = op3->GetRegNum(); // New element value to be stored

// Store the vector to the temp location.
GetEmitter()->emitIns_S_R(ins_Store(simdType, compiler->isSIMDTypeLocalAligned(simdInitTempVarNum)),
emitTypeSize(simdType), op1Reg, simdInitTempVarNum, 0);

// Set the desired element.
GetEmitter()->emitIns_ARX_R(ins_Store(op3->TypeGet()), // Store
emitTypeSize(baseType), // Of the vector baseType
valueReg, // From valueReg
(isEBPbased) ? REG_EBP : REG_ESP, // Stack-based
indexReg, // Indexed
genTypeSize(baseType), // by the size of the baseType
offs); // Offset

// Write back the modified vector to the original location.
GetEmitter()->emitIns_R_S(ins_Load(simdType, compiler->isSIMDTypeLocalAligned(simdInitTempVarNum)),
emitTypeSize(simdType), targetReg, simdInitTempVarNum, 0);
break;
}

case NI_Vector128_GetElement:
case NI_Vector256_GetElement:
case NI_Vector512_GetElement:
Expand Down
28 changes: 0 additions & 28 deletions src/coreclr/jit/hwintrinsicxarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4273,34 +4273,6 @@ GenTree* Compiler::impSpecialIntrinsic(NamedIntrinsic intrinsic,
assert(sig->numArgs == 3);
GenTree* indexOp = impStackTop(1).val;

if (!indexOp->OperIsConst())
{
if (!opts.OptimizationEnabled())
{
// Only enable late stage rewriting if optimizations are enabled
// as we won't otherwise encounter a constant at the later point
return nullptr;
}

op3 = impPopStack().val;
op2 = impPopStack().val;
op1 = impSIMDPopStack();

retNode = gtNewSimdHWIntrinsicNode(retType, op1, op2, op3, intrinsic, simdBaseJitType, simdSize);

retNode->AsHWIntrinsic()->SetMethodHandle(this, method R2RARG(*entryPoint));
break;
}

ssize_t imm8 = indexOp->AsIntCon()->IconValue();
ssize_t count = simdSize / genTypeSize(simdBaseType);

if ((imm8 >= count) || (imm8 < 0))
{
// Using software fallback if index is out of range (throw exception)
return nullptr;
}

switch (simdBaseType)
{
// Using software fallback if simdBaseType is not supported by hardware
Expand Down
24 changes: 17 additions & 7 deletions src/coreclr/jit/lowerxarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5172,8 +5172,8 @@ GenTree* Lowering::LowerHWIntrinsicGetElement(GenTreeHWIntrinsic* node)
return LowerNode(node);
}

uint32_t count = simdSize / genTypeSize(simdBaseType);
uint32_t elemSize = genTypeSize(simdBaseType);
uint32_t count = simdSize / elemSize;

if (op1->OperIs(GT_IND))
{
Expand Down Expand Up @@ -5658,14 +5658,24 @@ GenTree* Lowering::LowerHWIntrinsicWithElement(GenTreeHWIntrinsic* node)
GenTree* op2 = node->Op(2);
GenTree* op3 = node->Op(3);

assert(op2->OperIsConst());
if (!op2->OperIsConst())
{
// We will specially handle WithElement in codegen when op2 isn't a constant
ContainCheckHWIntrinsic(node);
return node->gtNext;
}

// We should have a bounds check inserted for any index outside the allowed range
// but we need to generate some code anyways, and so we'll simply mask here for simplicity.

ssize_t count = simdSize / genTypeSize(simdBaseType);
ssize_t imm8 = op2->AsIntCon()->IconValue();
ssize_t simd16Cnt = 16 / genTypeSize(simdBaseType);
ssize_t simd16Idx = imm8 / simd16Cnt;
uint32_t elemSize = genTypeSize(simdBaseType);
uint32_t count = simdSize / elemSize;

assert(0 <= imm8 && imm8 < count);
uint32_t imm8 = static_cast<uint8_t>(op2->AsIntCon()->IconValue()) % count;
uint32_t simd16Cnt = 16 / elemSize;
uint32_t simd16Idx = imm8 / simd16Cnt;

assert((0 <= imm8) && (imm8 < count));

switch (simdBaseType)
{
Expand Down
27 changes: 27 additions & 0 deletions A3E2 src/coreclr/jit/lsraxarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2317,6 +2317,33 @@ int LinearScan::BuildHWIntrinsic(GenTreeHWIntrinsic* intrinsicTree, int* pDstCou
break;
}

case NI_Vector128_WithElement:
case NI_Vector256_WithElement:
case NI_Vector512_WithElement:
{
assert(numArgs == 3);

assert(!op1->isContained());
assert(!op2->OperIsConst());

// If the index is not a constant
// we will use the SIMD temp location to store the vector.

var_types requiredSimdTempType = intrinsicTree->TypeGet();
compiler->getSIMDInitTempVarNum(requiredSimdTempType);

// We then customize the uses as we will effectively be spilling
// op1, storing op3 to that spill location based on op2. Then
// reloading the updated value to the destination

srcCount += BuildOperandUses(op1);
srcCount += BuildOperandUses(op2);
srcCount += BuildOperandUses(op3, varTypeIsByte(baseType) ? allByteRegs() : RBM_NONE);

buildUses = false;
break;
}

case NI_Vector128_AsVector128Unsafe:
case NI_Vector128_AsVector2:
case NI_Vector128_AsVector3:
Expand Down
51 changes: 0 additions & 51 deletions src/coreclr/jit/rationalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,57 +385,6 @@ void Rationalizer::RewriteHWIntrinsicAsUserCall(GenTree** use, ArrayStack<GenTre
break;
}

case NI_Vector128_WithElement:
#if defined(TARGET_XARCH)
case NI_Vector256_WithElement:
case NI_Vector512_WithElement:
#elif defined(TARGET_ARM64)
case NI_Vector64_WithElement:
#endif
{
assert(operandCount == 3);

GenTree* op1 = operands[0];
GenTree* op2 = operands[1];
GenTree* op3 = operands[2];

if (op2->OperIsConst())
{
ssize_t imm8 = op2->AsIntCon()->IconValue();
ssize_t count = simdSize / genTypeSize(simdBaseType);

if ((imm8 >= count) || (imm8 < 0))
{
// Using software fallback if index is out of range (throw exception)
break;
}

#if defined(TARGET_XARCH)
if (varTypeIsIntegral(simdBaseType))
{
if (varTypeIsLong(simdBaseType))
{
if (!comp->compOpportunisticallyDependsOn(InstructionSet_SSE41_X64))
{
break;
}
}
else if (!varTypeIsShort(simdBaseType))
{
if (!comp->compOpportunisticallyDependsOn(InstructionSet_SSE41))
{
break;
}
}
}
#endif // TARGET_XARCH

result = comp->gtNewSimdWithElementNode(retType, op1, op2, op3, simdBaseJitType, simdSize);
break;
}
break;
}

default:
{
if (sigInfo.numArgs == 0)
Expand Down
16 changes: 14 additions & 2 deletions src/mono/mono/mini/simd-intrinsics.c
Original file line number Diff line number Diff line change
Expand Up @@ -2553,6 +2553,9 @@ emit_sri_vector (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsi
if (index < 0 || index >= elems) {
MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, args [1]->dreg, elems);
MONO_EMIT_NEW_COND_EXC (cfg, GE_UN, "ArgumentOutOfRangeException");

// Fixup the index to be in range so codegen is still valid
index %= elems;
}

// Bounds check is elided if we know the index is safe.
Expand Down Expand Up @@ -3220,8 +3223,11 @@ emit_sri_vector (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsi
// If the index is provably a constant, we can generate vastly better code.
int index = GTMREG_TO_INT (args[1]->inst_c0);
if (index < 0 || index >= elems) {
MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, args [1]->dreg, elems);
MONO_EMIT_NEW_COND_EXC (cfg, GE_UN, "ArgumentOutOfRangeException");
MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, args [1]->dreg, elems);
MONO_EMIT_NEW_COND_EXC (cfg, GE_UN, "ArgumentOutOfRangeException");

// Fixup the index to be in range so codegen is still valid
index %= elems;
}

return emit_vector_insert_element (cfg, klass, args [0], arg0_type, args [2], index, FALSE);
Expand Down Expand Up @@ -3766,6 +3772,9 @@ emit_vector_2_3_4 (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *f
if (index < 0 || index >= len) {
MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, args [1]->dreg, len);
MONO_EMIT_NEW_COND_EXC (cfg, GE_UN, "ArgumentOutOfRangeException");

// Fixup the index to be in range so codegen is still valid
index %= len;
}

int opcode = type_to_extract_op (ty);
Expand Down Expand Up @@ -3852,6 +3861,9 @@ emit_vector_2_3_4 (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *f
if (index < 0 || index >= len) {
MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, args [1]->dreg, len);
MONO_EMIT_NEW_COND_EXC (cfg, GE_UN, "ArgumentOutOfRangeException");

// Fixup the index to be in range so codegen is still valid
index %= len;
}

if (args [0]->dreg == dreg) {
Expand Down
57 changes: 57 additions & 0 deletions src/tests/JIT/Regression/JitBlue/Runtime_115487/Runtime_115487.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// Found by Antigen
// Reduced from 313.92 KB to 1.05 KB.

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.Arm;
using System.Runtime.Intrinsics.X86;
using System.Numerics;
using Xunit;

public class Runtime_115487
{
static int s_int_13 = 0;
static Vector128<ushort> s_v128_ushort_25 = Vector128.Create((ushort)5, 1, 1, 1, 5, 4, 53, 2);
ushort ushort_81 = 1;
Vector128<ushort> v128_ushort_88 = Vector128.Create((ushort)2, 53, 5, 5, 0, 1, 1, 4);
private static List<string> toPrint = new List<string>();

private void Method0()
{
unchecked
{
s_v128_ushort_25 = Vector128.WithElement(s_v128_ushort_25 * v128_ushort_88 ^ s_v128_ushort_25 & Vector128.MaxNative(s_v128_ushort_25, s_v128_ushort_25), s_int_13 >> s_int_13 & 3, ushort_81);
return;
}
}

[Fact]
public static void Problem()
{
Assert.Equal(Vector128.Create((ushort)5, 1, 1, 1, 5, 4, 53, 2), s_v128_ushort_25);
_ = Antigen();
Assert.Equal(Vector128.Create((ushort)1, 52, 4, 4, 5, 0, 0, 10), s_v128_ushort_25);
}

private static int Antigen()
{
new Runtime_115487().Method0();
return string.Join(Environment.NewLine, toPrint).GetHashCode();
}
}
/*
Environment:

set DOTNET_TieredCompilation=0

JIT assert failed:
Assertion failed '(insCodesMR[ins] != BAD_CODE)' in 'TestClass:Method0():this' during 'Generate code' (IL size 83; hash 0x46e9aa75; MinOpts)

File: D:\a\_work\1\s\src\coreclr\jit\emitxarch.cpp Line: 4203

*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
</Project>
Loading
Loading
0