FLANG
Utils.h
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 "flang/Semantics/tools.h"
19#include "mlir/Dialect/Func/IR/FuncOps.h"
20#include "mlir/IR/BuiltinAttributes.h"
21#include "llvm/ADT/SmallSet.h"
22#include "llvm/ADT/StringRef.h"
23
24namespace Fortran::evaluate {
25class Component;
26} // namespace Fortran::evaluate
27
28namespace Fortran::lower {
29using SomeExpr = Fortran::evaluate::Expr<Fortran::evaluate::SomeType>;
30// FIXME: needed for privatizeSymbol that does not belong to this header.
32class SymMap;
33} // end namespace Fortran::lower
34
35namespace fir {
36class FirOpBuilder;
37}
38
39//===----------------------------------------------------------------------===//
40// Small inline helper functions to deal with repetitive, clumsy conversions.
41//===----------------------------------------------------------------------===//
42
44inline llvm::StringRef toStringRef(const Fortran::parser::CharBlock &cb) {
45 return {cb.begin(), cb.size()};
46}
47
49template <typename A>
50const A &removeIndirection(const A &a) {
51 return a;
52}
53template <typename A>
54const A &removeIndirection(const Fortran::common::Indirection<A> &a) {
55 return a.value();
56}
57
59template <typename A>
60static Fortran::lower::SomeExpr toEvExpr(const A &x) {
61 return Fortran::evaluate::AsGenericExpr(Fortran::common::Clone(x));
62}
63
64template <Fortran::common::TypeCategory FROM>
65static Fortran::lower::SomeExpr
66ignoreEvConvert(const Fortran::evaluate::Convert<
68 FROM> &x) {
69 CHECK(x.kind() == 8);
70 return toEvExpr(x.left());
71}
72template <typename A>
73static Fortran::lower::SomeExpr ignoreEvConvert(const A &x) {
74 return toEvExpr(x);
75}
76
80inline Fortran::lower::SomeExpr ignoreEvConvert(
83 CHECK(x.kind() == 8);
84 return Fortran::common::visit(
85 [](const auto &v) { return ignoreEvConvert(v); }, x.u);
86}
87
88namespace Fortran::lower {
89unsigned getHashValue(const Fortran::lower::SomeExpr *x);
90unsigned getHashValue(const Fortran::evaluate::Component *x);
91
92bool isEqual(const Fortran::lower::SomeExpr *x,
93 const Fortran::lower::SomeExpr *y);
94bool isEqual(const Fortran::evaluate::Component *x,
95 const Fortran::evaluate::Component *y);
96
97template <typename OpType, typename OperandsStructType>
98void privatizeSymbol(
99 lower::AbstractConverter &converter, fir::FirOpBuilder &firOpBuilder,
100 lower::SymMap &symTable,
101 llvm::SetVector<const semantics::Symbol *> &allPrivatizedSymbols,
102 llvm::SmallPtrSet<const semantics::Symbol *, 16> &mightHaveReadHostSym,
103 const semantics::Symbol *symToPrivatize, OperandsStructType *clauseOps,
104 std::optional<llvm::omp::Directive> dir = std::nullopt,
105 bool forceHeapAllocationForPrivateDynamicArrays = false,
106 bool isLinear = false);
107
108} // end namespace Fortran::lower
109
110// DenseMapInfo for pointers to Fortran::lower::SomeExpr.
111namespace llvm {
112template <>
113struct DenseMapInfo<const Fortran::lower::SomeExpr *> {
114 static unsigned getHashValue(const Fortran::lower::SomeExpr *v) {
115 return Fortran::lower::getHashValue(v);
116 }
117 static bool isEqual(const Fortran::lower::SomeExpr *lhs,
118 const Fortran::lower::SomeExpr *rhs) {
119 return Fortran::lower::isEqual(lhs, rhs);
120 }
121};
122
123// DenseMapInfo for pointers to Fortran::evaluate::Component.
124template <>
125struct DenseMapInfo<const Fortran::evaluate::Component *> {
126 static unsigned getHashValue(const Fortran::evaluate::Component *v) {
127 return Fortran::lower::getHashValue(v);
128 }
129 static bool isEqual(const Fortran::evaluate::Component *lhs,
130 const Fortran::evaluate::Component *rhs) {
131 return Fortran::lower::isEqual(lhs, rhs);
132 }
133};
134} // namespace llvm
135
136#endif // FORTRAN_LOWER_SUPPORT_UTILS_H
Definition indirection.h:31
Definition variable.h:73
Definition common.h:222
Definition type.h:61
Definition AbstractConverter.h:87
Definition SymbolMap.h:181
Definition char-block.h:27
Definition FIRBuilder.h:66
Definition call.h:34
Definition ParserActions.h:24
Definition bit-population-count.h:20
Definition AbstractConverter.h:37
Definition expression.h:218