Skip to content

Commit ca877df

Browse files
committed
Added counter and deleter passes for debug info
1 parent 782a91e commit ca877df

File tree

8 files changed

+164
-0
lines changed

8 files changed

+164
-0
lines changed

foo_00.ll

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
; ModuleID = '/home/ana-marija/Documents/foo.c'
2+
source_filename = "/home/ana-marija/Documents/foo.c"
3+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
4+
target triple = "x86_64-unknown-linux-gnu"
5+
6+
; Function Attrs: noinline nounwind optnone uwtable
7+
define dso_local i32 @foo() #0 !dbg !10 {
8+
%1 = alloca i32, align 4
9+
%2 = alloca i32, align 4
10+
%3 = alloca i32, align 4
11+
#dbg_declare(ptr %1, !16, !DIExpression(), !17)
12+
store i32 21, ptr %1, align 4, !dbg !17
13+
#dbg_declare(ptr %2, !18, !DIExpression(), !19)
14+
store i32 22, ptr %2, align 4, !dbg !19
15+
#dbg_declare(ptr %3, !20, !DIExpression(), !22)
16+
store i32 23, ptr %3, align 4, !dbg !22
17+
%4 = load i32, ptr %1, align 4, !dbg !23
18+
store i32 %4, ptr %3, align 4, !dbg !24
19+
%5 = load i32, ptr %2, align 4, !dbg !25
20+
store i32 %5, ptr %1, align 4, !dbg !26
21+
%6 = load i32, ptr %1, align 4, !dbg !27
22+
%7 = load i32, ptr %2, align 4, !dbg !28
23+
%8 = add nsw i32 %6, %7, !dbg !29
24+
ret i32 %8, !dbg !30
25+
}
26+
27+
; Function Attrs: noinline nounwind optnone uwtable
28+
define dso_local i32 @main() #0 !dbg !31 {
29+
%1 = alloca i32, align 4
30+
%2 = alloca i32, align 4
31+
store i32 0, ptr %1, align 4
32+
#dbg_declare(ptr %2, !32, !DIExpression(), !33)
33+
%3 = call i32 @foo(), !dbg !34
34+
store i32 %3, ptr %2, align 4, !dbg !33
35+
%4 = load i32, ptr %2, align 4, !dbg !35
36+
ret i32 %4, !dbg !36
37+
}
38+
39+
attributes #0 = { noinline nounwind optnone uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
40+
41+
!llvm.dbg.cu = !{!0}
42+
!llvm.module.flags = !{!2, !3, !4, !5, !6, !7, !8}
43+
!llvm.ident = !{!9}
44+
45+
!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang version 22.0.0git (https://github.com/anamaoh/llvm-project.git 782a91e1fc94d9c82495f60afc5ed5edd72de776)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
46+
!1 = !DIFile(filename: "/home/ana-marija/Documents/foo.c", directory: "/home/ana-marija/Desktop/LLVM/llvm-project", checksumkind: CSK_MD5, checksum: "22bc4641d6e8e63df371a758d86cafff")
47+
!2 = !{i32 7, !"Dwarf Version", i32 5}
48+
!3 = !{i32 2, !"Debug Info Version", i32 3}
49+
!4 = !{i32 1, !"wchar_size", i32 4}
50+
!5 = !{i32 8, !"PIC Level", i32 2}
51+
!6 = !{i32 7, !"PIE Level", i32 2}
52+
!7 = !{i32 7, !"uwtable", i32 2}
53+
!8 = !{i32 7, !"frame-pointer", i32 2}
54+
!9 = !{!"clang version 22.0.0git (https://github.com/anamaoh/llvm-project.git 782a91e1fc94d9c82495f60afc5ed5edd72de776)"}
55+
!10 = distinct !DISubprogram(name: "foo", scope: !11, file: !11, line: 1, type: !12, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !15)
56+
!11 = !DIFile(filename: "Documents/foo.c", directory: "/home/ana-marija", checksumkind: CSK_MD5, checksum: "22bc4641d6e8e63df371a758d86cafff")
57+
!12 = !DISubroutineType(types: !13)
58+
!13 = !{!14}
59+
!14 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
60+
!15 = !{}
61+
!16 = !DILocalVariable(name: "X", scope: !10, file: !11, line: 2, type: !14)
62+
!17 = !DILocation(line: 2, column: 6, scope: !10)
63+
!18 = !DILocalVariable(name: "Y", scope: !10, file: !11, line: 3, type: !14)
64+
!19 = !DILocation(line: 3, column: 6, scope: !10)
65+
!20 = !DILocalVariable(name: "Z", scope: !21, file: !11, line: 5, type: !14)
66+
!21 = distinct !DILexicalBlock(scope: !10, file: !11, line: 4, column: 2)
67+
!22 = !DILocation(line: 5, column: 8, scope: !21)
68+
!23 = !DILocation(line: 6, column: 8, scope: !21)
69+
!24 = !DILocation(line: 6, column: 6, scope: !21)
70+
!25 = !DILocation(line: 8, column: 6, scope: !10)
71+
!26 = !DILocation(line: 8, column: 4, scope: !10)
72+
!27 = !DILocation(line: 9, column: 9, scope: !10)
73+
!28 = !DILocation(line: 9, column: 13, scope: !10)
74+
!29 = !DILocation(line: 9, column: 11, scope: !10)
75+
!30 = !DILocation(line: 9, column: 2, scope: !10)
76+
!31 = distinct !DISubprogram(name: "main", scope: !11, file: !11, line: 12, type: !12, scopeLine: 12, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !15)
77+
!32 = !DILocalVariable(name: "A", scope: !31, file: !11, line: 13, type: !14)
78+
!33 = !DILocation(line: 13, column: 6, scope: !31)
79+
!34 = !DILocation(line: 13, column: 10, scope: !31)
80+
!35 = !DILocation(line: 14, column: 9, scope: !31)
81+
!36 = !DILocation(line: 14, column: 2, scope: !31)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef LLVM_TRANSFORMS_UTILS_DEBUG_RECORD_COUNTER_H
2+
#define LLVM_TRANSFORMS_UTILS_DEBUG_RECORD_COUNTER_H
3+
4+
#include "llvm/IR/PassManager.h"
5+
6+
namespace llvm {
7+
8+
class DebugRecordCounterPass : public PassInfoMixin<DebugRecordCounterPass> {
9+
public:
10+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
11+
};
12+
13+
} // namespace llvm
14+
15+
#endif // LLVM_TRANSFORMS_UTILS_DEBUG_RECORD_COUNTER_H
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef LLVM_TRANSFORMS_UTILS_DEBUG_RECORD_DELETER_H
2+
#define LLVM_TRANSFORMS_UTILS_DEBUG_RECORD_DELETER_H
3+
4+
#include "llvm/IR/PassManager.h"
5+
6+
namespace llvm {
7+
8+
class DebugRecordDeleterPass : public PassInfoMixin<DebugRecordDeleterPass> {
9+
public:
10+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
11+
};
12+
13+
} // namespace llvm
14+
15+
#endif // LLVM_TRANSFORMS_UTILS_DEBUG_RECORD_DELETER_H

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,8 @@
350350
#include "llvm/Transforms/Utils/CountVisits.h"
351351
#include "llvm/Transforms/Utils/DXILUpgrade.h"
352352
#include "llvm/Transforms/Utils/Debugify.h"
353+
#include "llvm/Transforms/Utils/DebugRecordCounter.h"
354+
#include "llvm/Transforms/Utils/DebugRecordDeleter.h"
353355
#include "llvm/Transforms/Utils/DeclareRuntimeLibcalls.h"
354356
#include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
355357
#include "llvm/Transforms/Utils/FixIrreducible.h"

llvm/lib/Passes/PassRegistry.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ MODULE_ALIAS_ANALYSIS("globals-aa", GlobalsAA())
4949
#ifndef MODULE_PASS
5050
#define MODULE_PASS(NAME, CREATE_PASS)
5151
#endif
52+
MODULE_PASS("dbg-counter", DebugRecordCounterPass())
53+
MODULE_PASS("dbg-deleter", DebugRecordDeleterPass())
5254
MODULE_PASS("always-inline", AlwaysInlinerPass())
5355
MODULE_PASS("annotation2metadata", Annotation2MetadataPass())
5456
MODULE_PASS("assign-guid", AssignGUIDPass())

llvm/lib/Transforms/Utils/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ add_llvm_component_library(LLVMTransformUtils
2020
CtorUtils.cpp
2121
CountVisits.cpp
2222
Debugify.cpp
23+
DebugRecordCounter.cpp
24+
DebugRecordDeleter.cpp
2325
DebugSSAUpdater.cpp
2426
DeclareRuntimeLibcalls.cpp
2527
DemoteRegToStack.cpp
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "llvm/Transforms/Utils/DebugRecordCounter.h"
2+
#include "llvm/IR/Function.h"
3+
#include "llvm/IR/Module.h"
4+
5+
using namespace llvm;
6+
7+
// build/bin/opt -disable-output /home/ana-marija/Documents/foo_00.ll -passes=dbg-counter
8+
PreservedAnalyses DebugRecordCounterPass::run(Module &M, ModuleAnalysisManager &AM) {
9+
10+
int dbg_values, dbg_declares, dbg_assigns;
11+
12+
for (Function &F : M) {
13+
errs() << "Function: " << F.getName() << "\n";
14+
15+
dbg_assigns = dbg_declares = dbg_values = 0;
16+
17+
for(BasicBlock &BB : F) {
18+
for(Instruction &I : BB) {
19+
for(DbgVariableRecord &DVR : filterDbgVars(I.getDbgRecordRange())) {
20+
if(DVR.isDbgDeclare()) dbg_declares++;
21+
if(DVR.isDbgValue()) dbg_values++;
22+
if(DVR.isDbgAssign()) dbg_assigns++;
23+
}
24+
}
25+
}
26+
errs() << "\t#dbg_value: " << dbg_values << "\n";
27+
errs() << "\t#dbg_declare: " << dbg_declares << "\n";
28+
errs() << "\t#dbg_assign: " << dbg_assigns << "\n";
29+
}
30+
return PreservedAnalyses::all();
31+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "llvm/Transforms/Utils/DebugRecordDeleter.h"
2+
#include "llvm/IR/Function.h"
3+
#include "llvm/IR/Module.h"
4+
#include "llvm/IR/DebugInfo.h"
5+
#include "llvm/IR/DebugInfoMetadata.h"
6+
7+
using namespace llvm;
8+
9+
// build/bin/opt -S /home/ana-marija/Documents/foo_00.ll -passes=dbg-deleter
10+
PreservedAnalyses DebugRecordDeleterPass::run(Module &M, ModuleAnalysisManager &AM) {
11+
12+
bool modified = StripDebugInfo(M);
13+
14+
return PreservedAnalyses::all();
15+
16+
}

0 commit comments

Comments
 (0)