Skip to content

Vector128.WithElement codegen regression in .NET 9.0 #115348

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 10 commits into from
May 8, 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 @@ -27886,14 +27886,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 @@ -27959,6 +27953,20 @@ GenTree* Compiler::gtNewSimdWithElementNode(
#error Unsupported platform
#endif // !TARGET_XARCH && !TARGET_ARM64

int immUpperBound = getSIMDVectorLength(simdSize, simdBaseType) - 1;
Copy link
Preview

Copilot AI May 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an explanatory comment regarding the rationale for using 'getSIMDVectorLength(simdSize, simdBaseType) - 1' as the upper bound to aid future maintainability.

Copilot uses AI. Check for mistakes.

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
52 changes: 52 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,57 @@ 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(!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_Move_Extend(op3->TypeGet(), false), // 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 @@ -3806,34 +3806,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
9 changes: 8 additions & 1 deletion src/coreclr/jit/lowerxarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5653,7 +5653,14 @@ GenTree* Lowering::LowerHWIntrinsicWithElement(GenTreeHWIntrinsic* node)
GenTree* op2 = node->Op(2);
GenTree* op3 = node->Op(3);

assert(op2->OperIsConst());
if (!op2->OperIsConst())
Copy link
Preview

Copilot AI May 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding an inline comment explaining why a SIMD initialization temp variable is obtained when op2 is not a constant for improved clarity.

Copilot uses AI. Check for mistakes.

{
comp->getSIMDInitTempVarNum(simdType);

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

ssize_t count = simdSize / genTypeSize(simdBaseType);
ssize_t imm8 = op2->AsIntCon()->IconValue();
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
Loading