Skip to content

Commit 987ee6e

Browse files
committed
[flang][fir] Upstream the pre-FIR tree changes.
The PFT has been updated to support Fortran 77. clang-tidy cleanup. Authors: Val Donaldson, Jean Perier, Eric Schweitz, et.al. Differential Revision: https://reviews.llvm.org/D98283
1 parent 2015508 commit 987ee6e

File tree

10 files changed

+1341
-457
lines changed

10 files changed

+1341
-457
lines changed

flang/include/flang/Lower/IO.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ namespace pft {
4343
struct Evaluation;
4444
using LabelEvalMap = llvm::DenseMap<Fortran::parser::Label, Evaluation *>;
4545
using SymbolRef = Fortran::common::Reference<const Fortran::semantics::Symbol>;
46-
using LabelSet = llvm::SmallSet<Fortran::parser::Label, 5>;
46+
using LabelSet = llvm::SmallSet<Fortran::parser::Label, 4>;
4747
using SymbolLabelMap = llvm::DenseMap<SymbolRef, LabelSet>;
4848
} // namespace pft
4949

flang/include/flang/Lower/PFTBuilder.h

Lines changed: 314 additions & 106 deletions
Large diffs are not rendered by default.

flang/include/flang/Lower/PFTDefs.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//===-- Lower/PFTDefs.h -- shared PFT info ----------------------*- 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_PFTDEFS_H
14+
#define FORTRAN_LOWER_PFTDEFS_H
15+
16+
#include "llvm/ADT/DenseMap.h"
17+
#include "llvm/ADT/SmallSet.h"
18+
#include "llvm/ADT/StringRef.h"
19+
20+
namespace mlir {
21+
class Block;
22+
}
23+
24+
namespace Fortran {
25+
namespace semantics {
26+
class Symbol;
27+
class SemanticsContext;
28+
class Scope;
29+
} // namespace semantics
30+
31+
namespace evaluate {
32+
template <typename A>
33+
class Expr;
34+
struct SomeType;
35+
} // namespace evaluate
36+
37+
namespace common {
38+
template <typename A>
39+
class Reference;
40+
}
41+
42+
namespace lower {
43+
44+
bool definedInCommonBlock(const semantics::Symbol &sym);
45+
bool defaultRecursiveFunctionSetting();
46+
47+
namespace pft {
48+
49+
struct Evaluation;
50+
51+
using SomeExpr = Fortran::evaluate::Expr<Fortran::evaluate::SomeType>;
52+
using SymbolRef = Fortran::common::Reference<const Fortran::semantics::Symbol>;
53+
using Label = std::uint64_t;
54+
using LabelSet = llvm::SmallSet<Label, 4>;
55+
using SymbolLabelMap = llvm::DenseMap<SymbolRef, LabelSet>;
56+
using LabelEvalMap = llvm::DenseMap<Label, Evaluation *>;
57+
58+
} // namespace pft
59+
} // namespace lower
60+
} // namespace Fortran
61+
62+
#endif // FORTRAN_LOWER_PFTDEFS_H
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//===-- Lower/Support/Utils.h -- utilities ----------------------*- 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_SUPPORT_UTILS_H
14+
#define FORTRAN_LOWER_SUPPORT_UTILS_H
15+
16+
#include "flang/Common/indirection.h"
17+
#include "flang/Parser/char-block.h"
18+
#include "mlir/Dialect/StandardOps/IR/Ops.h"
19+
#include "mlir/IR/BuiltinAttributes.h"
20+
#include "llvm/ADT/StringRef.h"
21+
#include <cstdint>
22+
23+
//===----------------------------------------------------------------------===//
24+
// Small inline helper functions to deal with repetitive, clumsy conversions.
25+
//===----------------------------------------------------------------------===//
26+
27+
/// Convert an F18 CharBlock to an LLVM StringRef.
28+
inline llvm::StringRef toStringRef(const Fortran::parser::CharBlock &cb) {
29+
return {cb.begin(), cb.size()};
30+
}
31+
32+
namespace fir {
33+
/// Return the integer value of a ConstantOp.
34+
inline std::int64_t toInt(mlir::ConstantOp cop) {
35+
return cop.getValue().cast<mlir::IntegerAttr>().getValue().getSExtValue();
36+
}
37+
} // namespace fir
38+
39+
/// Template helper to remove Fortran::common::Indirection wrappers.
40+
template <typename A>
41+
const A &removeIndirection(const A &a) {
42+
return a;
43+
}
44+
template <typename A>
45+
const A &removeIndirection(const Fortran::common::Indirection<A> &a) {
46+
return a.value();
47+
}
48+
49+
#endif // FORTRAN_LOWER_SUPPORT_UTILS_H

