Warning: This file is not a C or C++ file. It does not have highlighting.
1 | //===- Lower/ConvertVariable.h -- lowering of variables to FIR --*- 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 | /// Instantiation of pft::Variable in FIR/MLIR. |
14 | /// |
15 | //===----------------------------------------------------------------------===// |
16 | |
17 | #ifndef FORTRAN_LOWER_CONVERT_VARIABLE_H |
18 | #define FORTRAN_LOWER_CONVERT_VARIABLE_H |
19 | |
20 | #include "flang/Lower/Support/Utils.h" |
21 | #include "flang/Optimizer/Dialect/FIRAttr.h" |
22 | #include "flang/Semantics/symbol.h" |
23 | #include "mlir/IR/Value.h" |
24 | #include "llvm/ADT/DenseMap.h" |
25 | |
26 | namespace fir { |
27 | class ExtendedValue; |
28 | class FirOpBuilder; |
29 | class GlobalOp; |
30 | class FortranVariableFlagsAttr; |
31 | } // namespace fir |
32 | |
33 | namespace Fortran { |
34 | namespace semantics { |
35 | class Scope; |
36 | } // namespace semantics |
37 | |
38 | namespace lower { |
39 | class AbstractConverter; |
40 | class CallerInterface; |
41 | class StatementContext; |
42 | class SymMap; |
43 | namespace pft { |
44 | struct Variable; |
45 | } |
46 | |
47 | /// AggregateStoreMap is used to keep track of instantiated aggregate stores |
48 | /// when lowering a scope containing equivalences (aliases). It must only be |
49 | /// owned by the code lowering a scope and provided to instantiateVariable. |
50 | using AggregateStoreKey = |
51 | std::tuple<const Fortran::semantics::Scope *, std::size_t>; |
52 | using AggregateStoreMap = llvm::DenseMap<AggregateStoreKey, mlir::Value>; |
53 | |
54 | /// Instantiate variable \p var and add it to \p symMap. |
55 | /// The AbstractConverter builder must be set. |
56 | /// The AbstractConverter own symbol mapping is not used during the |
57 | /// instantiation and can be different form \p symMap. |
58 | void instantiateVariable(AbstractConverter &, const pft::Variable &var, |
59 | SymMap &symMap, AggregateStoreMap &storeMap); |
60 | |
61 | /// Create a fir::GlobalOp given a module variable definition. This is intended |
62 | /// to be used when lowering a module definition, not when lowering variables |
63 | /// used from a module. For used variables instantiateVariable must directly be |
64 | /// called. |
65 | void defineModuleVariable(AbstractConverter &, const pft::Variable &var); |
66 | |
67 | /// Create fir::GlobalOp for all common blocks, including their initial values |
68 | /// if they have one. This should be called before lowering any scopes so that |
69 | /// common block globals are available when a common appear in a scope. |
70 | void defineCommonBlocks( |
71 | AbstractConverter &, |
72 | const std::vector<std::pair<semantics::SymbolRef, std::size_t>> |
73 | &commonBlocks); |
74 | |
75 | /// The COMMON block is a global structure. \p commonValue is the base address |
76 | /// of the COMMON block. As the offset from the symbol \p sym, generate the |
77 | /// COMMON block member value (commonValue + offset) for the symbol. |
78 | mlir::Value genCommonBlockMember(AbstractConverter &converter, |
79 | mlir::Location loc, |
80 | const Fortran::semantics::Symbol &sym, |
81 | mlir::Value commonValue); |
82 | |
83 | /// Lower a symbol attributes given an optional storage \p and add it to the |
84 | /// provided symbol map. If \preAlloc is not provided, a temporary storage will |
85 | /// be allocated. This is a low level function that should only be used if |
86 | /// instantiateVariable cannot be called. |
87 | void mapSymbolAttributes(AbstractConverter &, const pft::Variable &, SymMap &, |
88 | StatementContext &, mlir::Value preAlloc = {}); |
89 | void mapSymbolAttributes(AbstractConverter &, const semantics::SymbolRef &, |
90 | SymMap &, StatementContext &, |
91 | mlir::Value preAlloc = {}); |
92 | |
93 | /// Instantiate the variables that appear in the specification expressions |
94 | /// of the result of a function call. The instantiated variables are added |
95 | /// to \p symMap. |
96 | void mapCallInterfaceSymbolsForResult( |
97 | AbstractConverter &, const Fortran::lower::CallerInterface &caller, |
98 | SymMap &symMap); |
99 | |
100 | /// Instantiate the variables that appear in the specification expressions |
101 | /// of a dummy argument of a procedure call. The instantiated variables are |
102 | /// added to \p symMap. |
103 | void mapCallInterfaceSymbolsForDummyArgument( |
104 | AbstractConverter &, const Fortran::lower::CallerInterface &caller, |
105 | SymMap &symMap, const Fortran::semantics::Symbol &dummySymbol); |
106 | |
107 | // TODO: consider saving the initial expression symbol dependence analysis in |
108 | // in the PFT variable and dealing with the dependent symbols instantiation in |
109 | // the fir::GlobalOp body at the fir::GlobalOp creation point rather than by |
110 | // having genExtAddrInInitializer and genInitialDataTarget custom entry points |
111 | // here to deal with this while lowering the initial expression value. |
112 | |
113 | /// Create initial-data-target fir.box in a global initializer region. |
114 | /// This handles the local instantiation of the target variable. |
115 | mlir::Value genInitialDataTarget(Fortran::lower::AbstractConverter &, |
116 | mlir::Location, mlir::Type boxType, |
117 | const SomeExpr &initialTarget, |
118 | bool couldBeInEquivalence = false); |
119 | |
120 | /// Call \p genInit to generate code inside \p global initializer region. |
121 | void createGlobalInitialization( |
122 | fir::FirOpBuilder &builder, fir::GlobalOp global, |
123 | std::function<void(fir::FirOpBuilder &)> genInit); |
124 | |
125 | /// Generate address \p addr inside an initializer. |
126 | fir::ExtendedValue |
127 | genExtAddrInInitializer(Fortran::lower::AbstractConverter &converter, |
128 | mlir::Location loc, const SomeExpr &addr); |
129 | |
130 | /// Create a global variable for an intrinsic module object. |
131 | void createIntrinsicModuleGlobal(Fortran::lower::AbstractConverter &converter, |
132 | const pft::Variable &); |
133 | |
134 | /// Create a global variable for a compiler generated object that describes a |
135 | /// derived type for the runtime. |
136 | void createRuntimeTypeInfoGlobal(Fortran::lower::AbstractConverter &converter, |
137 | const Fortran::semantics::Symbol &typeInfoSym); |
138 | |
139 | /// Translate the Fortran attributes of \p sym into the FIR variable attribute |
140 | /// representation. |
141 | fir::FortranVariableFlagsAttr |
142 | translateSymbolAttributes(mlir::MLIRContext *mlirContext, |
143 | const Fortran::semantics::Symbol &sym, |
144 | fir::FortranVariableFlagsEnum extraFlags = |
145 | fir::FortranVariableFlagsEnum::None); |
146 | |
147 | /// Translate the CUDA Fortran attributes of \p sym into the FIR CUDA attribute |
148 | /// representation. |
149 | fir::CUDADataAttributeAttr |
150 | translateSymbolCUDADataAttribute(mlir::MLIRContext *mlirContext, |
151 | const Fortran::semantics::Symbol &sym); |
152 | |
153 | /// Map a symbol to a given fir::ExtendedValue. This will generate an |
154 | /// hlfir.declare when lowering to HLFIR and map the hlfir.declare result to the |
155 | /// symbol. |
156 | void genDeclareSymbol(Fortran::lower::AbstractConverter &converter, |
157 | Fortran::lower::SymMap &symMap, |
158 | const Fortran::semantics::Symbol &sym, |
159 | const fir::ExtendedValue &exv, |
160 | fir::FortranVariableFlagsEnum extraFlags = |
161 | fir::FortranVariableFlagsEnum::None, |
162 | bool force = false); |
163 | |
164 | /// Given the Fortran type of a Cray pointee, return the fir.box type used to |
165 | /// track the cray pointee as Fortran pointer. |
166 | mlir::Type getCrayPointeeBoxType(mlir::Type); |
167 | |
168 | } // namespace lower |
169 | } // namespace Fortran |
170 | #endif // FORTRAN_LOWER_CONVERT_VARIABLE_H |
171 |
Warning: This file is not a C or C++ file. It does not have highlighting.