26 if (
auto fileLoc = dyn_cast<FileLineColLoc>(loc))
28 if (
auto nameLoc = dyn_cast<NameLoc>(loc))
30 if (
auto opaqueLoc = dyn_cast<OpaqueLoc>(loc))
32 if (
auto fusedLoc = dyn_cast<FusedLoc>(loc)) {
33 for (
auto loc : fusedLoc.getLocations()) {
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));
98 LLVM::DIFileAttr calleeFileAttr =
99 LLVM::DIFileAttr::get(context, llvm::sys::path::filename(calleeFileName),
100 llvm::sys::path::parent_path(calleeFileName));
101 auto lexicalBlockFileAttr = LLVM::DILexicalBlockFileAttr::get(
102 context, scopeAttr, calleeFileAttr, 0);
105 if (
auto callSiteLoc = dyn_cast<CallSiteLoc>(calleeLoc)) {
106 auto nestedLoc = callSiteLoc.getCallee();
107 loc =
getNestedLoc(op, lexicalBlockFileAttr, nestedLoc);
109 return FusedLoc::get(context, {loc}, lexicalBlockFileAttr);
113 if (
auto callSiteLoc = dyn_cast<CallSiteLoc>(op->
getLoc())) {
114 auto callerLoc = callSiteLoc.getCaller();
115 auto calleeLoc = callSiteLoc.getCallee();
120 if (
auto funcOpLoc = llvm::dyn_cast_if_present<FusedLoc>(funcOp.getLoc())) {
121 scopeAttr = cast<LLVM::DISubprogramAttr>(funcOpLoc.getMetadata());
123 CallSiteLoc::get(
getNestedLoc(op, scopeAttr, calleeLoc), callerLoc));
130struct DIScopeForLLVMFuncOpPass
132 DIScopeForLLVMFuncOpPass> {
135 void runOnOperation()
override {
136 ModuleOp module = getOperation();
137 Location loc =
module.getLoc();
141 emitError(loc,
"LLVM dialect is not loaded.");
142 return signalPassFailure();
147 LLVM::DICompileUnitAttr compileUnitAttr;
148 if (
auto fusedCompileUnitAttr =
150 ->findInstanceOf<FusedLocWith<LLVM::DICompileUnitAttr>>()) {
151 compileUnitAttr = fusedCompileUnitAttr.getMetadata();
153 LLVM::DIFileAttr fileAttr;
155 StringRef inputFilePath = fileLoc.getFilename().getValue();
156 fileAttr = LLVM::DIFileAttr::get(
157 context, llvm::sys::path::filename(inputFilePath),
158 llvm::sys::path::parent_path(inputFilePath));
160 fileAttr = LLVM::DIFileAttr::get(context,
"<unknown>",
"");
163 compileUnitAttr = LLVM::DICompileUnitAttr::get(
165 fileAttr, StringAttr::get(context,
"MLIR"),
169 module.walk<WalkOrder::PreOrder>([&](Operation *op) -> void {
170 if (auto funcOp = dyn_cast<LLVM::LLVMFuncOp>(op)) {
173 addScopeToFunction(funcOp, compileUnitAttr);
MLIRContext is the top-level object for a collection of MLIR operations.
Dialect * getLoadedDialect(StringRef name)
Get a registered IR dialect with the given namespace.