FLANG
TBAAForest.h
1//===-- TBAAForest.h - A TBAA tree for each function -----------*- 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_OPTIMIZER_ANALYSIS_TBAA_FOREST_H
10#define FORTRAN_OPTIMIZER_ANALYSIS_TBAA_FOREST_H
11
12#include "flang/Optimizer/Dialect/FIROpsSupport.h"
13#include "mlir/Dialect/Func/IR/FuncOps.h"
14#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
15#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
16#include "mlir/IR/Attributes.h"
17#include "mlir/IR/MLIRContext.h"
18#include "llvm/ADT/DenseMap.h"
19#include <string>
20
21namespace fir {
22
23//===----------------------------------------------------------------------===//
24// TBAATree
25//===----------------------------------------------------------------------===//
28struct TBAATree {
29 //===----------------------------------------------------------------------===//
30 // TBAAForrest::TBAATree::SubtreeState
31 //===----------------------------------------------------------------------===//
34 class SubtreeState {
35 friend TBAATree; // only allow construction by TBAATree
36 public:
37 SubtreeState() = delete;
38 SubtreeState(const SubtreeState &) = delete;
39 SubtreeState(SubtreeState &&) = default;
40
41 mlir::LLVM::TBAATagAttr getTag(llvm::StringRef uniqueId) const;
42
45 mlir::LLVM::TBAATagAttr getTag() const;
46
47 mlir::LLVM::TBAATypeDescriptorAttr getRoot() const { return parent; }
48
53 SubtreeState &getOrCreateNamedSubtree(mlir::StringAttr name);
54
55 private:
56 SubtreeState(mlir::MLIRContext *ctx, std::string name,
57 mlir::LLVM::TBAANodeAttr grandParent)
58 : parentId{std::move(name)}, context(ctx) {
59 parent = mlir::LLVM::TBAATypeDescriptorAttr::get(
60 context, parentId, mlir::LLVM::TBAAMemberAttr::get(grandParent, 0));
61 }
62
63 const std::string parentId;
64 mlir::MLIRContext *const context;
65 mlir::LLVM::TBAATypeDescriptorAttr parent;
66 // A map of named sub-trees, e.g. sub-trees of the COMMON blocks
67 // placed under the "global data" root.
68 llvm::DenseMap<mlir::StringAttr, SubtreeState> namedSubtrees;
69 };
70
89 mlir::LLVM::TBAATypeDescriptorAttr anyAccessDesc;
90 mlir::LLVM::TBAATypeDescriptorAttr boxMemberTypeDesc;
91 mlir::LLVM::TBAATypeDescriptorAttr anyDataTypeDesc;
92
93 // Structure of the created tree:
94 // Function root
95 // |
96 // "any access"
97 // |
98 // |- "descriptor member"
99 // |- "any data access"
100 // |
101 // |- "dummy arg data"
102 // |
103 // |- <dummy arg name 1>
104 // |- <dummy arg name 2>
105 // |- "target data" <-- Any POINTER variable or TARGET dummy arg
106 // |
107 // |- <target name 1> <--- any TARGET variable which isn't a dummy arg
108 // |- <target name 2>
109 // |- "allocated data"
110 // |
111 // |- <allocated name 1>
112 // |- <allocated name 2>
113 // |- "direct data"
114 // |
115 // |- <direct name 1>
116 // |- <direct name 2>
117 // |- "global data"
118 // |
119 // |- <global name 1>
120 // |- <global name 2>
121 static TBAATree buildTree(mlir::StringAttr functionName);
122
123private:
124 TBAATree(mlir::LLVM::TBAATypeDescriptorAttr anyAccess,
125 mlir::LLVM::TBAATypeDescriptorAttr dataRoot,
126 mlir::LLVM::TBAATypeDescriptorAttr boxMemberTypeDesc);
127};
128
129//===----------------------------------------------------------------------===//
130// TBAAForrest
131//===----------------------------------------------------------------------===//
134class TBAAForrest {
135public:
136 explicit TBAAForrest(bool separatePerFunction = true)
137 : separatePerFunction{separatePerFunction} {}
138
139 inline const TBAATree &operator[](mlir::func::FuncOp func) {
140 return getFuncTree(func.getSymNameAttr());
141 }
142 inline const TBAATree &operator[](mlir::LLVM::LLVMFuncOp func) {
143 // the external name conversion pass may rename some functions. Their old
144 // name must be used so that we add to the tbaa tree added in the FIR pass
145 mlir::Attribute attr = func->getAttr(getInternalFuncNameAttrName());
146 if (attr) {
147 return getFuncTree(mlir::cast<mlir::StringAttr>(attr));
148 }
149 return getFuncTree(func.getSymNameAttr());
150 }
151 // Returns the TBAA tree associated with the scope enclosed
152 // within the given function. With MLIR inlining, there may
153 // be multiple scopes within a single function. It is the caller's
154 // responsibility to provide unique name for the scope.
155 // If the scope string is empty, returns the TBAA tree for the
156 // "root" scope of the given function.
157 inline TBAATree &getMutableFuncTreeWithScope(mlir::func::FuncOp func,
158 llvm::StringRef scope) {
159 mlir::StringAttr name = func.getSymNameAttr();
160 if (!scope.empty())
161 name = mlir::StringAttr::get(name.getContext(),
162 llvm::Twine(name) + " - " + scope);
163 return getFuncTree(name);
164 }
165
166 inline const TBAATree &getFuncTreeWithScope(mlir::func::FuncOp func,
167 llvm::StringRef scope) {
168 return getMutableFuncTreeWithScope(func, scope);
169 }
170
171private:
172 TBAATree &getFuncTree(mlir::StringAttr symName) {
173 if (!separatePerFunction)
174 symName = mlir::StringAttr::get(symName.getContext(), "");
175 if (!trees.contains(symName))
176 trees.insert({symName, TBAATree::buildTree(symName)});
177 auto it = trees.find(symName);
178 assert(it != trees.end());
179 return it->second;
180 }
181
182 // Should each function use a different tree?
183 const bool separatePerFunction;
184 // TBAA tree per function
185 llvm::DenseMap<mlir::StringAttr, TBAATree> trees;
186};
187
188} // namespace fir
189
190#endif // FORTRAN_OPTIMIZER_ANALYSIS_TBAA_FOREST_H
Definition TBAAForest.h:34
SubtreeState & getOrCreateNamedSubtree(mlir::StringAttr name)
Definition TBAAForest.cpp:22
mlir::LLVM::TBAATagAttr getTag() const
Definition TBAAForest.cpp:33
Definition AbstractConverter.h:37
Definition TBAAForest.h:28
SubtreeState directDataTree
A subtree for global variables descriptors.
Definition TBAAForest.h:88
SubtreeState globalDataTree
A subtree for global variables data (e.g. user module variables).
Definition TBAAForest.h:77
SubtreeState targetDataTree
Definition TBAAForest.h:75
SubtreeState allocatedDataTree
A subtree for variables allocated via fir.alloca or fir.allocmem.
Definition TBAAForest.h:79
SubtreeState dummyArgDataTree
Definition TBAAForest.h:86