Skip to content

Commit 213ab96

Browse files
authored
[flang][NFC] Use tablegen to reduce MemoryAllocationOpt boilerplate (#90062)
This is another one that runs on functions but isn't appropriate to also run on other top level operations. It needs to find all paths that return from the function to free heap allocated memory. There isn't a generic concept for general top level operations which is equivalent to looking for function returns. I removed the manual definition of the options structure because there is already an identical definition in tablegen and the options are documented in Passes.td.
1 parent 46b66df commit 213ab96

File tree

4 files changed

+9
-29
lines changed

4 files changed

+9
-29
lines changed

flang/include/flang/Optimizer/Transforms/Passes.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,10 @@ std::unique_ptr<mlir::Pass>
5959
createExternalNameConversionPass(bool appendUnderscore);
6060
std::unique_ptr<mlir::Pass> createMemDataFlowOptPass();
6161
std::unique_ptr<mlir::Pass> createPromoteToAffinePass();
62-
std::unique_ptr<mlir::Pass> createMemoryAllocationPass();
6362
std::unique_ptr<mlir::Pass> createAliasTagsPass();
6463
std::unique_ptr<mlir::Pass>
6564
createAddDebugInfoPass(fir::AddDebugInfoOptions options = {});
6665

67-
std::unique_ptr<mlir::Pass>
68-
createMemoryAllocationPass(bool dynOnHeap, std::size_t maxStackSize);
6966
std::unique_ptr<mlir::Pass> createAnnotateConstantOperandsPass();
7067
std::unique_ptr<mlir::Pass> createAlgebraicSimplificationPass();
7168
std::unique_ptr<mlir::Pass>

flang/include/flang/Optimizer/Transforms/Passes.td

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,6 @@ def MemoryAllocationOpt : Pass<"memory-allocation-opt", "mlir::func::FuncOp"> {
247247
"std::size_t", /*default=*/"~static_cast<std::size_t>(0)",
248248
"Set maximum number of elements of an array allocated on the stack.">
249249
];
250-
let constructor = "::fir::createMemoryAllocationPass()";
251250
}
252251

253252
def StackArrays : Pass<"stack-arrays", "mlir::ModuleOp"> {

flang/include/flang/Tools/CLOptions.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ inline void addAVC(
163163

164164
inline void addMemoryAllocationOpt(mlir::PassManager &pm) {
165165
addNestedPassConditionally<mlir::func::FuncOp>(pm, disableFirMao, [&]() {
166-
return fir::createMemoryAllocationPass(
167-
dynamicArrayStackToHeapAllocation, arrayStackAllocationThreshold);
166+
return fir::createMemoryAllocationOpt(
167+
{dynamicArrayStackToHeapAllocation, arrayStackAllocationThreshold});
168168
});
169169
}
170170

flang/lib/Optimizer/Transforms/MemoryAllocation.cpp

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,6 @@ namespace fir {
2828
static constexpr std::size_t unlimitedArraySize = ~static_cast<std::size_t>(0);
2929

3030
namespace {
31-
struct MemoryAllocationOptions {
32-
// Always move dynamic array allocations to the heap. This may result in more
33-
// heap fragmentation, so may impact performance negatively.
34-
bool dynamicArrayOnHeap = false;
35-
36-
// Number of elements in array threshold for moving to heap. In environments
37-
// with limited stack size, moving large arrays to the heap can avoid running
38-
// out of stack space.
39-
std::size_t maxStackArraySize = unlimitedArraySize;
40-
};
41-
4231
class ReturnAnalysis {
4332
public:
4433
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(ReturnAnalysis)
@@ -68,8 +57,9 @@ class ReturnAnalysis {
6857

6958
/// Return `true` if this allocation is to remain on the stack (`fir.alloca`).
7059
/// Otherwise the allocation should be moved to the heap (`fir.allocmem`).
71-
static inline bool keepStackAllocation(fir::AllocaOp alloca, mlir::Block *entry,
72-
const MemoryAllocationOptions &options) {
60+
static inline bool
61+
keepStackAllocation(fir::AllocaOp alloca, mlir::Block *entry,
62+
const fir::MemoryAllocationOptOptions &options) {
7363
// Limitation: only arrays allocated on the stack in the entry block are
7464
// considered for now.
7565
// TODO: Generalize the algorithm and placement of the freemem nodes.
@@ -168,6 +158,9 @@ class MemoryAllocationOpt
168158
options = {dynOnHeap, maxStackSize};
169159
}
170160

161+
MemoryAllocationOpt(const fir::MemoryAllocationOptOptions &options)
162+
: options{options} {}
163+
171164
/// Override `options` if command-line options have been set.
172165
inline void useCommandLineOptions() {
173166
if (dynamicArrayOnHeap)
@@ -211,15 +204,6 @@ class MemoryAllocationOpt
211204
}
212205

213206
private:
214-
MemoryAllocationOptions options;
207+
fir::MemoryAllocationOptOptions options;
215208
};
216209
} // namespace
217-
218-
std::unique_ptr<mlir::Pass> fir::createMemoryAllocationPass() {
219-
return std::make_unique<MemoryAllocationOpt>();
220-
}
221-
222-
std::unique_ptr<mlir::Pass>
223-
fir::createMemoryAllocationPass(bool dynOnHeap, std::size_t maxStackSize) {
224-
return std::make_unique<MemoryAllocationOpt>(dynOnHeap, maxStackSize);
225-
}

0 commit comments

Comments
 (0)