Skip to content

Commit b7627f5

Browse files
author
git apple-llvm automerger
committed
Merge commit 'a8148769af1e' from apple/stable/20210107 into swift/release/5.5
2 parents 04cbb3a + a814876 commit b7627f5

File tree

5 files changed

+98
-32
lines changed

5 files changed

+98
-32
lines changed

llvm/include/llvm/IR/DebugInfo.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ bool stripDebugInfo(Function &F);
5252
bool stripNonLineTableDebugInfo(Module &M);
5353

5454
/// Update the debug locations contained within the MD_loop metadata attached
55-
/// to the instruction \p I, if one exists. \p Updater is applied to each debug
56-
/// location in the MD_loop metadata: the returned value is included in the
55+
/// to the instruction \p I, if one exists. \p Updater is applied to Metadata
56+
/// operand in the MD_loop metadata: the returned value is included in the
5757
/// updated loop metadata node if it is non-null.
5858
void updateLoopMetadataDebugLocations(
59-
Instruction &I, function_ref<DILocation *(const DILocation &)> Updater);
59+
Instruction &I, function_ref<Metadata *(Metadata *)> Updater);
6060

6161
/// Return Debug Info Metadata Version by checking module flags.
6262
unsigned getDebugMetadataVersionFromModule(const Module &M);

llvm/lib/IR/DebugInfo.cpp

Lines changed: 60 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,7 @@ bool DebugInfoFinder::addScope(DIScope *Scope) {
262262
}
263263

