13 #include "llvm/BinaryFormat/Dwarf.h"
14 #include "llvm/Support/Debug.h"
15 #include "llvm/Support/Path.h"
19 #define GEN_PASS_DEF_DISCOPEFORLLVMFUNCOP
20 #include "mlir/Dialect/LLVMIR/Transforms/Passes.h.inc"
28 if (
auto fileLoc = dyn_cast<FileLineColLoc>(loc))
30 if (
auto nameLoc = dyn_cast<NameLoc>(loc))
32 if (
auto opaqueLoc = dyn_cast<OpaqueLoc>(loc))
34 return FileLineColLoc();
39 struct DIScopeForLLVMFuncOp
40 :
public LLVM::impl::DIScopeForLLVMFuncOpBase<DIScopeForLLVMFuncOp> {
41 void runOnOperation()
override {
42 LLVM::LLVMFuncOp llvmFunc = getOperation();
51 LLVM::DICompileUnitAttr compileUnitAttr;
52 if (ModuleOp module = llvmFunc->getParentOfType<ModuleOp>()) {
53 auto fusedCompileUnitAttr =
56 if (fusedCompileUnitAttr)
57 compileUnitAttr = fusedCompileUnitAttr.
getMetadata();
61 LLVM::DIFileAttr fileAttr;
62 int64_t line = 1, col = 1;
64 if (!fileLoc && compileUnitAttr) {
65 fileAttr = compileUnitAttr.getFile();
66 }
else if (!fileLoc) {
69 line = fileLoc.getLine();
70 col = fileLoc.getColumn();
71 StringRef inputFilePath = fileLoc.getFilename().getValue();
73 context, llvm::sys::path::filename(inputFilePath),
74 llvm::sys::path::parent_path(inputFilePath));
76 if (!compileUnitAttr) {
78 context, llvm::dwarf::DW_LANG_C, fileAttr,
80 LLVM::DIEmissionKind::LineTablesOnly);
82 auto subroutineTypeAttr =
85 StringAttr funcNameAttr = llvmFunc.getNameAttr();
88 funcNameAttr, funcNameAttr, fileAttr,
91 LLVM::DISubprogramFlags::Definition |
92 LLVM::DISubprogramFlags::Optimized,
94 llvmFunc->setLoc(
FusedLoc::get(context, {loc}, subprogramAttr));
101 return std::make_unique<DIScopeForLLVMFuncOp>();
static FileLineColLoc extractFileLoc(Location loc)
Attempt to extract a filename for the given loc.
static MLIRContext * getContext(OpFoldResult val)
This class represents a fused location whose metadata is known to be an instance of the given type.
MetadataT getMetadata() const
Return the metadata associated with this fused location.
T findInstanceOf()
Return an instance of the given location type if one is nested under the current location.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
MLIRContext is the top-level object for a collection of MLIR operations.
std::unique_ptr< Pass > createDIScopeForLLVMFuncOpPass()
Create a pass to add DIScope to LLVMFuncOp that are missing it.
Include the generated interface declarations.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...