-
Notifications
You must be signed in to change notification settings - Fork 5.4k
JIT: Accelerate long -> floating casts on x86 #113930
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
04b1bbe
use SIMD conversion instructions for long -> floating casts
saucecontrol 2214900
Merge remote-tracking branch 'upstream/main' into x86convert
saucecontrol ab5d919
move transform to DecomposeLongs, restore double intermediate
saucecontrol 1bd047a
formatting
saucecontrol c73acd5
handle constants
saucecontrol 71d055a
Merge remote-tracking branch 'upstream/main' into x86convert
saucecontrol da0791c
Merge branch 'main' into x86convert
BruceForstall File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -137,7 +137,12 @@ GenTree* DecomposeLongs::DecomposeNode(GenTree* tree) | |
| } | ||
| } | ||
|
|
||
| #if defined(FEATURE_HW_INTRINSICS) && defined(TARGET_X86) | ||
| if (!tree->TypeIs(TYP_LONG) && | ||
| !(tree->OperIs(GT_CAST) && varTypeIsLong(tree->AsCast()->CastOp()) && varTypeIsFloating(tree))) | ||
| #else | ||
| if (!tree->TypeIs(TYP_LONG)) | ||
| #endif // FEATURE_HW_INTRINSICS && TARGET_X86 | ||
| { | ||
| return tree->gtNext; | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The fact that from this point onwards it can now also be |
||
|
|
@@ -157,15 +162,18 @@ GenTree* DecomposeLongs::DecomposeNode(GenTree* tree) | |
|
|
||
| GenTree* user = use.User(); | ||
|
|
||
| if (user->OperIsHWIntrinsic()) | ||
| if (tree->TypeIs(TYP_LONG) && (user->OperIsHWIntrinsic() || (user->OperIs(GT_CAST) && varTypeIsFloating(user)))) | ||
| { | ||
| if (tree->OperIs(GT_CNS_LNG) || | ||
| (tree->OperIs(GT_IND, GT_LCL_FLD) && m_lowering->IsSafeToContainMem(user, tree))) | ||
| { | ||
| NamedIntrinsic intrinsicId = user->AsHWIntrinsic()->GetHWIntrinsicId(); | ||
| assert(HWIntrinsicInfo::IsVectorCreate(intrinsicId) || | ||
| HWIntrinsicInfo::IsVectorCreateScalar(intrinsicId) || | ||
| HWIntrinsicInfo::IsVectorCreateScalarUnsafe(intrinsicId)); | ||
| if (user->OperIsHWIntrinsic()) | ||
| { | ||
| NamedIntrinsic intrinsicId = user->AsHWIntrinsic()->GetHWIntrinsicId(); | ||
| assert(HWIntrinsicInfo::IsVectorCreate(intrinsicId) || | ||
| HWIntrinsicInfo::IsVectorCreateScalar(intrinsicId) || | ||
| HWIntrinsicInfo::IsVectorCreateScalarUnsafe(intrinsicId)); | ||
| } | ||
|
|
||
| return tree->gtNext; | ||
| } | ||
|
|
@@ -562,28 +570,78 @@ GenTree* DecomposeLongs::DecomposeStoreLclFld(LIR::Use& use) | |
| GenTree* DecomposeLongs::DecomposeCast(LIR::Use& use) | ||
| { | ||
| assert(use.IsInitialized()); | ||
| assert(use.Def()->OperGet() == GT_CAST); | ||
| assert(use.Def()->OperIs(GT_CAST)); | ||
|
|
||
| GenTree* cast = use.Def()->AsCast(); | ||
| GenTree* loResult = nullptr; | ||
| GenTree* hiResult = nullptr; | ||
| GenTreeCast* cast = use.Def()->AsCast(); | ||
| var_types srcType = cast->CastFromType(); | ||
| var_types dstType = cast->CastToType(); | ||
|
|
||
| var_types srcType = cast->CastFromType(); | ||
| var_types dstType = cast->CastToType(); | ||
|
|
||
| if ((cast->gtFlags & GTF_UNSIGNED) != 0) | ||
| if (cast->IsUnsigned()) | ||
| { | ||
| srcType = varTypeToUnsigned(srcType); | ||
| } | ||
|
|
||
| bool skipDecomposition = false; | ||
| #if defined(FEATURE_HW_INTRINSICS) && defined(TARGET_X86) | ||
| if (varTypeIsFloating(dstType)) | ||
| { | ||
| // We will reach this path only if morph did not convert the cast to a helper call, | ||
| // meaning we can perform the cast using SIMD instructions. | ||
| // The sequence this creates is simply: | ||
| // AVX512DQ.VL.ConvertToVector128Single(Vector128.CreateScalarUnsafe(LONG)).ToScalar() | ||
|
|
||
| NamedIntrinsic intrinsicId = NI_Illegal; | ||
| GenTree* srcOp = cast->CastOp(); | ||
| var_types dstType = cast->CastToType(); | ||
| CorInfoType baseFloatingType = (dstType == TYP_FLOAT) ? CORINFO_TYPE_FLOAT : CORINFO_TYPE_DOUBLE; | ||
| CorInfoType baseIntegralType = cast->IsUnsigned() ? CORINFO_TYPE_ULONG : CORINFO_TYPE_LONG; | ||
|
|
||
| assert(!cast->gtOverflow()); | ||
|
|
||
| if (m_compiler->compOpportunisticallyDependsOn(InstructionSet_AVX512DQ_VL)) | ||
| { | ||
| intrinsicId = (dstType == TYP_FLOAT) ? NI_AVX512DQ_VL_ConvertToVector128Single | ||
| : NI_AVX512DQ_VL_ConvertToVector128Double; | ||
| } | ||
| else | ||
| { | ||
| assert(m_compiler->compIsaSupportedDebugOnly(InstructionSet_AVX10v1)); | ||
| intrinsicId = | ||
| (dstType == TYP_FLOAT) ? NI_AVX10v1_ConvertToVector128Single : NI_AVX10v1_ConvertToVector128Double; | ||
| } | ||
|
saucecontrol marked this conversation as resolved.
|
||
|
|
||
| GenTree* createScalar = m_compiler->gtNewSimdCreateScalarUnsafeNode(TYP_SIMD16, srcOp, baseIntegralType, 16); | ||
| GenTree* convert = | ||
| m_compiler->gtNewSimdHWIntrinsicNode(TYP_SIMD16, createScalar, intrinsicId, baseIntegralType, 16); | ||
| GenTree* toScalar = m_compiler->gtNewSimdToScalarNode(dstType, convert, baseFloatingType, 16); | ||
|
|
||
| Range().InsertAfter(cast, createScalar, convert, toScalar); | ||
| Range().Remove(cast); | ||
|
|
||
| if (createScalar->IsCnsVec()) | ||
| { | ||
| Range().Remove(srcOp); | ||
| } | ||
|
|
||
| if (use.IsDummyUse()) | ||
| { | ||
| toScalar->SetUnusedValue(); | ||
| } | ||
| use.ReplaceWith(toScalar); | ||
|
|
||
| return toScalar->gtNext; | ||
| } | ||
| #endif // FEATURE_HW_INTRINSICS && TARGET_X86 | ||
|
|
||
| bool skipDecomposition = false; | ||
| GenTree* loResult = nullptr; | ||
| GenTree* hiResult = nullptr; | ||
|
|
||
| if (varTypeIsLong(srcType)) | ||
| { | ||
| if (cast->gtOverflow() && (varTypeIsUnsigned(srcType) != varTypeIsUnsigned(dstType))) | ||
| { | ||
| GenTree* srcOp = cast->gtGetOp1(); | ||
| noway_assert(srcOp->OperGet() == GT_LONG); | ||
| GenTree* srcOp = cast->CastOp(); | ||
| noway_assert(srcOp->OperIs(GT_LONG)); | ||
| GenTree* loSrcOp = srcOp->gtGetOp1(); | ||
| GenTree* hiSrcOp = srcOp->gtGetOp2(); | ||
|
|
||
|
|
@@ -595,13 +653,13 @@ GenTree* DecomposeLongs::DecomposeCast(LIR::Use& use) | |
| // check provided by codegen. | ||
| // | ||
|
|
||
| const bool signExtend = (cast->gtFlags & GTF_UNSIGNED) == 0; | ||
| const bool signExtend = !cast->IsUnsigned(); | ||
| loResult = EnsureIntSized(loSrcOp, signExtend); | ||
|
|
||
| hiResult = cast; | ||
| hiResult->gtType = TYP_INT; | ||
| hiResult->AsCast()->gtCastType = TYP_UINT; | ||
| hiResult->gtFlags &= ~GTF_UNSIGNED; | ||
| hiResult->ClearUnsigned(); | ||
| hiResult->AsOp()->gtOp1 = hiSrcOp; | ||
|
|
||
| Range().Remove(srcOp); | ||
|
|
@@ -631,7 +689,7 @@ GenTree* DecomposeLongs::DecomposeCast(LIR::Use& use) | |
| } | ||
| else | ||
| { | ||
| if (!use.IsDummyUse() && (use.User()->OperGet() == GT_MUL)) | ||
| if (!use.IsDummyUse() && use.User()->OperIs(GT_MUL)) | ||
| { | ||
| // | ||
| // This int->long cast is used by a GT_MUL that will be transformed by DecomposeMul into a | ||
|
|
@@ -646,7 +704,7 @@ GenTree* DecomposeLongs::DecomposeCast(LIR::Use& use) | |
| } | ||
| else if (varTypeIsUnsigned(srcType)) | ||
| { | ||
| const bool signExtend = (cast->gtFlags & GTF_UNSIGNED) == 0; | ||
| const bool signExtend = !cast->IsUnsigned(); | ||
| loResult = EnsureIntSized(cast->gtGetOp1(), signExtend); | ||
|
|
||
| hiResult = m_compiler->gtNewZeroConNode(TYP_INT); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: negated conditions like this can be hard to read. A small comment covering that we want to handle nodes that produce
longorGT_CAST float->longwould be beneficial IMO.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. I actually plan on extending this to handle casts in the opposite direction as well, and that will make this check even more hairy. I'll do something to simplify it then.