Skip to content

Commit 0335154

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 07f9c1f commit 0335154

File tree

2 files changed

+182
-6
lines changed

2 files changed

+182
-6
lines changed

llvm/lib/IR/DebugInfoMetadata.cpp

+48-6
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,15 @@ DILocation *DILocation::getMergedLocation(DILocation *LocA, DILocation *LocB) {
189189

190190
// Merge the two locations if possible, using the supplied
191191
// inlined-at location for the created location.
192-
auto MergeLocPair = [&C](const DILocation *L1, const DILocation *L2,
193-
DILocation *InlinedAt) -> DILocation * {
192+
auto *LocAIA = LocA->getInlinedAt();
193+
auto *LocBIA = LocB->getInlinedAt();
194+
auto MergeLocPair = [&C, LocAIA,
195+
LocBIA](const DILocation *L1, const DILocation *L2,
196+
DILocation *InlinedAt) -> DILocation * {
194197
if (L1 == L2)
195198
return DILocation::get(C, L1->getLine(), L1->getColumn(), L1->getScope(),
196-
InlinedAt);
199+
InlinedAt, L1->isImplicitCode(),
200+
L1->getAtomGroup(), L1->getAtomRank());
197201

198202
// If the locations originate from different subprograms we can't produce
199203
// a common location.
@@ -226,8 +230,44 @@ DILocation *DILocation::getMergedLocation(DILocation *LocA, DILocation *LocB) {
226230
bool SameCol = L1->getColumn() == L2->getColumn();
227231
unsigned Line = SameLine ? L1->getLine() : 0;
228232
unsigned Col = SameLine && SameCol ? L1->getColumn() : 0;
229-
230-
return DILocation::get(C, Line, Col, Scope, InlinedAt);
233+
bool IsImplicitCode = L1->isImplicitCode() && L2->isImplicitCode();
234+
uint64_t Group = 0;
235+
uint64_t Rank = 0;
236+
if (SameLine) {
237+
if (L1->getAtomGroup() || L2->getAtomGroup()) {
238+
// If we're preserving the same matching inlined-at field we can
239+
// preserve the atom.
240+
if (LocBIA == LocAIA && InlinedAt == LocBIA) {
241+
// Deterministically keep the lowest non-zero ranking atom group
242+
// number.
243+
// FIXME: It would be nice if we could track that an instruction
244+
// belongs to two source atoms.
245+
bool UseL1Atom = [L1, L2]() {
246+
if (L1->getAtomRank() == L2->getAtomRank()) {
247+
// Arbitrarily choose the lowest non-zero group number.
248+
if (!L1->getAtomGroup() || !L2->getAtomGroup())
249+
return !L2->getAtomGroup();
250+
return L1->getAtomGroup() < L2->getAtomGroup();
251+
}
252+
// Choose the lowest non-zero rank.
253+
if (!L1->getAtomRank() || !L2->getAtomRank())
254+
return !L2->getAtomRank();
255+
return L1->getAtomRank() < L2->getAtomRank();
256+
}();
257+
Group = UseL1Atom ? L1->getAtomGroup() : L2->getAtomGroup();
258+
Rank = UseL1Atom ? L1->getAtomRank() : L2->getAtomRank();
259+
} else {
260+
// If either instruction is part of a source atom, reassign it a new
261+
// atom group. This essentially regresses to non-key-instructions
262+
// behaviour (now that it's the only instruction in its group it'll
263+
// probably get is_stmt applied).
264+
Group = C.incNextAtomGroup();
265+
Rank = 1;
266+
}
267+
}
268+
}
269+
return DILocation::get(C, Line, Col, Scope, InlinedAt, IsImplicitCode,
270+
Group, Rank);
231271
};
232272

233273
DILocation *Result = ARIt != ALocs.rend() ? (*ARIt)->getInlinedAt() : nullptr;
@@ -254,7 +294,9 @@ DILocation *DILocation::getMergedLocation(DILocation *LocA, DILocation *LocB) {
254294
// historically picked A's scope, and a nullptr inlined-at location, so that
255295
// behavior is mimicked here but I am not sure if this is always the correct
256296
// way to handle this.
257-
return DILocation::get(C, 0, 0, LocA->getScope(), nullptr);
297+
// Key Instructions: it's fine to drop atom group and rank here, as line 0
298+
// is a nonsensical is_stmt location.
299+
return DILocation::get(C, 0, 0, LocA->getScope(), nullptr, false, 0, 0);
258300
}
259301

260302
std::optional<unsigned>

llvm/unittests/IR/MetadataTest.cpp

+134
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,140 @@ TEST_F(DILocationTest, Merge) {
12431243
auto *M2 = DILocation::getMergedLocation(A2, B);
12441244
EXPECT_EQ(M1, M2);
12451245
}
1246+
1247+
#ifdef EXPERIMENTAL_KEY_INSTRUCTIONS
1248+
#define EXPECT_ATOM(Loc, Group, Rank) \
1249+
EXPECT_EQ(Group, M->getAtomGroup()); \
1250+
EXPECT_EQ(Rank, M->getAtomRank());
1251+
#else
1252+
#define EXPECT_ATOM(Loc, Group, Rank) \
1253+
EXPECT_EQ(0u, M->getAtomGroup()); \
1254+
EXPECT_EQ(0u, M->getAtomRank()); \
1255+
(void)Group; \
1256+
(void)Rank;
1257+
#endif
1258+
// Identical, including source atom numbers.
1259+
{
1260+
auto *A = DILocation::get(Context, 2, 7, N, nullptr, false, 1, 1);
1261+
auto *B = DILocation::get(Context, 2, 7, N, nullptr, false, 1, 1);
1262+
auto *M = DILocation::getMergedLocation(A, B);
1263+
EXPECT_ATOM(M, 1u, 1u);
1264+
// DILocations are uniqued, so we can check equality by ptr.
1265+
EXPECT_EQ(M, DILocation::getMergedLocation(A, B));
1266+
}
1267+
1268+
// Identical but different atom ranks (same atom) - choose the lowest nonzero
1269+
// rank.
1270+
{
1271+
auto *A = DILocation::get(Context, 2, 7, N, nullptr, false, 1, 1);
1272+
auto *B = DILocation::get(Context, 2, 7, N, nullptr, false, 1, 2);
1273+
auto *M = DILocation::getMergedLocation(A, B);
1274+
EXPECT_ATOM(M, 1u, 1u);
1275+
EXPECT_EQ(M, DILocation::getMergedLocation(B, A));
1276+
1277+
A = DILocation::get(Context, 2, 7, N, nullptr, false, 1, 0);
1278+
B = DILocation::get(Context, 2, 7, N, nullptr, false, 1, 2);
1279+
M = DILocation::getMergedLocation(A, B);
1280+
EXPECT_ATOM(M, 1u, 2u);
1281+
EXPECT_EQ(M, DILocation::getMergedLocation(B, A));
1282+
}
1283+
1284+
// Identical but different atom ranks (different atom) - choose the lowest
1285+
// nonzero rank.
1286+
{
1287+
auto *A = DILocation::get(Context, 2, 7, N, nullptr, false, 1, 1);
1288+
auto *B = DILocation::get(Context, 2, 7, N, nullptr, false, 2, 2);
1289+
auto *M = DILocation::getMergedLocation(A, B);
1290+
EXPECT_ATOM(M, 1u, 1u);
1291+
EXPECT_EQ(M, DILocation::getMergedLocation(B, A));
1292+
1293+
A = DILocation::get(Context, 2, 7, N, nullptr, false, 1, 0);
1294+
B = DILocation::get(Context, 2, 7, N, nullptr, false, 2, 2);
1295+
M = DILocation::getMergedLocation(A, B);
1296+
EXPECT_ATOM(M, 2u, 2u);
1297+
EXPECT_EQ(M, DILocation::getMergedLocation(B, A));
1298+
}
1299+
1300+
// Identical but equal atom rank (different atom) - choose the lowest non-zero
1301+
// group (arbitrary choice for deterministic behaviour).
1302+
{
1303+
auto *A = DILocation::get(Context, 2, 7, N, nullptr, false, 1, 1);
1304+
auto *B = DILocation::get(Context, 2, 7, N, nullptr, false, 2, 1);
1305+
auto *M = DILocation::getMergedLocation(A, B);
1306+
EXPECT_ATOM(M, 1u, 1u);
1307+
EXPECT_EQ(M, DILocation::getMergedLocation(B, A));
1308+
1309+
A = DILocation::get(Context, 2, 7, N, nullptr, false, 0, 1);
1310+
B = DILocation::get(Context, 2, 7, N, nullptr, false, 2, 1);
1311+
M = DILocation::getMergedLocation(A, B);
1312+
EXPECT_ATOM(M, 2u, 1u);
1313+
EXPECT_EQ(M, DILocation::getMergedLocation(B, A));
1314+
}
1315+
1316+
// Completely different except same atom numbers. Zero out the atoms.
1317+
{
1318+
auto *I = DILocation::get(Context, 2, 7, N);
1319+
auto *A = DILocation::get(Context, 1, 6, S, I, false, 1, 1);
1320+
auto *B =
1321+
DILocation::get(Context, 2, 7, getSubprogram(), nullptr, false, 1, 1);
1322+
auto *M = DILocation::getMergedLocation(A, B);
1323+
EXPECT_EQ(0u, M->getLine());
1324+
EXPECT_EQ(0u, M->getColumn());
1325+
EXPECT_TRUE(isa<DILocalScope>(M->getScope()));
1326+
EXPECT_EQ(S, M->getScope());
1327+
EXPECT_EQ(nullptr, M->getInlinedAt());
1328+
}
1329+
1330+
// Same inlined-at chain but different atoms. Choose the lowest
1331+
// non-zero group (arbitrary choice for deterministic behaviour).
1332+
{
1333+
auto *I = DILocation::get(Context, 1, 7, N);
1334+
auto *F = getSubprogram();
1335+
auto *A = DILocation::get(Context, 1, 1, F, I, false, 1, 2);
1336+
auto *B = DILocation::get(Context, 1, 1, F, I, false, 2, 1);
1337+
auto *M = DILocation::getMergedLocation(A, B);
1338+
EXPECT_ATOM(M, 2u, 1u);
1339+
EXPECT_EQ(M, DILocation::getMergedLocation(B, A));
1340+
1341+
A = DILocation::get(Context, 1, 1, F, I, false, 1, 2);
1342+
B = DILocation::get(Context, 1, 1, F, I, false, 2, 0);
1343+
M = DILocation::getMergedLocation(A, B);
1344+
EXPECT_ATOM(M, 1u, 2u);
1345+
EXPECT_EQ(M, DILocation::getMergedLocation(B, A));
1346+
}
1347+
1348+
// Partially equal inlined-at chain but different atoms. Generate a new atom
1349+
// group (if either have a group number). This configuration seems unlikely
1350+
// to occur as line numbers must match, but isn't impossible.
1351+
{
1352+
// Reset global counter to ensure EXPECT numbers line up.
1353+
Context.pImpl->NextAtomGroup = 1;
1354+
// x1 -> y2 -> z4
1355+
// y3 -> z4
1356+
auto *FX = getSubprogram();
1357+
auto *FY = getSubprogram();
1358+
auto *FZ = getSubprogram();
1359+
auto *Z4 = DILocation::get(Context, 1, 4, FZ);
1360+
auto *Y3IntoZ4 = DILocation::get(Context, 1, 3, FY, Z4, false, 1, 1);
1361+
auto *Y2IntoZ4 = DILocation::get(Context, 1, 2, FY, Z4);
1362+
auto *X1IntoY2 = DILocation::get(Context, 1, 1, FX, Y2IntoZ4);
1363+
auto *M = DILocation::getMergedLocation(X1IntoY2, Y3IntoZ4);
1364+
EXPECT_EQ(M->getScope(), FY);
1365+
EXPECT_EQ(M->getInlinedAt()->getScope(), FZ);
1366+
EXPECT_ATOM(M, 2u, 1u);
1367+
1368+
// This swapped merge will produce a new atom group too.
1369+
M = DILocation::getMergedLocation(Y3IntoZ4, X1IntoY2);
1370+
1371+
// Same again, even if the atom numbers match.
1372+
auto *X1IntoY2SameAtom =
1373+
DILocation::get(Context, 1, 1, FX, Y2IntoZ4, false, 1, 1);
1374+
M = DILocation::getMergedLocation(X1IntoY2SameAtom, Y3IntoZ4);
1375+
EXPECT_ATOM(M, 4u, 1u);
1376+
M = DILocation::getMergedLocation(Y3IntoZ4, X1IntoY2SameAtom);
1377+
EXPECT_ATOM(M, 5u, 1u);
1378+
}
1379+
#undef EXPECT_ATOM
12461380
}
12471381

12481382
TEST_F(DILocationTest, getDistinct) {

0 commit comments

Comments
 (0)