-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[SampleProf] Templatize longestCommonSequence (NFC) #114633
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
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
116 changes: 116 additions & 0 deletions
116
llvm/include/llvm/Transforms/Utils/LongestCommonSequence.h
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,116 @@ | ||
| //===- LongestCommonSequence.h - Compute LCS --------------------*- C++ -*-===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This file implements longestCommonSequence, useful for finding matches | ||
| // between two sequences, such as lists of profiling points. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLVM_TRANSFORMS_UTILS_LONGESTCOMMONSEQEUNCE_H | ||
| #define LLVM_TRANSFORMS_UTILS_LONGESTCOMMONSEQEUNCE_H | ||
|
|
||
| #include "llvm/ADT/ArrayRef.h" | ||
|
|
||
| #include <cstdint> | ||
| #include <vector> | ||
|
|
||
| namespace llvm { | ||
|
|
||
| // This function implements the Myers diff algorithm used for stale profile | ||
| // matching. The algorithm provides a simple and efficient way to find the | ||
| // Longest Common Subsequence(LCS) or the Shortest Edit Script(SES) of two | ||
| // sequences. For more details, refer to the paper 'An O(ND) Difference | ||
| // Algorithm and Its Variations' by Eugene W. Myers. | ||
| // In the scenario of profile fuzzy matching, the two sequences are the IR | ||
| // callsite anchors and profile callsite anchors. The subsequence equivalent | ||
| // parts from the resulting SES are used to remap the IR locations to the | ||
| // profile locations. As the number of function callsite is usually not big, | ||
| // we currently just implements the basic greedy version(page 6 of the paper). | ||
| template <typename Loc, typename Function, | ||
| typename AnchorList = ArrayRef<std::pair<Loc, Function>>> | ||
| void longestCommonSequence( | ||
| AnchorList AnchorList1, AnchorList AnchorList2, | ||
| llvm::function_ref<bool(const Function &, const Function &)> | ||
| FunctionMatchesProfile, | ||
| llvm::function_ref<void(Loc, Loc)> InsertMatching) { | ||
| int32_t Size1 = AnchorList1.size(), Size2 = AnchorList2.size(), | ||
| MaxDepth = Size1 + Size2; | ||
| auto Index = [&](int32_t I) { return I + MaxDepth; }; | ||
|
|
||
| if (MaxDepth == 0) | ||
| return; | ||
|
|
||
| // Backtrack the SES result. | ||
| auto Backtrack = [&](ArrayRef<std::vector<int32_t>> Trace, | ||
| AnchorList AnchorList1, AnchorList AnchorList2) { | ||
| int32_t X = Size1, Y = Size2; | ||
| for (int32_t Depth = Trace.size() - 1; X > 0 || Y > 0; Depth--) { | ||
| const auto &P = Trace[Depth]; | ||
| int32_t K = X - Y; | ||
| int32_t PrevK = K; | ||
| if (K == -Depth || (K != Depth && P[Index(K - 1)] < P[Index(K + 1)])) | ||
| PrevK = K + 1; | ||
| else | ||
| PrevK = K - 1; | ||
|
|
||
| int32_t PrevX = P[Index(PrevK)]; | ||
| int32_t PrevY = PrevX - PrevK; | ||
| while (X > PrevX && Y > PrevY) { | ||
| X--; | ||
| Y--; | ||
| InsertMatching(AnchorList1[X].first, AnchorList2[Y].first); | ||
| } | ||
|
|
||
| if (Depth == 0) | ||
| break; | ||
|
|
||
| if (Y == PrevY) | ||
| X--; | ||
| else if (X == PrevX) | ||
| Y--; | ||
| X = PrevX; | ||
| Y = PrevY; | ||
| } | ||
| }; | ||
|
|
||
| // The greedy LCS/SES algorithm. | ||
|
|
||
| // An array contains the endpoints of the furthest reaching D-paths. | ||
| std::vector<int32_t> V(2 * MaxDepth + 1, -1); | ||
| V[Index(1)] = 0; | ||
| // Trace is used to backtrack the SES result. | ||
| std::vector<std::vector<int32_t>> Trace; | ||
| for (int32_t Depth = 0; Depth <= MaxDepth; Depth++) { | ||
| Trace.push_back(V); | ||
| for (int32_t K = -Depth; K <= Depth; K += 2) { | ||
| int32_t X = 0, Y = 0; | ||
| if (K == -Depth || (K != Depth && V[Index(K - 1)] < V[Index(K + 1)])) | ||
| X = V[Index(K + 1)]; | ||
| else | ||
| X = V[Index(K - 1)] + 1; | ||
| Y = X - K; | ||
| while ( | ||
| X < Size1 && Y < Size2 && | ||
| FunctionMatchesProfile(AnchorList1[X].second, AnchorList2[Y].second)) | ||
| X++, Y++; | ||
|
|
||
| V[Index(K)] = X; | ||
|
|
||
| if (X >= Size1 && Y >= Size2) { | ||
| // Length of an SES is D. | ||
| Backtrack(Trace, AnchorList1, AnchorList2); | ||
| return; | ||
| } | ||
| } | ||
| } | ||
| // Length of an SES is greater than MaxDepth. | ||
| } | ||
|
|
||
| } // end namespace llvm | ||
|
|
||
| #endif // LLVM_TRANSFORMS_UTILS_LONGESTCOMMONSEQEUNCE_H |
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.
Uh oh!
There was an error while loading. Please reload this page.