forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[flang] handle alloca outside of entry blocks in MemoryAllocation (ll…
…vm#98457) This patch generalizes the MemoryAllocation pass (alloca -> heap) to handle fir.alloca regardless of their postion in the IR. Currently, it only dealt with fir.alloca in function entry blocks. The logic is placed in a utility that can be used to replace alloca in an operation on demand to whatever kind of allocation the utility user wants via callbacks (allocmem, or custom runtime calls to instrument the code...). To do so, a concept of ownership, that was already implied a bit and used in passes like stack-reclaim, is formalized. Any operation with the LoopLikeInterface, AutomaticAllocationScope, or IsolatedFromAbove owns the alloca directly nested inside its regions, and they must not be used after the operation. The pass then looks for the exit points of region with such interface, and use that to insert deallocation. If dominance is not proved, the pass fallbacks to storing the new address into a C pointer variable created in the entry of the owning region which allows inserting deallocation as needed, included near the alloca itself to avoid leaks when the alloca is executed multiple times due to block CFGs loops. This should fix llvm#88344. In a next step, I will try to refactor lowering a bit to introduce lifetime operation for alloca so that the deallocation points can be inserted as soon as possible.
- Loading branch information
1 parent
2f8de7c
commit 00486d9
Showing
9 changed files
with
610 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//===-- Optimizer/Transforms/MemoryUtils.h ----------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/ | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file defines a utility to replace fir.alloca by dynamic allocation and | ||
// deallocation. The exact kind of dynamic allocation is left to be defined by | ||
// the utility user via callbacks (could be fir.allocmem or custom runtime | ||
// calls). | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef FORTRAN_OPTIMIZER_TRANSFORMS_MEMORYUTILS_H | ||
#define FORTRAN_OPTIMIZER_TRANSFORMS_MEMORYUTILS_H | ||
|
||
#include "flang/Optimizer/Dialect/FIROps.h" | ||
|
||
namespace mlir { | ||
class RewriterBase; | ||
} | ||
|
||
namespace fir { | ||
|
||
/// Type of callbacks that indicate if a given fir.alloca must be | ||
/// rewritten. | ||
using MustRewriteCallBack = llvm::function_ref<bool(fir::AllocaOp)>; | ||
|
||
/// Type of callbacks that produce the replacement for a given fir.alloca. | ||
/// It is provided extra information about the dominance of the deallocation | ||
/// points that have been identified, and may refuse to replace the alloca, | ||
/// even if the MustRewriteCallBack previously returned true, in which case | ||
/// it should return a null value. | ||
/// The callback should not delete the alloca, the utility will do it. | ||
using AllocaRewriterCallBack = llvm::function_ref<mlir::Value( | ||
mlir::OpBuilder &, fir::AllocaOp, bool allocaDominatesDeallocLocations)>; | ||
/// Type of callbacks that must generate deallocation of storage obtained via | ||
/// AllocaRewriterCallBack calls. | ||
using DeallocCallBack = | ||
llvm::function_ref<void(mlir::Location, mlir::OpBuilder &, mlir::Value)>; | ||
|
||
/// Utility to replace fir.alloca by dynamic allocations inside \p parentOp. | ||
/// \p MustRewriteCallBack lets the user control which fir.alloca should be | ||
/// replaced. \p AllocaRewriterCallBack lets the user define how the new memory | ||
/// should be allocated. \p DeallocCallBack lets the user decide how the memory | ||
/// should be deallocated. The boolean result indicates if the utility succeeded | ||
/// to replace all fir.alloca as requested by the user. Causes of failures are | ||
/// the presence of unregistered operations, or OpenMP/ACC recipe operations | ||
/// that return memory allocated inside their region. | ||
bool replaceAllocas(mlir::RewriterBase &rewriter, mlir::Operation *parentOp, | ||
MustRewriteCallBack, AllocaRewriterCallBack, | ||
DeallocCallBack); | ||
|
||
} // namespace fir | ||
|
||
#endif // FORTRAN_OPTIMIZER_TRANSFORMS_MEMORYUTILS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.