|
| 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 "mlir/Dialect/Func/IR/FuncOps.h" |
| 13 | +#include "mlir/Dialect/LLVMIR/LLVMAttrs.h" |
| 14 | +#include "mlir/Dialect/LLVMIR/LLVMDialect.h" |
| 15 | +#include "mlir/IR/MLIRContext.h" |
| 16 | +#include "llvm/ADT/DenseMap.h" |
| 17 | +#include <string> |
| 18 | + |
| 19 | +namespace fir { |
| 20 | + |
| 21 | +//===----------------------------------------------------------------------===// |
| 22 | +// TBAATree |
| 23 | +//===----------------------------------------------------------------------===// |
| 24 | +/// Per-function TBAA tree. Each tree contains branches for data (of various |
| 25 | +/// kinds) and descriptor access |
| 26 | +struct TBAATree { |
| 27 | + //===----------------------------------------------------------------------===// |
| 28 | + // TBAAForrest::TBAATree::SubtreeState |
| 29 | + //===----------------------------------------------------------------------===// |
| 30 | + /// This contains a TBAA subtree based on some parent. New tags can be added |
| 31 | + /// under the parent using getTag. |
| 32 | + class SubtreeState { |
| 33 | + friend TBAATree; // only allow construction by TBAATree |
| 34 | + public: |
| 35 | + SubtreeState() = delete; |
| 36 | + SubtreeState(const SubtreeState &) = delete; |
| 37 | + SubtreeState(SubtreeState &&) = default; |
| 38 | + |
| 39 | + mlir::LLVM::TBAATagAttr getTag(llvm::StringRef uniqueId) const; |
| 40 | + |
| 41 | + private: |
| 42 | + SubtreeState(mlir::MLIRContext *ctx, std::string name, |
| 43 | + mlir::LLVM::TBAANodeAttr grandParent) |
| 44 | + : parentId{std::move(name)}, context(ctx) { |
| 45 | + parent = mlir::LLVM::TBAATypeDescriptorAttr::get( |
| 46 | + context, parentId, mlir::LLVM::TBAAMemberAttr::get(grandParent, 0)); |
| 47 | + } |
| 48 | + |
| 49 | + const std::string parentId; |
| 50 | + mlir::MLIRContext *const context; |
| 51 | + mlir::LLVM::TBAATypeDescriptorAttr parent; |
| 52 | + llvm::DenseMap<llvm::StringRef, mlir::LLVM::TBAATagAttr> tagDedup; |
| 53 | + }; |
| 54 | + |
| 55 | + SubtreeState globalDataTree; |
| 56 | + SubtreeState allocatedDataTree; |
| 57 | + SubtreeState dummyArgDataTree; |
| 58 | + mlir::LLVM::TBAATypeDescriptorAttr anyAccessDesc; |
| 59 | + mlir::LLVM::TBAATypeDescriptorAttr boxMemberTypeDesc; |
| 60 | + mlir::LLVM::TBAATypeDescriptorAttr anyDataTypeDesc; |
| 61 | + |
| 62 | + static TBAATree buildTree(mlir::StringAttr functionName); |
| 63 | + |
| 64 | +private: |
| 65 | + TBAATree(mlir::LLVM::TBAATypeDescriptorAttr anyAccess, |
| 66 | + mlir::LLVM::TBAATypeDescriptorAttr dataRoot, |
| 67 | + mlir::LLVM::TBAATypeDescriptorAttr boxMemberTypeDesc); |
| 68 | +}; |
| 69 | + |
| 70 | +//===----------------------------------------------------------------------===// |
| 71 | +// TBAAForrest |
| 72 | +//===----------------------------------------------------------------------===// |
| 73 | +/// Collection of TBAATrees, usually indexed by function (so that each function |
| 74 | +/// has a different TBAATree) |
| 75 | +class TBAAForrest { |
| 76 | +public: |
| 77 | + explicit TBAAForrest(bool separatePerFunction = true) |
| 78 | + : separatePerFunction{separatePerFunction} {} |
| 79 | + |
| 80 | + inline const TBAATree &operator[](mlir::func::FuncOp func) { |
| 81 | + return getFuncTree(func.getSymNameAttr()); |
| 82 | + } |
| 83 | + inline const TBAATree &operator[](mlir::LLVM::LLVMFuncOp func) { |
| 84 | + return getFuncTree(func.getSymNameAttr()); |
| 85 | + } |
| 86 | + |
| 87 | +private: |
| 88 | + const TBAATree &getFuncTree(mlir::StringAttr symName) { |
| 89 | + if (!separatePerFunction) |
| 90 | + symName = mlir::StringAttr::get(symName.getContext(), ""); |
| 91 | + if (!trees.contains(symName)) |
| 92 | + trees.insert({symName, TBAATree::buildTree(symName)}); |
| 93 | + return trees.at(symName); |
| 94 | + } |
| 95 | + |
| 96 | + // Should each function use a different tree? |
| 97 | + const bool separatePerFunction; |
| 98 | + // TBAA tree per function |
| 99 | + llvm::DenseMap<mlir::StringAttr, TBAATree> trees; |
| 100 | +}; |
| 101 | + |
| 102 | +} // namespace fir |
| 103 | + |
| 104 | +#endif // FORTRAN_OPTIMIZER_ANALYSIS_TBAA_FOREST_H |
0 commit comments