MLIR  18.0.0git
DebugImporter.h
Go to the documentation of this file.
1 //===- DebugImporter.h - LLVM to MLIR Debug conversion -------*- C++ -*----===//
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 //
9 // This file implements the translation between LLVMIR debug information and
10 // the corresponding MLIR representation.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_LIB_TARGET_LLVMIR_DEBUGIMPORTER_H_
15 #define MLIR_LIB_TARGET_LLVMIR_DEBUGIMPORTER_H_
16 
18 #include "mlir/IR/BuiltinOps.h"
19 #include "mlir/IR/MLIRContext.h"
20 #include "llvm/IR/DebugInfoMetadata.h"
21 
22 namespace mlir {
23 class Operation;
24 
25 namespace LLVM {
26 class LLVMFuncOp;
27 
28 namespace detail {
29 
31 public:
32  DebugImporter(ModuleOp mlirModule)
33  : context(mlirModule.getContext()), mlirModule(mlirModule) {}
34 
35  /// Translates the given LLVM debug location to an MLIR location.
36  Location translateLoc(llvm::DILocation *loc);
37 
38  /// Translates the debug information for the given function into a Location.
39  /// Returns UnknownLoc if `func` has no debug information attached to it.
40  Location translateFuncLocation(llvm::Function *func);
41 
42  /// Translates the given LLVM debug metadata to MLIR.
43  DINodeAttr translate(llvm::DINode *node);
44 
45  /// Infers the metadata type and translates it to MLIR.
46  template <typename DINodeT>
47  auto translate(DINodeT *node) {
48  // Infer the MLIR type from the LLVM metadata type.
49  using MLIRTypeT = decltype(translateImpl(node));
50  return cast_or_null<MLIRTypeT>(
51  translate(static_cast<llvm::DINode *>(node)));
52  }
53 
54 private:
55  /// Translates the given LLVM debug metadata to the corresponding attribute.
56  DIBasicTypeAttr translateImpl(llvm::DIBasicType *node);
57  DICompileUnitAttr translateImpl(llvm::DICompileUnit *node);
58  DICompositeTypeAttr translateImpl(llvm::DICompositeType *node);
59  DIDerivedTypeAttr translateImpl(llvm::DIDerivedType *node);
60  DIFileAttr translateImpl(llvm::DIFile *node);
61  DILabelAttr translateImpl(llvm::DILabel *node);
62  DILexicalBlockAttr translateImpl(llvm::DILexicalBlock *node);
63  DILexicalBlockFileAttr translateImpl(llvm::DILexicalBlockFile *node);
64  DILocalVariableAttr translateImpl(llvm::DILocalVariable *node);
65  DIModuleAttr translateImpl(llvm::DIModule *node);
66  DINamespaceAttr translateImpl(llvm::DINamespace *node);
67  DIScopeAttr translateImpl(llvm::DIScope *node);
68  DISubprogramAttr translateImpl(llvm::DISubprogram *node);
69  DISubrangeAttr translateImpl(llvm::DISubrange *node);
70  DISubroutineTypeAttr translateImpl(llvm::DISubroutineType *node);
71  DITypeAttr translateImpl(llvm::DIType *node);
72 
73  /// Constructs a StringAttr from the MDString if it is non-null. Returns a
74  /// null attribute otherwise.
75  StringAttr getStringAttrOrNull(llvm::MDString *stringNode);
76 
77  /// A mapping between LLVM debug metadata and the corresponding attribute.
79 
80  /// A stack that stores the metadata nodes that are being traversed. The stack
81  /// is used to detect cyclic dependencies during the metadata translation.
82  SetVector<llvm::DINode *> translationStack;
83 
84  MLIRContext *context;
85  ModuleOp mlirModule;
86 };
87 
88 } // namespace detail
89 } // namespace LLVM
90 } // namespace mlir
91 
92 #endif // MLIR_LIB_TARGET_LLVMIR_DEBUGIMPORTER_H_
static MLIRContext * getContext(OpFoldResult val)
This class represents the base attribute for all debug info attributes.
Definition: LLVMAttrs.h:27
This class represents a LLVM attribute that describes a debug info scope.
Definition: LLVMAttrs.h:36
This class represents a LLVM attribute that describes a debug info type.
Definition: LLVMAttrs.h:55
DebugImporter(ModuleOp mlirModule)
Definition: DebugImporter.h:32
DINodeAttr translate(llvm::DINode *node)
Translates the given LLVM debug metadata to MLIR.
auto translate(DINodeT *node)
Infers the metadata type and translates it to MLIR.
Definition: DebugImporter.h:47
Location translateLoc(llvm::DILocation *loc)
Translates the given LLVM debug location to an MLIR location.
Location translateFuncLocation(llvm::Function *func)
Translates the debug information for the given function into a Location.
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
This header declares functions that assist transformations in the MemRef dialect.