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))
34 if (
auto fusedLoc = dyn_cast<FusedLoc>(loc)) {
35 for (
auto loc : fusedLoc.getLocations()) {
40 if (
auto callerLoc = dyn_cast<CallSiteLoc>(loc))
49 LLVM::DICompileUnitAttr compileUnitAttr) {
58 LLVM::DIFileAttr fileAttr;
61 line = fileLoc.getLine();
62 StringRef inputFilePath = fileLoc.getFilename().getValue();
65 llvm::sys::path::parent_path(inputFilePath));
67 fileAttr = compileUnitAttr
68 ? compileUnitAttr.getFile()
71 auto subroutineTypeAttr =
79 auto subprogramFlags = LLVM::DISubprogramFlags::Optimized;
80 if (!llvmFunc.isExternal()) {
82 subprogramFlags = subprogramFlags | LLVM::DISubprogramFlags::Definition;
86 auto funcNameAttr = llvmFunc.getNameAttr();
88 context,
id, compileUnitAttr, fileAttr, funcNameAttr, funcNameAttr,
90 line, line, subprogramFlags, subroutineTypeAttr,
92 llvmFunc->setLoc(
FusedLoc::get(context, {loc}, subprogramAttr));
100 LLVM::DIFileAttr calleeFileAttr =
102 llvm::sys::path::parent_path(calleeFileName));
104 context, scopeAttr, calleeFileAttr, 0);
107 if (
auto callSiteLoc = dyn_cast<CallSiteLoc>(calleeLoc)) {
108 auto nestedLoc = callSiteLoc.getCallee();
109 loc =
getNestedLoc(op, lexicalBlockFileAttr, nestedLoc);
115 if (
auto callSiteLoc = dyn_cast<CallSiteLoc>(op->
getLoc())) {
116 auto callerLoc = callSiteLoc.getCaller();
117 auto calleeLoc = callSiteLoc.getCallee();
122 if (
auto funcOpLoc = llvm::dyn_cast_if_present<FusedLoc>(funcOp.getLoc())) {
123 scopeAttr = cast<LLVM::DISubprogramAttr>(funcOpLoc.getMetadata());
132 struct DIScopeForLLVMFuncOpPass
133 :
public LLVM::impl::DIScopeForLLVMFuncOpPassBase<
134 DIScopeForLLVMFuncOpPass> {
137 void runOnOperation()
override {
138 ModuleOp module = getOperation();
143 emitError(loc,
"LLVM dialect is not loaded.");
144 return signalPassFailure();
149 LLVM::DICompileUnitAttr compileUnitAttr;
150 if (
auto fusedCompileUnitAttr =
153 compileUnitAttr = fusedCompileUnitAttr.getMetadata();
155 LLVM::DIFileAttr fileAttr;
157 StringRef inputFilePath = fileLoc.getFilename().getValue();
159 context, llvm::sys::path::filename(inputFilePath),
160 llvm::sys::path::parent_path(inputFilePath));
172 if (
auto funcOp = dyn_cast<LLVM::LLVMFuncOp>(op)) {
static Location getNestedLoc(Operation *op, LLVM::DIScopeAttr scopeAttr, Location calleeLoc)
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 void setLexicalBlockFileAttr(Operation *op)
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
This class represents a fused location whose metadata is known to be an instance of the given type.
This class represents a LLVM attribute that describes a debug info scope.
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.
Operation is the basic unit of execution within MLIR.
void setLoc(Location loc)
Set the source location the operation was defined or derived from.
MLIRContext * getContext()
Return the context this operation is associated with.
Location getLoc()
The source location the operation was defined or derived from.
OpTy getParentOfType()
Return the closest surrounding parent operation that is of type 'OpTy'.
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...