Skip to content

Commit 021099d

Browse files
dsandersllvmadrian-prantl
authored andcommitted
stripDebugInfo() should remove DILocation's found in !llvm.loop metadata
Summary: Patch by Michele Scandale (with a small tweak to 'CHECK-NOT' the last DILocation in the test) Subscribers: bogner, llvm-commits Differential Revision: https://reviews.llvm.org/D27980 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@293377 91177308-0d34-0410-b5e6-96231b3b80d8 (cherry picked from commit 0028bf5)
1 parent 0112e12 commit 021099d

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

lib/IR/DebugInfo.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,43 @@ bool DebugInfoFinder::addScope(DIScope *Scope) {
241241
return true;
242242
}
243243

244+
static llvm::MDNode *stripDebugLocFromLoopID(llvm::MDNode *N) {
245+
assert(N->op_begin() != N->op_end() && "Missing self reference?");
246+
auto DebugLocOp =
247+
std::find_if(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) {
248+
return isa<DILocation>(Op.get());
249+
});
250+
251+
// No debug location, we do not have to rewrite this MDNode.
252+
if (DebugLocOp == N->op_end())
253+
return N;
254+
255+
// There is only the debug location without any actual loop metadata, hence we
256+
// can remove the metadata.
257+
if (N->getNumOperands() == 2)
258+
return nullptr;
259+
260+
SmallVector<Metadata *, 4> Args;
261+
// Reserve operand 0 for loop id self reference.
262+
auto TempNode = MDNode::getTemporary(N->getContext(), None);
263+
Args.push_back(TempNode.get());
264+
Args.append(N->op_begin() + 1, DebugLocOp);
265+
Args.append(DebugLocOp + 1, N->op_end());
266+
267+
// Set the first operand to itself.
268+
MDNode *LoopID = MDNode::get(N->getContext(), Args);
269+
LoopID->replaceOperandWith(0, LoopID);
270+
return LoopID;
271+
}
272+
244273
bool llvm::stripDebugInfo(Function &F) {
245274
bool Changed = false;
246275
if (F.getSubprogram()) {
247276
Changed = true;
248277
F.setSubprogram(nullptr);
249278
}
250279

280+
llvm::DenseMap<llvm::MDNode*, llvm::MDNode*> LoopIDsMap;
251281
for (BasicBlock &BB : F) {
252282
for (auto II = BB.begin(), End = BB.end(); II != End;) {
253283
Instruction &I = *II++; // We may delete the instruction, increment now.
@@ -261,6 +291,15 @@ bool llvm::stripDebugInfo(Function &F) {
261291
I.setDebugLoc(DebugLoc());
262292
}
263293
}
294+
295+
auto *TermInst = BB.getTerminator();
296+
if (auto *LoopID = TermInst->getMetadata(LLVMContext::MD_loop)) {
297+
auto *NewLoopID = LoopIDsMap.lookup(LoopID);
298+
if (!NewLoopID)
299+
NewLoopID = LoopIDsMap[LoopID] = stripDebugLocFromLoopID(LoopID);
300+
if (NewLoopID != LoopID)
301+
TermInst->setMetadata(LLVMContext::MD_loop, NewLoopID);
302+
}
264303
}
265304
return Changed;
266305
}

test/DebugInfo/strip-loop-metadata.ll

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
; RUN: opt -S -strip-debug < %s | FileCheck %s
2+
3+
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
4+
target triple = "x86_64-apple-macosx10.12.0"
5+
6+
; CHECK-LABEL: _Z5test1v
7+
; CHECK-NOT: br {{.*}} !llvm.loop
8+
define void @_Z5test1v() !dbg !7 {
9+
entry:
10+
br label %while.body, !dbg !9
11+
12+
while.body:
13+
call void @_Z3barv(), !dbg !10
14+
br label %while.body, !dbg !11, !llvm.loop !13
15+
16+
return:
17+
ret void, !dbg !14
18+
}
19+
20+
declare void @_Z3barv()
21+
22+
; CHECK-LABEL: _Z5test2v
23+
; CHECK: br {{.*}} !llvm.loop [[LOOP:![0-9]+]]
24+
define void @_Z5test2v() !dbg !15 {
25+
entry:
26+
br label %while.body, !dbg !16
27+
28+
while.body:
29+
call void @_Z3barv(), !dbg !17
30+
br label %while.body, !dbg !18, !llvm.loop !19
31+
32+
return:
33+
ret void, !dbg !21
34+
}
35+
36+
!llvm.dbg.cu = !{!0}
37+
!llvm.module.flags = !{!3, !4, !5}
38+
!llvm.ident = !{!6}
39+
40+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 4.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: LineTablesOnly, enums: !2)
41+
!1 = !DIFile(filename: "test.cpp", directory: "/tmp")
42+
!2 = !{}
43+
!3 = !{i32 2, !"Dwarf Version", i32 4}
44+
!4 = !{i32 2, !"Debug Info Version", i32 3}
45+
!5 = !{i32 1, !"PIC Level", i32 2}
46+
!6 = !{!"clang version 4.0.0"}
47+
!7 = distinct !DISubprogram(name: "test1", scope: !1, file: !1, line: 3, type: !8, isLocal: false, isDefinition: true, scopeLine: 3, flags: DIFlagPrototyped, isOptimized: false, unit: !0, variables: !2)
48+
!8 = !DISubroutineType(types: !2)
49+
!9 = !DILocation(line: 4, column: 3, scope: !7)
50+
!10 = !DILocation(line: 5, column: 5, scope: !7)
51+
!11 = !DILocation(line: 4, column: 3, scope: !12)
52+
!12 = !DILexicalBlockFile(scope: !7, file: !1, discriminator: 1)
53+
!13 = distinct !{!13, !9}
54+
!14 = !DILocation(line: 6, column: 1, scope: !7)
55+
!15 = distinct !DISubprogram(name: "test2", scope: !1, file: !1, line: 8, type: !8, isLocal: false, isDefinition: true, scopeLine: 8, flags: DIFlagPrototyped, isOptimized: false, unit: !0, variables: !2)
56+
!16 = !DILocation(line: 8, column: 14, scope: !15)
57+
!17 = !DILocation(line: 11, column: 5, scope: !15)
58+
!18 = !DILocation(line: 10, column: 3, scope: !15)
59+
!19 = distinct !{!19, !16, !20}
60+
!20 = !{!"llvm.loop.unroll.enable"}
61+
!21 = !DILocation(line: 12, column: 1, scope: !15)
62+
63+
; CHECK-NOT: !DICompileUnit
64+
; CHECK-NOT: !DIFile
65+
; CHECK-NOT: !DISubprogram
66+
; CHECK-NOT: !DISubroutineType
67+
; CHECK-NOT: !DILocation
68+
; CHECK-NOT: !DILexicalBlockFile
69+
; CHECK: [[LOOP]] = distinct !{[[LOOP]], [[LOOP_UNROLL:![0-9]+]]}
70+
; CHECK-NEXT: [[LOOP_UNROLL]] = !{!"llvm.loop.unroll.enable"}
71+
; CHECK-NOT: !DILocation

0 commit comments

Comments
 (0)