-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[MCA] Optimize memory consumption in resource pressure view (NFC) #124904
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 1 commit
Commits
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 |
|---|---|---|
|
|
@@ -37,8 +37,16 @@ ResourcePressureView::ResourcePressureView(const llvm::MCSubtargetInfo &sti, | |
| } | ||
|
|
||
| NumResourceUnits = R2VIndex; | ||
| ResourceUsage.resize(NumResourceUnits * (getSource().size() + 1)); | ||
| std::fill(ResourceUsage.begin(), ResourceUsage.end(), 0.0); | ||
| ResourceUsage.resize(getSource().size()); | ||
|
|
||
| ResourceReleaseAtCycles InitValue{0, 0}; | ||
| auto Generator = [&InitValue]() { | ||
| auto Old = InitValue; | ||
| ++InitValue.ResourceIdx; | ||
| return Old; | ||
| }; | ||
| std::generate_n(std::back_inserter(CommonResourceUsage), NumResourceUnits, | ||
| Generator); | ||
| } | ||
|
|
||
| void ResourcePressureView::onEvent(const HWInstructionEvent &Event) { | ||
|
|
@@ -60,8 +68,19 @@ void ResourcePressureView::onEvent(const HWInstructionEvent &Event) { | |
| assert(Resource2VecIndex.contains(RR.first)); | ||
| unsigned R2VIndex = Resource2VecIndex[RR.first]; | ||
| R2VIndex += llvm::countr_zero(RR.second); | ||
| ResourceUsage[R2VIndex + NumResourceUnits * SourceIdx] += Use.second; | ||
| ResourceUsage[R2VIndex + NumResourceUnits * Source.size()] += Use.second; | ||
|
|
||
| InstResourceUsage &RU = ResourceUsage[SourceIdx]; | ||
| ResourceReleaseAtCycles NewUsage{R2VIndex, Use.second}; | ||
| auto ResCyclesIt = | ||
| lower_bound(RU, NewUsage, [](const auto &L, const auto &R) { | ||
| return L.ResourceIdx < R.ResourceIdx; | ||
| }); | ||
| if (ResCyclesIt != RU.end() && ResCyclesIt->ResourceIdx == R2VIndex) | ||
| ResCyclesIt->Cycles += NewUsage.Cycles; | ||
| else | ||
| RU.insert(ResCyclesIt, std::move(NewUsage)); | ||
|
|
||
| CommonResourceUsage[R2VIndex].Cycles += NewUsage.Cycles; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -135,10 +154,17 @@ void ResourcePressureView::printResourcePressurePerIter(raw_ostream &OS) const { | |
|
|
||
| ArrayRef<llvm::MCInst> Source = getSource(); | ||
| const unsigned Executions = LastInstructionIdx / Source.size() + 1; | ||
| auto UsageEntryEnd = CommonResourceUsage.end(); | ||
| auto UsageEntryIt = CommonResourceUsage.begin(); | ||
| for (unsigned I = 0, E = NumResourceUnits; I < E; ++I) { | ||
| double Usage = ResourceUsage[I + Source.size() * E]; | ||
| printResourcePressure(FOS, Usage / Executions, (I + 1) * 7); | ||
| double Pressure = 0.0; | ||
| if (UsageEntryIt != UsageEntryEnd && UsageEntryIt->ResourceIdx == I) { | ||
| Pressure = UsageEntryIt->Cycles / Executions; | ||
| ++UsageEntryIt; | ||
| } | ||
| printResourcePressure(FOS, Pressure, (I + 1) * 7); | ||
| } | ||
| assert(UsageEntryIt == UsageEntryEnd); | ||
|
|
||
| FOS.flush(); | ||
| OS << Buffer; | ||
|
|
@@ -157,11 +183,17 @@ void ResourcePressureView::printResourcePressurePerInst(raw_ostream &OS) const { | |
| ArrayRef<llvm::MCInst> Source = getSource(); | ||
| const unsigned Executions = LastInstructionIdx / Source.size() + 1; | ||
| for (const MCInst &MCI : Source) { | ||
| unsigned BaseEltIdx = InstrIndex * NumResourceUnits; | ||
| auto UsageEntryEnd = ResourceUsage[InstrIndex].end(); | ||
| auto UsageEntryIt = ResourceUsage[InstrIndex].begin(); | ||
| for (unsigned J = 0; J < NumResourceUnits; ++J) { | ||
| double Usage = ResourceUsage[J + BaseEltIdx]; | ||
| printResourcePressure(FOS, Usage / Executions, (J + 1) * 7); | ||
| double Pressure = 0.0; | ||
| if (UsageEntryIt != UsageEntryEnd && UsageEntryIt->ResourceIdx == J) { | ||
| Pressure = UsageEntryIt->Cycles / Executions; | ||
| ++UsageEntryIt; | ||
| } | ||
| printResourcePressure(FOS, Pressure, (J + 1) * 7); | ||
| } | ||
| assert(UsageEntryIt == UsageEntryEnd); | ||
|
|
||
| FOS << printInstructionString(MCI) << '\n'; | ||
| FOS.flush(); | ||
|
|
@@ -180,17 +212,24 @@ json::Value ResourcePressureView::toJSON() const { | |
| // non-zero values. | ||
| ArrayRef<llvm::MCInst> Source = getSource(); | ||
| const unsigned Executions = LastInstructionIdx / Source.size() + 1; | ||
| for (const auto &R : enumerate(ResourceUsage)) { | ||
| const ReleaseAtCycles &RU = R.value(); | ||
| if (RU.getNumerator() == 0) | ||
| continue; | ||
| unsigned InstructionIndex = R.index() / NumResourceUnits; | ||
| unsigned ResourceIndex = R.index() % NumResourceUnits; | ||
| double Usage = RU / Executions; | ||
|
|
||
| auto AddToJSON = [&ResourcePressureInfo, Executions]( | ||
| const ResourceReleaseAtCycles &RU, unsigned InstIndex) { | ||
| assert(RU.Cycles.getNumerator() != 0); | ||
| double Usage = RU.Cycles / Executions; | ||
| ResourcePressureInfo.push_back( | ||
| json::Object({{"InstructionIndex", InstructionIndex}, | ||
| {"ResourceIndex", ResourceIndex}, | ||
| json::Object({{"InstructionIndex", InstIndex}, | ||
| {"ResourceIndex", RU.ResourceIdx}, | ||
| {"ResourceUsage", Usage}})); | ||
| }; | ||
| for (const auto &R : enumerate(ResourceUsage)) { | ||
|
||
| unsigned InstructionIndex = R.index(); | ||
| for (const auto &RU : R.value()) | ||
| AddToJSON(RU, InstructionIndex); | ||
| } | ||
| for (const auto &RU : CommonResourceUsage) { | ||
| if (RU.Cycles.getNumerator() != 0) | ||
| AddToJSON(RU, Source.size()); | ||
| } | ||
|
|
||
| json::Object JO({{"ResourcePressureInfo", std::move(ResourcePressureInfo)}}); | ||
|
|
||
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
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.
please spell out the type
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.
Addressed