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/StatementContext.h"
19#include "flang/Lower/Support/Utils.h"
20#include "flang/Lower/SymbolMap.h"
21#include "flang/Optimizer/Builder/BoxValue.h"
22#include "flang/Optimizer/Builder/FIRBuilder.h"
23#include "flang/Optimizer/Dialect/FIRAttr.h"
24#include "flang/Semantics/symbol.h"
25#include "flang/Support/Fortran.h"
26#include "mlir/IR/Builders.h"
27#include "mlir/IR/BuiltinOps.h"
28#include "mlir/IR/Operation.h"
29#include "llvm/ADT/ArrayRef.h"
30#include "llvm/ADT/DenseMap.h"
31
32namespace mlir {
33class SymbolTable;
34class StateStack;
35}
36
37namespace fir {
38class KindMapping;
39} // namespace fir
40
41namespace Fortran {
42namespace common {
43template <typename>
44class Reference;
45}
46
47namespace evaluate {
48struct DataRef;
49template <typename>
50class Expr;
51class FoldingContext;
52struct SomeType;
53} // namespace evaluate
54
55namespace parser {
56class CharBlock;
57}
58namespace semantics {
59class Symbol;
60class Scope;
61class DerivedTypeSpec;
62} // namespace semantics
63
64namespace lower {
65class SymMap;
66struct SymbolBox;
67namespace pft {
68struct Variable;
69struct FunctionLikeUnit;
70} // namespace pft
71
72using SomeExpr = Fortran::evaluate::Expr<Fortran::evaluate::SomeType>;
73using SymbolRef = Fortran::common::Reference<const Fortran::semantics::Symbol>;
74using TypeConstructionStack =
75 llvm::DenseMap<const Fortran::semantics::Scope *, mlir::Type>;
77
78using ExprToValueMap = llvm::DenseMap<const SomeExpr *, mlir::Value>;
79
80//===----------------------------------------------------------------------===//
81// AbstractConverter interface
82//===----------------------------------------------------------------------===//
83
87class AbstractConverter {
88public:
89 //===--------------------------------------------------------------------===//
90 // Symbols
91 //===--------------------------------------------------------------------===//
92
94 virtual mlir::Value getSymbolAddress(SymbolRef sym) = 0;
95
96 virtual fir::ExtendedValue
97 symBoxToExtendedValue(const Fortran::lower::SymbolBox &symBox) = 0;
98
99 virtual fir::ExtendedValue
100 getSymbolExtendedValue(const Fortran::semantics::Symbol &sym,
101 Fortran::lower::SymMap *symMap = nullptr) = 0;
102
104 virtual mlir::Value impliedDoBinding(llvm::StringRef name) = 0;
105
107 virtual void copySymbolBinding(SymbolRef src, SymbolRef target) = 0;
108
111 virtual void bindSymbol(SymbolRef sym, const fir::ExtendedValue &exval) = 0;
112
118 virtual void
119 bindSymbolStorage(SymbolRef sym,
121
126 getSymbolStorage(SymbolRef sym) = 0;
127
131
136 virtual void overrideExprValues(const ExprToValueMap *) = 0;
137 void resetExprOverrides() { overrideExprValues(nullptr); }
138 virtual const ExprToValueMap *getExprOverrides() = 0;
139
141 virtual bool lookupLabelSet(SymbolRef sym, pft::LabelSet &labelSet) = 0;
142
144 virtual pft::Evaluation *lookupLabel(pft::Label label) = 0;
145
150 virtual bool
152 bool skipDefaultInit) = 0;
153
154 virtual void
155 createHostAssociateVarCloneDealloc(const Fortran::semantics::Symbol &sym) = 0;
156
165 virtual void
167 mlir::OpBuilder::InsertPoint *copyAssignIP = nullptr,
168 bool hostIsSource = true) = 0;
169
170 virtual void copyVar(mlir::Location loc, mlir::Value dst, mlir::Value src,
171 fir::FortranVariableFlagsEnum attrs) = 0;
172
175 virtual bool
177
185 virtual void collectSymbolSet(
186 pft::Evaluation &eval,
187 llvm::SetVector<const Fortran::semantics::Symbol *> &symbolSet,
188 Fortran::semantics::Symbol::Flag flag, bool collectSymbols = true,
189 bool collectHostAssociatedSymbols = false) = 0;
190
197 virtual llvm::StringRef
198 getUniqueLitName(mlir::Location loc,
199 std::unique_ptr<Fortran::lower::SomeExpr> expression,
200 mlir::Type eleTy) = 0;
201
202 //===--------------------------------------------------------------------===//
203 // Expressions
204 //===--------------------------------------------------------------------===//
205
210 virtual fir::ExtendedValue genExprAddr(const SomeExpr &expr,
211 StatementContext &context,
212 mlir::Location *locPtr = nullptr) = 0;
213
215 fir::ExtendedValue genExprAddr(mlir::Location loc, const SomeExpr *expr,
216 StatementContext &stmtCtx) {
217 return genExprAddr(*expr, stmtCtx, &loc);
218 }
219 fir::ExtendedValue genExprAddr(mlir::Location loc, const SomeExpr &expr,
220 StatementContext &stmtCtx) {
221 return genExprAddr(expr, stmtCtx, &loc);
222 }
223
225 virtual fir::ExtendedValue genExprValue(const SomeExpr &expr,
226 StatementContext &context,
227 mlir::Location *locPtr = nullptr) = 0;
228
230 fir::ExtendedValue genExprValue(mlir::Location loc, const SomeExpr *expr,
231 StatementContext &stmtCtx) {
232 return genExprValue(*expr, stmtCtx, &loc);
233 }
234 fir::ExtendedValue genExprValue(mlir::Location loc, const SomeExpr &expr,
235 StatementContext &stmtCtx) {
236 return genExprValue(expr, stmtCtx, &loc);
237 }
238
242 virtual fir::ExtendedValue genExprBox(mlir::Location loc,
243 const SomeExpr &expr,
244 StatementContext &stmtCtx) = 0;
245
249 virtual fir::MutableBoxValue genExprMutableBox(mlir::Location loc,
250 const SomeExpr &expr) = 0;
251
255
258 virtual mlir::Value hostAssocTupleValue() = 0;
259
262 virtual void bindHostAssocTuple(mlir::Value val) = 0;
263
267 virtual mlir::Value dummyArgsScopeValue() const = 0;
268
273 virtual bool
274 isRegisteredDummySymbol(Fortran::semantics::SymbolRef symRef) const = 0;
275
279 virtual unsigned
281
285
287 virtual void checkCoarrayEnabled() = 0;
288
289 //===--------------------------------------------------------------------===//
290 // Types
291 //===--------------------------------------------------------------------===//
292
294 virtual mlir::Type genType(const SomeExpr &) = 0;
296 virtual mlir::Type genType(SymbolRef) = 0;
298 virtual mlir::Type genType(Fortran::common::TypeCategory tc) = 0;
300 virtual mlir::Type
301 genType(Fortran::common::TypeCategory tc, int kind,
302 llvm::ArrayRef<std::int64_t> lenParameters = {}) = 0;
304 virtual mlir::Type genType(const Fortran::semantics::DerivedTypeSpec &) = 0;
306 virtual mlir::Type genType(const pft::Variable &) = 0;
307
310 virtual void
311 registerTypeInfo(mlir::Location loc, SymbolRef typeInfoSym,
313 fir::RecordType type) = 0;
314
317 virtual TypeConstructionStack &getTypeConstructionStack() = 0;
318
319 //===--------------------------------------------------------------------===//
320 // Locations
321 //===--------------------------------------------------------------------===//
322
324 virtual mlir::Location getCurrentLocation() = 0;
326 virtual mlir::Location genUnknownLocation() = 0;
328 virtual mlir::Location genLocation(const Fortran::parser::CharBlock &) = 0;
329
332
333 //===--------------------------------------------------------------------===//
334 // FIR/MLIR
335 //===--------------------------------------------------------------------===//
336
340 virtual mlir::ModuleOp getModuleOp() = 0;
342 virtual mlir::MLIRContext &getMLIRContext() = 0;
344 virtual std::string mangleName(const Fortran::semantics::Symbol &) = 0;
346 virtual std::string
349 virtual std::string mangleName(std::string &) = 0;
351 virtual std::string mangleName(std::string &, const semantics::Scope &) = 0;
354 virtual std::string
356
358 virtual const fir::KindMapping &getKindMap() = 0;
359
360 virtual Fortran::lower::StatementContext &getFctCtx() = 0;
361
363 virtual std::pair<mlir::Value, mlir::Value>
364 genStatAndErrmsg(mlir::Location loc,
365 const std::list<Fortran::parser::StatOrErrmsg> &) = 0;
366
367 AbstractConverter(const Fortran::lower::LoweringOptions &loweringOptions)
368 : loweringOptions(loweringOptions) {}
369 virtual ~AbstractConverter() = default;
370
371 //===--------------------------------------------------------------------===//
372 // Miscellaneous
373 //===--------------------------------------------------------------------===//
374
376 virtual void genEval(pft::Evaluation &eval,
377 bool unstructuredContext = true) = 0;
378
381 return loweringOptions;
382 }
383
388
392
400 virtual mlir::SymbolTable *getMLIRSymbolTable() = 0;
401
402 virtual mlir::StateStack &getStateStack() = 0;
403
404private:
406 const Fortran::lower::LoweringOptions &loweringOptions;
407};
408
409} // namespace lower
410} // namespace Fortran
411
412#endif // FORTRAN_LOWER_ABSTRACTCONVERTER_H
Definition common.h:214
Definition common.h:216
Definition AbstractConverter.h:87
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:215
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 std::pair< mlir::Value, mlir::Value > genStatAndErrmsg(mlir::Location loc, const std::list< Fortran::parser::StatOrErrmsg > &)=0
Generate STAT and ERRMSG from a list of StatOrErrmsg.
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 unsigned getDummyArgPosition(const Fortran::semantics::Symbol &sym) const =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:230
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:380
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:182
std::pair< mlir::Value, std::uint64_t > StorageDesc
Definition SymbolMap.h:187
Definition char-block.h:28
Definition scope.h:58
Definition symbol.h:809
Definition BoxValue.h:478
Definition FIRBuilder.h:55
Definition KindMapping.h:48
Definition BoxValue.h:360
Definition FIRType.h:92
Definition call.h:34
Definition ParserActions.h:24
Definition check-expression.h:19
Definition bit-population-count.h:20
Definition AbstractConverter.h:37
Definition AbstractConverter.h:32
Definition variable.h:288
Definition type.h:417
Definition SymbolMap.h:53
Definition PFTBuilder.h:221
Definition PFTBuilder.h:615
Definition PFTBuilder.h:410