1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
//===-- SymbolMap.cpp -----------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Pretty printers for symbol boxes, etc.
//
//===----------------------------------------------------------------------===//
#include "flang/Lower/SymbolMap.h"
#include "flang/Optimizer/Builder/Todo.h"
#include "mlir/IR/BuiltinTypes.h"
#include "llvm/Support/Debug.h"
#include <optional>
#define DEBUG_TYPE "flang-lower-symbol-map"
void Fortran::lower::SymMap::addSymbol(Fortran::semantics::SymbolRef sym,
const fir::ExtendedValue &exv,
bool force) {
exv.match([&](const fir::UnboxedValue &v) { addSymbol(sym, v, force); },
[&](const fir::CharBoxValue &v) { makeSym(sym, v, force); },
[&](const fir::ArrayBoxValue &v) { makeSym(sym, v, force); },
[&](const fir::CharArrayBoxValue &v) { makeSym(sym, v, force); },
[&](const fir::BoxValue &v) { makeSym(sym, v, force); },
[&](const fir::MutableBoxValue &v) { makeSym(sym, v, force); },
[&](const fir::PolymorphicValue &v) { makeSym(sym, v, force); },
[](auto) {
llvm::report_fatal_error("value not added to symbol table");
});
}
Fortran::lower::SymbolBox
Fortran::lower::SymMap::lookupSymbol(Fortran::semantics::SymbolRef symRef) {
auto *sym = symRef->HasLocalLocality() ? &*symRef : &symRef->GetUltimate();
for (auto jmap = symbolMapStack.rbegin(), jend = symbolMapStack.rend();
jmap != jend; ++jmap) {
auto iter = jmap->find(sym);
if (iter != jmap->end())
return iter->second;
}
return SymbolBox::None{};
}
Fortran::lower::SymbolBox Fortran::lower::SymMap::shallowLookupSymbol(
Fortran::semantics::SymbolRef symRef) {
auto *sym = symRef->HasLocalLocality() ? &*symRef : &symRef->GetUltimate();
auto &map = symbolMapStack.back();
auto iter = map.find(sym);
if (iter != map.end())
return iter->second;
return SymbolBox::None{};
}
/// Skip one level when looking up the symbol. The use case is such as looking
/// up the host variable symbol box by skipping the associated level in
/// host-association in OpenMP code.
Fortran::lower::SymbolBox Fortran::lower::SymMap::lookupOneLevelUpSymbol(
Fortran::semantics::SymbolRef symRef) {
auto *sym = symRef->HasLocalLocality() ? &*symRef : &symRef->GetUltimate();
auto jmap = symbolMapStack.rbegin();
auto jend = symbolMapStack.rend();
if (jmap == jend)
return SymbolBox::None{};
// Skip one level in symbol map stack.
for (++jmap; jmap != jend; ++jmap) {
auto iter = jmap->find(sym);
if (iter != jmap->end())
return iter->second;
}
return SymbolBox::None{};
}
mlir::Value
Fortran::lower::SymMap::lookupImpliedDo(Fortran::lower::SymMap::AcDoVar var) {
for (auto [marker, binding] : llvm::reverse(impliedDoStack))
if (var == marker)
return binding;
return {};
}
llvm::raw_ostream &
Fortran::lower::operator<<(llvm::raw_ostream &os,
const Fortran::lower::SymbolBox &symBox) {
symBox.match(
[&](const Fortran::lower::SymbolBox::None &box) {
os << "** symbol not properly mapped **\n";
},
[&](const Fortran::lower::SymbolBox::Intrinsic &val) {
os << val.getAddr() << '\n';
},
[&](const auto &box) { os << box << '\n'; });
return os;
}
llvm::raw_ostream &
Fortran::lower::operator<<(llvm::raw_ostream &os,
const Fortran::lower::SymMap &symMap) {
os << "Symbol map:\n";
for (auto i : llvm::enumerate(symMap.symbolMapStack)) {
os << " level " << i.index() << "<{\n";
for (auto iter : i.value()) {
os << " symbol @" << static_cast<const void *>(iter.first) << " ["
<< *iter.first << "] ->\n ";
os << iter.second;
}
os << " }>\n";
}
return os;
}
|