Skip to content

Commit

Permalink
Cleaning up ToScalar implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
DeepakRajendrakumaran committed Jan 9, 2024
1 parent 9e3657f commit 76e307e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 22 deletions.
4 changes: 4 additions & 0 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -3257,6 +3257,10 @@ class Compiler
GenTree* op4,
CorInfoType simdBaseJitType,
unsigned simdSize);
GenTree* gtNewSimdToScalarNode(var_types type,
GenTree* op1,
CorInfoType simdBaseJitType,
unsigned simdSize);
#endif // TARGET_XARCH

GenTree* gtNewSimdUnOpNode(genTreeOps op,
Expand Down
60 changes: 49 additions & 11 deletions src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24525,7 +24525,7 @@ GenTree* Compiler::gtNewSimdSumNode(var_types type, GenTree* op1, CorInfoType si
// Finally adding the results gets us [(0 + 1) + (2 + 3), (1 + 0) + (3 + 2), (2 + 3) + (0 + 1), (3 + 2) + (1
// + 0)]
op1 = gtNewSimdBinOpNode(GT_ADD, TYP_SIMD16, op1, op1Shuffled, simdBaseJitType, simdSize);
return gtNewSimdHWIntrinsicNode(type, op1, NI_Vector128_ToScalar, simdBaseJitType, simdSize);
return gtNewSimdToScalarNode(type, op1, simdBaseJitType, simdSize);
}
else
{
Expand All @@ -24547,7 +24547,7 @@ GenTree* Compiler::gtNewSimdSumNode(var_types type, GenTree* op1, CorInfoType si
}
// Finally adding the results gets us [0 + 1, 1 + 0]
op1 = gtNewSimdBinOpNode(GT_ADD, TYP_SIMD16, op1, op1Shuffled, simdBaseJitType, simdSize);
return gtNewSimdHWIntrinsicNode(type, op1, NI_Vector128_ToScalar, simdBaseJitType, simdSize);
return gtNewSimdToScalarNode(type, op1, simdBaseJitType, simdSize);
}
}

Expand All @@ -24570,15 +24570,7 @@ GenTree* Compiler::gtNewSimdSumNode(var_types type, GenTree* op1, CorInfoType si
shiftVal = shiftVal / 2;
}

#if defined(TARGET_X86)
if (varTypeIsLong(simdBaseType))
{
GenTree* op2 = gtNewIconNode(0);
return gtNewSimdGetElementNode(type, op1, op2, simdBaseJitType, simdSize);
}
#endif // TARGET_X86

return gtNewSimdHWIntrinsicNode(type, op1, NI_Vector128_ToScalar, simdBaseJitType, simdSize);
return gtNewSimdToScalarNode(type, op1, simdBaseJitType, simdSize);

#elif defined(TARGET_ARM64)
switch (simdBaseType)
Expand Down Expand Up @@ -24710,6 +24702,52 @@ GenTree* Compiler::gtNewSimdTernaryLogicNode(var_types type,
}
#endif // TARGET_XARCH

#if defined(TARGET_XARCH)
//----------------------------------------------------------------------------------------------
// Compiler::gtNewSimdToScalarNode: Creates a new simd ToScalar node.
//
// Arguments:
// type - The return type of SIMD node being created.
// op1 - The SIMD operand.
// simdBaseJitType - The base JIT type of SIMD type of the intrinsic.
// simdSize - The size of the SIMD type of the intrinsic.
//
// Returns:
// The created node that has the ToScalar implementation.
//
GenTree* Compiler::gtNewSimdToScalarNode(var_types type, GenTree* op1, CorInfoType simdBaseJitType, unsigned simdSize)
{

#if defined(TARGET_X86)
var_types simdBaseType = JitType2PreciseVarType(simdBaseJitType);
if (varTypeIsLong(simdBaseType))
{
// We need SSE41 to handle long, use software fallback
assert(compIsaSupportedDebugOnly(InstructionSet_SSE41));

// Create a GetElement node which handles decomposition
GenTree* op2 = gtNewIconNode(0);
return gtNewSimdGetElementNode(type, op1, op2, simdBaseJitType, simdSize);
}
#endif // TARGET_X86
// Ensure MOVD/MOVQ support exists
assert(compIsaSupportedDebugOnly(InstructionSet_SSE2));
NamedIntrinsic intrinsic = NI_Vector128_ToScalar;

if (simdSize == 32)
{
assert(compIsaSupportedDebugOnly(InstructionSet_AVX));
intrinsic = NI_Vector256_ToScalar;
}
else if (simdSize == 64)
{
assert(IsBaselineVector512IsaSupportedDebugOnly());
intrinsic = NI_Vector512_ToScalar;
}
return gtNewSimdHWIntrinsicNode(type, op1, intrinsic, simdBaseJitType, simdSize);
}
#endif // TARGET_XARCH

GenTree* Compiler::gtNewSimdUnOpNode(
genTreeOps op, var_types type, GenTree* op1, CorInfoType simdBaseJitType, unsigned simdSize)
{
Expand Down
21 changes: 10 additions & 11 deletions src/coreclr/jit/hwintrinsicxarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2891,6 +2891,13 @@ GenTree* Compiler::impSpecialIntrinsic(NamedIntrinsic intrinsic,
{
break;
}
#if defined(TARGET_X86)
else if (varTypeIsLong(simdBaseType) && !compOpportunisticallyDependsOn(InstructionSet_SSE41))
{
// We need SSE41 to handle long, use software fallback
break;
}
#endif // TARGET_X86

op1 = impSIMDPopStack();
retNode = gtNewSimdSumNode(retType, op1, simdBaseJitType, simdSize);
Expand All @@ -2904,23 +2911,15 @@ GenTree* Compiler::impSpecialIntrinsic(NamedIntrinsic intrinsic,
assert(sig->numArgs == 1);

#if defined(TARGET_X86)
if (varTypeIsLong(simdBaseType))
if (varTypeIsLong(simdBaseType) && !compOpportunisticallyDependsOn(InstructionSet_SSE41))
{
if (!compOpportunisticallyDependsOn(InstructionSet_SSE41))
{
// We need SSE41 to handle long, use software fallback
break;
}
// Create a GetElement node which handles decomposition
op1 = impSIMDPopStack();
op2 = gtNewIconNode(0);
retNode = gtNewSimdGetElementNode(retType, op1, op2, simdBaseJitType, simdSize);
// We need SSE41 to handle long, use software fallback
break;
}
#endif // TARGET_X86

op1 = impSIMDPopStack();
retNode = gtNewSimdHWIntrinsicNode(retType, op1, intrinsic, simdBaseJitType, simdSize);
retNode = gtNewSimdToScalarNode(retType, op1, simdBaseJitType, simdSize);
break;
}

Expand Down

0 comments on commit 76e307e

Please sign in to comment.