Skip to content

Commit 2c538d7

Browse files
committed
[KeyInstr] Merge atoms in DILocation::getMergedLocation
NFC for builds with LLVM_EXPERIMENTAL_KEY_INSTRUCTIONS=OFF (default). In an ideal world we would be able to track that the merged location is used in multiple source atoms. We can't do this though, so instead we arbitrarily but deterministically pick one. In cases where the InlinedAt field is unchanged we keep the atom with the lowest non-zero rank (highest precedence). If the ranks are equal we choose the smaller non-zero group number (arbitrary choice). In cases where the InlinedAt field is adjusted we generate a new atom group. Keeping the group wouldn't make sense (a source atom is identified by the group number and InlinedAt pair) but discarding the atom info could result in missed is_stmts. Add unittest in MetadataTest.cpp.
1 parent 71329a0 commit 2c538d7

File tree

2 files changed

+182
-6
lines changed

2 files changed

+182
-6
lines changed

llvm/lib/IR/DebugInfoMetadata.cpp

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,15 @@ DILocation *DILocation::getMergedLocation(DILocation *LocA, DILocation *LocB) {
302302

303303
// Merge the two locations if possible, using the supplied
304304
// inlined-at location for the created location.
305-
auto MergeLocPair = [&C](const DILocation *L1, const DILocation *L2,
306-
DILocation *InlinedAt) -> DILocation * {
305+
auto *LocAIA = LocA->getInlinedAt();
306+
auto *LocBIA = LocB->getInlinedAt();
307+
auto MergeLocPair = [&C, LocAIA,
308+
LocBIA](const DILocation *L1, const DILocation *L2,
309+
DILocation *InlinedAt) -> DILocation * {
307310
if (L1 == L2)
308311
return DILocation::get(C, L1->getLine(), L1->getColumn(), L1->getScope(),
309-
InlinedAt);
312+
InlinedAt, L1->isImplicitCode(),
313+
L1->getAtomGroup(), L1->getAtomRank());
310314

311315
// If the locations originate from different subprograms we can't produce
312316
// a common location.
@@ -342,8 +346,44 @@ DILocation *DILocation::getMergedLocation(DILocation *LocA, DILocation *LocB) {
342346
bool SameCol = L1->getColumn() == L2->getColumn();
343347
unsigned Line = SameLine ? L1->getLine() : 0;
344348
unsigned Col = SameLine && SameCol ? L1->getColumn() : 0;
345-
346-
return DILocation::get(C, Line, Col, Scope, InlinedAt);
349+
bool IsImplicitCode = L1->isImplicitCode() && L2->isImplicitCode();
350+
uint64_t Group = 0;
351+
uint64_t Rank = 0;
352+
if (SameLine) {
353+
if (L1->getAtomGroup() || L2->getAtomGroup()) {
354+
// If we're preserving the same matching inlined-at field we can
355+
// preserve the atom.
356+
if (LocBIA == LocAIA && InlinedAt == LocBIA) {
357+
// Deterministically keep the lowest non-zero ranking atom group
358+
// number.
359+
// FIXME: It would be nice if we could track that an instruction
360+
// belongs to two source atoms.
361+
bool UseL1Atom = [L1, L2]() {
362+
if (L1->getAtomRank() == L2->getAtomRank()) {
363+
// Arbitrarily choose the lowest non-zero group number.
364+
if (!L1->getAtomGroup() || !L2->getAtomGroup())
365+
return !L2->getAtomGroup();
366+
return L1->getAtomGroup() < L2->getAtomGroup();
367+
}
368+
// Choose the lowest non-zero rank.
369+
if (!L1->getAtomRank() || !L2->getAtomRank())
370+
return !L2->getAtomRank();
371+
return L1->getAtomRank() < L2->getAtomRank();
372+
}();
373+
Group = UseL1Atom ? L1->getAtomGroup() : L2->getAtomGroup();
374+
Rank = UseL1Atom ? L1->getAtomRank() : L2->getAtomRank();
375+
} else {
376+
// If either instruction is part of a source atom, reassign it a new
377+
// atom group. This essentially regresses to non-key-instructions
378+
// behaviour (now that it's the only instruction in its group it'll
379+
// probably get is_stmt applied).
380+
Group = C.incNextAtomGroup();
381+
Rank = 1;
382+
}
383+
}
384+
}
385+
return DILocation::get(C, Line, Col, Scope, InlinedAt, IsImplicitCode,
386+
Group, Rank);
347387
};
348388

349389
DILocation *Result = ARIt != ALocs.rend() ? (*ARIt)->getInlinedAt() : nullptr;
@@ -370,7 +410,9 @@ DILocation *DILocation::getMergedLocation(DILocation *LocA, DILocation *LocB) {
370410
// historically picked A's scope, and a nullptr inlined-at location, so that
371411
// behavior is mimicked here but I am not sure if this is always the correct
372412
// way to handle this.
373-
return DILocation::get(C, 0, 0, LocA->getScope(), nullptr);
413+
// Key Instructions: it's fine to drop atom group and rank here, as line 0
414+
// is a nonsensical is_stmt location.
415+
return DILocation::get(C, 0, 0, LocA->getScope(), nullptr, false, 0, 0);
374416
}
375417

376418
std::optional<unsigned>

llvm/unittests/IR/MetadataTest.cpp

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,140 @@ TEST_F(DILocationTest, Merge) {
14451445
auto *M2 = DILocation::getMergedLocation(A2, B);
14461446
EXPECT_EQ(M1, M2);
14471447
}
1448+
1449+
#ifdef EXPERIMENTAL_KEY_INSTRUCTIONS
1450+
#define EXPECT_ATOM(Loc, Group, Rank) \
1451+
EXPECT_EQ(Group, M->getAtomGroup()); \
1452+
EXPECT_EQ(Rank, M->getAtomRank());
1453+
#else
1454+
#define EXPECT_ATOM(Loc, Group, Rank) \
1455+
EXPECT_EQ(0u, M->getAtomGroup()); \
1456+
EXPECT_EQ(0u, M->getAtomRank()); \
1457+
(void)Group; \
1458+
(void)Rank;
1459+
#endif
1460+
// Identical, including source atom numbers.
1461+
{
1462+
auto *A = DILocation::get(Context, 2, 7, N, nullptr, false, 1, 1);
1463+
auto *B = DILocation::get(Context, 2, 7, N, nullptr, false, 1, 1);
1464+
auto *M = DILocation::getMergedLocation(A, B);
1465+
EXPECT_ATOM(M, 1u, 1u);
1466+
// DILocations are uniqued, so we can check equality by ptr.
1467+
EXPECT_EQ(M, DILocation::getMergedLocation(A, B));
1468+
}
1469+
1470+
// Identical but different atom ranks (same atom) - choose the lowest nonzero
1471+
// rank.
1472+
{
1473+
auto *A = DILocation::get(Context, 2, 7, N, nullptr, false, 1, 1);
1474+
auto *B = DILocation::get(Context, 2, 7, N, nullptr, false, 1, 2);
1475+
auto *M = DILocation::getMergedLocation(A, B);
1476+
EXPECT_ATOM(M, 1u, 1u);
1477+
EXPECT_EQ(M, DILocation::getMergedLocation(B, A));
1478+
1479+
A = DILocation::get(Context, 2, 7, N, nullptr, false, 1, 0);
1480+
B = DILocation::get(Context, 2, 7, N, nullptr, false, 1, 2);
1481+
M = DILocation::getMergedLocation(A, B);
1482+
EXPECT_ATOM(M, 1u, 2u);
1483+
EXPECT_EQ(M, DILocation::getMergedLocation(B, A));
1484+
}
1485+
1486+
// Identical but different atom ranks (different atom) - choose the lowest
1487+
// nonzero rank.
1488+
{
1489+
auto *A = DILocation::get(Context, 2, 7, N, nullptr, false, 1, 1);
1490+
auto *B = DILocation::get(Context, 2, 7, N, nullptr, false, 2, 2);
1491+
auto *M = DILocation::getMergedLocation(A, B);
1492+
EXPECT_ATOM(M, 1u, 1u);
1493+
EXPECT_EQ(M, DILocation::getMergedLocation(B, A));
1494+
1495+
A = DILocation::get(Context, 2, 7, N, nullptr, false, 1, 0);
1496+
B = DILocation::get(Context, 2, 7, N, nullptr, false, 2, 2);
1497+
M = DILocation::getMergedLocation(A, B);
1498+
EXPECT_ATOM(M, 2u, 2u);
1499+
EXPECT_EQ(M, DILocation::getMergedLocation(B, A));
1500+
}
1501+
1502+
// Identical but equal atom rank (different atom) - choose the lowest non-zero
1503+
// group (arbitrary choice for deterministic behaviour).
1504+
{
1505+
auto *A = DILocation::get(Context, 2, 7, N, nullptr, false, 1, 1);
1506+
auto *B = DILocation::get(Context, 2, 7, N, nullptr, false, 2, 1);
1507+
auto *M = DILocation::getMergedLocation(A, B);
1508+
EXPECT_ATOM(M, 1u, 1u);
1509+
EXPECT_EQ(M, DILocation::getMergedLocation(B, A));
1510+
1511+
A = DILocation::get(Context, 2, 7, N, nullptr, false, 0, 1);
1512+
B = DILocation::get(Context, 2, 7, N, nullptr, false, 2, 1);
1513+
M = DILocation::getMergedLocation(A, B);
1514+
EXPECT_ATOM(M, 2u, 1u);
1515+
EXPECT_EQ(M, DILocation::getMergedLocation(B, A));
1516+
}
1517+
1518+
// Completely different except same atom numbers. Zero out the atoms.
1519+
{
1520+
auto *I = DILocation::get(Context, 2, 7, N);
1521+
auto *A = DILocation::get(Context, 1, 6, S, I, false, 1, 1);
1522+
auto *B =
1523+
DILocation::get(Context, 2, 7, getSubprogram(), nullptr, false, 1, 1);
1524+
auto *M = DILocation::getMergedLocation(A, B);
1525+
EXPECT_EQ(0u, M->getLine());
1526+
EXPECT_EQ(0u, M->getColumn());
1527+
EXPECT_TRUE(isa<DILocalScope>(M->getScope()));
1528+
EXPECT_EQ(S, M->getScope());
1529+
EXPECT_EQ(nullptr, M->getInlinedAt());
1530+
}
1531+
1532+
// Same inlined-at chain but different atoms. Choose the lowest
1533+
// non-zero group (arbitrary choice for deterministic behaviour).
1534+
{
1535+
auto *I = DILocation::get(Context, 1, 7, N);
1536+
auto *F = getSubprogram();
1537+
auto *A = DILocation::get(Context, 1, 1, F, I, false, 1, 2);
1538+
auto *B = DILocation::get(Context, 1, 1, F, I, false, 2, 1);
1539+
auto *M = DILocation::getMergedLocation(A, B);
1540+
EXPECT_ATOM(M, 2u, 1u);
1541+
EXPECT_EQ(M, DILocation::getMergedLocation(B, A));
1542+
1543+
A = DILocation::get(Context, 1, 1, F, I, false, 1, 2);
1544+
B = DILocation::get(Context, 1, 1, F, I, false, 2, 0);
1545+
M = DILocation::getMergedLocation(A, B);
1546+
EXPECT_ATOM(M, 1u, 2u);
1547+
EXPECT_EQ(M, DILocation::getMergedLocation(B, A));
1548+
}
1549+
1550+
// Partially equal inlined-at chain but different atoms. Generate a new atom
1551+
// group (if either have a group number). This configuration seems unlikely
1552+
// to occur as line numbers must match, but isn't impossible.
1553+
{
1554+
// Reset global counter to ensure EXPECT numbers line up.
1555+
Context.pImpl->NextAtomGroup = 1;
1556+
// x1 -> y2 -> z4
1557+
// y3 -> z4
1558+
auto *FX = getSubprogram();
1559+
auto *FY = getSubprogram();
1560+
auto *FZ = getSubprogram();
1561+
auto *Z4 = DILocation::get(Context, 1, 4, FZ);
1562+
auto *Y3IntoZ4 = DILocation::get(Context, 1, 3, FY, Z4, false, 1, 1);
1563+
auto *Y2IntoZ4 = DILocation::get(Context, 1, 2, FY, Z4);
1564+
auto *X1IntoY2 = DILocation::get(Context, 1, 1, FX, Y2IntoZ4);
1565+
auto *M = DILocation::getMergedLocation(X1IntoY2, Y3IntoZ4);
1566+
EXPECT_EQ(M->getScope(), FY);
1567+
EXPECT_EQ(M->getInlinedAt()->getScope(), FZ);
1568+
EXPECT_ATOM(M, 2u, 1u);
1569+
1570+
// This swapped merge will produce a new atom group too.
1571+
M = DILocation::getMergedLocation(Y3IntoZ4, X1IntoY2);
1572+
1573+
// Same again, even if the atom numbers match.
1574+
auto *X1IntoY2SameAtom =
1575+
DILocation::get(Context, 1, 1, FX, Y2IntoZ4, false, 1, 1);
1576+
M = DILocation::getMergedLocation(X1IntoY2SameAtom, Y3IntoZ4);
1577+
EXPECT_ATOM(M, 4u, 1u);
1578+
M = DILocation::getMergedLocation(Y3IntoZ4, X1IntoY2SameAtom);
1579+
EXPECT_ATOM(M, 5u, 1u);
1580+
}
1581+
#undef EXPECT_ATOM
14481582
}
14491583

14501584
TEST_F(DILocationTest, getDistinct) {

0 commit comments

Comments
 (0)