FLANG
SymbolMap.h
1//===-- SymbolMap.h -- lowering internal symbol map -------------*- 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_SYMBOLMAP_H
14#define FORTRAN_LOWER_SYMBOLMAP_H
15
16#include "flang/Common/reference.h"
17#include "flang/Optimizer/Builder/BoxValue.h"
18#include "flang/Optimizer/Dialect/FIRType.h"
19#include "flang/Optimizer/Dialect/FortranVariableInterface.h"
20#include "flang/Optimizer/Support/Matcher.h"
21#include "flang/Semantics/symbol.h"
22#include "mlir/IR/Value.h"
23#include "llvm/ADT/ArrayRef.h"
24#include "llvm/ADT/DenseMap.h"
25#include "llvm/ADT/SmallVector.h"
26#include "llvm/Support/Compiler.h"
27#include <optional>
28
29namespace Fortran::lower {
30
31struct SymbolBox;
32class SymMap;
33llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const SymbolBox &symMap);
34llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const SymMap &symMap);
35
36//===----------------------------------------------------------------------===//
37// Symbol information
38//===----------------------------------------------------------------------===//
39
52struct SymbolBox : public fir::details::matcher<SymbolBox> {
53 // For lookups that fail, have a monostate
54 using None = std::monostate;
55
56 // Trivial intrinsic type
57 using Intrinsic = fir::AbstractBox;
58
59 // Array variable that uses bounds notation
60 using FullDim = fir::ArrayBoxValue;
61
62 // CHARACTER type variable with its dependent type LEN parameter
63 using Char = fir::CharBoxValue;
64
65 // CHARACTER array variable using bounds notation
66 using CharFullDim = fir::CharArrayBoxValue;
67
68 // Pointer or allocatable variable
69 using PointerOrAllocatable = fir::MutableBoxValue;
70
71 // Non pointer/allocatable variable that must be tracked with
72 // a fir.box (either because it is not contiguous, or assumed rank, or assumed
73 // type, or polymorphic, or because the fir.box is describing an optional
74 // value and cannot be read into one of the other category when lowering the
75 // symbol).
76 using Box = fir::BoxValue;
77
78 using VT =
79 std::variant<Intrinsic, FullDim, Char, CharFullDim, PointerOrAllocatable,
80 Box, fir::FortranVariableOpInterface, None>;
81
82 //===--------------------------------------------------------------------===//
83 // Constructors
84 //===--------------------------------------------------------------------===//
85
86 SymbolBox() : box{None{}} {}
87 template <typename A>
88 SymbolBox(const A &x) : box{x} {}
89
90 explicit operator bool() const { return !std::holds_alternative<None>(box); }
91
92 //===--------------------------------------------------------------------===//
93 // Accessors
94 //===--------------------------------------------------------------------===//
95
99 mlir::Value getAddr() const {
100 return match([](const None &) { return mlir::Value{}; },
101 [](const fir::FortranVariableOpInterface &x) {
102 return fir::FortranVariableOpInterface(x).getBase();
103 },
104 [](const auto &x) { return x.getAddr(); });
105 }
106
107 std::optional<fir::FortranVariableOpInterface>
108 getIfFortranVariableOpInterface() {
109 return match(
110 [](const fir::FortranVariableOpInterface &x)
111 -> std::optional<fir::FortranVariableOpInterface> { return x; },
112 [](const auto &x) -> std::optional<fir::FortranVariableOpInterface> {
113 return std::nullopt;
114 });
115 }
116
118 template <typename ON, typename RT>
119 constexpr RT apply(RT (&&func)(const ON &)) const {
120 if (auto *x = std::get_if<ON>(&box))
121 return func(*x);
122 return RT{};
123 }
124
125 const VT &matchee() const { return box; }
126
127 friend llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
128 const SymbolBox &symBox);
129
131 LLVM_DUMP_METHOD void dump() const;
132
133private:
134 VT box;
135};
136
137//===----------------------------------------------------------------------===//
138// Map of symbol information
139//===----------------------------------------------------------------------===//
140
146class SymMap {
147public:
148 using AcDoVar = llvm::StringRef;
151 using StorageDesc = std::pair<mlir::Value, std::uint64_t>;
152
153 SymMap() { pushScope(); }
154 SymMap(const SymMap &) = delete;
155
156 void pushScope() {
157 symbolMapStack.emplace_back();
158 storageMapStack.emplace_back();
159 }
160 void popScope() {
161 symbolMapStack.pop_back();
162 assert(symbolMapStack.size() >= 1);
163 storageMapStack.pop_back();
164 assert(storageMapStack.size() >= 1);
165 }
166
168 void addSymbol(semantics::SymbolRef sym, const fir::ExtendedValue &ext,
169 bool force = false);
170
172 void addSymbol(semantics::SymbolRef sym, mlir::Value value,
173 bool force = false) {
174 makeSym(sym, SymbolBox::Intrinsic(value), force);
175 }
176
178 void addCharSymbol(semantics::SymbolRef sym, mlir::Value value,
179 mlir::Value len, bool force = false) {
180 makeSym(sym, SymbolBox::Char(value, len), force);
181 }
182 void addCharSymbol(semantics::SymbolRef sym, const SymbolBox::Char &value,
183 bool force = false) {
184 makeSym(sym, value, force);
185 }
186
188 void addSymbolWithShape(semantics::SymbolRef sym, mlir::Value value,
190 bool force = false) {
191 makeSym(sym, SymbolBox::FullDim(value, shape), force);
192 }
193 void addSymbolWithShape(semantics::SymbolRef sym,
194 const SymbolBox::FullDim &value, bool force = false) {
195 makeSym(sym, value, force);
196 }
197
199 void addCharSymbolWithShape(semantics::SymbolRef sym, mlir::Value value,
200 mlir::Value len,
202 bool force = false) {
203 makeSym(sym, SymbolBox::CharFullDim(value, len, shape), force);
204 }
205 void addCharSymbolWithShape(semantics::SymbolRef sym,
206 const SymbolBox::CharFullDim &value,
207 bool force = false) {
208 makeSym(sym, value, force);
209 }
210
212 void addSymbolWithBounds(semantics::SymbolRef sym, mlir::Value value,
215 bool force = false) {
216 makeSym(sym, SymbolBox::FullDim(value, extents, lbounds), force);
217 }
218 void addSymbolWithBounds(semantics::SymbolRef sym,
219 const SymbolBox::FullDim &value,
220 bool force = false) {
221 makeSym(sym, value, force);
222 }
223
225 void addCharSymbolWithBounds(semantics::SymbolRef sym, mlir::Value value,
226 mlir::Value len,
229 bool force = false) {
230 makeSym(sym, SymbolBox::CharFullDim(value, len, extents, lbounds), force);
231 }
232 void addCharSymbolWithBounds(semantics::SymbolRef sym,
233 const SymbolBox::CharFullDim &value,
234 bool force = false) {
235 makeSym(sym, value, force);
236 }
237
238 void addAllocatableOrPointer(semantics::SymbolRef sym,
239 fir::MutableBoxValue box, bool force = false) {
240 makeSym(sym, box, force);
241 }
242
243 void addBoxSymbol(semantics::SymbolRef sym, mlir::Value irBox,
245 llvm::ArrayRef<mlir::Value> explicitParams,
246 llvm::ArrayRef<mlir::Value> explicitExtents,
247 bool force = false) {
248 makeSym(sym,
249 SymbolBox::Box(irBox, lbounds, explicitParams, explicitExtents),
250 force);
251 }
252 void addBoxSymbol(semantics::SymbolRef sym, const SymbolBox::Box &value,
253 bool force = false) {
254 makeSym(sym, value, force);
255 }
256
258 SymbolBox lookupSymbol(semantics::SymbolRef sym);
259 SymbolBox lookupSymbol(const semantics::Symbol *sym) {
260 return lookupSymbol(*sym);
261 }
262
265 const semantics::Symbol *lookupSymbolByName(llvm::StringRef symName);
266
269 SymbolBox shallowLookupSymbol(semantics::SymbolRef sym);
270 SymbolBox shallowLookupSymbol(const semantics::Symbol *sym) {
271 return shallowLookupSymbol(*sym);
272 }
273
276 SymbolBox lookupOneLevelUpSymbol(semantics::SymbolRef sym);
277 SymbolBox lookupOneLevelUpSymbol(const semantics::Symbol *sym) {
278 return lookupOneLevelUpSymbol(*sym);
279 }
280
282 void pushImpliedDoBinding(AcDoVar var, mlir::Value value) {
283 impliedDoStack.emplace_back(var, value);
284 }
285
288 assert(!impliedDoStack.empty());
289 impliedDoStack.pop_back();
290 }
291
294 mlir::Value lookupImpliedDo(AcDoVar var);
295
297 void clear() {
298 symbolMapStack.clear();
299 symbolMapStack.emplace_back();
300 assert(symbolMapStack.size() == 1);
301 impliedDoStack.clear();
302 storageMapStack.clear();
303 storageMapStack.emplace_back();
304 }
305
306 friend llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
307 const SymMap &symMap);
308
310 LLVM_DUMP_METHOD void dump() const;
311
312 void addVariableDefinition(semantics::SymbolRef symRef,
313 fir::FortranVariableOpInterface definingOp,
314 bool force = false) {
315 makeSym(symRef, SymbolBox(definingOp), force);
316 }
317
318 void copySymbolBinding(semantics::SymbolRef src,
319 semantics::SymbolRef target) {
320 auto symBox = lookupSymbol(src);
321 assert(symBox && "source binding does not exists");
322 makeSym(target, symBox, /*force=*/false);
323 }
324
325 std::optional<fir::FortranVariableOpInterface>
326 lookupVariableDefinition(semantics::SymbolRef sym) {
327 if (auto symBox = lookupSymbol(sym))
328 return symBox.getIfFortranVariableOpInterface();
329 return std::nullopt;
330 }
331
335 void registerStorage(semantics::SymbolRef sym, StorageDesc storage);
337 StorageDesc lookupStorage(semantics::SymbolRef sym);
338 StorageDesc lookupStorage(const semantics::Symbol *sym) {
339 return lookupStorage(*sym);
340 }
341
342private:
344 void makeSym(semantics::SymbolRef symRef, const SymbolBox &box,
345 bool force = false) {
346 auto *sym = symRef->HasLocalLocality() ? &*symRef : &symRef->GetUltimate();
347 if (force)
348 symbolMapStack.back().erase(sym);
349 assert(box && "cannot add an undefined symbol box");
350 symbolMapStack.back().try_emplace(sym, box);
351 }
352
353 llvm::SmallVector<llvm::DenseMap<const semantics::Symbol *, SymbolBox>>
354 symbolMapStack;
355
356 // Implied DO induction variables are not represented as Se::Symbol in
357 // Ev::Expr. Keep the variable markers in their own stack.
358 llvm::SmallVector<std::pair<AcDoVar, mlir::Value>> impliedDoStack;
359
360 // A stack of maps between the symbols and their storage descriptors.
361 llvm::SmallVector<llvm::DenseMap<const semantics::Symbol *, StorageDesc>>
362 storageMapStack;
363};
364
366class SymMapScope {
367public:
368 explicit SymMapScope(SymMap &map) : map(map) { map.pushScope(); }
369 ~SymMapScope() { map.popScope(); }
370
371private:
372 SymMap &map;
373};
374
375} // namespace Fortran::lower
376
377#endif // FORTRAN_LOWER_SYMBOLMAP_H
Definition SymbolMap.h:146
void addSymbolWithShape(semantics::SymbolRef sym, mlir::Value value, llvm::ArrayRef< mlir::Value > shape, bool force=false)
Add an array mapping with (address, shape).
Definition SymbolMap.h:188
void pushImpliedDoBinding(AcDoVar var, mlir::Value value)
Add a new binding from the ac-do-variable var to value.
Definition SymbolMap.h:282
SymbolBox lookupSymbol(semantics::SymbolRef sym)
Find symbol and return its value if it appears in the current mappings.
Definition SymbolMap.cpp:37
void addSymbolWithBounds(semantics::SymbolRef sym, mlir::Value value, llvm::ArrayRef< mlir::Value > extents, llvm::ArrayRef< mlir::Value > lbounds, bool force=false)
Add an array mapping with bounds notation.
Definition SymbolMap.h:212
mlir::Value lookupImpliedDo(AcDoVar var)
Definition SymbolMap.cpp:88
void addCharSymbolWithShape(semantics::SymbolRef sym, mlir::Value value, mlir::Value len, llvm::ArrayRef< mlir::Value > shape, bool force=false)
Add an array of CHARACTER mapping.
Definition SymbolMap.h:199
void popImpliedDoBinding()
Pop the most recent implied do binding off the stack.
Definition SymbolMap.h:287
void addSymbol(semantics::SymbolRef sym, mlir::Value value, bool force=false)
Add a trivial symbol mapping to an address.
Definition SymbolMap.h:172
std::pair< mlir::Value, std::uint64_t > StorageDesc
Definition SymbolMap.h:151
void addCharSymbolWithBounds(semantics::SymbolRef sym, mlir::Value value, mlir::Value len, llvm::ArrayRef< mlir::Value > extents, llvm::ArrayRef< mlir::Value > lbounds, bool force=false)
Add an array of CHARACTER with bounds notation.
Definition SymbolMap.h:225
void registerStorage(semantics::SymbolRef sym, StorageDesc storage)
Definition SymbolMap.cpp:95
void clear()
Remove all symbols from the map.
Definition SymbolMap.h:297
SymbolBox lookupOneLevelUpSymbol(semantics::SymbolRef sym)
Definition SymbolMap.cpp:71
void addSymbol(semantics::SymbolRef sym, const fir::ExtendedValue &ext, bool force=false)
Add an extended value to the symbol table.
Definition SymbolMap.cpp:21
void addCharSymbol(semantics::SymbolRef sym, mlir::Value value, mlir::Value len, bool force=false)
Add a scalar CHARACTER mapping to an (address, len).
Definition SymbolMap.h:178
SymbolBox shallowLookupSymbol(semantics::SymbolRef sym)
Definition SymbolMap.cpp:58
StorageDesc lookupStorage(semantics::SymbolRef sym)
Lookup the symbol's storage at the innermost level of the symbol table.
Definition SymbolMap.cpp:103
LLVM_DUMP_METHOD void dump() const
Dump the map. For debugging.
Definition SymbolMap.cpp:114
const semantics::Symbol * lookupSymbolByName(llvm::StringRef symName)
Definition SymbolMap.cpp:49
Abstract base class.
Definition BoxValue.h:61
Definition BoxValue.h:153
Definition BoxValue.h:291
Expressions of type CHARACTER and with rank > 0.
Definition BoxValue.h:170
Definition BoxValue.h:77
Definition BoxValue.h:478
Definition BoxValue.h:360
Definition FIRType.h:89
Definition ParserActions.h:24
Definition SymbolMap.h:52
mlir::Value getAddr() const
Definition SymbolMap.h:99
constexpr RT apply(RT(&&func)(const ON &)) const
Apply the lambda func to this box value.
Definition SymbolMap.h:119
LLVM_DUMP_METHOD void dump() const
Dump the map. For debugging.
Definition SymbolMap.cpp:112
Definition Matcher.h:25