xref: /llvm-project/flang/lib/Optimizer/Transforms/DebugTypeGenerator.h (revision 2e5a5237daf82a657561c490845c406e13657311)
1 //===-- DebugTypeGenerator.h -- type 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 // Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef FORTRAN_OPTIMIZER_TRANSFORMS_DEBUGTYPEGENERATOR_H
14 #define FORTRAN_OPTIMIZER_TRANSFORMS_DEBUGTYPEGENERATOR_H
15 
16 #include "flang/Optimizer/CodeGen/CGOps.h"
17 #include "flang/Optimizer/CodeGen/TypeConverter.h"
18 #include "flang/Optimizer/Dialect/FIRType.h"
19 #include "flang/Optimizer/Dialect/Support/FIRContext.h"
20 #include "flang/Optimizer/Dialect/Support/KindMapping.h"
21 #include "flang/Optimizer/Support/DataLayout.h"
22 #include "llvm/Support/Debug.h"
23 
24 namespace fir {
25 
26 /// This converts FIR/mlir type to DITypeAttr.
27 class DebugTypeGenerator {
28 public:
29   DebugTypeGenerator(mlir::ModuleOp module, mlir::SymbolTable *symbolTable,
30                      const mlir::DataLayout &dl);
31 
32   mlir::LLVM::DITypeAttr convertType(mlir::Type Ty,
33                                      mlir::LLVM::DIFileAttr fileAttr,
34                                      mlir::LLVM::DIScopeAttr scope,
35                                      fir::cg::XDeclareOp declOp);
36 
37 private:
38   mlir::LLVM::DITypeAttr convertRecordType(fir::RecordType Ty,
39                                            mlir::LLVM::DIFileAttr fileAttr,
40                                            mlir::LLVM::DIScopeAttr scope,
41                                            fir::cg::XDeclareOp declOp);
42   mlir::LLVM::DITypeAttr convertTupleType(mlir::TupleType Ty,
43                                           mlir::LLVM::DIFileAttr fileAttr,
44                                           mlir::LLVM::DIScopeAttr scope,
45                                           fir::cg::XDeclareOp declOp);
46   mlir::LLVM::DITypeAttr convertSequenceType(fir::SequenceType seqTy,
47                                              mlir::LLVM::DIFileAttr fileAttr,
48                                              mlir::LLVM::DIScopeAttr scope,
49                                              fir::cg::XDeclareOp declOp);
50   mlir::LLVM::DITypeAttr convertVectorType(fir::VectorType vecTy,
51                                            mlir::LLVM::DIFileAttr fileAttr,
52                                            mlir::LLVM::DIScopeAttr scope,
53                                            fir::cg::XDeclareOp declOp);
54 
55   /// The 'genAllocated' is true when we want to generate 'allocated' field
56   /// in the DICompositeType. It is needed for the allocatable arrays.
57   /// Similarly, 'genAssociated' is used with 'pointer' type to generate
58   /// 'associated' field.
59   mlir::LLVM::DITypeAttr convertBoxedSequenceType(
60       fir::SequenceType seqTy, mlir::LLVM::DIFileAttr fileAttr,
61       mlir::LLVM::DIScopeAttr scope, fir::cg::XDeclareOp declOp,
62       bool genAllocated, bool genAssociated);
63   mlir::LLVM::DITypeAttr convertCharacterType(fir::CharacterType charTy,
64                                               mlir::LLVM::DIFileAttr fileAttr,
65                                               mlir::LLVM::DIScopeAttr scope,
66                                               fir::cg::XDeclareOp declOp,
67                                               bool hasDescriptor);
68 
69   mlir::LLVM::DITypeAttr convertPointerLikeType(mlir::Type elTy,
70                                                 mlir::LLVM::DIFileAttr fileAttr,
71                                                 mlir::LLVM::DIScopeAttr scope,
72                                                 fir::cg::XDeclareOp declOp,
73                                                 bool genAllocated,
74                                                 bool genAssociated);
75   mlir::LLVM::DILocalVariableAttr
76   generateArtificialVariable(mlir::MLIRContext *context, mlir::Value Val,
77                              mlir::LLVM::DIFileAttr fileAttr,
78                              mlir::LLVM::DIScopeAttr scope,
79                              fir::cg::XDeclareOp declOp);
80   std::pair<std::uint64_t, unsigned short>
81   getFieldSizeAndAlign(mlir::Type fieldTy);
82 
83   mlir::ModuleOp module;
84   mlir::SymbolTable *symbolTable;
85   const mlir::DataLayout *dataLayout;
86   KindMapping kindMapping;
87   fir::LLVMTypeConverter llvmTypeConverter;
88   std::uint64_t dimsSize;
89   std::uint64_t dimsOffset;
90   std::uint64_t ptrSize;
91   std::uint64_t lenOffset;
92   std::uint64_t rankOffset;
93   std::uint64_t rankSize;
94   int32_t derivedTypeDepth;
95   llvm::DenseMap<mlir::Type, mlir::LLVM::DITypeAttr> typeCache;
96 };
97 
98 } // namespace fir
99 
100 static uint32_t getLineFromLoc(mlir::Location loc) {
101   uint32_t line = 1;
102   if (auto fileLoc = mlir::dyn_cast<mlir::FileLineColLoc>(loc))
103     line = fileLoc.getLine();
104   return line;
105 }
106 
107 #endif // FORTRAN_OPTIMIZER_TRANSFORMS_DEBUGTYPEGENERATOR_H
108