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(getInternalOrSymbolName(func));
141 }
142 inline const TBAATree &operator[](mlir::LLVM::LLVMFuncOp func) {
143 return getFuncTree(getInternalOrSymbolName(func));
144 }
145 // Returns the TBAA tree associated with the scope enclosed
146 // within the given function. With MLIR inlining, there may
147 // be multiple scopes within a single function. It is the caller's
148 // responsibility to provide unique name for the scope.
149 // If the scope string is empty, returns the TBAA tree for the
150 // "root" scope of the given function.
151 inline TBAATree &getMutableFuncTreeWithScope(mlir::func::FuncOp func,
152 llvm::StringRef scope) {
153 mlir::StringAttr name = getInternalOrSymbolName(func);
154 if (!scope.empty())
155 name = mlir::StringAttr::get(name.getContext(),
156 llvm::Twine(name) + " - " + scope);
157 return getFuncTree(name);
158 }
159
160 inline const TBAATree &getFuncTreeWithScope(mlir::func::FuncOp func,
161 llvm::StringRef scope) {
162 return getMutableFuncTreeWithScope(func, scope);
163 }
164
165private:
166 template <typename FuncOp>
167 static mlir::StringAttr getInternalOrSymbolName(FuncOp func) {
168 // External name conversion may rename a function before TBAA construction.
169 // Use its original name consistently so tags created at different pipeline
170 // stages belong to the same tree.
171 if (auto name = func->template getAttrOfType<mlir::StringAttr>(
172 getInternalFuncNameAttrName()))
173 return name;
174 return func.getSymNameAttr();
175 }
176
177 TBAATree &getFuncTree(mlir::StringAttr symName) {
178 if (!separatePerFunction)
179 symName = mlir::StringAttr::get(symName.getContext(), "");
180 if (!trees.contains(symName))
181 trees.insert({symName, TBAATree::buildTree(symName)});
182 auto it = trees.find(symName);
183 assert(it != trees.end());
184 return it->second;
185 }
186
187 // Should each function use a different tree?
188 const bool separatePerFunction;
189 // TBAA tree per function
190 llvm::DenseMap<mlir::StringAttr, TBAATree> trees;
191};
192
193} // namespace fir
194
195#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