38 if (
auto callerLoc = dyn_cast<CallSiteLoc>(loc))
47 LLVM::DICompileUnitAttr compileUnitAttr) {
56 LLVM::DIFileAttr fileAttr;
59 line = fileLoc.getLine();
60 StringRef inputFilePath = fileLoc.getFilename().getValue();
62 LLVM::DIFileAttr::get(context, llvm::sys::path::filename(inputFilePath),
63 llvm::sys::path::parent_path(inputFilePath));
65 fileAttr = compileUnitAttr
66 ? compileUnitAttr.getFile()
67 : LLVM::DIFileAttr::get(context,
"<unknown>",
"");
69 auto subroutineTypeAttr =
70 LLVM::DISubroutineTypeAttr::get(context, llvm::dwarf::DW_CC_normal, {});
77 auto subprogramFlags = LLVM::DISubprogramFlags::Optimized;
78 if (!llvmFunc.isExternal()) {
80 subprogramFlags |= LLVM::DISubprogramFlags::Definition;
84 auto funcNameAttr = llvmFunc.getNameAttr();
85 auto subprogramAttr = LLVM::DISubprogramAttr::get(
86 context,
id, compileUnitAttr, fileAttr, funcNameAttr, funcNameAttr,
88 line, line, subprogramFlags, subroutineTypeAttr,
90 llvmFunc->setLoc(FusedLoc::get(context, {loc}, subprogramAttr));
97 LLVM::DIFileAttr calleeFileAttr;
99 auto calleeFileName = calleeFileLoc.getFilename();
100 calleeFileAttr = LLVM::DIFileAttr::get(
101 context, llvm::sys::path::filename(calleeFileName),
102 llvm::sys::path::parent_path(calleeFileName));
104 calleeFileAttr = LLVM::DIFileAttr::get(context,
"<unknown>",
"");
106 auto lexicalBlockFileAttr = LLVM::DILexicalBlockFileAttr::get(
107 context, scopeAttr, calleeFileAttr, 0);
110 if (
auto callSiteLoc = dyn_cast<CallSiteLoc>(calleeLoc)) {
111 auto nestedLoc = callSiteLoc.getCallee();
112 loc =
getNestedLoc(op, lexicalBlockFileAttr, nestedLoc);
114 return FusedLoc::get(context, {loc}, lexicalBlockFileAttr);
122 if (
auto callSiteLoc = dyn_cast<CallSiteLoc>(opLoc)) {
123 auto callerLoc = callSiteLoc.getCaller();
124 auto calleeLoc = callSiteLoc.getCallee();
130 llvm::dyn_cast_if_present<FusedLoc>(funcOp.getLoc())) {
131 scopeAttr = cast<LLVM::DISubprogramAttr>(funcOpLoc.getMetadata());
152 StringRef opFile = opFileLoc.
getFilename().getValue();
153 StringRef funcFile = funcFileLoc.
getFilename().getValue();
157 if (opFile != funcFile) {
158 auto funcOpLoc = llvm::dyn_cast_if_present<FusedLoc>(funcOp.getLoc());
161 auto scopeAttr = dyn_cast<LLVM::DISubprogramAttr>(funcOpLoc.getMetadata());
166 LLVM::DIFileAttr opFileAttr =
167 LLVM::DIFileAttr::get(context, llvm::sys::path::filename(opFile),
168 llvm::sys::path::parent_path(opFile));
170 LLVM::DILexicalBlockFileAttr lexicalBlockFileAttr =
171 LLVM::DILexicalBlockFileAttr::get(context, scopeAttr, opFileAttr, 0);
173 Location newLoc = FusedLoc::get(context, {opLoc}, lexicalBlockFileAttr);
180struct DIScopeForLLVMFuncOpPass
182 DIScopeForLLVMFuncOpPass> {
185 void runOnOperation()
override {
186 ModuleOp module = getOperation();
187 Location loc =
module.getLoc();
191 emitError(loc,
"LLVM dialect is not loaded.");
192 return signalPassFailure();
197 LLVM::DICompileUnitAttr compileUnitAttr;
198 if (
auto fusedCompileUnitAttr =
200 ->findInstanceOf<FusedLocWith<LLVM::DICompileUnitAttr>>()) {
201 compileUnitAttr = fusedCompileUnitAttr.getMetadata();
203 LLVM::DIFileAttr fileAttr;
205 StringRef inputFilePath = fileLoc.getFilename().getValue();
206 fileAttr = LLVM::DIFileAttr::get(
207 context, llvm::sys::path::filename(inputFilePath),
208 llvm::sys::path::parent_path(inputFilePath));
210 fileAttr = LLVM::DIFileAttr::get(context,
"<unknown>",
"");
213 compileUnitAttr = LLVM::DICompileUnitAttr::get(
215 fileAttr, StringAttr::get(context,
"MLIR"),
219 module.walk<WalkOrder::PreOrder>([&](Operation *op) -> void {
220 if (auto funcOp = dyn_cast<LLVM::LLVMFuncOp>(op)) {
223 addScopeToFunction(funcOp, compileUnitAttr);