Debian Codegraph @

llvm/unittests/CodeGen/DroppedVariableStatsMIRTest.cpp

raw
1 //===- unittests/CodeGen/DroppedVariableStatsMIRTest.cpp ------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/CodeGen/DroppedVariableStatsMIR.h"
10 #include "llvm/AsmParser/Parser.h"
11 #include "llvm/CodeGen/MIRParser/MIRParser.h"
12 #include "llvm/CodeGen/MachineModuleInfo.h"
13 #include "llvm/IR/Function.h"
14 #include "llvm/IR/LegacyPassManager.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/MC/TargetRegistry.h"
17 #include "llvm/Pass.h"
18 #include "llvm/Support/SourceMgr.h"
19 #include "llvm/Support/TargetSelect.h"
20 #include "llvm/Target/TargetMachine.h"
21 #include "gtest/gtest.h"
22 #include <llvm/ADT/SmallString.h>
23 #include <llvm/IR/LLVMContext.h>
24 #include <llvm/IR/PassInstrumentation.h>
25 #include <llvm/IR/PassManager.h>
26 #include <llvm/IR/PassTimingInfo.h>
27 #include <llvm/Support/raw_ostream.h>
28
29 using namespace llvm;
30
31 namespace {
32
33 std::unique_ptr<TargetMachine>
34 createTargetMachine(std::string TargetStr, StringRef CPU, StringRef FS) {
35 std::string Error;
36 Triple TT(TargetStr);
37 const Target *T = TargetRegistry::lookupTarget(TT, Error);
38 if (!T)
39 return nullptr;
40 TargetOptions Options;
41 return std::unique_ptr<TargetMachine>(
42 static_cast<TargetMachine *>(T->createTargetMachine(
43 TT, CPU, FS, Options, std::nullopt, std::nullopt)));
44 }
45
46 std::unique_ptr<Module> parseMIR(const TargetMachine &TM, StringRef MIRCode,
47 MachineModuleInfo &MMI, LLVMContext *Context) {
48 SMDiagnostic Diagnostic;
49 std::unique_ptr<Module> M;
50 std::unique_ptr<MemoryBuffer> MBuffer = MemoryBuffer::getMemBuffer(MIRCode);
51 auto MIR = createMIRParser(std::move(MBuffer), *Context);
52 if (!MIR)
53 return nullptr;
54
55 std::unique_ptr<Module> Mod = MIR->parseIRModule();
56 if (!Mod)
57 return nullptr;
58
59 Mod->setDataLayout(TM.createDataLayout());
60
61 if (MIR->parseMachineFunctions(*Mod, MMI)) {
62 M.reset();
63 return nullptr;
64 }
65 return Mod;
66 }
67 // This test ensures that if a DBG_VALUE and an instruction that exists in the
68 // same scope as that DBG_VALUE are both deleted as a result of an optimization
69 // pass, debug information is considered not dropped.
70 TEST(DroppedVariableStatsMIR, BothDeleted) {
71 InitializeAllTargetInfos();
72 InitializeAllTargets();
73 InitializeAllTargetMCs();
74 PassInstrumentationCallbacks PIC;
75 PassInstrumentation PI(&PIC);
76
77 LLVMContext C;
78
79 const char *MIR =
80 R"(
81 --- |
82 ; ModuleID = '/tmp/test.ll'
83 source_filename = "/tmp/test.ll"
84 target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-Fn32"
85
86 define noundef range(i32 -2147483647, -2147483648) i32 @_Z3fooi(i32 noundef %x) local_unnamed_addr !dbg !4 {
87 entry:
88 #dbg_value(i32 %x, !10, !DIExpression(), !11)
89 %add = add nsw i32 %x, 1, !dbg !12
90 ret i32 0
91 }
92
93 !llvm.dbg.cu = !{!0}
94 !llvm.module.flags = !{!2}
95 !llvm.ident = !{!3}
96
97 !0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: Apple, sysroot: "/")
98 !1 = !DIFile(filename: "/tmp/code.cpp", directory: "/")
99 !2 = !{i32 2, !"Debug Info Version", i32 3}
100 !3 = !{!"clang"}
101 !4 = distinct !DISubprogram(name: "foo", linkageName: "_Z3fooi", scope: !5, file: !5, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !9)
102 !5 = !DIFile(filename: "/tmp/code.cpp", directory: "")
103 !6 = !DISubroutineType(types: !7)
104 !7 = !{!8, !8}
105 !8 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
106 !9 = !{!10}
107 !10 = !DILocalVariable(name: "x", arg: 1, scope: !4, file: !5, line: 1, type: !8)
108 !11 = !DILocation(line: 0, scope: !4)
109 !12 = !DILocation(line: 2, column: 11, scope: !4)
110
111 ...
112 ---
113 name: _Z3fooi
114 alignment: 4
115 exposesReturnsTwice: false
116 legalized: false
117 regBankSelected: false
118 selected: false
119 failedISel: false
120 tracksRegLiveness: true
121 hasWinCFI: false
122 noPhis: false
123 isSSA: true
124 noVRegs: false
125 hasFakeUses: false
126 callsEHReturn: false
127 callsUnwindInit: false
128 hasEHContTarget: false
129 hasEHScopes: false
130 hasEHFunclets: false
131 isOutlined: false
132 debugInstrRef: false
133 failsVerification: false
134 tracksDebugUserValues: false
135 registers:
136 - { id: 0, class: _, preferred-register: '', flags: [ ] }
137 - { id: 1, class: _, preferred-register: '', flags: [ ] }
138 - { id: 2, class: _, preferred-register: '', flags: [ ] }
139 - { id: 3, class: _, preferred-register: '', flags: [ ] }
140 liveins:
141 - { reg: '$w0', virtual-reg: '' }
142 frameInfo:
143 isFrameAddressTaken: false
144 isReturnAddressTaken: false
145 hasStackMap: false
146 hasPatchPoint: false
147 stackSize: 0
148 offsetAdjustment: 0
149 maxAlignment: 1
150 adjustsStack: false
151 hasCalls: false
152 stackProtector: ''
153 functionContext: ''
154 maxCallFrameSize: 4294967295
155 cvBytesOfCalleeSavedRegisters: 0
156 hasOpaqueSPAdjustment: false
157 hasVAStart: false
158 hasMustTailInVarArgFunc: false
159 hasTailCall: false
160 isCalleeSavedInfoValid: false
161 localFrameSize: 0
162 savePoint: ''
163 restorePoint: ''
164 fixedStack: []
165 stack: []
166 entry_values: []
167 callSites: []
168 debugValueSubstitutions: []
169 constants: []
170 machineFunctionInfo: {}
171 body: |
172 bb.1.entry:
173 liveins: $w0
174
175 %0:_(s32) = COPY $w0
176 %1:_(s32) = G_CONSTANT i32 1
177 %3:_(s32) = G_CONSTANT i32 0
178 DBG_VALUE %0(s32), $noreg, !10, !DIExpression(), debug-location !11
179 %2:_(s32) = nsw G_ADD %0, %1, debug-location !12
180 $w0 = COPY %3(s32)
181 RET_ReallyLR implicit $w0
182 )";
183 auto TM = createTargetMachine(Triple::normalize("aarch64--"), "", "");
184 if (!TM)
185 return;
186 MachineModuleInfo MMI(TM.get());
187 std::unique_ptr<Module> M = parseMIR(*TM, MIR, MMI, &C);
188 ASSERT_TRUE(M);
189
190 DroppedVariableStatsMIR Stats;
191 auto *MF = MMI.getMachineFunction(*M->getFunction("_Z3fooi"));
192 Stats.runBeforePass("Test", MF);
193
194 // This loop simulates an IR pass that drops debug information.
195 for (auto &MBB : *MF) {
196 for (auto &MI : MBB) {
197 if (MI.isDebugValueLike()) {
198 MI.eraseFromParent();
199 break;
200 }
201 }
202 for (auto &MI : MBB) {
203 auto *DbgLoc = MI.getDebugLoc().get();
204 if (DbgLoc) {
205 MI.eraseFromParent();
206 break;
207 }
208 }
209 break;
210 }
211
212 Stats.runAfterPass("Test", MF);
213 ASSERT_EQ(Stats.getPassDroppedVariables(), false);
214 }
215
216 // This test ensures that if a DBG_VALUE is dropped after an optimization pass,
217 // but an instruction that shares the same scope as the DBG_VALUE still exists,
218 // debug information is conisdered dropped.
219 TEST(DroppedVariableStatsMIR, DbgValLost) {
220 InitializeAllTargetInfos();
221 InitializeAllTargets();
222 InitializeAllTargetMCs();
223 PassInstrumentationCallbacks PIC;
224 PassInstrumentation PI(&PIC);
225
226 LLVMContext C;
227
228 const char *MIR =
229 R"(
230 --- |
231 ; ModuleID = '/tmp/test.ll'
232 source_filename = "/tmp/test.ll"
233 target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-Fn32"
234
235 define noundef range(i32 -2147483647, -2147483648) i32 @_Z3fooi(i32 noundef %x) local_unnamed_addr !dbg !4 {
236 entry:
237 #dbg_value(i32 %x, !10, !DIExpression(), !11)
238 %add = add nsw i32 %x, 1, !dbg !12
239 ret i32 0
240 }
241
242 !llvm.dbg.cu = !{!0}
243 !llvm.module.flags = !{!2}
244 !llvm.ident = !{!3}
245
246 !0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: Apple, sysroot: "/")
247 !1 = !DIFile(filename: "/tmp/code.cpp", directory: "/")
248 !2 = !{i32 2, !"Debug Info Version", i32 3}
249 !3 = !{!"clang"}
250 !4 = distinct !DISubprogram(name: "foo", linkageName: "_Z3fooi", scope: !5, file: !5, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !9)
251 !5 = !DIFile(filename: "/tmp/code.cpp", directory: "")
252 !6 = !DISubroutineType(types: !7)
253 !7 = !{!8, !8}
254 !8 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
255 !9 = !{!10}
256 !10 = !DILocalVariable(name: "x", arg: 1, scope: !4, file: !5, line: 1, type: !8)
257 !11 = !DILocation(line: 0, scope: !4)
258 !12 = !DILocation(line: 2, column: 11, scope: !4)
259
260 ...
261 ---
262 name: _Z3fooi
263 alignment: 4
264 exposesReturnsTwice: false
265 legalized: false
266 regBankSelected: false
267 selected: false
268 failedISel: false
269 tracksRegLiveness: true
270 hasWinCFI: false
271 noPhis: false
272 isSSA: true
273 noVRegs: false
274 hasFakeUses: false
275 callsEHReturn: false
276 callsUnwindInit: false
277 hasEHContTarget: false
278 hasEHScopes: false
279 hasEHFunclets: false
280 isOutlined: false
281 debugInstrRef: false
282 failsVerification: false
283 tracksDebugUserValues: false
284 registers:
285 - { id: 0, class: _, preferred-register: '', flags: [ ] }
286 - { id: 1, class: _, preferred-register: '', flags: [ ] }
287 - { id: 2, class: _, preferred-register: '', flags: [ ] }
288 - { id: 3, class: _, preferred-register: '', flags: [ ] }
289 liveins:
290 - { reg: '$w0', virtual-reg: '' }
291 frameInfo:
292 isFrameAddressTaken: false
293 isReturnAddressTaken: false
294 hasStackMap: false
295 hasPatchPoint: false
296 stackSize: 0
297 offsetAdjustment: 0
298 maxAlignment: 1
299 adjustsStack: false
300 hasCalls: false
301 stackProtector: ''
302 functionContext: ''
303 maxCallFrameSize: 4294967295
304 cvBytesOfCalleeSavedRegisters: 0
305 hasOpaqueSPAdjustment: false
306 hasVAStart: false
307 hasMustTailInVarArgFunc: false
308 hasTailCall: false
309 isCalleeSavedInfoValid: false
310 localFrameSize: 0
311 savePoint: ''
312 restorePoint: ''
313 fixedStack: []
314 stack: []
315 entry_values: []
316 callSites: []
317 debugValueSubstitutions: []
318 constants: []
319 machineFunctionInfo: {}
320 body: |
321 bb.1.entry:
322 liveins: $w0
323
324 %0:_(s32) = COPY $w0
325 %1:_(s32) = G_CONSTANT i32 1
326 %3:_(s32) = G_CONSTANT i32 0
327 DBG_VALUE %0(s32), $noreg, !10, !DIExpression(), debug-location !11
328 %2:_(s32) = nsw G_ADD %0, %1, debug-location !12
329 $w0 = COPY %3(s32)
330 RET_ReallyLR implicit $w0
331 )";
332 auto TM = createTargetMachine(Triple::normalize("aarch64--"), "", "");
333 if (!TM)
334 return;
335 MachineModuleInfo MMI(TM.get());
336 std::unique_ptr<Module> M = parseMIR(*TM, MIR, MMI, &C);
337 ASSERT_TRUE(M);
338
339 DroppedVariableStatsMIR Stats;
340 auto *MF = MMI.getMachineFunction(*M->getFunction("_Z3fooi"));
341 Stats.runBeforePass("Test", MF);
342
343 // This loop simulates an IR pass that drops debug information.
344 for (auto &MBB : *MF) {
345 for (auto &MI : MBB) {
346 if (MI.isDebugValueLike()) {
347 MI.eraseFromParent();
348 break;
349 }
350 }
351 break;
352 }
353
354 Stats.runAfterPass("Test", MF);
355 ASSERT_EQ(Stats.getPassDroppedVariables(), true);
356 }
357
358 // This test ensures that if a #dbg_value is dropped after an optimization pass,
359 // but an instruction that has an unrelated scope as the #dbg_value still
360 // exists, debug information is conisdered not dropped.
361 TEST(DroppedVariableStatsMIR, UnrelatedScopes) {
362 InitializeAllTargetInfos();
363 InitializeAllTargets();
364 InitializeAllTargetMCs();
365 PassInstrumentationCallbacks PIC;
366 PassInstrumentation PI(&PIC);
367
368 LLVMContext C;
369
370 const char *MIR =
371 R"(
372 --- |
373 ; ModuleID = '/tmp/test.ll'
374 source_filename = "/tmp/test.ll"
375 target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-Fn32"
376
377 define noundef range(i32 -2147483647, -2147483648) i32 @_Z3fooi(i32 noundef %x) local_unnamed_addr !dbg !4 {
378 entry:
379 #dbg_value(i32 %x, !10, !DIExpression(), !11)
380 %add = add nsw i32 %x, 1, !dbg !12
381 ret i32 0
382 }
383
384 !llvm.dbg.cu = !{!0}
385 !llvm.module.flags = !{!2}
386 !llvm.ident = !{!3}
387
388 !0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: Apple, sysroot: "/")
389 !1 = !DIFile(filename: "/tmp/code.cpp", directory: "/")
390 !2 = !{i32 2, !"Debug Info Version", i32 3}
391 !3 = !{!"clang"}
392 !4 = distinct !DISubprogram(name: "foo", linkageName: "_Z3fooi", scope: !5, file: !5, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !9)
393 !5 = !DIFile(filename: "/tmp/code.cpp", directory: "")
394 !6 = !DISubroutineType(types: !7)
395 !7 = !{!8, !8}
396 !8 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
397 !9 = !{!10}
398 !10 = !DILocalVariable(name: "x", arg: 1, scope: !4, file: !5, line: 1, type: !8)
399 !11 = !DILocation(line: 0, scope: !4)
400 !12 = !DILocation(line: 2, column: 11, scope: !13)
401 !13 = distinct !DISubprogram(name: "bar", linkageName: "_Z3bari", scope: !5, file: !5, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !9)
402
403 ...
404 ---
405 name: _Z3fooi
406 alignment: 4
407 exposesReturnsTwice: false
408 legalized: false
409 regBankSelected: false
410 selected: false
411 failedISel: false
412 tracksRegLiveness: true
413 hasWinCFI: false
414 noPhis: false
415 isSSA: true
416 noVRegs: false
417 hasFakeUses: false
418 callsEHReturn: false
419 callsUnwindInit: false
420 hasEHContTarget: false
421 hasEHScopes: false
422 hasEHFunclets: false
423 isOutlined: false
424 debugInstrRef: false
425 failsVerification: false
426 tracksDebugUserValues: false
427 registers:
428 - { id: 0, class: _, preferred-register: '', flags: [ ] }
429 - { id: 1, class: _, preferred-register: '', flags: [ ] }
430 - { id: 2, class: _, preferred-register: '', flags: [ ] }
431 - { id: 3, class: _, preferred-register: '', flags: [ ] }
432 liveins:
433 - { reg: '$w0', virtual-reg: '' }
434 frameInfo:
435 isFrameAddressTaken: false
436 isReturnAddressTaken: false
437 hasStackMap: false
438 hasPatchPoint: false
439 stackSize: 0
440 offsetAdjustment: 0
441 maxAlignment: 1
442 adjustsStack: false
443 hasCalls: false
444 stackProtector: ''
445 functionContext: ''
446 maxCallFrameSize: 4294967295
447 cvBytesOfCalleeSavedRegisters: 0
448 hasOpaqueSPAdjustment: false
449 hasVAStart: false
450 hasMustTailInVarArgFunc: false
451 hasTailCall: false
452 isCalleeSavedInfoValid: false
453 localFrameSize: 0
454 savePoint: ''
455 restorePoint: ''
456 fixedStack: []
457 stack: []
458 entry_values: []
459 callSites: []
460 debugValueSubstitutions: []
461 constants: []
462 machineFunctionInfo: {}
463 body: |
464 bb.1.entry:
465 liveins: $w0
466
467 %0:_(s32) = COPY $w0
468 %1:_(s32) = G_CONSTANT i32 1
469 %3:_(s32) = G_CONSTANT i32 0
470 DBG_VALUE %0(s32), $noreg, !10, !DIExpression(), debug-location !11
471 %2:_(s32) = nsw G_ADD %0, %1, debug-location !12
472 $w0 = COPY %3(s32)
473 RET_ReallyLR implicit $w0
474 )";
475 auto TM = createTargetMachine(Triple::normalize("aarch64--"), "", "");
476 if (!TM)
477 return;
478 MachineModuleInfo MMI(TM.get());
479 std::unique_ptr<Module> M = parseMIR(*TM, MIR, MMI, &C);
480 ASSERT_TRUE(M);
481
482 DroppedVariableStatsMIR Stats;
483 auto *MF = MMI.getMachineFunction(*M->getFunction("_Z3fooi"));
484 Stats.runBeforePass("Test", MF);
485
486 // This loop simulates an IR pass that drops debug information.
487 for (auto &MBB : *MF) {
488 for (auto &MI : MBB) {
489 if (MI.isDebugValueLike()) {
490 MI.eraseFromParent();
491 break;
492 }
493 }
494 break;
495 }
496
497 Stats.runAfterPass("Test", MF);
498 ASSERT_EQ(Stats.getPassDroppedVariables(), false);
499 }
500
501 // This test ensures that if a #dbg_value is dropped after an optimization pass,
502 // but an instruction that has a scope which is a child of the #dbg_value scope
503 // still exists, debug information is conisdered dropped.
504 TEST(DroppedVariableStatsMIR, ChildScopes) {
505 InitializeAllTargetInfos();
506 InitializeAllTargets();
507 InitializeAllTargetMCs();
508 PassInstrumentationCallbacks PIC;
509 PassInstrumentation PI(&PIC);
510
511 LLVMContext C;
512
513 const char *MIR =
514 R"(
515 --- |
516 ; ModuleID = '/tmp/test.ll'
517 source_filename = "/tmp/test.ll"
518 target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-Fn32"
519
520 define noundef range(i32 -2147483647, -2147483648) i32 @_Z3fooi(i32 noundef %x) local_unnamed_addr !dbg !4 {
521 entry:
522 #dbg_value(i32 %x, !10, !DIExpression(), !11)
523 %add = add nsw i32 %x, 1, !dbg !12
524 ret i32 0
525 }
526
527 !llvm.dbg.cu = !{!0}
528 !llvm.module.flags = !{!2}
529 !llvm.ident = !{!3}
530
531 !0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: Apple, sysroot: "/")
532 !1 = !DIFile(filename: "/tmp/code.cpp", directory: "/")
533 !2 = !{i32 2, !"Debug Info Version", i32 3}
534 !3 = !{!"clang"}
535 !4 = distinct !DISubprogram(name: "foo", linkageName: "_Z3fooi", scope: !5, file: !5, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !9)
536 !5 = !DIFile(filename: "/tmp/code.cpp", directory: "")
537 !6 = !DISubroutineType(types: !7)
538 !7 = !{!8, !8}
539 !8 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
540 !9 = !{!10}
541 !10 = !DILocalVariable(name: "x", arg: 1, scope: !4, file: !5, line: 1, type: !8)
542 !11 = !DILocation(line: 0, scope: !4)
543 !12 = !DILocation(line: 2, column: 11, scope: !13)
544 !13 = distinct !DILexicalBlock(scope: !4, file: !5, line: 10, column: 28)
545
546 ...
547 ---
548 name: _Z3fooi
549 alignment: 4
550 exposesReturnsTwice: false
551 legalized: false
552 regBankSelected: false
553 selected: false
554 failedISel: false
555 tracksRegLiveness: true
556 hasWinCFI: false
557 noPhis: false
558 isSSA: true
559 noVRegs: false
560 hasFakeUses: false
561 callsEHReturn: false
562 callsUnwindInit: false
563 hasEHContTarget: false
564 hasEHScopes: false
565 hasEHFunclets: false
566 isOutlined: false
567 debugInstrRef: false
568 failsVerification: false
569 tracksDebugUserValues: false
570 registers:
571 - { id: 0, class: _, preferred-register: '', flags: [ ] }
572 - { id: 1, class: _, preferred-register: '', flags: [ ] }
573 - { id: 2, class: _, preferred-register: '', flags: [ ] }
574 - { id: 3, class: _, preferred-register: '', flags: [ ] }
575 liveins:
576 - { reg: '$w0', virtual-reg: '' }
577 frameInfo:
578 isFrameAddressTaken: false
579 isReturnAddressTaken: false
580 hasStackMap: false
581 hasPatchPoint: false
582 stackSize: 0
583 offsetAdjustment: 0
584 maxAlignment: 1
585 adjustsStack: false
586 hasCalls: false
587 stackProtector: ''
588 functionContext: ''
589 maxCallFrameSize: 4294967295
590 cvBytesOfCalleeSavedRegisters: 0
591 hasOpaqueSPAdjustment: false
592 hasVAStart: false
593 hasMustTailInVarArgFunc: false
594 hasTailCall: false
595 isCalleeSavedInfoValid: false
596 localFrameSize: 0
597 savePoint: ''
598 restorePoint: ''
599 fixedStack: []
600 stack: []
601 entry_values: []
602 callSites: []
603 debugValueSubstitutions: []
604 constants: []
605 machineFunctionInfo: {}
606 body: |
607 bb.1.entry:
608 liveins: $w0
609
610 %0:_(s32) = COPY $w0
611 %1:_(s32) = G_CONSTANT i32 1
612 %3:_(s32) = G_CONSTANT i32 0
613 DBG_VALUE %0(s32), $noreg, !10, !DIExpression(), debug-location !11
614 %2:_(s32) = nsw G_ADD %0, %1, debug-location !12
615 $w0 = COPY %3(s32)
616 RET_ReallyLR implicit $w0
617 )";
618 auto TM = createTargetMachine(Triple::normalize("aarch64--"), "", "");
619 if (!TM)
620 return;
621 MachineModuleInfo MMI(TM.get());
622 std::unique_ptr<Module> M = parseMIR(*TM, MIR, MMI, &C);
623 ASSERT_TRUE(M);
624
625 DroppedVariableStatsMIR Stats;
626 auto *MF = MMI.getMachineFunction(*M->getFunction("_Z3fooi"));
627 Stats.runBeforePass("Test", MF);
628
629 // This loop simulates an IR pass that drops debug information.
630 for (auto &MBB : *MF) {
631 for (auto &MI : MBB) {
632 if (MI.isDebugValueLike()) {
633 MI.eraseFromParent();
634 break;
635 }
636 }
637 break;
638 }
639
640 Stats.runAfterPass("Test", MF);
641 ASSERT_EQ(Stats.getPassDroppedVariables(), true);
642 }
643
644 // This test ensures that if a DBG_VALUE is dropped after an optimization pass,
645 // but an instruction that has a scope which is a child of the DBG_VALUE scope
646 // still exists, and the DBG_VALUE is inlined at another location, debug
647 // information is conisdered not dropped.
648 TEST(DroppedVariableStatsMIR, InlinedAt) {
649 InitializeAllTargetInfos();
650 InitializeAllTargets();
651 InitializeAllTargetMCs();
652 PassInstrumentationCallbacks PIC;
653 PassInstrumentation PI(&PIC);
654
655 LLVMContext C;
656
657 const char *MIR =
658 R"(
659 --- |
660 ; ModuleID = '/tmp/test.ll'
661 source_filename = "/tmp/test.ll"
662 target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-Fn32"
663
664 define noundef range(i32 -2147483647, -2147483648) i32 @_Z3fooi(i32 noundef %x) local_unnamed_addr !dbg !4 {
665 entry:
666 #dbg_value(i32 %x, !10, !DIExpression(), !11)
667 %add = add nsw i32 %x, 1, !dbg !12
668 ret i32 0
669 }
670
671 !llvm.dbg.cu = !{!0}
672 !llvm.module.flags = !{!2}
673 !llvm.ident = !{!3}
674
675 !0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: Apple, sysroot: "/")
676 !1 = !DIFile(filename: "/tmp/code.cpp", directory: "/")
677 !2 = !{i32 2, !"Debug Info Version", i32 3}
678 !3 = !{!"clang"}
679 !4 = distinct !DISubprogram(name: "foo", linkageName: "_Z3fooi", scope: !5, file: !5, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !9)
680 !5 = !DIFile(filename: "/tmp/code.cpp", directory: "")
681 !6 = !DISubroutineType(types: !7)
682 !7 = !{!8, !8}
683 !8 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
684 !9 = !{!10}
685 !10 = !DILocalVariable(name: "x", arg: 1, scope: !4, file: !5, line: 1, type: !8)
686 !11 = !DILocation(line: 0, scope: !4, inlinedAt: !14)
687 !12 = !DILocation(line: 2, column: 11, scope: !13)
688 !13 = distinct !DILexicalBlock(scope: !4, file: !5, line: 10, column: 28)
689 !14 = !DILocation(line: 3, column: 2, scope: !4)
690
691 ...
692 ---
693 name: _Z3fooi
694 alignment: 4
695 exposesReturnsTwice: false
696 legalized: false
697 regBankSelected: false
698 selected: false
699 failedISel: false
700 tracksRegLiveness: true
701 hasWinCFI: false
702 noPhis: false
703 isSSA: true
704 noVRegs: false
705 hasFakeUses: false
706 callsEHReturn: false
707 callsUnwindInit: false
708 hasEHContTarget: false
709 hasEHScopes: false
710 hasEHFunclets: false
711 isOutlined: false
712 debugInstrRef: false
713 failsVerification: false
714 tracksDebugUserValues: false
715 registers:
716 - { id: 0, class: _, preferred-register: '', flags: [ ] }
717 - { id: 1, class: _, preferred-register: '', flags: [ ] }
718 - { id: 2, class: _, preferred-register: '', flags: [ ] }
719 - { id: 3, class: _, preferred-register: '', flags: [ ] }
720 liveins:
721 - { reg: '$w0', virtual-reg: '' }
722 frameInfo:
723 isFrameAddressTaken: false
724 isReturnAddressTaken: false
725 hasStackMap: false
726 hasPatchPoint: false
727 stackSize: 0
728 offsetAdjustment: 0
729 maxAlignment: 1
730 adjustsStack: false
731 hasCalls: false
732 stackProtector: ''
733 functionContext: ''
734 maxCallFrameSize: 4294967295
735 cvBytesOfCalleeSavedRegisters: 0
736 hasOpaqueSPAdjustment: false
737 hasVAStart: false
738 hasMustTailInVarArgFunc: false
739 hasTailCall: false
740 isCalleeSavedInfoValid: false
741 localFrameSize: 0
742 savePoint: ''
743 restorePoint: ''
744 fixedStack: []
745 stack: []
746 entry_values: []
747 callSites: []
748 debugValueSubstitutions: []
749 constants: []
750 machineFunctionInfo: {}
751 body: |
752 bb.1.entry:
753 liveins: $w0
754
755 %0:_(s32) = COPY $w0
756 %1:_(s32) = G_CONSTANT i32 1
757 %3:_(s32) = G_CONSTANT i32 0
758 DBG_VALUE %0(s32), $noreg, !10, !DIExpression(), debug-location !11
759 %2:_(s32) = nsw G_ADD %0, %1, debug-location !12
760 $w0 = COPY %3(s32)
761 RET_ReallyLR implicit $w0
762 )";
763 auto TM = createTargetMachine(Triple::normalize("aarch64--"), "", "");
764 if (!TM)
765 return;
766 MachineModuleInfo MMI(TM.get());
767 std::unique_ptr<Module> M = parseMIR(*TM, MIR, MMI, &C);
768 ASSERT_TRUE(M);
769
770 DroppedVariableStatsMIR Stats;
771 auto *MF = MMI.getMachineFunction(*M->getFunction("_Z3fooi"));
772 Stats.runBeforePass("Test", MF);
773
774 // This loop simulates an IR pass that drops debug information.
775 for (auto &MBB : *MF) {
776 for (auto &MI : MBB) {
777 if (MI.isDebugValueLike()) {
778 MI.eraseFromParent();
779 break;
780 }
781 }
782 break;
783 }
784
785 Stats.runAfterPass("Test", MF);
786 ASSERT_EQ(Stats.getPassDroppedVariables(), false);
787 }
788
789 // This test ensures that if a DBG_VALUE is dropped after an optimization pass,
790 // but an instruction that has a scope which is a child of the DBG_VALUE scope
791 // still exists, and the DBG_VALUE and the instruction are inlined at another
792 // location, debug information is conisdered dropped.
793 TEST(DroppedVariableStatsMIR, InlinedAtShared) {
794 InitializeAllTargetInfos();
795 InitializeAllTargets();
796 InitializeAllTargetMCs();
797 PassInstrumentationCallbacks PIC;
798 PassInstrumentation PI(&PIC);
799
800 LLVMContext C;
801
802 const char *MIR =
803 R"(
804 --- |
805 ; ModuleID = '/tmp/test.ll'
806 source_filename = "/tmp/test.ll"
807 target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-Fn32"
808
809 define noundef range(i32 -2147483647, -2147483648) i32 @_Z3fooi(i32 noundef %x) local_unnamed_addr !dbg !4 {
810 entry:
811 #dbg_value(i32 %x, !10, !DIExpression(), !11)
812 %add = add nsw i32 %x, 1, !dbg !12
813 ret i32 0
814 }
815
816 !llvm.dbg.cu = !{!0}
817 !llvm.module.flags = !{!2}
818 !llvm.ident = !{!3}
819
820 !0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: Apple, sysroot: "/")
821 !1 = !DIFile(filename: "/tmp/code.cpp", directory: "/")
822 !2 = !{i32 2, !"Debug Info Version", i32 3}
823 !3 = !{!"clang"}
824 !4 = distinct !DISubprogram(name: "foo", linkageName: "_Z3fooi", scope: !5, file: !5, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !9)
825 !5 = !DIFile(filename: "/tmp/code.cpp", directory: "")
826 !6 = !DISubroutineType(types: !7)
827 !7 = !{!8, !8}
828 !8 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
829 !9 = !{!10}
830 !10 = !DILocalVariable(name: "x", arg: 1, scope: !4, file: !5, line: 1, type: !8)
831 !11 = !DILocation(line: 0, scope: !4, inlinedAt: !14)
832 !12 = !DILocation(line: 2, column: 11, scope: !13, inlinedAt: !14)
833 !13 = distinct !DILexicalBlock(scope: !4, file: !5, line: 10, column: 28)
834 !14 = !DILocation(line: 3, column: 2, scope: !4)
835
836 ...
837 ---
838 name: _Z3fooi
839 alignment: 4
840 exposesReturnsTwice: false
841 legalized: false
842 regBankSelected: false
843 selected: false
844 failedISel: false
845 tracksRegLiveness: true
846 hasWinCFI: false
847 noPhis: false
848 isSSA: true
849 noVRegs: false
850 hasFakeUses: false
851 callsEHReturn: false
852 callsUnwindInit: false
853 hasEHContTarget: false
854 hasEHScopes: false
855 hasEHFunclets: false
856 isOutlined: false
857 debugInstrRef: false
858 failsVerification: false
859 tracksDebugUserValues: false
860 registers:
861 - { id: 0, class: _, preferred-register: '', flags: [ ] }
862 - { id: 1, class: _, preferred-register: '', flags: [ ] }
863 - { id: 2, class: _, preferred-register: '', flags: [ ] }
864 - { id: 3, class: _, preferred-register: '', flags: [ ] }
865 liveins:
866 - { reg: '$w0', virtual-reg: '' }
867 frameInfo:
868 isFrameAddressTaken: false
869 isReturnAddressTaken: false
870 hasStackMap: false
871 hasPatchPoint: false
872 stackSize: 0
873 offsetAdjustment: 0
874 maxAlignment: 1
875 adjustsStack: false
876 hasCalls: false
877 stackProtector: ''
878 functionContext: ''
879 maxCallFrameSize: 4294967295
880 cvBytesOfCalleeSavedRegisters: 0
881 hasOpaqueSPAdjustment: false
882 hasVAStart: false
883 hasMustTailInVarArgFunc: false
884 hasTailCall: false
885 isCalleeSavedInfoValid: false
886 localFrameSize: 0
887 savePoint: ''
888 restorePoint: ''
889 fixedStack: []
890 stack: []
891 entry_values: []
892 callSites: []
893 debugValueSubstitutions: []
894 constants: []
895 machineFunctionInfo: {}
896 body: |
897 bb.1.entry:
898 liveins: $w0
899
900 %0:_(s32) = COPY $w0
901 %1:_(s32) = G_CONSTANT i32 1
902 %3:_(s32) = G_CONSTANT i32 0
903 DBG_VALUE %0(s32), $noreg, !10, !DIExpression(), debug-location !11
904 %2:_(s32) = nsw G_ADD %0, %1, debug-location !12
905 $w0 = COPY %3(s32)
906 RET_ReallyLR implicit $w0
907 )";
908 auto TM = createTargetMachine(Triple::normalize("aarch64--"), "", "");
909 if (!TM)
910 return;
911 MachineModuleInfo MMI(TM.get());
912 std::unique_ptr<Module> M = parseMIR(*TM, MIR, MMI, &C);
913 ASSERT_TRUE(M);
914
915 DroppedVariableStatsMIR Stats;
916 auto *MF = MMI.getMachineFunction(*M->getFunction("_Z3fooi"));
917 Stats.runBeforePass("Test", MF);
918
919 // This loop simulates an IR pass that drops debug information.
920 for (auto &MBB : *MF) {
921 for (auto &MI : MBB) {
922 if (MI.isDebugValueLike()) {
923 MI.eraseFromParent();
924 break;
925 }
926 }
927 break;
928 }
929
930 Stats.runAfterPass("Test", MF);
931 ASSERT_EQ(Stats.getPassDroppedVariables(), true);
932 }
933
934 // This test ensures that if a DBG_VALUE is dropped after an optimization pass,
935 // but an instruction that has a scope which is a child of the DBG_VALUE scope
936 // still exists, and the instruction is inlined at a location that is the
937 // DBG_VALUE's inlined at location, debug information is conisdered dropped.
938 TEST(DroppedVariableStatsMIR, InlinedAtChild) {
939 InitializeAllTargetInfos();
940 InitializeAllTargets();
941 InitializeAllTargetMCs();
942 PassInstrumentationCallbacks PIC;
943 PassInstrumentation PI(&PIC);
944
945 LLVMContext C;
946
947 const char *MIR =
948 R"(
949 --- |
950 ; ModuleID = '/tmp/test.ll'
951 source_filename = "/tmp/test.ll"
952 target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-Fn32"
953
954 define noundef range(i32 -2147483647, -2147483648) i32 @_Z3fooi(i32 noundef %x) local_unnamed_addr !dbg !4 {
955 entry:
956 #dbg_value(i32 %x, !10, !DIExpression(), !11)
957 %add = add nsw i32 %x, 1, !dbg !12
958 ret i32 0
959 }
960
961 !llvm.dbg.cu = !{!0}
962 !llvm.module.flags = !{!2}
963 !llvm.ident = !{!3}
964
965 !0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: Apple, sysroot: "/")
966 !1 = !DIFile(filename: "/tmp/code.cpp", directory: "/")
967 !2 = !{i32 2, !"Debug Info Version", i32 3}
968 !3 = !{!"clang"}
969 !4 = distinct !DISubprogram(name: "foo", linkageName: "_Z3fooi", scope: !5, file: !5, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !9)
970 !5 = !DIFile(filename: "/tmp/code.cpp", directory: "")
971 !6 = !DISubroutineType(types: !7)
972 !7 = !{!8, !8}
973 !8 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
974 !9 = !{!10}
975 !10 = !DILocalVariable(name: "x", arg: 1, scope: !4, file: !5, line: 1, type: !8)
976 !11 = !DILocation(line: 0, scope: !4, inlinedAt: !14)
977 !12 = !DILocation(line: 2, column: 11, scope: !13, inlinedAt: !15)
978 !13 = distinct !DILexicalBlock(scope: !4, file: !5, line: 10, column: 28)
979 !14 = !DILocation(line: 3, column: 2, scope: !4)
980 !15 = !DILocation(line: 4, column: 5, scope: !13, inlinedAt: !14)
981
982 ...
983 ---
984 name: _Z3fooi
985 alignment: 4
986 exposesReturnsTwice: false
987 legalized: false
988 regBankSelected: false
989 selected: false
990 failedISel: false
991 tracksRegLiveness: true
992 hasWinCFI: false
993 noPhis: false
994 isSSA: true
995 noVRegs: false
996 hasFakeUses: false
997 callsEHReturn: false
998 callsUnwindInit: false
999 hasEHContTarget: false
1000 hasEHScopes: false
1001 hasEHFunclets: false
1002 isOutlined: false
1003 debugInstrRef: false
1004 failsVerification: false
1005 tracksDebugUserValues: false
1006 registers:
1007 - { id: 0, class: _, preferred-register: '', flags: [ ] }
1008 - { id: 1, class: _, preferred-register: '', flags: [ ] }
1009 - { id: 2, class: _, preferred-register: '', flags: [ ] }
1010 - { id: 3, class: _, preferred-register: '', flags: [ ] }
1011 liveins:
1012 - { reg: '$w0', virtual-reg: '' }
1013 frameInfo:
1014 isFrameAddressTaken: false
1015 isReturnAddressTaken: false
1016 hasStackMap: false
1017 hasPatchPoint: false
1018 stackSize: 0
1019 offsetAdjustment: 0
1020 maxAlignment: 1
1021 adjustsStack: false
1022 hasCalls: false
1023 stackProtector: ''
1024 functionContext: ''
1025 maxCallFrameSize: 4294967295
1026 cvBytesOfCalleeSavedRegisters: 0
1027 hasOpaqueSPAdjustment: false
1028 hasVAStart: false
1029 hasMustTailInVarArgFunc: false
1030 hasTailCall: false
1031 isCalleeSavedInfoValid: false
1032 localFrameSize: 0
1033 savePoint: ''
1034 restorePoint: ''
1035 fixedStack: []
1036 stack: []
1037 entry_values: []
1038 callSites: []
1039 debugValueSubstitutions: []
1040 constants: []
1041 machineFunctionInfo: {}
1042 body: |
1043 bb.1.entry:
1044 liveins: $w0
1045
1046 %0:_(s32) = COPY $w0
1047 %1:_(s32) = G_CONSTANT i32 1
1048 %3:_(s32) = G_CONSTANT i32 0
1049 DBG_VALUE %0(s32), $noreg, !10, !DIExpression(), debug-location !11
1050 %2:_(s32) = nsw G_ADD %0, %1, debug-location !12
1051 $w0 = COPY %3(s32)
1052 RET_ReallyLR implicit $w0
1053 )";
1054 auto TM = createTargetMachine(Triple::normalize("aarch64--"), "", "");
1055 if (!TM)
1056 return;
1057 MachineModuleInfo MMI(TM.get());
1058 std::unique_ptr<Module> M = parseMIR(*TM, MIR, MMI, &C);
1059 ASSERT_TRUE(M);
1060
1061 DroppedVariableStatsMIR Stats;
1062 auto *MF = MMI.getMachineFunction(*M->getFunction("_Z3fooi"));
1063 Stats.runBeforePass("Test", MF);
1064
1065 // This loop simulates an IR pass that drops debug information.
1066 for (auto &MBB : *MF) {
1067 for (auto &MI : MBB) {
1068 if (MI.isDebugValueLike()) {
1069 MI.eraseFromParent();
1070 break;
1071 }
1072 }
1073 break;
1074 }
1075
1076 Stats.runAfterPass("Test", MF);
1077 ASSERT_EQ(Stats.getPassDroppedVariables(), true);
1078 }
1079
1080 } // end anonymous namespace