10#include "mlir/Dialect/Func/IR/FuncOps.h"
11#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
12#include "mlir/IR/PatternMatch.h"
13#include "mlir/Support/LogicalResult.h"
14#include "mlir/Transforms/DialectConversion.h"
15#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
19#include "llvm/Support/TimeProfiler.h"
25#define GEN_PASS_DEF_HOISTALLOCAS
26#include "clang/CIR/Dialect/Passes.h.inc"
31struct HoistAllocasPass :
public impl::HoistAllocasBase<HoistAllocasPass> {
33 HoistAllocasPass() =
default;
34 void runOnOperation()
override;
42static mlir::Block *getHoistDestBlock(cir::AllocaOp alloca) {
43 mlir::Region *region = alloca->getParentRegion();
45 mlir::Operation *parentOp = region->getParentOp();
49 if (parentOp->hasTrait<mlir::OpTrait::IsIsolatedFromAbove>() ||
50 mlir::isa<mlir::omp::OutlineableOpenMPOpInterface>(parentOp))
51 return ®ion->front();
52 region = parentOp->getParentRegion();
56static void process(mlir::ModuleOp mod, cir::FuncOp func) {
57 if (func.getRegion().empty())
61 llvm::DenseMap<mlir::Block *, mlir::Operation *> insertPoints;
65 func.getBody().walk<mlir::WalkOrder::PostOrder>([&](cir::AllocaOp alloca) {
66 mlir::Block *destBlock = getHoistDestBlock(alloca);
67 if (alloca->getBlock() == destBlock)
70 if (alloca.getDynAllocSize())
82 if (alloca.getConstant())
83 alloca.setConstant(
false);
85 mlir::Operation *&insertPoint =
86 insertPoints.try_emplace(destBlock, &*destBlock->begin()).first->second;
87 alloca->moveBefore(insertPoint);
91void HoistAllocasPass::runOnOperation() {
92 llvm::TimeTraceScope scope(
"Hoist Allocas");
93 llvm::SmallVector<Operation *, 16> ops;
95 Operation *op = getOperation();
96 auto mod = mlir::dyn_cast<mlir::ModuleOp>(op);
98 mod = op->getParentOfType<mlir::ModuleOp>();
102 getOperation()->walk<mlir::WalkOrder::PreOrder>([&](cir::FuncOp op) {
104 return mlir::WalkResult::skip();
111 return std::make_unique<HoistAllocasPass>();
std::unique_ptr< Pass > createHoistAllocasPass()
static bool optInfoAttr()