Skip to content

[flang][NFC] Use tablegen to reduce MemoryAllocationOpt boilerplate #90062

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions flang/include/flang/Optimizer/Transforms/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,10 @@ std::unique_ptr<mlir::Pass>
createExternalNameConversionPass(bool appendUnderscore);
std::unique_ptr<mlir::Pass> createMemDataFlowOptPass();
std::unique_ptr<mlir::Pass> createPromoteToAffinePass();
std::unique_ptr<mlir::Pass> createMemoryAllocationPass();
std::unique_ptr<mlir::Pass> createAliasTagsPass();
std::unique_ptr<mlir::Pass>
createAddDebugInfoPass(fir::AddDebugInfoOptions options = {});

std::unique_ptr<mlir::Pass>
createMemoryAllocationPass(bool dynOnHeap, std::size_t maxStackSize);
std::unique_ptr<mlir::Pass> createAnnotateConstantOperandsPass();
std::unique_ptr<mlir::Pass> createAlgebraicSimplificationPass();
std::unique_ptr<mlir::Pass>
Expand Down
1 change: 0 additions & 1 deletion flang/include/flang/Optimizer/Transforms/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ def MemoryAllocationOpt : Pass<"memory-allocation-opt", "mlir::func::FuncOp"> {
"std::size_t", /*default=*/"~static_cast<std::size_t>(0)",
"Set maximum number of elements of an array allocated on the stack.">
];
let constructor = "::fir::createMemoryAllocationPass()";
}

def StackArrays : Pass<"stack-arrays", "mlir::ModuleOp"> {
Expand Down
4 changes: 2 additions & 2 deletions flang/include/flang/Tools/CLOptions.inc
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ inline void addAVC(

inline void addMemoryAllocationOpt(mlir::PassManager &pm) {
addNestedPassConditionally<mlir::func::FuncOp>(pm, disableFirMao, [&]() {
return fir::createMemoryAllocationPass(
dynamicArrayStackToHeapAllocation, arrayStackAllocationThreshold);
return fir::createMemoryAllocationOpt(
{dynamicArrayStackToHeapAllocation, arrayStackAllocationThreshold});
});
}

Expand Down
30 changes: 7 additions & 23 deletions flang/lib/Optimizer/Transforms/MemoryAllocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,6 @@ namespace fir {
static constexpr std::size_t unlimitedArraySize = ~static_cast<std::size_t>(0);

namespace {
struct MemoryAllocationOptions {
// Always move dynamic array allocations to the heap. This may result in more
// heap fragmentation, so may impact performance negatively.
bool dynamicArrayOnHeap = false;

// Number of elements in array threshold for moving to heap. In environments
// with limited stack size, moving large arrays to the heap can avoid running
// out of stack space.
std::size_t maxStackArraySize = unlimitedArraySize;
};

class ReturnAnalysis {
public:
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(ReturnAnalysis)
Expand Down Expand Up @@ -68,8 +57,9 @@ class ReturnAnalysis {

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

MemoryAllocationOpt(const fir::MemoryAllocationOptOptions &options)
: options{options} {}

/// Override `options` if command-line options have been set.
inline void useCommandLineOptions() {
if (dynamicArrayOnHeap)
Expand Down Expand Up @@ -211,15 +204,6 @@ class MemoryAllocationOpt
}

private:
MemoryAllocationOptions options;
fir::MemoryAllocationOptOptions options;
};
} // namespace

std::unique_ptr<mlir::Pass> fir::createMemoryAllocationPass() {
return std::make_unique<MemoryAllocationOpt>();
}

std::unique_ptr<mlir::Pass>
fir::createMemoryAllocationPass(bool dynOnHeap, std::size_t maxStackSize) {
return std::make_unique<MemoryAllocationOpt>(dynOnHeap, maxStackSize);
}