Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 5.6k
Fix SIMD MinMax constant special cases#133173
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
tannergooding
merged 3 commits into
dotnet:main
from
tannergooding:tannergooding-fix-jit-constant-special-casesSep 4, 2026
+432
−69
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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 |
|---|---|---|
| @@ -20943,23 +20943,32 @@ bool GenTreeVecCon::IsBroadcast(var_types simdBaseType) const | ||
| bool GenTreeVecCon::IsNaN(var_types simdBaseType) const | ||
| { | ||
| assert(varTypeIsFloating(simdBaseType)); | ||
| uint32_t elementCount = ElementCount(genTypeSize(gtType), simdBaseType); | ||
| for (uint32_t i = 0; i < elementCount; i++) | ||
| { | ||
| double element = GetElementFloating(simdBaseType, i); | ||
| unsigned simdSize = genTypeSize(gtType); | ||
| simd_t result = EvaluateSimdIsNaN(simdBaseType, gtSimdVal, simdSize); | ||
| return EvaluateSimdAllWhereAllBitsSet(simdBaseType, result, simdSize); | ||
| } | ||
| if (!FloatingPointUtils::isNaN(element)) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
| //------------------------------------------------------------------------ | ||
| // GenTreeVecCon::ContainsNaN: Determines if this vector constant contains a NaN | ||
| // | ||
| // Arguments: | ||
| // simdBaseType - the base type of the constant being checked | ||
| // | ||
| // Returns: | ||
| // true if any element is NaN; otherwise, false | ||
| // | ||
| bool GenTreeVecCon::ContainsNaN(var_types simdBaseType) const | ||
| { | ||
| assert(varTypeIsFloating(simdBaseType)); | ||
| return true; | ||
| unsigned simdSize = genTypeSize(gtType); | ||
| simd_t result = EvaluateSimdIsNaN(simdBaseType, gtSimdVal, simdSize); | ||
| return EvaluateSimdAnyWhereAllBitsSet(simdBaseType, result, simdSize); | ||
| } | ||
| //------------------------------------------------------------------------ | ||
| // GenTreeVecCon::IsNaN: Determines if this vector constant has all elements being -0 | ||
| // GenTreeVecCon::IsNegativeZero: Determines if this vector constant has all elements being -0 | ||
| // | ||
| // Arguments: | ||
| // simdBaseType - the base type of the constant being checked | ||
| @@ -20970,19 +20979,46 @@ bool GenTreeVecCon::IsNaN(var_types simdBaseType) const | ||
| bool GenTreeVecCon::IsNegativeZero(var_types simdBaseType) const | ||
| { | ||
| assert(varTypeIsFloating(simdBaseType)); | ||
| uint32_t elementCount = ElementCount(genTypeSize(gtType), simdBaseType); | ||
| for (uint32_t i = 0; i < elementCount; i++) | ||
| { | ||
| double element = GetElementFloating(simdBaseType, i); | ||
| unsigned simdSize = genTypeSize(gtType); | ||
| simd_t result = EvaluateSimdIsNegativeZero(simdBaseType, gtSimdVal, simdSize); | ||
| return EvaluateSimdAllWhereAllBitsSet(simdBaseType, result, simdSize); | ||
| } | ||
| if (!FloatingPointUtils::isNegativeZero(element)) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
| //------------------------------------------------------------------------ | ||
| // GenTreeVecCon::ContainsNegativeZero: Determines if this vector constant contains -0 | ||
| // | ||
| // Arguments: | ||
| // simdBaseType - the base type of the constant being checked | ||
| // | ||
| // Returns: | ||
| // true if any element is -0; otherwise, false | ||
| // | ||
| bool GenTreeVecCon::ContainsNegativeZero(var_types simdBaseType) const | ||
| { | ||
| assert(varTypeIsFloating(simdBaseType)); | ||
| return true; | ||
| unsigned simdSize = genTypeSize(gtType); | ||
| simd_t result = EvaluateSimdIsNegativeZero(simdBaseType, gtSimdVal, simdSize); | ||
| return EvaluateSimdAnyWhereAllBitsSet(simdBaseType, result, simdSize); | ||
| } | ||
| //------------------------------------------------------------------------ | ||
| // GenTreeVecCon::ContainsPositiveZero: Determines if this vector constant contains +0 | ||
| // | ||
| // Arguments: | ||
| // simdBaseType - the base type of the constant being checked | ||
| // | ||
| // Returns: | ||
| // true if any element is +0; otherwise, false | ||
| // | ||
| bool GenTreeVecCon::ContainsPositiveZero(var_types simdBaseType) const | ||
| { | ||
| assert(varTypeIsFloating(simdBaseType)); | ||
| unsigned simdSize = genTypeSize(gtType); | ||
| simd_t result = EvaluateSimdIsPositiveZero(simdBaseType, gtSimdVal, simdSize); | ||
| return EvaluateSimdAnyWhereAllBitsSet(simdBaseType, result, simdSize); | ||
| } | ||
| #if defined(FEATURE_MASKED_HW_INTRINSICS) | ||
| @@ -26502,8 +26538,15 @@ GenTree* Compiler::gtNewSimdMinMaxNode(var_types type, | ||
| if (!isMagnitude) | ||
| { | ||
| bool needsFixup = false; | ||
| bool canHandle = false; | ||
| // xarch min/max return op2 if both inputs are 0 of either sign or if either input | ||
| // is NaN. We can exploit that to get the IEEE 754 behavior for free by ordering | ||
| // the operands such that the constant is the one that gets returned. | ||
| // Partially NaN constants cannot use operand ordering, while mixed zero constants | ||
| // require the per-element fixup below. | ||
| bool hasPartialNaN = !isScalar && cnsNode->AsVecCon()->ContainsNaN(simdBaseType); | ||
| bool needsFixup = false; | ||
| bool canHandle = false; | ||
tannergooding marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if (isMax) | ||
| { | ||
| @@ -26512,10 +26555,10 @@ GenTree* Compiler::gtNewSimdMinMaxNode(var_types type, | ||
| // not be propagated for isNumber and to be propagated otherwise. | ||
| // | ||
| // This means for isNumber we want to do `max other, cns` and | ||
| // can only handle cns being -0 if Avx512F is supported. This is | ||
| // because if other was NaN, we want to return the non-NaN cns. | ||
| // But if cns was -0 and other was +0 we'd want to return +0 and | ||
| // so need to be able to fixup the result. | ||
| // cannot handle cns being -0. If other was NaN, we want to return | ||
| // the non-NaN cns. But if cns was -0 and other was +0 we'd want | ||
| // to return +0, and the ZERO fixup token cannot distinguish the | ||
| // opaque operand's sign. | ||
| // | ||
| // For !isNumber we have the inverse and want `max cns, other` and | ||
| // can only handle cns being +0 if Avx512F is supported. This is | ||
| @@ -26531,7 +26574,7 @@ GenTree* Compiler::gtNewSimdMinMaxNode(var_types type, | ||
| } | ||
| else | ||
| { | ||
| needsFixup = cnsNode->IsVectorNegativeZero(simdBaseType); | ||
| needsFixup |= cnsNode->AsVecCon()->ContainsNegativeZero(simdBaseType); | ||
| } | ||
| } | ||
| else if (isScalar) | ||
| @@ -26540,10 +26583,11 @@ GenTree* Compiler::gtNewSimdMinMaxNode(var_types type, | ||
| } | ||
| else | ||
| { | ||
| needsFixup = cnsNode->IsVectorZero(); | ||
| needsFixup |= cnsNode->AsVecCon()->ContainsPositiveZero(simdBaseType); | ||
| } | ||
| if (!needsFixup || compOpportunisticallyDependsOn(InstructionSet_AVX512)) | ||
| if (!hasPartialNaN && | ||
| (!needsFixup || (!isNumber && compOpportunisticallyDependsOn(InstructionSet_AVX512)))) | ||
| { | ||
| // Given the checks, op1 can safely be the cns and op2 the other node | ||
| @@ -26562,10 +26606,10 @@ GenTree* Compiler::gtNewSimdMinMaxNode(var_types type, | ||
| // not be propagated for isNumber and to be propagated otherwise. | ||
| // | ||
| // This means for isNumber we want to do `min other, cns` and | ||
| // can only handle cns being +0 if Avx512F is supported. This is | ||
| // because if other was NaN, we want to return the non-NaN cns. | ||
| // But if cns was +0 and other was -0 we'd want to return -0 and | ||
| // so need to be able to fixup the result. | ||
| // cannot handle cns being +0. If other was NaN, we want to return | ||
| // the non-NaN cns. But if cns was +0 and other was -0 we'd want | ||
| // to return -0, and the ZERO fixup token cannot distinguish the | ||
| // opaque operand's sign. | ||
| // | ||
| // For !isNumber we have the inverse and want `min cns, other` and | ||
| // can only handle cns being -0 if Avx512F is supported. This is | ||
| @@ -26581,7 +26625,7 @@ GenTree* Compiler::gtNewSimdMinMaxNode(var_types type, | ||
| } | ||
| else | ||
| { | ||
| needsFixup = cnsNode->IsVectorZero(); | ||
| needsFixup |= cnsNode->AsVecCon()->ContainsPositiveZero(simdBaseType); | ||
| } | ||
| } | ||
| else if (isScalar) | ||
| @@ -26590,10 +26634,11 @@ GenTree* Compiler::gtNewSimdMinMaxNode(var_types type, | ||
| } | ||
| else | ||
| { | ||
| needsFixup = cnsNode->IsVectorNegativeZero(simdBaseType); | ||
| needsFixup |= cnsNode->AsVecCon()->ContainsNegativeZero(simdBaseType); | ||
| } | ||
| if (!needsFixup || compOpportunisticallyDependsOn(InstructionSet_AVX512)) | ||
| if (!hasPartialNaN && | ||
| (!needsFixup || (!isNumber && compOpportunisticallyDependsOn(InstructionSet_AVX512)))) | ||
| { | ||
| // Given the checks, op1 can safely be the cns and op2 the other node | ||
| @@ -26627,18 +26672,16 @@ GenTree* Compiler::gtNewSimdMinMaxNode(var_types type, | ||
| retNode->AsHWIntrinsic()->Op(2) = op2; | ||
| gtUpdateNodeSideEffects(retNode); | ||
| GenTreeVecCon* tblVecCon = gtNewVconNode(type); | ||
| // FixupScalar(left, right, table, control) computes the input type of right | ||
| // Fixup(left, right, table, control) computes the input type of right | ||
| // adjusts it based on the table and then returns | ||
| // | ||
| // In our case, left is going to be the result of the RangeScalar operation | ||
| // and right is going to be op1 or op2. In the case op1/op2 is QNaN or SNaN | ||
| // we want to preserve it instead. Otherwise we want to preserve the original | ||
| // result computed by RangeScalar. | ||
| // | ||
| // If both inputs are NaN, then we'll end up taking op1 by virtue of it being | ||
| // the latter fixup. | ||
| // In our case, left is the result of the min/max operation and right is the | ||
| // opaque operand. The table preserves left except where the constant is the | ||
| // problematic zero. | ||
| GenTreeVecCon* tblVecCon = gtNewVconNode(type); | ||
| int64_t tblValue; | ||
| simd_t zeroMask = {}; | ||
| if (isMax) | ||
| { | ||
| @@ -26651,9 +26694,13 @@ GenTree* Compiler::gtNewSimdMinMaxNode(var_types type, | ||
| // -VAL: 0b0000 | ||
| // +VAL: 0b0000 | ||
| const int64_t tblValue = 0x00000800; | ||
| tblVecCon->EvaluateBroadcastInPlace((simdBaseType == TYP_FLOAT) ? TYP_INT : TYP_LONG, | ||
| tblValue); | ||
| tblValue = 0x00000800; | ||
| if (!isScalar) | ||
| { | ||
| zeroMask = | ||
| EvaluateSimdIsPositiveZero(simdBaseType, cnsNode->AsVecCon()->gtSimdVal, simdSize); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| @@ -26666,9 +26713,24 @@ GenTree* Compiler::gtNewSimdMinMaxNode(var_types type, | ||
| // -VAL: 0b0000 | ||
| // +VAL: 0b0000 | ||
| const int64_t tblValue = 0x00000700; | ||
| tblVecCon->EvaluateBroadcastInPlace((simdBaseType == TYP_FLOAT) ? TYP_INT : TYP_LONG, | ||
| tblValue); | ||
| tblValue = 0x00000700; | ||
| if (!isScalar) | ||
| { | ||
| zeroMask = | ||
| EvaluateSimdIsNegativeZero(simdBaseType, cnsNode->AsVecCon()->gtSimdVal, simdSize); | ||
| } | ||
| } | ||
| var_types tblType = (simdBaseType == TYP_FLOAT) ? TYP_INT : TYP_LONG; | ||
| tblVecCon->EvaluateBroadcastInPlace(tblType, tblValue); | ||
| if (!isScalar) | ||
| { | ||
| simd_t result = {}; | ||
| EvaluateBinarySimd<simd_t>(GT_AND, false, tblType, &result, tblVecCon->gtSimdVal, zeroMask, | ||
| simdSize); | ||
| tblVecCon->gtSimdVal = result; | ||
| } | ||
| intrinsic = isScalar ? NI_AVX512_FixupScalar : NI_AVX512_Fixup; | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Uh oh!
There was an error while loading. Please reload this page.