flang/lib/Lower/IntervalSet.h

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
//===-- IntervalSet.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+
#ifndef FORTRAN_LOWER_INTERVALSET_H
10+
#define FORTRAN_LOWER_INTERVALSET_H
11+
12+
#include <cassert>
13+
#include <map>
14+
15+
namespace Fortran::lower {
16+
17+
//===----------------------------------------------------------------------===//
18+
// Interval set
19+
//===----------------------------------------------------------------------===//
20+
21+
/// Interval set to keep track of intervals, merging them when they overlap one
22+
/// another. Used to refine the pseudo-offset ranges of the front-end symbols
23+
/// into groups of aliasing variables.
24+
struct IntervalSet {
25+
using MAP = std::map<std::size_t, std::size_t>;
26+
using Iterator = MAP::const_iterator;
27+
28+
// Handles the merging of overlapping intervals correctly, efficiently.
29+
void merge(std::size_t lo, std::size_t up) {
30+
assert(lo <= up);
31+
if (empty()) {
32+
m.insert({lo, up});
33+
return;
34+
}
35+
auto i = m.lower_bound(lo);
36+
// i->first >= lo
37+
if (i == begin()) {
38+
if (up < i->first) {
39+
// [lo..up] < i->first
40+
m.insert({lo, up});
41+
return;
42+
}
43+
// up >= i->first
44+
if (i->second > up)
45+
up = i->second;
46+
fuse(lo, up, i);
47+
return;
48+
}
49+
auto i1 = i;
50+
if (i == end() || i->first > lo)
51+
i = std::prev(i);
52+
// i->first <= lo
53+
if (i->second >= up) {
54+
// i->first <= lo && up <= i->second, keep i
55+
return;
56+
}
57+
// i->second < up
58+
if (i->second < lo) {
59+
if (i1 == end() || i1->first > up) {
60+
// i < [lo..up] < i1
61+
m.insert({lo, up});
62+
return;
63+
}
64+
// i < [lo..up], i1->first <= up --> [lo..up] union [i1..?]
65+
i = i1;
66+
} else {
67+
// i->first <= lo, lo <= i->second --> [i->first..up] union [i..?]
68+
lo = i->first;
69+
}
70+
fuse(lo, up, i);
71+
}
72+
73+
Iterator find(std::size_t pt) const {
74+
auto i = m.lower_bound(pt);
75+
if (i != end() && i->first == pt)
76+
return i;
77+
if (i == begin())
78+
return end();
79+
i = std::prev(i);
80+
if (i->second < pt)
81+
return end();
82+
return i;
83+
}
84+
85+
Iterator begin() const { return m.begin(); }
86+
Iterator end() const { return m.end(); }
87+
bool empty() const { return m.empty(); }
88+
std::size_t size() const { return m.size(); }
89+
90+
private:
91+
// Find and fuse overlapping sets.
92+
void fuse(std::size_t lo, std::size_t up, Iterator i) {
93+
auto j = m.upper_bound(up);
94+
// up < j->first
95+
auto cu = std::prev(j)->second;
96+
// cu < j->first
97+
if (cu > up)
98+
up = cu;
99+
m.erase(i, j);
100+
// merge [i .. j) with [i->first, max(up, cu)]
101+
m.insert({lo, up});
102+
}
103+
104+
MAP m{};
105+
};
106+
107+
} // namespace Fortran::lower
108+
109+
#endif // FORTRAN_LOWER_INTERVALSET_H

0 commit comments

Comments
 (0)