Skip to content

Commit 858b040

Browse files
committed
addressing comments
1 parent 51c8adc commit 858b040

File tree

3 files changed

+43
-17
lines changed

3 files changed

+43
-17
lines changed

llvm/include/llvm/Transforms/IPO/SampleProfileMatcher.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,30 @@ class MyersDiff {
4444
public:
4545
struct DiffResult {
4646
LocToLocMap EqualLocations;
47+
#ifndef NDEBUG
4748
// New IR locations that are inserted in the new version.
4849
std::vector<LineLocation> Insertions;
4950
// Old Profile locations that are deleted in the new version.
5051
std::vector<LineLocation> Deletions;
52+
#endif
5153
void addEqualLocations(const LineLocation &IRLoc,
5254
const LineLocation &ProfLoc) {
5355
EqualLocations.insert({IRLoc, ProfLoc});
5456
}
57+
#ifndef NDEBUG
5558
void addInsertion(const LineLocation &IRLoc) {
5659
Insertions.push_back(IRLoc);
5760
}
5861
void addDeletion(const LineLocation &ProfLoc) {
5962
Deletions.push_back(ProfLoc);
6063
}
64+
#endif
6165
};
6266

6367
// The basic greedy version of Myers's algorithm. Refer to page 6 of the
6468
// original paper.
65-
DiffResult shortestEdit(const std::vector<Anchor> &A,
66-
const std::vector<Anchor> &B) const;
69+
DiffResult longestCommonSequence(const std::vector<Anchor> &A,
70+
const std::vector<Anchor> &B) const;
6771
};
6872

6973
// Sample profile matching - fuzzy match.

llvm/lib/Transforms/IPO/SampleProfileMatcher.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ void SampleProfileMatcher::findProfileAnchors(
123123
}
124124

