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 module
15// so that policy-aware passes can make consistent decisions and dumped IR
16// replays with the policy it was compiled with.
17//
18//===----------------------------------------------------------------------===//
19
20#ifndef FORTRAN_OPTIMIZER_SUPPORT_ALLOCATIONPOLICY_H
21#define FORTRAN_OPTIMIZER_SUPPORT_ALLOCATIONPOLICY_H
22
23#include <cstddef>
24#include <cstdint>
25#include <functional>
26#include <optional>
27
28namespace mlir {
29class ModuleOp;
30class Operation;
31} // namespace mlir
32
33namespace fir {
34
40 static constexpr bool stackArraysDefault = false;
41 static constexpr std::uint64_t smallArrayThresholdBytesDefault = 1024;
42 static constexpr std::uint64_t totalStackLimitBytesDefault =
43 4ull * 1024 * 1024;
44
47 bool stackArrays = stackArraysDefault;
49 std::uint64_t smallArrayThresholdBytes = smallArrayThresholdBytesDefault;
51 std::uint64_t totalStackLimitBytes = totalStackLimitBytesDefault;
52};
53
63
73 bool isTemporary = false;
78 bool isDynamic = false;
80 std::optional<std::int64_t> byteSize;
81};
82
86 mlir::Operation *op = nullptr;
88 bool isCurrentlyOnStack = false;
89};
90
96 const AllocationPolicy &policy,
97 std::size_t stackBytesUsed);
98
104 const AllocationPolicy &policy,
105 std::size_t stackBytesUsed);
106
112template <typename FieldT, typename OptionT>
113void overrideIfExplicitlySet(FieldT &field, const OptionT &option) {
114 if (option.hasValue())
115 field = static_cast<FieldT>(option);
116}
117
123 const AllocationInfo & /*info*/, const AllocationPolicy & /*policy*/,
124 std::size_t /*stackBytesUsed*/)>;
125
131
134void setAllocationPolicy(mlir::ModuleOp mod, const AllocationPolicy &policy);
135
137AllocationPolicy getAllocationPolicy(mlir::ModuleOp mod);
138
142AllocationPolicy getAllocationPolicy(mlir::Operation *op);
143
144} // namespace fir
145
146#endif // FORTRAN_OPTIMIZER_SUPPORT_ALLOCATIONPOLICY_H
Definition AbstractConverter.h:37
AllocationPolicy getCommandLineAllocationPolicy(bool stackArrays)
Definition AllocationPolicy.cpp:86
AllocationPlacement decideAllocationPlacement(const AllocationInfo &info, const AllocationPolicy &policy, std::size_t stackBytesUsed)
Definition AllocationPolicy.cpp:67
AllocationPlacement
Desired placement for an array allocation.
Definition AllocationPolicy.h:55
@ Stack
The allocation should live on the stack (fir.alloca).
Definition AllocationPolicy.h:57
@ Heap
The allocation should live on the heap (fir.allocmem).
Definition AllocationPolicy.h:59
@ Leave
The allocation should be left where it currently is.
Definition AllocationPolicy.h:61
AllocationPolicy getAllocationPolicy(mlir::ModuleOp mod)
Get the policy recorded on mod, or the defaults if none was recorded.
Definition AllocationPolicy.cpp:102
std::function< AllocationPlacement( const AllocationInfo &, const AllocationPolicy &, std::size_t)> AllocationPlacementHook
Definition AllocationPolicy.h:122
bool shouldAllocateOnStack(const PendingAllocationInfo &info, const AllocationPolicy &policy, std::size_t stackBytesUsed)
Definition AllocationPolicy.cpp:36
void setAllocationPolicy(mlir::ModuleOp mod, const AllocationPolicy &policy)
Definition AllocationPolicy.cpp:94
void overrideIfExplicitlySet(FieldT &field, const OptionT &option)
Definition AllocationPolicy.h:113
Definition AbstractConverter.h:32
Facts about a single existing array allocation used to decide its placement.
Definition AllocationPolicy.h:84
mlir::Operation * op
The allocation operation (fir.alloca or fir.allocmem).
Definition AllocationPolicy.h:86
bool isCurrentlyOnStack
True if the allocation currently lives on the stack (fir.alloca).
Definition AllocationPolicy.h:88
Definition AllocationPolicy.h:39
std::uint64_t smallArrayThresholdBytes
Constant-size arrays up to this many bytes are considered "small".
Definition AllocationPolicy.h:49
std::uint64_t totalStackLimitBytes
Per-function budget (in bytes) for small arrays placed on the stack.
Definition AllocationPolicy.h:51
bool stackArrays
Definition AllocationPolicy.h:47
Definition AllocationPolicy.h:70
bool isTemporary
Definition AllocationPolicy.h:73
bool isDynamic
Definition AllocationPolicy.h:78
std::optional< std::int64_t > byteSize
The constant size of the allocation in bytes, if it can be determined.
Definition AllocationPolicy.h:80