@@ -34,69 +34,93 @@ static FileLineColLoc extractFileLoc(Location loc) {
34
34
return FileLineColLoc ();
35
35
}
36
36
37
+ // / Creates a DISubprogramAttr with the provided compile unit and attaches it
38
+ // / to the function. Does nothing when the function already has an attached
39
+ // / subprogram.
40
+ static void addScopeToFunction (LLVM::LLVMFuncOp llvmFunc,
41
+ LLVM::DICompileUnitAttr compileUnitAttr) {
42
+
43
+ Location loc = llvmFunc.getLoc ();
44
+ if (loc->findInstanceOf <mlir::FusedLocWith<LLVM::DISubprogramAttr>>())
45
+ return ;
46
+
47
+ MLIRContext *context = llvmFunc->getContext ();
48
+
49
+ // Filename, line and colmun to associate to the function.
50
+ LLVM::DIFileAttr fileAttr;
51
+ int64_t line = 1 , col = 1 ;
52
+ FileLineColLoc fileLoc = extractFileLoc (loc);
53
+ if (!fileLoc && compileUnitAttr) {
54
+ fileAttr = compileUnitAttr.getFile ();
55
+ } else if (!fileLoc) {
56
+ fileAttr = LLVM::DIFileAttr::get (context, " <unknown>" , " " );
57
+ } else {
58
+ line = fileLoc.getLine ();
59
+ col = fileLoc.getColumn ();
60
+ StringRef inputFilePath = fileLoc.getFilename ().getValue ();
61
+ fileAttr =
62
+ LLVM::DIFileAttr::get (context, llvm::sys::path::filename (inputFilePath),
63
+ llvm::sys::path::parent_path (inputFilePath));
64
+ }
65
+ auto subroutineTypeAttr =
66
+ LLVM::DISubroutineTypeAttr::get (context, llvm::dwarf::DW_CC_normal, {});
67
+
68
+ StringAttr funcNameAttr = llvmFunc.getNameAttr ();
69
+ auto subprogramAttr = LLVM::DISubprogramAttr::get (
70
+ context, compileUnitAttr, fileAttr, funcNameAttr, funcNameAttr, fileAttr,
71
+ /* line=*/ line,
72
+ /* scopeline=*/ col,
73
+ LLVM::DISubprogramFlags::Definition | LLVM::DISubprogramFlags::Optimized,
74
+ subroutineTypeAttr);
75
+ llvmFunc->setLoc (FusedLoc::get (context, {loc}, subprogramAttr));
76
+ }
77
+
37
78
namespace {
38
79
// / Add a debug info scope to LLVMFuncOp that are missing it.
39
80
struct DIScopeForLLVMFuncOp
40
81
: public LLVM::impl::DIScopeForLLVMFuncOpBase<DIScopeForLLVMFuncOp> {
41
82
void runOnOperation () override {
42
- LLVM::LLVMFuncOp llvmFunc = getOperation ();
43
- Location loc = llvmFunc.getLoc ();
44
- if (loc->findInstanceOf <mlir::FusedLocWith<LLVM::DISubprogramAttr>>())
45
- return ;
83
+ ModuleOp module = getOperation ();
84
+ Location loc = module .getLoc ();
46
85
47
86
MLIRContext *context = &getContext ();
48
87
49
88
// To find a DICompileUnitAttr attached to a parent (the module for
50
89
// example), otherwise create a default one.
90
+ // Find a DICompileUnitAttr attached to the module, otherwise create a
91
+ // default one.
51
92
LLVM::DICompileUnitAttr compileUnitAttr;
52
- if (ModuleOp module = llvmFunc->getParentOfType <ModuleOp>()) {
53
- auto fusedCompileUnitAttr =
54
- module ->getLoc ()
55
- ->findInstanceOf <mlir::FusedLocWith<LLVM::DICompileUnitAttr>>();
56
- if (fusedCompileUnitAttr)
57
- compileUnitAttr = fusedCompileUnitAttr.getMetadata ();
58
- }
59
-
60
- // Filename, line and colmun to associate to the function.
61
- LLVM::DIFileAttr fileAttr;
62
- int64_t line = 1 , col = 1 ;
63
- FileLineColLoc fileLoc = extractFileLoc (loc);
64
- if (!fileLoc && compileUnitAttr) {
65
- fileAttr = compileUnitAttr.getFile ();
66
- } else if (!fileLoc) {
67
- fileAttr = LLVM::DIFileAttr::get (context, " <unknown>" , " " );
93
+ auto fusedCompileUnitAttr =
94
+ module ->getLoc ()
95
+ ->findInstanceOf <mlir::FusedLocWith<LLVM::DICompileUnitAttr>>();
96
+ if (fusedCompileUnitAttr) {
97
+ compileUnitAttr = fusedCompileUnitAttr.getMetadata ();
68
98
} else {
69
- line = fileLoc.getLine ();
70
- col = fileLoc.getColumn ();
71
- StringRef inputFilePath = fileLoc.getFilename ().getValue ();
72
- fileAttr = LLVM::DIFileAttr::get (
73
- context, llvm::sys::path::filename (inputFilePath),
74
- llvm::sys::path::parent_path (inputFilePath));
75
- }
76
- if (!compileUnitAttr) {
99
+ LLVM::DIFileAttr fileAttr;
100
+ if (FileLineColLoc fileLoc = extractFileLoc (loc)) {
101
+ StringRef inputFilePath = fileLoc.getFilename ().getValue ();
102
+ fileAttr = LLVM::DIFileAttr::get (
103
+ context, llvm::sys::path::filename (inputFilePath),
104
+ llvm::sys::path::parent_path (inputFilePath));
105
+ } else {
106
+ fileAttr = LLVM::DIFileAttr::get (context, " <unknown>" , " " );
107
+ }
108
+
77
109
compileUnitAttr = LLVM::DICompileUnitAttr::get (
78
- context, llvm::dwarf::DW_LANG_C, fileAttr ,
79
- StringAttr::get (context, " MLIR" ), /* isOptimized= */ true ,
80
- LLVM::DIEmissionKind::LineTablesOnly);
110
+ context, DistinctAttr::create ( UnitAttr::get (context)) ,
111
+ llvm::dwarf::DW_LANG_C, fileAttr, StringAttr::get (context, " MLIR" ),
112
+ /* isOptimized= */ true , LLVM::DIEmissionKind::LineTablesOnly);
81
113
}
82
- auto subroutineTypeAttr =
83
- LLVM::DISubroutineTypeAttr::get (context, llvm::dwarf::DW_CC_normal, {});
84
-
85
- StringAttr funcNameAttr = llvmFunc.getNameAttr ();
86
- auto subprogramAttr =
87
- LLVM::DISubprogramAttr::get (context, compileUnitAttr, fileAttr,
88
- funcNameAttr, funcNameAttr, fileAttr,
89
- /* line=*/ line,
90
- /* scopeline=*/ col,
91
- LLVM::DISubprogramFlags::Definition |
92
- LLVM::DISubprogramFlags::Optimized,
93
- subroutineTypeAttr);
94
- llvmFunc->setLoc (FusedLoc::get (context, {loc}, subprogramAttr));
114
+
115
+ // Create subprograms for each function with the same distinct compile unit.
116
+ module .walk ([&](LLVM::LLVMFuncOp func) {
117
+ addScopeToFunction (func, compileUnitAttr);
118
+ });
95
119
}
96
120
};
97
121
98
122
} // end anonymous namespace
99
123
100
124
std::unique_ptr<Pass> mlir::LLVM::createDIScopeForLLVMFuncOpPass () {
101
125
return std::make_unique<DIScopeForLLVMFuncOp>();
102
- }
126
+ }
0 commit comments