clang 23.0.0git
HoistAllocas.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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#include "PassDetail.h"
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"
20
21using namespace mlir;
22using namespace cir;
23
24namespace mlir {
25#define GEN_PASS_DEF_HOISTALLOCAS
26#include "clang/CIR/Dialect/Passes.h.inc"
27} // namespace mlir
28
29namespace {
30
31struct HoistAllocasPass : public impl::HoistAllocasBase<HoistAllocasPass> {
32
33 HoistAllocasPass() = default;
34 void runOnOperation() override;
35};
36
37// Find the block that an alloca should be hoisted into. Allocas are normally
38// hoisted to the entry block of the enclosing function. However, an alloca may
39// be nested inside an OpenMP region such as omp.parallel, omp.teams
40// etc. Hoisting it out of these ops breaks the isolated from above requirement
41// for omp.teams and it changes privatization semantics.
42static mlir::Block *getHoistDestBlock(cir::AllocaOp alloca) {
43 mlir::Region *region = alloca->getParentRegion();
44 while (true) {
45 mlir::Operation *parentOp = region->getParentOp();
46
47 // Note: We may want some kind of interface in the future for blocking
48 // alloca hoisting since other dialects may have similar restrictions.
49 if (parentOp->hasTrait<mlir::OpTrait::IsIsolatedFromAbove>() ||
50 mlir::isa<mlir::omp::OutlineableOpenMPOpInterface>(parentOp))
51 return &region->front();
52 region = parentOp->getParentRegion();
53 }
54}
55
56static void process(mlir::ModuleOp mod, cir::FuncOp func) {
57 if (func.getRegion().empty())
58 return;
59
60 // Keep track of destination so that the order of allocas is preserved.
61 llvm::DenseMap<mlir::Block *, mlir::Operation *> insertPoints;
62
63 // Post-order is the default, but the code below requires it, so
64 // let's not depend on the default staying that way.
65 func.getBody().walk<mlir::WalkOrder::PostOrder>([&](cir::AllocaOp alloca) {
66 mlir::Block *destBlock = getHoistDestBlock(alloca);
67 if (alloca->getBlock() == destBlock)
68 return;
69 // Don't hoist allocas with dynamic alloca size.
70 if (alloca.getDynAllocSize())
71 return;
72
73 // Hoist allocas into the entry block.
74
75 // Preserving the `const` attribute on hoisted allocas can cause LLVM to
76 // incorrectly introduce invariant group metadata in some circumstances.
77 // The incubator performs some analysis to determine whether the attribute
78 // can be preserved, but it only runs this analysis when optimizations are
79 // enabled. Until we start tracking the optimization level, we can just
80 // always remove the `const` attribute.
82 if (alloca.getConstant())
83 alloca.setConstant(false);
84
85 mlir::Operation *&insertPoint =
86 insertPoints.try_emplace(destBlock, &*destBlock->begin()).first->second;
87 alloca->moveBefore(insertPoint);
88 });
89}
90
91void HoistAllocasPass::runOnOperation() {
92 llvm::TimeTraceScope scope("Hoist Allocas");
93 llvm::SmallVector<Operation *, 16> ops;
94
95 Operation *op = getOperation();
96 auto mod = mlir::dyn_cast<mlir::ModuleOp>(op);
97 if (!mod)
98 mod = op->getParentOfType<mlir::ModuleOp>();
99
100 // If we ever introduce nested cir.function ops, we'll need to make this
101 // walk in post-order and recurse into nested functions.
102 getOperation()->walk<mlir::WalkOrder::PreOrder>([&](cir::FuncOp op) {
103 process(mod, op);
104 return mlir::WalkResult::skip();
105 });
106}
107
108} // namespace
109
110std::unique_ptr<Pass> mlir::createHoistAllocasPass() {
111 return std::make_unique<HoistAllocasPass>();
112}
std::unique_ptr< Pass > createHoistAllocasPass()
static bool optInfoAttr()