FLANG
StackArrays.h
1//===- StackArrays.h ------------------------------------------------------===//
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// This header exposes the reusable pieces of the StackArrays pass: the
14// analysis that determines which fir.allocmem operations can safely be moved
15// to the stack (and where the replacement fir.alloca should be inserted), and
16// the pattern that performs the heap-to-stack rewrite. They are shared so that
17// other passes can reuse the same "is this heap allocation safely stackifiable,
18// and where" logic.
19//
20//===----------------------------------------------------------------------===//
21
22#ifndef FORTRAN_OPTIMIZER_TRANSFORMS_STACKARRAYS_H
23#define FORTRAN_OPTIMIZER_TRANSFORMS_STACKARRAYS_H
24
25#include "flang/Optimizer/Dialect/FIROps.h"
26#include "flang/Optimizer/Dialect/Support/KindMapping.h"
27#include "mlir/IR/PatternMatch.h"
28#include "mlir/Interfaces/DataLayoutInterfaces.h"
29#include "mlir/Support/TypeID.h"
30#include "llvm/ADT/DenseMap.h"
31#include "llvm/ADT/DenseSet.h"
32#include "llvm/ADT/PointerUnion.h"
33#include "llvm/ADT/SmallVector.h"
34#include "llvm/Support/Casting.h"
35#include <optional>
36
37namespace fir {
38
43inline bool isStackArraysCandidateType(mlir::Type type) {
44 return mlir::isa<fir::SequenceType, fir::CharacterType>(type);
45}
46
56using BlockStackScopesMap = llvm::DenseMap<mlir::Block *, BlockStackScopes>;
57
61class InsertionPoint {
62 llvm::PointerUnion<mlir::Operation *, mlir::Block *> location;
63 bool saveRestoreStack;
64
66 template <class T>
67 T *tryGetPtr() const {
68 // Use llvm::dyn_cast_if_present because location may be null here.
69 if (T *ptr = llvm::dyn_cast_if_present<T *>(location))
70 return ptr;
71 return nullptr;
72 }
73
74public:
75 template <class T>
76 InsertionPoint(T *ptr, bool saveRestoreStack = false)
77 : location(ptr), saveRestoreStack{saveRestoreStack} {}
78 InsertionPoint(std::nullptr_t null)
79 : location(null), saveRestoreStack{false} {}
80
82 mlir::Operation *tryGetOperation() const {
83 return tryGetPtr<mlir::Operation>();
84 }
85
87 mlir::Block *tryGetBlock() const { return tryGetPtr<mlir::Block>(); }
88
92 bool shouldSaveRestoreStack() const { return saveRestoreStack; }
93
94 operator bool() const { return tryGetOperation() || tryGetBlock(); }
95
96 bool operator==(const InsertionPoint &rhs) const {
97 return (location == rhs.location) &&
98 (saveRestoreStack == rhs.saveRestoreStack);
99 }
100
101 bool operator!=(const InsertionPoint &rhs) const { return !(*this == rhs); }
102};
103
106class StackArraysAnalysisWrapper {
107public:
108 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(StackArraysAnalysisWrapper)
109
110 // Maps fir.allocmem -> place to insert alloca
111 using AllocMemMap = llvm::DenseMap<mlir::Operation *, InsertionPoint>;
112
113 StackArraysAnalysisWrapper(mlir::Operation *op) {}
114
115 // Returns nullptr if analysis failed.
116 // Note: the returned pointer points into funcMaps and is invalidated if
117 // funcMaps grows (i.e. when a not-yet-analysed function is queried). This
118 // does not happen currently because each StackArraysAnalysisWrapper instance
119 // is only used to analyse a single function.
120 const AllocMemMap *getCandidateOps(mlir::Operation *func);
121
122private:
123 llvm::DenseMap<mlir::Operation *, AllocMemMap> funcMaps;
124
125 llvm::LogicalResult analyseFunction(mlir::Operation *func);
126};
127
129class AllocMemConversion : public mlir::OpRewritePattern<fir::AllocMemOp> {
130public:
131 explicit AllocMemConversion(
132 mlir::MLIRContext *ctx,
133 const StackArraysAnalysisWrapper::AllocMemMap &candidateOps,
134 std::optional<mlir::DataLayout> &dl,
135 std::optional<fir::KindMapping> &kindMap)
136 : OpRewritePattern(ctx), candidateOps{candidateOps}, dl{dl},
137 kindMap{kindMap} {}
138
139 llvm::LogicalResult
140 matchAndRewrite(fir::AllocMemOp allocmem,
141 mlir::PatternRewriter &rewriter) const override;
142
145 static InsertionPoint
146 findAllocaInsertionPoint(fir::AllocMemOp &oldAlloc,
148 BlockStackScopesMap &blockStackScopes);
149
150private:
152 const StackArraysAnalysisWrapper::AllocMemMap &candidateOps;
153
154 const std::optional<mlir::DataLayout> &dl;
155 const std::optional<fir::KindMapping> &kindMap;
156 mutable llvm::DenseSet<mlir::Block *> blocksWithStackSaveRestore;
157
160 static InsertionPoint findAllocaLoopInsertionPoint(
161 fir::AllocMemOp &oldAlloc,
163 BlockStackScopesMap &blockStackScopes);
164
166 std::optional<fir::AllocaOp>
167 insertAlloca(fir::AllocMemOp &oldAlloc,
168 mlir::PatternRewriter &rewriter) const;
169
171 void insertStackSaveRestore(fir::AllocMemOp oldAlloc,
172 mlir::PatternRewriter &rewriter) const;
175 void insertLifetimeMarkers(fir::AllocMemOp oldAlloc, fir::AllocaOp newAlloc,
176 mlir::PatternRewriter &rewriter) const;
177};
178
179} // namespace fir
180
181#endif // FORTRAN_OPTIMIZER_TRANSFORMS_STACKARRAYS_H
static InsertionPoint findAllocaInsertionPoint(fir::AllocMemOp &oldAlloc, const llvm::SmallVector< mlir::Operation * > &freeOps, BlockStackScopesMap &blockStackScopes)
Definition StackArrays.cpp:585
Definition StackArrays.h:61
mlir::Block * tryGetBlock() const
Get contained block, or nullptr.
Definition StackArrays.h:87
mlir::Operation * tryGetOperation() const
Get contained operation, or nullptr.
Definition StackArrays.h:82
bool shouldSaveRestoreStack() const
Definition StackArrays.h:92
Definition OpenACC.h:20
Definition AbstractConverter.h:37
bool isStackArraysCandidateType(mlir::Type type)
Definition StackArrays.h:43
Definition StackArrays.h:49
llvm::SmallVector< std::pair< mlir::Operation *, mlir::Operation * > > pairs
The llvm.stacksave/llvm.stackrestore pairs already in the block.
Definition StackArrays.h:54
bool canSaveRestore
Definition StackArrays.h:52