125125
MyersDiff::DiffResult
126-
MyersDiff::shortestEdit(const std::vector<Anchor> &A,
127-
const std::vector<Anchor> &B) const {
126+
MyersDiff::longestCommonSequence(const std::vector<Anchor> &A,
127+
const std::vector<Anchor> &B) const {
128128
int32_t N = A.size(), M = B.size(), Max = N + M;
129129
auto Index = [&](int32_t I) { return I + Max; };
130130

@@ -159,10 +159,14 @@ MyersDiff::shortestEdit(const std::vector<Anchor> &A,
159159

160160
if (Y == PrevY) {
161161
X--;
162+
#ifndef NDEBUG
162163
Diff.addInsertion(A[X].Loc);
164+
#endif
163165
} else if (X == PrevX) {
164166
Y--;
167+
#ifndef NDEBUG
165168
Diff.addDeletion(B[Y].Loc);
169+
#endif
166170
}
167171
X = PrevX;
168172
Y = PrevY;
@@ -213,7 +217,7 @@ void SampleProfileMatcher::matchNonAnchorAndWriteResults(
213217
SmallVector<LineLocation> LastMatchedNonAnchors;
214218
for (const auto &IR : IRAnchors) {
215219
const auto &Loc = IR.first;
216-
StringRef CalleeName = IR.second;
220+
[[maybe_unused]] StringRef CalleeName = IR.second;
217221
bool IsMatchedAnchor = false;
218222

219223
// Match the anchor location in lexical order.
@@ -309,11 +313,13 @@ void SampleProfileMatcher::runStaleProfileMatching(
309313
if (IRCallsiteAnchors.empty() || ProfileCallsiteAnchors.empty())
310314
return;
311315

312-
// Use the diff algorithm to find the SES, the resulting equal locations from
313-
// IR to Profile are used as anchor to match other locations. Note that here
314-
// use IR anchor as base(A) to align with the order of IRToProfileLocationMap.
316+
// Use the diff algorithm to find the LCS/SES, the resulting equal locations
317+
// from IR to Profile are used as anchor to match other locations. Note that
318+
// here use IR anchor as base(A) to align with the order of
319+
// IRToProfileLocationMap.
315320
MyersDiff Diff;
316-
auto DiffRes = Diff.shortestEdit(IRCallsiteAnchors, ProfileCallsiteAnchors);
321+
auto DiffRes =
322+
Diff.longestCommonSequence(IRCallsiteAnchors, ProfileCallsiteAnchors);
317323

318324
matchNonAnchorAndWriteResults(DiffRes.EqualLocations, IRAnchors,
319325
IRToProfileLocationMap);

llvm/unittests/Transforms/IPO/SampleProfileMatcherTests.cpp

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,31 +43,37 @@ TEST(SampleProfileMatcherTests, MyersDiffTest1) {
4343

4444
std::vector<Anchor> AnchorsA;
4545
std::vector<Anchor> AnchorsB;
46-
auto R = Diff.shortestEdit(AnchorsA, AnchorsB);
46+
auto R = Diff.longestCommonSequence(AnchorsA, AnchorsB);
4747
EXPECT_TRUE(R.EqualLocations.empty());
48+
#ifndef NDEBUG
4849
EXPECT_TRUE(R.Deletions.empty());
4950
EXPECT_TRUE(R.Insertions.empty());
51+
#endif
5052
}
5153

5254
TEST(SampleProfileMatcherTests, MyersDiffTest2) {
5355
std::vector<std::string> A({"a", "b", "c"});
5456
std::vector<Anchor> AnchorsA = createAnchorsFromStrings(A);
5557
std::vector<Anchor> AnchorsB;
56-
auto R = Diff.shortestEdit(AnchorsA, AnchorsB);
58+
auto R = Diff.longestCommonSequence(AnchorsA, AnchorsB);
5759
EXPECT_TRUE(R.EqualLocations.empty());
60+
#ifndef NDEBUG
5861
EXPECT_EQ(R.Insertions, createLocations(std::vector<uint32_t>({2, 1, 0})));
5962
EXPECT_TRUE(R.Deletions.empty());
63+
#endif
6064
}
6165

6266
TEST(SampleProfileMatcherTests, MyersDiffTest3) {
6367

6468
std::vector<Anchor> AnchorsA;
6569
std::vector<std::string> B({"a", "b", "c"});
6670
std::vector<Anchor> AnchorsB = createAnchorsFromStrings(B);
67-
auto R = Diff.shortestEdit(AnchorsA, AnchorsB);
71+
auto R = Diff.longestCommonSequence(AnchorsA, AnchorsB);
6872
EXPECT_TRUE(R.EqualLocations.empty());
73+
#ifndef NDEBUG
6974
EXPECT_TRUE(R.Insertions.empty());
7075
EXPECT_EQ(R.Deletions, createLocations(std::vector<uint32_t>({2, 1, 0})));
76+
#endif
7177
}
7278

7379
TEST(SampleProfileMatcherTests, MyersDiffTest4) {
@@ -77,10 +83,12 @@ TEST(SampleProfileMatcherTests, MyersDiffTest4) {
7783
std::vector<Anchor> AnchorsB = createAnchorsFromStrings(B);
7884
LocToLocMap ExpectEqualLocations =
7985
createEqualLocations({{0, 0}, {1, 1}, {2, 2}});
80-
auto R = Diff.shortestEdit(AnchorsA, AnchorsB);
86+
auto R = Diff.longestCommonSequence(AnchorsA, AnchorsB);
8187
EXPECT_EQ(R.EqualLocations, ExpectEqualLocations);
88+
#ifndef NDEBUG
8289
EXPECT_TRUE(R.Insertions.empty());
8390
EXPECT_TRUE(R.Deletions.empty());
91+
#endif
8492
}
8593

8694
TEST(SampleProfileMatcherTests, MyersDiffTest5) {
@@ -89,10 +97,12 @@ TEST(SampleProfileMatcherTests, MyersDiffTest5) {
8997
std::vector<Anchor> AnchorsA = createAnchorsFromStrings(A);
9098
std::vector<Anchor> AnchorsB = createAnchorsFromStrings(B);
9199
LocToLocMap ExpectEqualLocations = createEqualLocations({{1, 0}, {2, 1}});
92-
auto R = Diff.shortestEdit(AnchorsA, AnchorsB);
100+
auto R = Diff.longestCommonSequence(AnchorsA, AnchorsB);
93101
EXPECT_EQ(R.EqualLocations, ExpectEqualLocations);
102+
#ifndef NDEBUG
94103
EXPECT_EQ(R.Insertions, createLocations(std::vector<uint32_t>({0})));
95104
EXPECT_EQ(R.Deletions, createLocations(std::vector<uint32_t>({2})));
105+
#endif
96106
}
97107

98108
TEST(SampleProfileMatcherTests, MyersDiffTest6) {
@@ -101,10 +111,12 @@ TEST(SampleProfileMatcherTests, MyersDiffTest6) {
101111
std::vector<Anchor> AnchorsA = createAnchorsFromStrings(A);
102112
std::vector<Anchor> AnchorsB = createAnchorsFromStrings(B);
103113
LocToLocMap ExpectEqualLocations = createEqualLocations({{0, 0}, {2, 2}});
104-
auto R = Diff.shortestEdit(AnchorsA, AnchorsB);
114+
auto R = Diff.longestCommonSequence(AnchorsA, AnchorsB);
105115
EXPECT_EQ(R.EqualLocations, ExpectEqualLocations);
116+
#ifndef NDEBUG
106117
EXPECT_EQ(R.Insertions, createLocations(std::vector<uint32_t>({1})));
107118
EXPECT_EQ(R.Deletions, createLocations(std::vector<uint32_t>({1})));
119+
#endif
108120
}
109121

110122
TEST(SampleProfileMatcherTests, MyersDiffTest7) {
@@ -114,10 +126,12 @@ TEST(SampleProfileMatcherTests, MyersDiffTest7) {
114126
std::vector<Anchor> AnchorsB = createAnchorsFromStrings(B);
115127
LocToLocMap ExpectEqualLocations =
116128
createEqualLocations({{2, 0}, {3, 2}, {4, 3}, {6, 4}});
117-
auto R = Diff.shortestEdit(AnchorsA, AnchorsB);
129+
auto R = Diff.longestCommonSequence(AnchorsA, AnchorsB);
118130
EXPECT_EQ(R.EqualLocations, ExpectEqualLocations);
131+
#ifndef NDEBUG
119132
EXPECT_EQ(R.Insertions, createLocations(std::vector<uint32_t>({5, 1, 0})));
120133
EXPECT_EQ(R.Deletions, createLocations(std::vector<uint32_t>({5, 1})));
134+
#endif
121135
}
122136

123137
TEST(SampleProfileMatcherTests, MyersDiffTest8) {
@@ -127,8 +141,10 @@ TEST(SampleProfileMatcherTests, MyersDiffTest8) {
127141
std::vector<Anchor> AnchorsB = createAnchorsFromStrings(B);
128142
LocToLocMap ExpectEqualLocations =
129143
createEqualLocations({{0, 0}, {2, 1}, {3, 2}, {4, 5}, {5, 8}});
130-
auto R = Diff.shortestEdit(AnchorsA, AnchorsB);
144+
auto R = Diff.longestCommonSequence(AnchorsA, AnchorsB);
131145
EXPECT_EQ(R.EqualLocations, ExpectEqualLocations);
146+
#ifndef NDEBUG
132147
EXPECT_EQ(R.Insertions, createLocations(std::vector<uint32_t>({6, 1})));
133148
EXPECT_EQ(R.Deletions, createLocations(std::vector<uint32_t>({7, 6, 4, 3})));
149+
#endif
134150
}

0 commit comments

Comments
 (0)