-
Notifications
You must be signed in to change notification settings - Fork 5.2k
JIT: revise may/must point to stack analysis #115080
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
2 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
|
|
@@ -133,7 +133,7 @@ unsigned ObjectAllocator::LocalToIndex(unsigned lclNum) | |
| { | ||
| unsigned result = BAD_VAR_NUM; | ||
|
|
||
| if (lclNum < comp->lvaCount) | ||
| if (lclNum < m_firstPseudoLocalNum) | ||
| { | ||
| assert(IsTrackedLocal(lclNum)); | ||
| LclVarDsc* const varDsc = comp->lvaGetDesc(lclNum); | ||
|
|
@@ -297,6 +297,7 @@ void ObjectAllocator::MarkLclVarAsEscaping(unsigned int lclNum) | |
| void ObjectAllocator::MarkLclVarAsPossiblyStackPointing(unsigned int lclNum) | ||
| { | ||
| const unsigned bvIndex = LocalToIndex(lclNum); | ||
| JITDUMP("Marking V%02u (0x%02x) as possibly stack-pointing\n", lclNum, bvIndex); | ||
| BitVecOps::AddElemD(&m_bitVecTraits, m_PossiblyStackPointingPointers, bvIndex); | ||
| } | ||
|
|
||
|
|
@@ -839,6 +840,11 @@ void ObjectAllocator::ComputeEscapingNodes(BitVecTraits* bitVecTraits, BitVec& e | |
|
|
||
| void ObjectAllocator::ComputeStackObjectPointers(BitVecTraits* bitVecTraits) | ||
| { | ||
| // Keep track of locals that we know may point at the heap | ||
| // | ||
| BitVec possiblyHeapPointingPointers = BitVecOps::MakeEmpty(&m_bitVecTraits); | ||
| BitVecOps::AddElemD(bitVecTraits, possiblyHeapPointingPointers, m_unknownSourceIndex); | ||
|
|
||
| bool changed = true; | ||
| unsigned pass = 0; | ||
| while (changed) | ||
|
|
@@ -859,23 +865,36 @@ void ObjectAllocator::ComputeStackObjectPointers(BitVecTraits* bitVecTraits) | |
| m_ConnGraphAdjacencyMatrix[lclIndex])) | ||
| { | ||
| // We discovered a new pointer that may point to the stack. | ||
| JITDUMPEXEC(DumpIndex(lclIndex)); | ||
| JITDUMP(" may point to the stack\n"); | ||
| MarkLclVarAsPossiblyStackPointing(lclNum); | ||
| changed = true; | ||
| } | ||
|
|
||
| // If all locals assignable to this local are stack pointing, so is this local. | ||
| // | ||
| const bool isStackPointing = BitVecOps::IsSubset(bitVecTraits, m_ConnGraphAdjacencyMatrix[lclIndex], | ||
| m_DefinitelyStackPointingPointers); | ||
|
|
||
| if (isStackPointing) | ||
| { | ||
| MarkLclVarAsDefinitelyStackPointing(lclNum); | ||
| JITDUMP("V%02u is stack pointing\n", lclNum); | ||
| } | ||
|
|
||
| if (!BitVecOps::IsMember(bitVecTraits, possiblyHeapPointingPointers, lclIndex) && | ||
| !BitVecOps::IsEmptyIntersection(bitVecTraits, possiblyHeapPointingPointers, | ||
| m_ConnGraphAdjacencyMatrix[lclIndex])) | ||
| { | ||
| // We discovered a new pointer that may point to the heap. | ||
| JITDUMPEXEC(DumpIndex(lclIndex)); | ||
| JITDUMP(" may point to the heap\n"); | ||
| BitVecOps::AddElemD(bitVecTraits, possiblyHeapPointingPointers, lclIndex); | ||
| changed = true; | ||
| } | ||
| } | ||
| } | ||
| JITDUMP("\n---- done computing stack pointing locals\n"); | ||
|
|
||
| // If a local is possibly stack pointing and not possibly heap pointing, then it is definitely stack pointing. | ||
| // | ||
| BitVec newDefinitelyStackPointingPointers = BitVecOps::UninitVal(); | ||
| BitVecOps::Assign(bitVecTraits, newDefinitelyStackPointingPointers, m_PossiblyStackPointingPointers); | ||
| BitVecOps::DiffD(bitVecTraits, newDefinitelyStackPointingPointers, possiblyHeapPointingPointers); | ||
|
Comment on lines
+890
to
+892
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. I think you can use 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. Good point. Will clean this up in a future escape PR. |
||
|
|
||
| // We should have only added to the set of things that are definitely stack pointing. | ||
| // | ||
| assert(BitVecOps::IsSubset(bitVecTraits, m_DefinitelyStackPointingPointers, newDefinitelyStackPointingPointers)); | ||
| BitVecOps::AssignNoCopy(bitVecTraits, m_DefinitelyStackPointingPointers, newDefinitelyStackPointingPointers); | ||
|
|
||
| #ifdef DEBUG | ||
| if (comp->verbose) | ||
|
|
@@ -1117,10 +1136,10 @@ bool ObjectAllocator::MorphAllocObjNodes() | |
| continue; | ||
| } | ||
|
|
||
| bool canStack = false; | ||
| bool bashCall = false; | ||
| const char* onHeapReason = nullptr; | ||
| unsigned int lclNum = stmtExpr->AsLclVar()->GetLclNum(); | ||
| bool canStack = false; | ||
| bool bashCall = false; | ||
| const char* onHeapReason = nullptr; | ||
| const unsigned int lclNum = stmtExpr->AsLclVar()->GetLclNum(); | ||
|
|
||
| // Don't attempt to do stack allocations inside basic blocks that may be in a loop. | ||
| // | ||
|
|
@@ -1306,6 +1325,11 @@ bool ObjectAllocator::MorphAllocObjNodes() | |
| stmtExpr->AsLclVar()->Data() = newData; | ||
| stmtExpr->AddAllEffectsFlags(newData); | ||
| } | ||
|
|
||
| if (IsTrackedLocal(lclNum)) | ||
| { | ||
| AddConnGraphEdge(lclNum, m_unknownSourceLocalNum); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
48 changes: 48 additions & 0 deletions
48
src/tests/JIT/opt/ObjectStackAllocation/ConnectionCycles.cs
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 |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Runtime.CompilerServices; | ||
| using Xunit; | ||
|
|
||
| public class X | ||
| { | ||
| public X() { a = 15; b = 35; } | ||
| public int a; | ||
| public int b; | ||
| } | ||
|
|
||
| public class ConnectionCycles | ||
| { | ||
| static bool b; | ||
|
|
||
| [Fact] | ||
| public static int Problem() | ||
| { | ||
| return SubProblem(false) + SubProblem(true); | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| static int SubProblem(bool b) | ||
| { | ||
| X x1 = new X(); | ||
| X x2 = new X(); | ||
|
|
||
| if (b) | ||
| { | ||
| x1 = x2; | ||
| } | ||
| else | ||
| { | ||
| x2 = x1; | ||
| } | ||
|
|
||
| SideEffect(); | ||
|
|
||
| return x1.a + x2.b; | ||
| } | ||
|
|
||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| static void SideEffect() { } | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/tests/JIT/opt/ObjectStackAllocation/ConnectionCycles.csproj
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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <DebugType>None</DebugType> | ||
| <Optimize>True</Optimize> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <Compile Include="$(MSBuildProjectName).cs" /> | ||
| </ItemGroup> | ||
| </Project> |
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.
Consider adding an inline comment to clarify that subtracting the possibly heap-pointing pointers from the possibly stack-pointing pointers isolates the definitely stack-pointing locals, especially to support future maintenance.