1 //===- DebugTranslation.h - MLIR to LLVM 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 an MLIR debug information and
10 // the corresponding LLVMIR representation.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_LIB_TARGET_LLVMIR_DEBUGTRANSLATION_H_
15 #define MLIR_LIB_TARGET_LLVMIR_DEBUGTRANSLATION_H_
16 
17 #include "mlir/IR/Location.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/IR/DIBuilder.h"
21 
22 namespace mlir {
23 class Operation;
24 
25 namespace LLVM {
26 class LLVMFuncOp;
27 
28 namespace detail {
29 class DebugTranslation {
30 public:
31   DebugTranslation(Operation *module, llvm::Module &llvmModule);
32 
33   /// Finalize the translation of debug information.
34   void finalize();
35 
36   /// Translate the given location to an llvm debug location.
37   const llvm::DILocation *translateLoc(Location loc, llvm::DILocalScope *scope);
38 
39   /// Translate the debug information for the given function.
40   void translate(LLVMFuncOp func, llvm::Function &llvmFunc);
41 
42 private:
43   /// Translate the given location to an llvm debug location with the given
44   /// scope and inlinedAt parameters.
45   const llvm::DILocation *translateLoc(Location loc, llvm::DILocalScope *scope,
46                                        const llvm::DILocation *inlinedAt);
47 
48   /// Create an llvm debug file for the given file path.
49   llvm::DIFile *translateFile(StringRef fileName);
50 
51   /// A mapping between mlir location+scope and the corresponding llvm debug
52   /// metadata.
53   DenseMap<std::pair<Location, llvm::DILocalScope *>, const llvm::DILocation *>
54       locationToLoc;
55 
56   /// A mapping between filename and llvm debug file.
57   /// TODO: Change this to DenseMap<Identifier, ...> when we can
58   /// access the Identifier filename in FileLineColLoc.
59   llvm::StringMap<llvm::DIFile *> fileMap;
60 
61   /// A string containing the current working directory of the compiler.
62   SmallString<256> currentWorkingDir;
63 
64   /// Debug information fields.
65   llvm::DIBuilder builder;
66   llvm::LLVMContext &llvmCtx;
67   llvm::DICompileUnit *compileUnit;
68 };
69 
70 } // end namespace detail
71 } // end namespace LLVM
72 } // end namespace mlir
73 
74 #endif // MLIR_LIB_TARGET_LLVMIR_DEBUGTRANSLATION_H_
75