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;
89 std::optional<std::int64_t> alignment;
93 mlir::Operation *context = nullptr;
94};
95
99std::optional<std::size_t> stackBytesNeeded(const PendingAllocationInfo &info);
100
103 std::optional<mlir::DataLayout> dataLayout;
104 std::optional<fir::KindMapping> kindMap;
105};
106
109
115 mlir::Location loc, mlir::Type sequenceType, const AllocationPolicy &policy,
116 const AllocationSizeContext &sizeContext,
117 std::optional<std::int64_t> alignment = std::nullopt);
118
122 mlir::Operation *op = nullptr;
124 bool isCurrentlyOnStack = false;
125};
126
134 const AllocationPolicy &policy,
135 std::size_t stackBytesUsed);
136
142 const AllocationPolicy &policy,
143 std::size_t stackBytesUsed);
144
150template <typename FieldT, typename OptionT>
151void overrideIfExplicitlySet(FieldT &field, const OptionT &option) {
152 if (option.hasValue())
153 field = static_cast<FieldT>(option);
154}
155
161 const AllocationInfo & /*info*/, const AllocationPolicy & /*policy*/,
162 std::size_t /*stackBytesUsed*/)>;
163
169
172void setAllocationPolicy(mlir::ModuleOp mod, const AllocationPolicy &policy);
173
176void setAllocationPolicy(mlir::Operation *op, const AllocationPolicy &policy);
177
179AllocationPolicy getAllocationPolicy(mlir::ModuleOp mod);
180
184std::optional<AllocationPolicy> getLocalAllocationPolicy(mlir::Operation *op);
185
190AllocationPolicy getAllocationPolicy(mlir::Operation *op);
191
192} // namespace fir
193
194#endif // FORTRAN_OPTIMIZER_SUPPORT_ALLOCATIONPOLICY_H
Definition AbstractConverter.h:37
AllocationPolicy getCommandLineAllocationPolicy(bool stackArrays)
Definition AllocationPolicy.cpp:152
AllocationPlacement decideAllocationPlacement(const AllocationInfo &info, const AllocationPolicy &policy, std::size_t stackBytesUsed)
Definition AllocationPolicy.cpp:132
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
std::optional< std::size_t > stackBytesNeeded(const PendingAllocationInfo &info)
Definition AllocationPolicy.cpp:71
AllocationPolicy getAllocationPolicy(mlir::ModuleOp mod)
Get the policy recorded on mod, or the defaults if none was recorded.
Definition AllocationPolicy.cpp:173
bool shouldUseStackForCopyin(mlir::Location loc, mlir::Type sequenceType, const AllocationPolicy &policy, const AllocationSizeContext &sizeContext, std::optional< std::int64_t > alignment=std::nullopt)
Definition AllocationPolicy.cpp:49
std::function< AllocationPlacement( const AllocationInfo &, const AllocationPolicy &, std::size_t)> AllocationPlacementHook
Definition AllocationPolicy.h:160
bool shouldAllocateOnStack(const PendingAllocationInfo &info, const AllocationPolicy &policy, std::size_t stackBytesUsed)
Definition AllocationPolicy.cpp:124
void setAllocationPolicy(mlir::ModuleOp mod, const AllocationPolicy &policy)
Definition AllocationPolicy.cpp:168
std::optional< AllocationPolicy > getLocalAllocationPolicy(mlir::Operation *op)
Definition AllocationPolicy.cpp:184
void overrideIfExplicitlySet(FieldT &field, const OptionT &option)
Definition AllocationPolicy.h:151
AllocationSizeContext getAllocationSizeContext(mlir::Operation *op)
Gather the module-level information needed to compute allocation sizes.
Definition AllocationPolicy.cpp:38
Definition AbstractConverter.h:32
Facts about a single existing array allocation used to decide its placement.
Definition AllocationPolicy.h:120
mlir::Operation * op
The allocation operation (fir.alloca or fir.allocmem).
Definition AllocationPolicy.h:122
bool isCurrentlyOnStack
True if the allocation currently lives on the stack (fir.alloca).
Definition AllocationPolicy.h:124
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:102
Definition AllocationPolicy.h:76
mlir::Operation * context
Definition AllocationPolicy.h:93
bool isTemporary
Definition AllocationPolicy.h:79
bool isDynamic
Definition AllocationPolicy.h:84
std::optional< std::int64_t > alignment
Definition AllocationPolicy.h:89
std::optional< std::int64_t > byteSize
The constant size of the allocation in bytes, if it can be determined.
Definition AllocationPolicy.h:86