FLANG
AbstractConverter.h
1//===-- Lower/AbstractConverter.h -------------------------------*- 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_LOWER_ABSTRACTCONVERTER_H
14#define FORTRAN_LOWER_ABSTRACTCONVERTER_H
15
17#include "flang/Lower/PFTDefs.h"
18#include "flang/Lower/Support/Utils.h"
19#include "flang/Optimizer/Builder/BoxValue.h"
20#include "flang/Optimizer/Dialect/FIRAttr.h"
21#include "flang/Semantics/symbol.h"
22#include "flang/Support/Fortran.h"
23#include "mlir/IR/Builders.h"
24#include "mlir/IR/BuiltinOps.h"
25#include "mlir/IR/Operation.h"
26#include "llvm/ADT/ArrayRef.h"
27#include "llvm/ADT/DenseMap.h"
28
29namespace mlir {
30class SymbolTable;
31class StateStack;
32}
33
34namespace fir {
35class KindMapping;
36class FirOpBuilder;
37} // namespace fir
38
39namespace Fortran {
40namespace common {
41template <typename>
42class Reference;
43}
44
45namespace evaluate {
46struct DataRef;
47template <typename>
48class Expr;
49class FoldingContext;
50struct SomeType;
51} // namespace evaluate
52
53namespace parser {
54class CharBlock;
55}
56namespace semantics {
57class Symbol;
58class Scope;
59class DerivedTypeSpec;
60} // namespace semantics
61
62namespace lower {
63class SymMap;
64struct SymbolBox;
65namespace pft {
66struct Variable;
67struct FunctionLikeUnit;
68} // namespace pft
69
70using SomeExpr = Fortran::evaluate::Expr<Fortran::evaluate::SomeType>;
71using SymbolRef = Fortran::common::Reference<const Fortran::semantics::Symbol>;
72using TypeConstructionStack =
73 llvm::DenseMap<const Fortran::semantics::Scope *, mlir::Type>;
75
76using ExprToValueMap = llvm::DenseMap<const SomeExpr *, mlir::Value>;
77
78//===----------------------------------------------------------------------===//
79// AbstractConverter interface
80//===----------------------------------------------------------------------===//
81
85class AbstractConverter {
86public:
87 //===--------------------------------------------------------------------===//
88 // Symbols
89 //===--------------------------------------------------------------------===//
90
92 virtual mlir::Value getSymbolAddress(SymbolRef sym) = 0;
93
94 virtual fir::ExtendedValue
95 symBoxToExtendedValue(const Fortran::lower::SymbolBox &symBox) = 0;
96
97 virtual fir::ExtendedValue
98 getSymbolExtendedValue(const Fortran::semantics::Symbol &sym,
99 Fortran::lower::SymMap *symMap = nullptr) = 0;
100
102 virtual mlir::Value impliedDoBinding(llvm::StringRef name) = 0;
103
105 virtual void copySymbolBinding(SymbolRef src, SymbolRef target) = 0;
106
109 virtual void bindSymbol(SymbolRef sym, const fir::ExtendedValue &exval) = 0;
110
116 virtual void
117 bindSymbolStorage(SymbolRef sym,
119
124 getSymbolStorage(SymbolRef sym) = 0;
125
129
134 virtual void overrideExprValues(const ExprToValueMap *) = 0;
135 void resetExprOverrides() { overrideExprValues(nullptr); }
136 virtual const ExprToValueMap *getExprOverrides() = 0;
137
139 virtual bool lookupLabelSet(SymbolRef sym, pft::LabelSet &labelSet) = 0;
140
142 virtual pft::Evaluation *lookupLabel(pft::Label label) = 0;
143
148 virtual bool
150 bool skipDefaultInit) = 0;
151
152 virtual void
153 createHostAssociateVarCloneDealloc(const Fortran::semantics::Symbol &sym) = 0;
154
163 virtual void
165 mlir::OpBuilder::InsertPoint *copyAssignIP = nullptr,
166 bool hostIsSource = true) = 0;
167
168 virtual void copyVar(mlir::Location loc, mlir::Value dst, mlir::Value src,
169 fir::FortranVariableFlagsEnum attrs) = 0;
170
173 virtual bool
175
183 virtual void collectSymbolSet(
184 pft::Evaluation &eval,
185 llvm::SetVector<const Fortran::semantics::Symbol *> &symbolSet,
186 Fortran::semantics::Symbol::Flag flag, bool collectSymbols = true,
187 bool collectHostAssociatedSymbols = false) = 0;
188
195 virtual llvm::StringRef
196 getUniqueLitName(mlir::Location loc,
197 std::unique_ptr<Fortran::lower::SomeExpr> expression,
198 mlir::Type eleTy) = 0;
199
200 //===--------------------------------------------------------------------===//
201 // Expressions
202 //===--------------------------------------------------------------------===//
203
208 virtual fir::ExtendedValue genExprAddr(const SomeExpr &expr,
209 StatementContext &context,
210 mlir::Location *locPtr = nullptr) = 0;
211
213 fir::ExtendedValue genExprAddr(mlir::Location loc, const SomeExpr *expr,
214 StatementContext &stmtCtx) {
215 return genExprAddr(*expr, stmtCtx, &loc);
216 }
217 fir::ExtendedValue genExprAddr(mlir::Location loc, const SomeExpr &expr,
218 StatementContext &stmtCtx) {
219 return genExprAddr(expr, stmtCtx, &loc);
220 }
221
223 virtual fir::ExtendedValue genExprValue(const SomeExpr &expr,
224 StatementContext &context,
225 mlir::Location *locPtr = nullptr) = 0;
226
228 fir::ExtendedValue genExprValue(mlir::Location loc, const SomeExpr *expr,
229 StatementContext &stmtCtx) {
230 return genExprValue(*expr, stmtCtx, &loc);
231 }
232 fir::ExtendedValue genExprValue(mlir::Location loc, const SomeExpr &expr,
233 StatementContext &stmtCtx) {
234 return genExprValue(expr, stmtCtx, &loc);
235 }
236
240 virtual fir::ExtendedValue genExprBox(mlir::Location loc,
241 const SomeExpr &expr,
242 StatementContext &stmtCtx) = 0;
243
247 virtual fir::MutableBoxValue genExprMutableBox(mlir::Location loc,
248 const SomeExpr &expr) = 0;
249
253
256 virtual mlir::Value hostAssocTupleValue() = 0;
257
260 virtual void bindHostAssocTuple(mlir::Value val) = 0;
261
265 virtual mlir::Value dummyArgsScopeValue() const = 0;
266
271 virtual bool
272 isRegisteredDummySymbol(Fortran::semantics::SymbolRef symRef) const = 0;
273
277
279 virtual void checkCoarrayEnabled() = 0;
280
281 //===--------------------------------------------------------------------===//
282 // Types
283 //===--------------------------------------------------------------------===//
284
286 virtual mlir::Type genType(const SomeExpr &) = 0;
288 virtual mlir::Type genType(SymbolRef) = 0;
290 virtual mlir::Type genType(Fortran::common::TypeCategory tc) = 0;
292 virtual mlir::Type
293 genType(Fortran::common::TypeCategory tc, int kind,
294 llvm::ArrayRef<std::int64_t> lenParameters = {}) = 0;
296 virtual mlir::Type genType(const Fortran::semantics::DerivedTypeSpec &) = 0;
298 virtual mlir::Type genType(const pft::Variable &) = 0;
299
302 virtual void
303 registerTypeInfo(mlir::Location loc, SymbolRef typeInfoSym,
305 fir::RecordType type) = 0;
306
309 virtual TypeConstructionStack &getTypeConstructionStack() = 0;
310
311 //===--------------------------------------------------------------------===//
312 // Locations
313 //===--------------------------------------------------------------------===//
314
316 virtual mlir::Location getCurrentLocation() = 0;
318 virtual mlir::Location genUnknownLocation() = 0;
320 virtual mlir::Location genLocation(const Fortran::parser::CharBlock &) = 0;
321
324
325 //===--------------------------------------------------------------------===//
326 // FIR/MLIR
327 //===--------------------------------------------------------------------===//
328
332 virtual mlir::ModuleOp getModuleOp() = 0;
334 virtual mlir::MLIRContext &getMLIRContext() = 0;
336 virtual std::string mangleName(const Fortran::semantics::Symbol &) = 0;
338 virtual std::string
341 virtual std::string mangleName(std::string &) = 0;
343 virtual std::string mangleName(std::string &, const semantics::Scope &) = 0;
346 virtual std::string
348
350 virtual const fir::KindMapping &getKindMap() = 0;
351
352 virtual Fortran::lower::StatementContext &getFctCtx() = 0;
353
354 AbstractConverter(const Fortran::lower::LoweringOptions &loweringOptions)
355 : loweringOptions(loweringOptions) {}
356 virtual ~AbstractConverter() = default;
357
358 //===--------------------------------------------------------------------===//
359 // Miscellaneous
360 //===--------------------------------------------------------------------===//
361
363 virtual void genEval(pft::Evaluation &eval,
364 bool unstructuredContext = true) = 0;
365
368 return loweringOptions;
369 }
370
375
379
387 virtual mlir::SymbolTable *getMLIRSymbolTable() = 0;
388
389 virtual mlir::StateStack &getStateStack() = 0;
390
391private:
393 const Fortran::lower::LoweringOptions &loweringOptions;
394};
395
396} // namespace lower
397} // namespace Fortran
398
399#endif // FORTRAN_LOWER_ABSTRACTCONVERTER_H
Definition common.h:214
Definition common.h:216
Definition AbstractConverter.h:85
virtual mlir::Value impliedDoBinding(llvm::StringRef name)=0
Get the binding of an implied do variable by name.
virtual void copyHostAssociateVar(const Fortran::semantics::Symbol &sym, mlir::OpBuilder::InsertPoint *copyAssignIP=nullptr, bool hostIsSource=true)=0
virtual Fortran::lower::SymMap & getSymbolMap()=0
virtual mlir::Location genLocation(const Fortran::parser::CharBlock &)=0
Generate the location as converted from a CharBlock.
virtual pft::Evaluation * lookupLabel(pft::Label label)=0
Get the code defined by a label.
virtual mlir::MLIRContext & getMLIRContext()=0
Get the MLIRContext.
virtual fir::MutableBoxValue genExprMutableBox(mlir::Location loc, const SomeExpr &expr)=0
fir::ExtendedValue genExprAddr(mlir::Location loc, const SomeExpr *expr, StatementContext &stmtCtx)
Generate the address of the location holding the expression, expr.
Definition AbstractConverter.h:213
virtual mlir::Value getSymbolAddress(SymbolRef sym)=0
Get the mlir instance of a symbol.
virtual Fortran::lower::SymbolBox shallowLookupSymbol(const Fortran::semantics::Symbol &sym)=0
Find the symbol in the inner-most level of the local map or return null.
virtual mlir::Location getCurrentLocation()=0
Get the converter's current location.
virtual Fortran::lower::SymbolBox lookupOneLevelUpSymbol(const Fortran::semantics::Symbol &sym)=0
virtual llvm::StringRef getUniqueLitName(mlir::Location loc, std::unique_ptr< Fortran::lower::SomeExpr > expression, mlir::Type eleTy)=0
virtual void checkCoarrayEnabled()=0
Check support of Multi-image features if -fcoarray is provided.
virtual std::string mangleName(std::string &)=0
Unique a compiler generated name (add a containing scope specific prefix)
virtual mlir::Location genUnknownLocation()=0
Generate a dummy location.
virtual mlir::ModuleOp getModuleOp()=0
Get the ModuleOp.
virtual Fortran::lower::SymMap::StorageDesc getSymbolStorage(SymbolRef sym)=0
virtual bool createHostAssociateVarClone(const Fortran::semantics::Symbol &sym, bool skipDefaultInit)=0
virtual mlir::Type genType(const SomeExpr &)=0
Generate the type of an Expr.
virtual bool isPresentShallowLookup(const Fortran::semantics::Symbol &sym)=0
virtual fir::ExtendedValue genExprValue(const SomeExpr &expr, StatementContext &context, mlir::Location *locPtr=nullptr)=0
Generate the computations of the expression to produce a value.
virtual fir::ExtendedValue genExprBox(mlir::Location loc, const SomeExpr &expr, StatementContext &stmtCtx)=0
virtual void overrideExprValues(const ExprToValueMap *)=0
virtual void bindSymbolStorage(SymbolRef sym, Fortran::lower::SymMap::StorageDesc storage)=0
virtual fir::FirOpBuilder & getFirOpBuilder()=0
Get the OpBuilder.
virtual mlir::Type genType(SymbolRef)=0
Generate the type of a Symbol.
virtual void registerTypeInfo(mlir::Location loc, SymbolRef typeInfoSym, const Fortran::semantics::DerivedTypeSpec &typeSpec, fir::RecordType type)=0
virtual mlir::Type genType(const pft::Variable &)=0
Generate the type from a Variable.
virtual const Fortran::semantics::Scope & getCurrentScope()=0
Get the converter's current scope.
virtual mlir::Type genType(Fortran::common::TypeCategory tc, int kind, llvm::ArrayRef< std::int64_t > lenParameters={})=0
Generate the type from a category and kind and length parameters.
virtual std::string getRecordTypeFieldName(const Fortran::semantics::Symbol &component)=0
virtual void genEval(pft::Evaluation &eval, bool unstructuredContext=true)=0
Generate IR for Evaluation eval.
virtual TypeConstructionStack & getTypeConstructionStack()=0
virtual void bindHostAssocTuple(mlir::Value val)=0
virtual const Fortran::lower::pft::FunctionLikeUnit * getCurrentFunctionUnit() const =0
Returns the FunctionLikeUnit being lowered, if any.
virtual mlir::Value hostAssocTupleValue()=0
virtual void copySymbolBinding(SymbolRef src, SymbolRef target)=0
Copy the binding of src to target symbol.
virtual Fortran::evaluate::FoldingContext & getFoldingContext()=0
virtual std::string mangleName(std::string &, const semantics::Scope &)=0
Unique a compiler generated name (add a provided scope specific prefix)
virtual void bindSymbol(SymbolRef sym, const fir::ExtendedValue &exval)=0
virtual mlir::SymbolTable * getMLIRSymbolTable()=0
virtual mlir::Type genType(const Fortran::semantics::DerivedTypeSpec &)=0
Generate the type from a DerivedTypeSpec.
virtual mlir::Type genType(Fortran::common::TypeCategory tc)=0
Generate the type from a category.
virtual mlir::Value dummyArgsScopeValue() const =0
virtual void collectSymbolSet(pft::Evaluation &eval, llvm::SetVector< const Fortran::semantics::Symbol * > &symbolSet, Fortran::semantics::Symbol::Flag flag, bool collectSymbols=true, bool collectHostAssociatedSymbols=false)=0
virtual fir::ExtendedValue genExprAddr(const SomeExpr &expr, StatementContext &context, mlir::Location *locPtr=nullptr)=0
virtual bool isRegisteredDummySymbol(Fortran::semantics::SymbolRef symRef) const =0
fir::ExtendedValue genExprValue(mlir::Location loc, const SomeExpr *expr, StatementContext &stmtCtx)
Generate the computations of the expression, expr, to produce a value.
Definition AbstractConverter.h:228
virtual std::string mangleName(const Fortran::semantics::DerivedTypeSpec &)=0
Unique a derived type (add a containing scope specific prefix)
const Fortran::lower::LoweringOptions & getLoweringOptions() const
Return options controlling lowering behavior.
Definition AbstractConverter.h:367
virtual bool lookupLabelSet(SymbolRef sym, pft::LabelSet &labelSet)=0
Get the label set associated with a symbol.
virtual std::string mangleName(const Fortran::semantics::Symbol &)=0
Unique a symbol (add a containing scope specific prefix)
virtual const fir::KindMapping & getKindMap()=0
Get the KindMap.
Definition LoweringOptions.h:34
Definition StatementContext.h:46
Definition SymbolMap.h:146
std::pair< mlir::Value, std::uint64_t > StorageDesc
Definition SymbolMap.h:151
Definition char-block.h:28
Definition scope.h:58
Definition symbol.h:788
Definition BoxValue.h:478
Definition FIRBuilder.h:55
Definition KindMapping.h:48
Definition BoxValue.h:360
Definition FIRType.h:89
Definition call.h:34
Definition ParserActions.h:24
Definition check-expression.h:19
Definition bit-population-count.h:20
Definition AbstractConverter.h:34
Definition AbstractConverter.h:29
Definition variable.h:284
Definition type.h:417
Definition SymbolMap.h:52
Definition PFTBuilder.h:221
Definition PFTBuilder.h:615
Definition PFTBuilder.h:410