Skip to content

Commit 556ff19

Browse files
authored
[flang] Create TBAA subtree for COMMON block variables. (#156558)
In order to help LLVM disambiguate accesses to the COMMON block variables, this patch creates a TBAA sub-tree for each COMMON block, and the places all variables belonging to this COMMON block into this sub-tree. The structure looks like this: ``` common /blk/ a, b, c "global data" | |- "blk_" | |- "blk_/bytes_0_to_3" |- "blk_/bytes_4_to_7" |- "blk_/bytes_8_to_11" ``` The TBAA tag for "a" is created in "blk_/bytes_0_to_3" root, etc. The byte spans are created based on the `storage` information provided by `fir.declare` (#155742). See also: https://discourse.llvm.org/t/rfc-flang-representation-for-objects-inside-physical-storage/88026
1 parent 83da8d0 commit 556ff19

File tree

17 files changed

+1081
-41
lines changed

17 files changed

+1081
-41
lines changed

flang/include/flang/Optimizer/Analysis/TBAAForest.h

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ struct TBAATree {
4646

4747
mlir::LLVM::TBAATypeDescriptorAttr getRoot() const { return parent; }
4848

49+
/// For the given name, get or create a subtree in the current
50+
/// subtree. For example, this is used for creating subtrees
51+
/// inside the "global data" subtree for the COMMON block variables
52+
/// belonging to the same COMMON block.
53+
SubtreeState &getOrCreateNamedSubtree(mlir::StringAttr name);
54+
4955
private:
5056
SubtreeState(mlir::MLIRContext *ctx, std::string name,
5157
mlir::LLVM::TBAANodeAttr grandParent)
@@ -57,6 +63,9 @@ struct TBAATree {
5763
const std::string parentId;
5864
mlir::MLIRContext *const context;
5965
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;
6069
};
6170

6271
/// A subtree for POINTER/TARGET variables data.
@@ -131,22 +140,29 @@ class TBAAForrest {
131140
// responsibility to provide unique name for the scope.
132141
// If the scope string is empty, returns the TBAA tree for the
133142
// "root" scope of the given function.
134-
inline const TBAATree &getFuncTreeWithScope(mlir::func::FuncOp func,
135-
llvm::StringRef scope) {
143+
inline TBAATree &getMutableFuncTreeWithScope(mlir::func::FuncOp func,
144+
llvm::StringRef scope) {
136145
mlir::StringAttr name = func.getSymNameAttr();
137146
if (!scope.empty())
138147
name = mlir::StringAttr::get(name.getContext(),
139148
llvm::Twine(name) + " - " + scope);
140149
return getFuncTree(name);
141150
}
142151

152+
inline const TBAATree &getFuncTreeWithScope(mlir::func::FuncOp func,
153+
llvm::StringRef scope) {
154+
return getMutableFuncTreeWithScope(func, scope);
155+
}
156+
143157
private:
144-
const TBAATree &getFuncTree(mlir::StringAttr symName) {
158+
TBAATree &getFuncTree(mlir::StringAttr symName) {
145159
if (!separatePerFunction)
146160
symName = mlir::StringAttr::get(symName.getContext(), "");
147161
if (!trees.contains(symName))
148162
trees.insert({symName, TBAATree::buildTree(symName)});
149-
return trees.at(symName);
163+
auto it = trees.find(symName);
164+
assert(it != trees.end());
165+
return it->second;
150166
}
151167

152168
// Should each function use a different tree?

flang/include/flang/Optimizer/Builder/FIRBuilder.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,12 @@ class FirOpBuilder : public mlir::OpBuilder, public mlir::OpBuilder::Listener {
365365
// Linkage helpers (inline). The default linkage is external.
366366
//===--------------------------------------------------------------------===//
367367

368-
mlir::StringAttr createCommonLinkage() { return getStringAttr("common"); }
368+
static mlir::StringAttr createCommonLinkage(mlir::MLIRContext *context) {
369+
return mlir::StringAttr::get(context, "common");
370+
}
371+
mlir::StringAttr createCommonLinkage() {
372+
return createCommonLinkage(getContext());
373+
}
369374

370375
mlir::StringAttr createInternalLinkage() { return getStringAttr("internal"); }
371376

flang/include/flang/Optimizer/Dialect/FIRType.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ std::optional<std::pair<uint64_t, unsigned short>>
551551
getTypeSizeAndAlignment(mlir::Location loc, mlir::Type ty,
552552
const mlir::DataLayout &dl,
553553
const fir::KindMapping &kindMap);
554+
554555
} // namespace fir
555556

556557
#endif // FORTRAN_OPTIMIZER_DIALECT_FIRTYPE_H

flang/include/flang/Optimizer/Transforms/Passes.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,8 @@ def AddAliasTags : Pass<"fir-add-alias-tags", "mlir::ModuleOp"> {
326326
theory, each operation could be considered in prallel, so long as there
327327
aren't races adding new tags to the mlir context.
328328
}];
329-
let dependentDialects = [ "fir::FIROpsDialect" ];
329+
// The pass inserts TBAA attributes from LLVM dialect.
330+
let dependentDialects = ["mlir::LLVM::LLVMDialect"];
330331
}
331332

332333
def SimplifyRegionLite : Pass<"simplify-region-lite", "mlir::ModuleOp"> {

flang/lib/Optimizer/Analysis/TBAAForest.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,23 @@
1111

1212
mlir::LLVM::TBAATagAttr
1313
fir::TBAATree::SubtreeState::getTag(llvm::StringRef uniqueName) const {
14-
std::string id = (parentId + "/" + uniqueName).str();
14+
std::string id = (parentId + '/' + uniqueName).str();
1515
mlir::LLVM::TBAATypeDescriptorAttr type =
1616
mlir::LLVM::TBAATypeDescriptorAttr::get(
1717
context, id, mlir::LLVM::TBAAMemberAttr::get(parent, 0));
1818
return mlir::LLVM::TBAATagAttr::get(type, type, 0);
19-
// return tag;
19+
}
20+
21+
fir::TBAATree::SubtreeState &
22+
fir::TBAATree::SubtreeState::getOrCreateNamedSubtree(mlir::StringAttr name) {
23+
auto it = namedSubtrees.find(name);
24+
if (it != namedSubtrees.end())
25+
return it->second;
26+
27+
return namedSubtrees
28+
.insert(
29+
{name, SubtreeState(context, parentId + '/' + name.str(), parent)})
30+
.first->second;
2031
}
2132

2233
mlir::LLVM::TBAATagAttr fir::TBAATree::SubtreeState::getTag() const {

0 commit comments

Comments
 (0)