13 #include "llvm/BinaryFormat/Dwarf.h"
14 #include "llvm/Support/Debug.h"
15 #include "llvm/Support/Path.h"
19 #define GEN_PASS_DEF_DISCOPEFORLLVMFUNCOPPASS
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))
41 LLVM::DICompileUnitAttr compileUnitAttr) {
50 LLVM::DIFileAttr fileAttr;
51 int64_t line = 1, col = 1;
53 if (!fileLoc && compileUnitAttr) {
54 fileAttr = compileUnitAttr.getFile();
55 }
else if (!fileLoc) {
60 StringRef inputFilePath = fileLoc.
getFilename().getValue();
63 llvm::sys::path::parent_path(inputFilePath));
65 auto subroutineTypeAttr =
70 auto subprogramFlags = LLVM::DISubprogramFlags::Optimized;
71 if (!llvmFunc.isExternal()) {
73 subprogramFlags = subprogramFlags | LLVM::DISubprogramFlags::Definition;
79 context,
id, compileUnitAttr, fileAttr, funcName, funcName, fileAttr,
80 line, col, subprogramFlags, subroutineTypeAttr,
82 llvmFunc->setLoc(
FusedLoc::get(context, {loc}, subprogramAttr));
87 struct DIScopeForLLVMFuncOpPass
88 :
public LLVM::impl::DIScopeForLLVMFuncOpPassBase<
89 DIScopeForLLVMFuncOpPass> {
92 void runOnOperation()
override {
93 ModuleOp module = getOperation();
98 emitError(loc,
"LLVM dialect is not loaded.");
99 return signalPassFailure();
106 LLVM::DICompileUnitAttr compileUnitAttr;
107 auto fusedCompileUnitAttr =
110 if (fusedCompileUnitAttr) {
111 compileUnitAttr = fusedCompileUnitAttr.
getMetadata();
113 LLVM::DIFileAttr fileAttr;
115 StringRef inputFilePath = fileLoc.getFilename().getValue();
117 context, llvm::sys::path::filename(inputFilePath),
118 llvm::sys::path::parent_path(inputFilePath));
130 module.walk([&](LLVM::LLVMFuncOp func) {
static void addScopeToFunction(LLVM::LLVMFuncOp llvmFunc, LLVM::DICompileUnitAttr compileUnitAttr)
Creates a DISubprogramAttr with the provided compile unit and attaches it to the function.
static FileLineColLoc extractFileLoc(Location loc)
Attempt to extract a filename for the given loc.
static MLIRContext * getContext(OpFoldResult val)
An attribute that associates a referenced attribute with a unique identifier.
static DistinctAttr create(Attribute referencedAttr)
Creates a distinct attribute that associates a referenced attribute with a unique identifier.
An instance of this location represents a tuple of file, line number, and column number.
StringAttr getFilename() const
unsigned getColumn() const
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.
Dialect * getLoadedDialect(StringRef name)
Get a registered IR dialect with the given namespace.
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...