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/Lower/Support/Utils.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
141public:
142 void insert(const Fortran::evaluate::Component &component,
143 fir::FortranVariableOpInterface definingOp) {
144 auto iter = componentMap.find(&component);
145 if (iter != componentMap.end()) {
146 iter->second = definingOp;
147 return;
148 }
149 componentStorage.push_back(
150 std::make_unique<Fortran::evaluate::Component>(component));
151 componentMap.insert({componentStorage.back().get(), definingOp});
152 }
153
154 std::optional<fir::FortranVariableOpInterface>
155 lookup(const Fortran::evaluate::Component *component) const {
156 auto iter = componentMap.find(component);
157 if (iter != componentMap.end())
158 return iter->second;
159 return std::nullopt;
160 }
161
162 LLVM_DUMP_METHOD void dump() const;
163
164private:
165 llvm::DenseMap<const Fortran::evaluate::Component *,
166 fir::FortranVariableOpInterface>
167 componentMap;
169 componentStorage;
170};
171
172//===----------------------------------------------------------------------===//
173// Map of symbol information
174//===----------------------------------------------------------------------===//
175
181class SymMap {
182public:
183 using AcDoVar = llvm::StringRef;
186 using StorageDesc = std::pair<mlir::Value, std::uint64_t>;
187
188 SymMap() { pushScope(); }
189 SymMap(const SymMap &) = delete;
190
191 void pushScope() {
192 symbolMapStack.emplace_back();
193 storageMapStack.emplace_back();
194 componentMapStack.emplace_back();
195 }
196 void popScope() {
197 symbolMapStack.pop_back();
198 assert(symbolMapStack.size() >= 1);
199 storageMapStack.pop_back();
200 assert(storageMapStack.size() >= 1);
201 componentMapStack.pop_back();
202 assert(componentMapStack.size() >= 1);
203 }
204
206 void addSymbol(semantics::SymbolRef sym, const fir::ExtendedValue &ext,
207 bool force = false);
208
210 void addSymbol(semantics::SymbolRef sym, mlir::Value value,
211 bool force = false) {
212 makeSym(sym, SymbolBox::Intrinsic(value), force);
213 }
214
216 void addCharSymbol(semantics::SymbolRef sym, mlir::Value value,
217 mlir::Value len, bool force = false) {
218 makeSym(sym, SymbolBox::Char(value, len), force);
219 }
220 void addCharSymbol(semantics::SymbolRef sym, const SymbolBox::Char &value,
221 bool force = false) {
222 makeSym(sym, value, force);
223 }
224
226 void addSymbolWithShape(semantics::SymbolRef sym, mlir::Value value,
228 bool force = false) {
229 makeSym(sym, SymbolBox::FullDim(value, shape), force);
230 }
231 void addSymbolWithShape(semantics::SymbolRef sym,
232 const SymbolBox::FullDim &value, bool force = false) {
233 makeSym(sym, value, force);
234 }
235
237 void addCharSymbolWithShape(semantics::SymbolRef sym, mlir::Value value,
238 mlir::Value len,
240 bool force = false) {
241 makeSym(sym, SymbolBox::CharFullDim(value, len, shape), force);
242 }
243 void addCharSymbolWithShape(semantics::SymbolRef sym,
244 const SymbolBox::CharFullDim &value,
245 bool force = false) {
246 makeSym(sym, value, force);
247 }
248
250 void addSymbolWithBounds(semantics::SymbolRef sym, mlir::Value value,
253 bool force = false) {
254 makeSym(sym, SymbolBox::FullDim(value, extents, lbounds), force);
255 }
256 void addSymbolWithBounds(semantics::SymbolRef sym,
257 const SymbolBox::FullDim &value,
258 bool force = false) {
259 makeSym(sym, value, force);
260 }
261
263 void addCharSymbolWithBounds(semantics::SymbolRef sym, mlir::Value value,
264 mlir::Value len,
267 bool force = false) {
268 makeSym(sym, SymbolBox::CharFullDim(value, len, extents, lbounds), force);
269 }
270 void addCharSymbolWithBounds(semantics::SymbolRef sym,
271 const SymbolBox::CharFullDim &value,
272 bool force = false) {
273 makeSym(sym, value, force);
274 }
275
276 void addAllocatableOrPointer(semantics::SymbolRef sym,
277 fir::MutableBoxValue box, bool force = false) {
278 makeSym(sym, box, force);
279 }
280
281 void addBoxSymbol(semantics::SymbolRef sym, mlir::Value irBox,
283 llvm::ArrayRef<mlir::Value> explicitParams,
284 llvm::ArrayRef<mlir::Value> explicitExtents,
285 bool force = false) {
286 makeSym(sym,
287 SymbolBox::Box(irBox, lbounds, explicitParams, explicitExtents),
288 force);
289 }
290 void addBoxSymbol(semantics::SymbolRef sym, const SymbolBox::Box &value,
291 bool force = false) {
292 makeSym(sym, value, force);
293 }
294
296 SymbolBox lookupSymbol(semantics::SymbolRef sym);
297 SymbolBox lookupSymbol(const semantics::Symbol *sym) {
298 return lookupSymbol(*sym);
299 }
300
303 const semantics::Symbol *lookupSymbolByName(llvm::StringRef symName);
304
307 SymbolBox shallowLookupSymbol(semantics::SymbolRef sym);
308 SymbolBox shallowLookupSymbol(const semantics::Symbol *sym) {
309 return shallowLookupSymbol(*sym);
310 }
311
314 SymbolBox lookupOneLevelUpSymbol(semantics::SymbolRef sym);
315 SymbolBox lookupOneLevelUpSymbol(const semantics::Symbol *sym) {
316 return lookupOneLevelUpSymbol(*sym);
317 }
318
320 void pushImpliedDoBinding(AcDoVar var, mlir::Value value) {
321 impliedDoStack.emplace_back(var, value);
322 }
323
326 assert(!impliedDoStack.empty());
327 impliedDoStack.pop_back();
328 }
329
332 mlir::Value lookupImpliedDo(AcDoVar var);
333
335 void clear() {
336 symbolMapStack.clear();
337 symbolMapStack.emplace_back();
338 assert(symbolMapStack.size() == 1);
339 impliedDoStack.clear();
340 storageMapStack.clear();
341 storageMapStack.emplace_back();
342 componentMapStack.clear();
343 componentMapStack.emplace_back();
344 }
345
346 friend llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
347 const SymMap &symMap);
348
350 LLVM_DUMP_METHOD void dump() const;
351
352 void addVariableDefinition(semantics::SymbolRef symRef,
353 fir::FortranVariableOpInterface definingOp,
354 bool force = false) {
355 makeSym(symRef, SymbolBox(definingOp), force);
356 }
357
358 void copySymbolBinding(semantics::SymbolRef src,
359 semantics::SymbolRef target) {
360 auto symBox = lookupSymbol(src);
361 assert(symBox && "source binding does not exists");
362 makeSym(target, symBox, /*force=*/false);
363 }
364
365 std::optional<fir::FortranVariableOpInterface>
366 lookupVariableDefinition(semantics::SymbolRef sym) {
367 if (auto symBox = lookupSymbol(sym))
368 return symBox.getIfFortranVariableOpInterface();
369 return std::nullopt;
370 }
371
376 fir::FortranVariableOpInterface definingOp) {
377 assert(!componentMapStack.empty() && "component map stack is empty");
378 if (!componentMapStack.back())
379 componentMapStack.back() = std::make_unique<ComponentMap>();
380 componentMapStack.back().value()->insert(component, definingOp);
381 }
382
385 std::optional<fir::FortranVariableOpInterface>
387 for (auto jmap = componentMapStack.rbegin(),
388 jend = componentMapStack.rend();
389 jmap != jend; ++jmap) {
390 if (*jmap) {
391 auto iter = (**jmap)->lookup(&component);
392 if (iter != std::nullopt)
393 return iter;
394 }
395 }
396 return std::nullopt;
397 }
398
402 void registerStorage(semantics::SymbolRef sym, StorageDesc storage);
404 StorageDesc lookupStorage(semantics::SymbolRef sym);
406 return lookupStorage(*sym);
407 }
408
409private:
411 void makeSym(semantics::SymbolRef symRef, const SymbolBox &box,
412 bool force = false) {
413 auto *sym = symRef->HasLocalLocality() ? &*symRef : &symRef->GetUltimate();
414 if (force)
415 symbolMapStack.back().erase(sym);
416 assert(box && "cannot add an undefined symbol box");
417 symbolMapStack.back().try_emplace(sym, box);
418 }
419
421 symbolMapStack;
422
423 // Implied DO induction variables are not represented as Se::Symbol in
424 // Ev::Expr. Keep the variable markers in their own stack.
426
427 // A stack of maps between the symbols and their storage descriptors.
429 storageMapStack;
430
431 // A stack of maps from front-end component references to the FIR variables
432 // that should be used to implement them. This allows overriding component
433 // references in specific lowering contexts.
435 componentMapStack;
436};
437
439class SymMapScope {
440public:
441 explicit SymMapScope(SymMap &map) : map(map) { map.pushScope(); }
442 ~SymMapScope() { map.popScope(); }
443
444private:
445 SymMap &map;
446};
447
448} // namespace Fortran::lower
449
450#endif // FORTRAN_LOWER_SYMBOLMAP_H
Definition variable.h:73
Definition SymbolMap.h:140
Definition SymbolMap.h:181
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:226
void addComponentOverride(const Fortran::evaluate::Component &component, fir::FortranVariableOpInterface definingOp)
Definition SymbolMap.h:375
void pushImpliedDoBinding(AcDoVar var, mlir::Value value)
Add a new binding from the ac-do-variable var to value.
Definition SymbolMap.h:320
SymbolBox lookupSymbol(semantics::SymbolRef sym)
Find symbol and return its value if it appears in the current mappings.
Definition SymbolMap.cpp:35
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:250
mlir::Value lookupImpliedDo(AcDoVar var)
Definition SymbolMap.cpp:86
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:237
void popImpliedDoBinding()
Pop the most recent implied do binding off the stack.
Definition SymbolMap.h:325
void addSymbol(semantics::SymbolRef sym, mlir::Value value, bool force=false)
Add a trivial symbol mapping to an address.
Definition SymbolMap.h:210
std::pair< mlir::Value, std::uint64_t > StorageDesc
Definition SymbolMap.h:186
std::optional< fir::FortranVariableOpInterface > lookupComponentOverride(const Fortran::evaluate::Component &component) const
Definition SymbolMap.h:386
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:263
void registerStorage(semantics::SymbolRef sym, StorageDesc storage)
Definition SymbolMap.cpp:93
void clear()
Remove all symbols from the map.
Definition SymbolMap.h:335
SymbolBox lookupOneLevelUpSymbol(semantics::SymbolRef sym)
Definition SymbolMap.cpp:69
void addSymbol(semantics::SymbolRef sym, const fir::ExtendedValue &ext, bool force=false)
Add an extended value to the symbol table.
Definition SymbolMap.cpp:19
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:216
SymbolBox shallowLookupSymbol(semantics::SymbolRef sym)
Definition SymbolMap.cpp:56
StorageDesc lookupStorage(semantics::SymbolRef sym)
Lookup the symbol's storage at the innermost level of the symbol table.
Definition SymbolMap.cpp:101
LLVM_DUMP_METHOD void dump() const
Dump the map. For debugging.
Definition SymbolMap.cpp:122
const semantics::Symbol * lookupSymbolByName(llvm::StringRef symName)
Definition SymbolMap.cpp:47
Definition symbol.h:896
Abstract base class.
Definition BoxValue.h:61
Definition BoxValue.h:153
Definition BoxValue.h:293
Expressions of type CHARACTER and with rank > 0.
Definition BoxValue.h:170
Definition BoxValue.h:77
Definition BoxValue.h:475
Definition BoxValue.h:362
Definition FIRType.h:106
Definition OpenACC.h:20
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:110
Definition Matcher.h:25