FLANG
DebugTypeGenerator.h
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/TypeConverter.h"
17#include "flang/Optimizer/Dialect/FIRCG/CGOps.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 <functional>
23
24namespace fir {
25
35public:
36 // Currently, the handling of recursive debug type in mlir has some
37 // limitations that were discussed at the end of the thread for following
38 // PR.
39 // https://github.com/llvm/llvm-project/pull/106571
40 //
41 // Problem could be explained with the following example code:
42 // type t2
43 // type(t1), pointer :: p1
44 // end type
45 // type t1
46 // type(t2), pointer :: p2
47 // end type
48 // In the description below, type_self means a temporary type that is
49 // generated
50 // as a place holder while the members of that type are being processed.
51 //
52 // If we process t1 first then we will have the following structure after
53 // it has been processed.
54 // t1 -> t2 -> t1_self
55 // This is because when we started processing t2, we did not have the
56 // complete t1 but its place holder t1_self.
57 // Now if some entity requires t2, we will already have that in cache and
58 // will return it. But this t2 refers to t1_self and not to t1. In mlir
59 // handling, only those types are allowed to have _self reference which are
60 // wrapped by entity whose reference it is. So t1 -> t2 -> t1_self is ok
61 // because the t1_self reference can be resolved by the outer t1. But
62 // standalone t2 is not because there will be no way to resolve it. Until
63 // this is fixed in mlir, we avoid caching such types. Please see
64 // DebugTranslation::translateRecursive for details on how mlir handles
65 // recursive types.
66 using ActiveLevels = llvm::SmallVector<int32_t, 1>;
67 mlir::LLVM::DITypeAttr lookup(mlir::Type);
68 ActiveLevels startTranslating(mlir::Type,
69 mlir::LLVM::DITypeAttr placeHolder = nullptr);
70 void finalize(mlir::Type, mlir::LLVM::DITypeAttr, ActiveLevels &&);
71 void preComponentVisitUpdate();
72 void postComponentVisitUpdate(ActiveLevels &);
73
74private:
75 void insertCacheCleanUp(mlir::Type type, int32_t depth);
76 void cleanUpCache(int32_t depth);
77 // Current depth inside a top level derived type being converted.
78 int32_t derivedTypeDepth = 0;
79 // Cache for already translated derived types with the minimum depth where
80 // this cache entry is valid. Zero means the translation is always valid, "i"
81 // means the type depends its derived type tree parent node at depth "i". Such
82 // types should be cleaned-up from the cache in the post visit of node "i".
83 // Note that any new metadata created for a type with a component in the cache
84 // with validity of "i" shall not be added to the cache with a validity
85 // smaller than "i".
86 llvm::DenseMap<mlir::Type, std::pair<mlir::LLVM::DITypeAttr, ActiveLevels>>
87 typeCache;
88 // List of parent nodes that are being recursively referred to in the
89 // component type that has just been computed.
90 ActiveLevels componentActiveRecursionLevels;
91 // Helper list that maintains the list of nodes that must be deleted from the
92 // cache when going back past listed parent depths.
94 cacheCleanupList;
95};
96
98class DebugTypeGenerator {
99public:
102 using ModuleScopeFn = std::function<mlir::LLVM::DIScopeAttr(
104
105 DebugTypeGenerator(mlir::ModuleOp module, mlir::SymbolTable *symbolTable,
106 const mlir::DataLayout &dl,
107 bool boundsInStackSlots = false,
108 ModuleScopeFn getModuleScope = {});
109
110 mlir::LLVM::DITypeAttr convertType(mlir::Type Ty,
111 mlir::LLVM::DIFileAttr fileAttr,
112 mlir::LLVM::DIScopeAttr scope,
113 fir::cg::XDeclareOp declOp);
114
117 bool hasBoundSlot(mlir::Value val, fir::cg::XDeclareOp declOp) const;
118
119private:
120 mlir::LLVM::DITypeAttr convertRecordType(fir::RecordType Ty,
121 mlir::LLVM::DIFileAttr fileAttr,
122 mlir::LLVM::DIScopeAttr scope,
123 fir::cg::XDeclareOp declOp);
124 mlir::LLVM::DITypeAttr convertTupleType(mlir::TupleType Ty,
125 mlir::LLVM::DIFileAttr fileAttr,
126 mlir::LLVM::DIScopeAttr scope,
127 fir::cg::XDeclareOp declOp);
128 mlir::LLVM::DITypeAttr convertSequenceType(fir::SequenceType seqTy,
129 mlir::LLVM::DIFileAttr fileAttr,
130 mlir::LLVM::DIScopeAttr scope,
131 fir::cg::XDeclareOp declOp);
132 mlir::LLVM::DITypeAttr convertVectorType(fir::VectorType vecTy,
133 mlir::LLVM::DIFileAttr fileAttr,
134 mlir::LLVM::DIScopeAttr scope,
135 fir::cg::XDeclareOp declOp);
136
141 mlir::LLVM::DITypeAttr convertBoxedSequenceType(
142 fir::SequenceType seqTy, mlir::LLVM::DIFileAttr fileAttr,
143 mlir::LLVM::DIScopeAttr scope, fir::cg::XDeclareOp declOp,
144 bool genAllocated, bool genAssociated);
145 mlir::LLVM::DITypeAttr convertCharacterType(fir::CharacterType charTy,
146 mlir::LLVM::DIFileAttr fileAttr,
147 mlir::LLVM::DIScopeAttr scope,
148 fir::cg::XDeclareOp declOp,
149 bool hasDescriptor,
150 bool genStringLocation = true);
151
152 mlir::LLVM::DITypeAttr convertPointerLikeType(mlir::Type elTy,
153 mlir::LLVM::DIFileAttr fileAttr,
154 mlir::LLVM::DIScopeAttr scope,
155 fir::cg::XDeclareOp declOp,
156 bool genAllocated,
157 bool genAssociated);
158 mlir::LLVM::DILocalVariableAttr
159 generateArtificialVariable(mlir::MLIRContext *context, mlir::Value Val,
160 mlir::LLVM::DIFileAttr fileAttr,
161 mlir::LLVM::DIScopeAttr scope,
162 fir::cg::XDeclareOp declOp);
163 std::pair<std::uint64_t, unsigned short>
164 getFieldSizeAndAlign(mlir::Type fieldTy);
165
166 mlir::ModuleOp module;
167 mlir::SymbolTable *symbolTable;
168 const mlir::DataLayout *dataLayout;
169 bool boundsInStackSlots;
170 struct ArtificialVar {
172 fir::AllocaOp slot;
173 mlir::LLVM::DILocalVariableAttr var;
174 };
177 llvm::DenseMap<std::pair<mlir::Value, mlir::Operation *>, ArtificialVar>
178 artificialVars;
179 KindMapping kindMapping;
180 fir::LLVMTypeConverter llvmTypeConverter;
181 ModuleScopeFn getModuleScope;
182 std::uint64_t dimsSize;
183 std::uint64_t dimsOffset;
184 std::uint64_t ptrSize;
185 std::uint64_t lenOffset;
186 std::uint64_t rankOffset;
187 std::uint64_t rankSize;
188 DerivedTypeCache derivedTypeCache;
189};
190
191} // namespace fir
192
193#endif // FORTRAN_OPTIMIZER_TRANSFORMS_DEBUGTYPEGENERATOR_H
std::function< mlir::LLVM::DIScopeAttr( llvm::ArrayRef< std::string > modules)> ModuleScopeFn
Definition DebugTypeGenerator.h:102
bool hasBoundSlot(mlir::Value val, fir::cg::XDeclareOp declOp) const
Definition DebugTypeGenerator.cpp:116
Definition DebugTypeGenerator.h:34
Definition TypeConverter.h:49
Definition FIRType.h:106
Definition OpenACC.h:20
Definition AbstractConverter.h:37