MLIR  18.0.0git
DIScopeForLLVMFuncOp.cpp
Go to the documentation of this file.
1 //===- DILineTableFromLocations.cpp - -------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 
12 #include "mlir/Pass/Pass.h"
13 #include "llvm/BinaryFormat/Dwarf.h"
14 #include "llvm/Support/Debug.h"
15 #include "llvm/Support/Path.h"
16 
17 namespace mlir {
18 namespace LLVM {
19 #define GEN_PASS_DEF_DISCOPEFORLLVMFUNCOP
20 #include "mlir/Dialect/LLVMIR/Transforms/Passes.h.inc"
21 } // namespace LLVM
22 } // namespace mlir
23 
24 using namespace mlir;
25 
26 /// Attempt to extract a filename for the given loc.
27 static FileLineColLoc extractFileLoc(Location loc) {
28  if (auto fileLoc = dyn_cast<FileLineColLoc>(loc))
29  return fileLoc;
30  if (auto nameLoc = dyn_cast<NameLoc>(loc))
31  return extractFileLoc(nameLoc.getChildLoc());
32  if (auto opaqueLoc = dyn_cast<OpaqueLoc>(loc))
33  return extractFileLoc(opaqueLoc.getFallbackLocation());
34  return FileLineColLoc();
35 }
36 
37 namespace {
38 /// Add a debug info scope to LLVMFuncOp that are missing it.
39 struct DIScopeForLLVMFuncOp
40  : public LLVM::impl::DIScopeForLLVMFuncOpBase<DIScopeForLLVMFuncOp> {
41  void runOnOperation() override {
42  LLVM::LLVMFuncOp llvmFunc = getOperation();
43  Location loc = llvmFunc.getLoc();
45  return;
46 
47  MLIRContext *context = &getContext();
48 
49  // To find a DICompileUnitAttr attached to a parent (the module for
50  // example), otherwise create a default one.
51  LLVM::DICompileUnitAttr compileUnitAttr;
52  if (ModuleOp module = llvmFunc->getParentOfType<ModuleOp>()) {
53  auto fusedCompileUnitAttr =
54  module->getLoc()
56  if (fusedCompileUnitAttr)
57  compileUnitAttr = fusedCompileUnitAttr.getMetadata();
58  }
59 
60  // Filename, line and colmun to associate to the function.
61  LLVM::DIFileAttr fileAttr;
62  int64_t line = 1, col = 1;
63  FileLineColLoc fileLoc = extractFileLoc(loc);
64  if (!fileLoc && compileUnitAttr) {
65  fileAttr = compileUnitAttr.getFile();
66  } else if (!fileLoc) {
67  fileAttr = LLVM::DIFileAttr::get(context, "<unknown>", "");
68  } else {
69  line = fileLoc.getLine();
70  col = fileLoc.getColumn();
71  StringRef inputFilePath = fileLoc.getFilename().getValue();
72  fileAttr = LLVM::DIFileAttr::get(
73  context, llvm::sys::path::filename(inputFilePath),
74  llvm::sys::path::parent_path(inputFilePath));
75  }
76  if (!compileUnitAttr) {
77  compileUnitAttr = LLVM::DICompileUnitAttr::get(
78  context, llvm::dwarf::DW_LANG_C, fileAttr,
79  StringAttr::get(context, "MLIR"), /*isOptimized=*/true,
80  LLVM::DIEmissionKind::LineTablesOnly);
81  }
82  auto subroutineTypeAttr =
83  LLVM::DISubroutineTypeAttr::get(context, llvm::dwarf::DW_CC_normal, {});
84 
85  StringAttr funcNameAttr = llvmFunc.getNameAttr();
86  auto subprogramAttr =
87  LLVM::DISubprogramAttr::get(context, compileUnitAttr, fileAttr,
88  funcNameAttr, funcNameAttr, fileAttr,
89  /*line=*/line,
90  /*scopeline=*/col,
91  LLVM::DISubprogramFlags::Definition |
92  LLVM::DISubprogramFlags::Optimized,
93  subroutineTypeAttr);
94  llvmFunc->setLoc(FusedLoc::get(context, {loc}, subprogramAttr));
95  }
96 };
97 
98 } // end anonymous namespace
99 
101  return std::make_unique<DIScopeForLLVMFuncOp>();
102 }
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.
Definition: Location.h:145
MetadataT getMetadata() const
Return the metadata associated with this fused location.
Definition: Location.h:150
T findInstanceOf()
Return an instance of the given location type if one is nested under the current location.
Definition: Location.h:41
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:63
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
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...