Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5d16a18

Browse files
authored
[Metadata] Return the valid DebugLoc if one of them is null with -pick-merged-source-locations. (#138148)
Previously when getMergedLocation was passed nullptr as one of the parameters we returned nullptr. Change that behaviour to instead return a valid DebugLoc. This is beneficial for binaries from which SamplePGO profiles are obtained.
1 parent 8313d2a commit 5d16a18

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

llvm/lib/IR/DebugInfoMetadata.cpp

+10-6
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ namespace llvm {
3333
cl::opt<bool> EnableFSDiscriminator(
3434
"enable-fs-discriminator", cl::Hidden,
3535
cl::desc("Enable adding flow sensitive discriminators"));
36-
} // namespace llvm
3736

3837
// When true, preserves line and column number by picking one of the merged
3938
// location info in a deterministic manner to assist sample based PGO.
40-
static cl::opt<bool> PickMergedSourceLocations(
39+
cl::opt<bool> PickMergedSourceLocations(
4140
"pick-merged-source-locations", cl::init(false), cl::Hidden,
4241
cl::desc("Preserve line and column number when merging locations."));
42+
} // namespace llvm
4343

4444
uint32_t DIType::getAlignInBits() const {
4545
return (getTag() == dwarf::DW_TAG_LLVM_ptrauth_type ? 0 : SubclassData32);
@@ -228,17 +228,18 @@ struct ScopeLocationsMatcher {
228228
};
229229

230230
DILocation *DILocation::getMergedLocation(DILocation *LocA, DILocation *LocB) {
231-
if (!LocA || !LocB)
232-
return nullptr;
233-
234231
if (LocA == LocB)
235232
return LocA;
236233

237234
// For some use cases (SamplePGO), it is important to retain distinct source
238235
// locations. When this flag is set, we choose arbitrarily between A and B,
239236
// rather than computing a merged location using line 0, which is typically
240-
// not useful for PGO.
237+
// not useful for PGO. If one of them is null, then try to return one which is
238+
// valid.
241239
if (PickMergedSourceLocations) {
240+
if (!LocA || !LocB)
241+
return LocA ? LocA : LocB;
242+
242243
auto A = std::make_tuple(LocA->getLine(), LocA->getColumn(),
243244
LocA->getDiscriminator(), LocA->getFilename(),
244245
LocA->getDirectory());
@@ -248,6 +249,9 @@ DILocation *DILocation::getMergedLocation(DILocation *LocA, DILocation *LocB) {
248249
return A < B ? LocA : LocB;
249250
}
250251

252+
if (!LocA || !LocB)
253+
return nullptr;
254+
251255
LLVMContext &C = LocA->getContext();
252256

253257
using LocVec = SmallVector<const DILocation *>;

llvm/unittests/IR/MetadataTest.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
#include <optional>
2626
using namespace llvm;
2727

28+
namespace llvm {
29+
extern cl::opt<bool> PickMergedSourceLocations;
30+
} // namespace llvm
31+
2832
namespace {
2933

3034
TEST(ContextAndReplaceableUsesTest, FromContext) {
@@ -1444,6 +1448,25 @@ TEST_F(DILocationTest, Merge) {
14441448
auto *M2 = DILocation::getMergedLocation(A2, B);
14451449
EXPECT_EQ(M1, M2);
14461450
}
1451+
1452+
{
1453+
// If PickMergedSourceLocation is enabled, when one source location is null
1454+
// we should return the valid location.
1455+
PickMergedSourceLocations = true;
1456+
auto *A = DILocation::get(Context, 2, 7, N);
1457+
auto *M1 = DILocation::getMergedLocation(A, nullptr);
1458+
ASSERT_NE(nullptr, M1);
1459+
EXPECT_EQ(2u, M1->getLine());
1460+
EXPECT_EQ(7u, M1->getColumn());
1461+
EXPECT_EQ(N, M1->getScope());
1462+
1463+
auto *M2 = DILocation::getMergedLocation(nullptr, A);
1464+
ASSERT_NE(nullptr, M2);
1465+
EXPECT_EQ(2u, M2->getLine());
1466+
EXPECT_EQ(7u, M2->getColumn());
1467+
EXPECT_EQ(N, M2->getScope());
1468+
PickMergedSourceLocations = false;
1469+
}
14471470
}
14481471

14491472
TEST_F(DILocationTest, getDistinct) {

0 commit comments

Comments
 (0)