FLANG
AllocationPolicy.h
1//===-- Optimizer/Support/AllocationPolicy.h --------------------*- 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// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
10//
11//===----------------------------------------------------------------------===//
12//
13// The policy controlling where array allocations should live: on the stack
14// (fir.alloca) or on the heap (fir.allocmem). Lowering records it on the
15// module, and on the functions that need a narrower one, so that policy-aware
16// passes can make consistent decisions and dumped IR replays with the policy it
17// was compiled with.
18//
19//===----------------------------------------------------------------------===//
20
21#ifndef FORTRAN_OPTIMIZER_SUPPORT_ALLOCATIONPOLICY_H
22#define FORTRAN_OPTIMIZER_SUPPORT_ALLOCATIONPOLICY_H
23
24#include "flang/Optimizer/Dialect/Support/FIRContext.h"
25#include "flang/Optimizer/Dialect/Support/KindMapping.h"
26#include "flang/Optimizer/Support/DataLayout.h"
27#include <cstddef>
28#include <cstdint>
29#include <functional>
30#include <optional>
31
32namespace mlir {
33class Location;
34class ModuleOp;
35class Operation;
36class Type;
37} // namespace mlir
38
39namespace fir {
40
46 static constexpr bool stackArraysDefault = false;
47 static constexpr std::uint64_t smallArrayThresholdBytesDefault = 1024;
48 static constexpr std::uint64_t totalStackLimitBytesDefault =
49 4ull * 1024 * 1024;
50
53 bool stackArrays = stackArraysDefault;
55 std::uint64_t smallArrayThresholdBytes = smallArrayThresholdBytesDefault;
57 std::uint64_t totalStackLimitBytes = totalStackLimitBytesDefault;
58};
59
69
79 bool isTemporary = false;
84 bool isDynamic = false;
86 std::optional<std::int64_t> byteSize;
87};
88
91 std::optional<mlir::DataLayout> dataLayout;
92 std::optional<fir::KindMapping> kindMap;
93};
94
97
101bool shouldUseStackForCopyin(mlir::Location loc, mlir::Type sequenceType,
102 const AllocationPolicy &policy,
103 const AllocationSizeContext &sizeContext);
104
108 mlir::Operation *op = nullptr;
110 bool isCurrentlyOnStack = false;
111};
112
118 const AllocationPolicy &policy,
119 std::size_t stackBytesUsed);
120
126 const AllocationPolicy &policy,
127 std::size_t stackBytesUsed);
128
134template <typename FieldT, typename OptionT>
135void overrideIfExplicitlySet(FieldT &field, const OptionT &option) {
136 if (option.hasValue())
137 field = static_cast<FieldT>(option);
138}
139
145 const AllocationInfo & /*info*/, const AllocationPolicy & /*policy*/,
146 std::size_t /*stackBytesUsed*/)>;
147
153
156void setAllocationPolicy(mlir::ModuleOp mod, const AllocationPolicy &policy);
157
160void setAllocationPolicy(mlir::Operation *op, const AllocationPolicy &policy);
161
163AllocationPolicy getAllocationPolicy(mlir::ModuleOp mod);
164
168std::optional<AllocationPolicy> getLocalAllocationPolicy(mlir::Operation *op);
169
174AllocationPolicy getAllocationPolicy(mlir::Operation *op);
175
176} // namespace fir
177
178#endif // FORTRAN_OPTIMIZER_SUPPORT_ALLOCATIONPOLICY_H
Definition AbstractConverter.h:37
AllocationPolicy getCommandLineAllocationPolicy(bool stackArrays)
Definition AllocationPolicy.cpp:116
AllocationPlacement decideAllocationPlacement(const AllocationInfo &info, const AllocationPolicy &policy, std::size_t stackBytesUsed)
Definition AllocationPolicy.cpp:97
AllocationPlacement
Desired placement for an array allocation.
Definition AllocationPolicy.h:61
@ Stack
The allocation should live on the stack (fir.alloca).
Definition AllocationPolicy.h:63
@ Heap
The allocation should live on the heap (fir.allocmem).
Definition AllocationPolicy.h:65
@ Leave
The allocation should be left where it currently is.
Definition AllocationPolicy.h:67
AllocationPolicy getAllocationPolicy(mlir::ModuleOp mod)
Get the policy recorded on mod, or the defaults if none was recorded.
Definition AllocationPolicy.cpp:137
std::function< AllocationPlacement( const AllocationInfo &, const AllocationPolicy &, std::size_t)> AllocationPlacementHook
Definition AllocationPolicy.h:144
bool shouldUseStackForCopyin(mlir::Location loc, mlir::Type sequenceType, const AllocationPolicy &policy, const AllocationSizeContext &sizeContext)
Definition AllocationPolicy.cpp:48
bool shouldAllocateOnStack(const PendingAllocationInfo &info, const AllocationPolicy &policy, std::size_t stackBytesUsed)
Definition AllocationPolicy.cpp:66
void setAllocationPolicy(mlir::ModuleOp mod, const AllocationPolicy &policy)
Definition AllocationPolicy.cpp:132
std::optional< AllocationPolicy > getLocalAllocationPolicy(mlir::Operation *op)
Definition AllocationPolicy.cpp:148
void overrideIfExplicitlySet(FieldT &field, const OptionT &option)
Definition AllocationPolicy.h:135
AllocationSizeContext getAllocationSizeContext(mlir::Operation *op)
Gather the module-level information needed to compute allocation sizes.
Definition AllocationPolicy.cpp:37
Definition AbstractConverter.h:32
Facts about a single existing array allocation used to decide its placement.
Definition AllocationPolicy.h:106
mlir::Operation * op
The allocation operation (fir.alloca or fir.allocmem).
Definition AllocationPolicy.h:108
bool isCurrentlyOnStack
True if the allocation currently lives on the stack (fir.alloca).
Definition AllocationPolicy.h:110
Definition AllocationPolicy.h:45
std::uint64_t smallArrayThresholdBytes
Constant-size arrays up to this many bytes are considered "small".
Definition AllocationPolicy.h:55
std::uint64_t totalStackLimitBytes
Per-function budget (in bytes) for small arrays placed on the stack.
Definition AllocationPolicy.h:57
bool stackArrays
Definition AllocationPolicy.h:53
Module-level information needed to compute constant allocation sizes.
Definition AllocationPolicy.h:90
Definition AllocationPolicy.h:76
bool isTemporary
Definition AllocationPolicy.h:79
bool isDynamic
Definition AllocationPolicy.h:84
std::optional< std::int64_t > byteSize
The constant size of the allocation in bytes, if it can be determined.
Definition AllocationPolicy.h:86