-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Use SSA-based TryGetRange to fold conditions #129354
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
Open
EgorBo
wants to merge
4
commits into
dotnet:main
Choose a base branch
from
EgorBo:ssa-based-relop
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+141
−41
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5144397
cleanup
EgorBo 9a1efbc
Merge branch 'main' into ssa-based-relop
EgorBo 8985263
JIT: fix unsound range for widening signed small type to small unsign…
EgorBo 422e472
JIT: fix unsound range for unsigned-widening cast of signed small type
EgorBo 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
Some comments aren't visible on the classic Files Changed page.
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
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
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 |
|---|---|---|
|
|
@@ -23,27 +23,28 @@ PhaseStatus Compiler::rangeCheckPhase() | |
| return madeChanges ? PhaseStatus::MODIFIED_EVERYTHING : PhaseStatus::MODIFIED_NOTHING; | ||
| } | ||
|
|
||
| // Max stack depth (path length) in walking the UD chain. | ||
| static const int MAX_SEARCH_DEPTH = 100; | ||
|
|
||
| // Max nodes to visit in the UD chain for the current method being compiled. | ||
| static const int MAX_VISIT_BUDGET = 8192; | ||
|
|
||
| //------------------------------------------------------------------------ | ||
| // GetRangeCheck: get the RangeCheck instance | ||
| // | ||
| // Returns: | ||
| // The range check object | ||
| // | ||
| RangeCheck* Compiler::GetRangeCheck() | ||
| RangeCheck* Compiler::GetRangeCheck(int customBudget) | ||
| { | ||
| if (optRangeCheck == nullptr) | ||
| { | ||
| optRangeCheck = new (this, CMK_Generic) RangeCheck(this); | ||
| } | ||
| optRangeCheck->SetBudget(customBudget > 0 ? customBudget : MAX_VISIT_BUDGET); | ||
| return optRangeCheck; | ||
| } | ||
|
|
||
| // Max stack depth (path length) in walking the UD chain. | ||
| static const int MAX_SEARCH_DEPTH = 100; | ||
|
|
||
| // Max nodes to visit in the UD chain for the current method being compiled. | ||
| static const int MAX_VISIT_BUDGET = 8192; | ||
|
|
||
| // RangeCheck constructor. | ||
| RangeCheck::RangeCheck(Compiler* pCompiler) | ||
| : m_preferredBound(ValueNumStore::NoVN) | ||
|
|
@@ -259,11 +260,9 @@ void RangeCheck::OptimizeRangeCheck(BasicBlock* block, Statement* stmt, GenTree* | |
|
|
||
| ValueNum arrLenVn = m_compiler->optConservativeNormalVN(bndsChk->GetArrayLength()); | ||
|
|
||
| m_preferredBound = arrLenVn; | ||
|
|
||
| // Get the range for this index. | ||
| Range range = Range(Limit(Limit::keUndef)); | ||
| if (!TryGetRange(block, treeIndex, &range)) | ||
| if (!TryGetRange(block, treeIndex, &range, arrLenVn)) | ||
| { | ||
| JITDUMP("Failed to get range\n"); | ||
| return; | ||
|
|
@@ -437,6 +436,14 @@ bool RangeCheck::IsMonotonicallyIncreasing(GenTree* expr, bool rejectNegativeCon | |
| // Given a lclvar use, try to find the lclvar's defining store and its containing block. | ||
| LclSsaVarDsc* RangeCheck::GetSsaDefStore(GenTreeLclVarCommon* lclUse) | ||
| { | ||
| // RangeCheck does not understand reads through LCL_FLD nodes: a LCL_FLD use reads a | ||
| // sub-range of the local (a different offset and/or a narrower type), so the value | ||
| // produced by the (full-width) definition does not describe the value being read. | ||
| if (lclUse->OperIs(GT_LCL_FLD)) | ||
|
Member
Author
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. a correctness issue I found as part of this PR. |
||
| { | ||
| return nullptr; | ||
| } | ||
|
|
||
| unsigned ssaNum = lclUse->GetSsaNum(); | ||
|
|
||
| if (ssaNum == SsaConfig::RESERVED_SSA_NUM) | ||
|
|
@@ -626,7 +633,25 @@ Range RangeCheck::GetRangeFromAssertionsWorker( | |
|
|
||
| var_types castFromType = srcIsUnsigned ? varTypeToUnsigned(arg0Typ) : arg0Typ; | ||
|
|
||
| if (genTypeSize(castFromType) < genTypeSize(castToType)) | ||
| // A zero-extending cast (srcIsUnsigned) of a signed sub-int source is unsound to bound by the | ||
| // small unsigned type: small signed types are held sign-extended in their int-width stack slot, | ||
| // so the zero-extension applies to that wider value. E.g. (uint)(sbyte)(-1) == 0xFFFFFFFF, which | ||
| // is far outside the [0..255] range of the unsigned small type. Use the unsigned form of the | ||
| // actual (int) source width so we fall back to an unknown range instead of an unsound one. | ||
| if (srcIsUnsigned && varTypeIsSigned(arg0Typ) && (genTypeSize(arg0Typ) < genTypeSize(TYP_INT))) | ||
| { | ||
| castFromType = varTypeToUnsigned(genActualType(arg0Typ)); | ||
| } | ||
|
|
||
| // Widening preserves the source value (so we can reuse the source range) UNLESS we widen a | ||
| // signed source into a smaller-than-int unsigned type (e.g. (ushort)(sbyte)). There, negative | ||
| // source values are zero-extended into large positive values (e.g. (ushort)(-1) == 65535), so | ||
| // the source range no longer bounds the result. | ||
| bool widensToSmallUnsigned = varTypeIsUnsigned(castToType) && | ||
|
Member
Author
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. another pre-existing correctness issue we had |
||
| (genTypeSize(castToType) < genTypeSize(TYP_INT)) && | ||
| varTypeIsSigned(castFromType); | ||
|
|
||
| if ((genTypeSize(castFromType) < genTypeSize(castToType)) && !widensToSmallUnsigned) | ||
| { | ||
| // We're going from a small type to a large type | ||
| // and so regardless of whether we zero or sign-extend | ||
|
|
@@ -2333,15 +2358,25 @@ void Indent(int indent) | |
| // TryGetRange: Try to obtain the range of an expression. | ||
| // | ||
| // Arguments: | ||
| // block - the block that contains `expr`; | ||
| // expr - expression to compute the range for; | ||
| // pRange - [Out] range of the expression; | ||
| // block - the block that contains `expr`; | ||
| // expr - expression to compute the range for; | ||
| // pRange - [Out] range of the expression; | ||
| // preferredBoundVN - a value number of the preferred bound. | ||
| // | ||
| // Return Value: | ||
| // false if the range is unknown or determined to overflow. | ||
| // | ||
| bool RangeCheck::TryGetRange(BasicBlock* block, GenTree* expr, Range* pRange) | ||
| bool RangeCheck::TryGetRange(BasicBlock* block, GenTree* expr, Range* pRange, ValueNum preferredBoundVN) | ||
| { | ||
| // Fast path for constants | ||
| if (expr->IsIntCnsFitsInI32()) | ||
| { | ||
| *pRange = Limit(Limit::keConstant, (int)expr->AsIntCon()->IconValue()); | ||
| return true; | ||
| } | ||
|
|
||
| m_preferredBound = preferredBoundVN; | ||
|
|
||
| // Reset the maps. | ||
| ClearRangeMap(); | ||
| ClearSearchPath(); | ||
|
|
||
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.
How do we arrive at these budget values?