264264
static MDNode *updateLoopMetadataDebugLocationsImpl(
265-
MDNode *OrigLoopID,
266-
function_ref<DILocation *(const DILocation &)> Updater) {
265+
MDNode *OrigLoopID, function_ref<Metadata *(Metadata *)> Updater) {
267266
assert(OrigLoopID && OrigLoopID->getNumOperands() > 0 &&
268267
"Loop ID needs at least one operand");
269268
assert(OrigLoopID && OrigLoopID->getOperand(0).get() == OrigLoopID &&
@@ -274,11 +273,10 @@ static MDNode *updateLoopMetadataDebugLocationsImpl(
274273

275274
for (unsigned i = 1; i < OrigLoopID->getNumOperands(); ++i) {
276275
Metadata *MD = OrigLoopID->getOperand(i);
277-
if (DILocation *DL = dyn_cast<DILocation>(MD)) {
278-
if (DILocation *NewDL = Updater(*DL))
279-
MDs.push_back(NewDL);
280-
} else
281-
MDs.push_back(MD);
276+
if (!MD)
277+
MDs.push_back(nullptr);
278+
else if (Metadata *NewMD = Updater(MD))
279+
MDs.push_back(NewMD);
282280
}
283281

284282
MDNode *NewLoopID = MDNode::getDistinct(OrigLoopID->getContext(), MDs);
@@ -288,34 +286,72 @@ static MDNode *updateLoopMetadataDebugLocationsImpl(
288286
}
289287

290288
void llvm::updateLoopMetadataDebugLocations(
291-
Instruction &I, function_ref<DILocation *(const DILocation &)> Updater) {
289+
Instruction &I, function_ref<Metadata *(Metadata *)> Updater) {
292290
MDNode *OrigLoopID = I.getMetadata(LLVMContext::MD_loop);
293291
if (!OrigLoopID)
294292
return;
295293
MDNode *NewLoopID = updateLoopMetadataDebugLocationsImpl(OrigLoopID, Updater);
296294
I.setMetadata(LLVMContext::MD_loop, NewLoopID);
297295
}
298296

297+
/// Return true if a node is a DILocation or if a DILocation is
298+
/// indirectly referenced by one of the node's children.
299+
static bool isDILocationReachable(SmallPtrSetImpl<Metadata *> &Visited,
300+
SmallPtrSetImpl<Metadata *> &Reachable,
301+
Metadata *MD) {
302+
MDNode *N = dyn_cast_or_null<MDNode>(MD);
303+
if (!N)
304+
return false;
305+
if (Reachable.count(N) || isa<DILocation>(N))
306+
return true;
307+
if (!Visited.insert(N).second)
308+
return false;
309+
for (auto &OpIt : N->operands()) {
310+
Metadata *Op = OpIt.get();
311+
if (isDILocationReachable(Visited, Reachable, Op)) {
312+
Reachable.insert(N);
313+
return true;
314+
}
315+
}
316+
return false;
317+
}
318+
299319
static MDNode *stripDebugLocFromLoopID(MDNode *N) {
300320
assert(!N->operands().empty() && "Missing self reference?");
321+
SmallPtrSet<Metadata *, 8> Visited, DILocationReachable;
322+
// If we already visited N, there is nothing to do.
323+
if (!Visited.insert(N).second)
324+
return N;
301325

302-
// if there is no debug location, we do not have to rewrite this MDNode.
303-
if (std::none_of(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) {
304-
return isa<DILocation>(Op.get());
305-
}))
326+
// If there is no debug location, we do not have to rewrite this
327+
// MDNode. This loop also initializes DILocationReachable, later
328+
// needed by updateLoopMetadataDebugLocationsImpl; the use of
329+
// count_if avoids an early exit.
330+
if (!std::count_if(N->op_begin() + 1, N->op_end(),
331+
[&Visited, &DILocationReachable](const MDOperand &Op) {
332+
return isa<DILocation>(Op.get()) ||
333+
isDILocationReachable(
334+
Visited, DILocationReachable, Op.get());
335+
}))
306336
return N;
307337

308338
// If there is only the debug location without any actual loop metadata, we
309339
// can remove the metadata.
310-
if (std::none_of(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) {
311-
return !isa<DILocation>(Op.get());
312-
}))
340+
if (std::all_of(
341+
N->op_begin() + 1, N->op_end(),
342+
[&Visited, &DILocationReachable](const MDOperand &Op) {
343+
return isa<DILocation>(Op.get()) ||
344+
isDILocationReachable(Visited, DILocationReachable,
345+
Op.get());
346+
}))
313347
return nullptr;
314348

315-
auto dropDebugLoc = [](const DILocation &) -> DILocation * {
316-
return nullptr;
317-
};
318-
return updateLoopMetadataDebugLocationsImpl(N, dropDebugLoc);
349+
return updateLoopMetadataDebugLocationsImpl(
350+
N, [&DILocationReachable](Metadata *MD) -> Metadata * {
351+
if (isa<DILocation>(MD) || DILocationReachable.count(MD))
352+
return nullptr;
353+
return MD;
354+
});
319355
}
320356

321357
bool llvm::stripDebugInfo(Function &F) {
@@ -325,7 +361,7 @@ bool llvm::stripDebugInfo(Function &F) {
325361
F.setSubprogram(nullptr);
326362
}
327363

328-
DenseMap<MDNode*, MDNode*> LoopIDsMap;
364+
DenseMap<MDNode *, MDNode *> LoopIDsMap;
329365
for (BasicBlock &BB : F) {
330366
for (auto II = BB.begin(), End = BB.end(); II != End;) {
331367
Instruction &I = *II++; // We may delete the instruction, increment now.
@@ -658,8 +694,10 @@ bool llvm::stripNonLineTableDebugInfo(Module &M) {
658694
I.setDebugLoc(remapDebugLoc(I.getDebugLoc()));
659695

660696
// Remap DILocations in llvm.loop attachments.
661-
updateLoopMetadataDebugLocations(I, [&](const DILocation &Loc) {
662-
return remapDebugLoc(&Loc).get();
697+
updateLoopMetadataDebugLocations(I, [&](Metadata *MD) -> Metadata * {
698+
if (auto *Loc = dyn_cast_or_null<DILocation>(MD))
699+
return remapDebugLoc(Loc).get();
700+
return MD;
663701
});
664702

665703
// Strip heapallocsite attachments, they point into the DIType system.

llvm/lib/Transforms/Utils/CodeExtractor.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,10 +1553,11 @@ static void fixupDebugInfoPostExtraction(Function &OldFunc, Function &NewFunc,
15531553
I.setDebugLoc(DILocation::get(Ctx, DL.getLine(), DL.getCol(), NewSP));
15541554

15551555
// Loop info metadata may contain line locations. Fix them up.
1556-
auto updateLoopInfoLoc = [&Ctx,
1557-
NewSP](const DILocation &Loc) -> DILocation * {
1558-
return DILocation::get(Ctx, Loc.getLine(), Loc.getColumn(), NewSP,
1559-
nullptr);
1556+
auto updateLoopInfoLoc = [&Ctx, NewSP](Metadata *MD) -> Metadata * {
1557+
if (auto *Loc = dyn_cast_or_null<DILocation>(MD))
1558+
return DILocation::get(Ctx, Loc->getLine(), Loc->getColumn(), NewSP,
1559+
nullptr);
1560+
return MD;
15601561
};
15611562
updateLoopMetadataDebugLocations(I, updateLoopInfoLoc);
15621563
}

llvm/lib/Transforms/Utils/InlineFunction.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,9 +1480,11 @@ static void fixupLineNumbers(Function *Fn, Function::iterator FI,
14801480
BI != BE; ++BI) {
14811481
// Loop metadata needs to be updated so that the start and end locs
14821482
// reference inlined-at locations.
1483-
auto updateLoopInfoLoc = [&Ctx, &InlinedAtNode, &IANodes](
1484-
const DILocation &Loc) -> DILocation * {
1485-
return inlineDebugLoc(&Loc, InlinedAtNode, Ctx, IANodes).get();
1483+
auto updateLoopInfoLoc = [&Ctx, &InlinedAtNode,
1484+
&IANodes](Metadata *MD) -> Metadata * {
1485+
if (auto *Loc = dyn_cast_or_null<DILocation>(MD))
1486+
return inlineDebugLoc(Loc, InlinedAtNode, Ctx, IANodes).get();
1487+
return MD;
14861488
};
14871489
updateLoopMetadataDebugLocations(*BI, updateLoopInfoLoc);
14881490

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
; RUN: llvm-as -disable-output < %s -o /dev/null 2>&1 | FileCheck %s
2+
; RUN: llvm-as < %s -o - | llvm-dis - | FileCheck %s --check-prefix=CHECK-STRIP
3+
; CHECK: DICompileUnit not listed in llvm.dbg.cu
4+
; CHECK: ignoring invalid debug info in
5+
; CHECK-NOT: DICompileUnit not listed in llvm.dbg.cu
6+
declare hidden void @g() local_unnamed_addr #1 align 2
7+
define hidden void @f() {
8+
tail call void @g() #2, !llvm.loop !5
9+
ret void
10+
}
11+
!llvm.module.flags = !{!0, !1}
12+
!0 = !{i32 2, !"Dwarf Version", i32 4}
13+
!1 = !{i32 2, !"Debug Info Version", i32 3}
14+
!5 = distinct !{!5, !14, !15, !"fake loop metadata"}
15+
; CHECK-STRIP: ![[MD:.*]] = distinct !{![[MD]], !{{[0-9]+}}, !"fake loop metadata"}
16+
!7 = distinct !DISubprogram(name: "f", scope: !8, file: !8, line: 1324, type: !9, scopeLine: 1324, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !11)
17+
!8 = !DIFile(filename: "/", directory: "f.cpp")
18+
!9 = !DISubroutineType(types: !10)
19+
!10 = !{}
20+
!11 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !8)
21+
!14 = !{!"metadata 1", i1 true}
22+
!15 = !{!"metadata 1", !16}
23+
!16 = distinct !{!16, !17}
24+
!17 = !DILocation(line: 105, column: 3, scope: !18)
25+
!18 = distinct !DILexicalBlock(scope: !7, file: !8, line: 105, column: 3)

0 commit comments

Comments
 (0)