@@ -34,69 +34,92 @@ static FileLineColLoc extractFileLoc(Location loc) {
3434 return FileLineColLoc ();
3535}
3636
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+
3778namespace {
3879// / Add a debug info scope to LLVMFuncOp that are missing it.
3980struct DIScopeForLLVMFuncOp
4081 : public LLVM::impl::DIScopeForLLVMFuncOpBase<DIScopeForLLVMFuncOp> {
4182 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 ();
4685
4786 MLIRContext *context = &getContext ();
4887
4988 // To find a DICompileUnitAttr attached to a parent (the module for
5089 // example), otherwise create a default one.
90+ // Find a DICompileUnitAttr attached to the module, otherwise create a
91+ // default one.
5192 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 ();
6898 } 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+
77109 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);
81113 }
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+ for (auto func : module .getOps <LLVM::LLVMFuncOp>())
117+ addScopeToFunction (func, compileUnitAttr);
95118 }
96119};
97120
98121} // end anonymous namespace
99122
100123std::unique_ptr<Pass> mlir::LLVM::createDIScopeForLLVMFuncOpPass () {
101124 return std::make_unique<DIScopeForLLVMFuncOp>();
102- }
125+ }
0 commit comments