@@ -34,20 +34,18 @@ static void process(mlir::ModuleOp mod, cir::FuncOp func) {
3434
3535 // Hoist all static allocas to the entry block.
3636 mlir::Block &entryBlock = func.getRegion ().front ();
37- llvm::SmallVector<cir::AllocaOp> allocas;
38- func.getBody ().walk ([&](cir::AllocaOp alloca) {
37+ mlir::Operation *insertPoint = &*entryBlock.begin ();
38+
39+ // Post-order is the default, but the code below requires it, so
40+ // let's not depend on the default staying that way.
41+ func.getBody ().walk <mlir::WalkOrder::PostOrder>([&](cir::AllocaOp alloca) {
3942 if (alloca->getBlock () == &entryBlock)
4043 return ;
4144 // Don't hoist allocas with dynamic alloca size.
4245 assert (!cir::MissingFeatures::opAllocaDynAllocSize ());
43- allocas.push_back (alloca);
44- });
45- if (allocas.empty ())
46- return ;
4746
48- mlir::Operation *insertPoint = &*entryBlock. begin ();
47+ // Hoist allocas into the entry block.
4948
50- for (cir::AllocaOp alloca : allocas) {
5149 // Preserving the `const` attribute on hoisted allocas can cause LLVM to
5250 // incorrectly introduce invariant group metadata in some circumstances.
5351 // The incubator performs some analysis to determine whether the attribute
@@ -59,7 +57,7 @@ static void process(mlir::ModuleOp mod, cir::FuncOp func) {
5957 alloca.setConstant (false );
6058
6159 alloca->moveBefore (insertPoint);
62- }
60+ });
6361}
6462
6563void HoistAllocasPass::runOnOperation () {
@@ -71,7 +69,12 @@ void HoistAllocasPass::runOnOperation() {
7169 if (!mod)
7270 mod = op->getParentOfType <mlir::ModuleOp>();
7371
74- getOperation ()->walk ([&](cir::FuncOp op) { process (mod, op); });
72+ // If we ever introduce nested cir.function ops, we'll need to make this
73+ // walk in post-order and recurse into nested functions.
74+ getOperation ()->walk <mlir::WalkOrder::PreOrder>([&](cir::FuncOp op) {
75+ process (mod, op);
76+ return mlir::WalkResult::skip ();
77+ });
7578}
7679
7780} // namespace
0 commit comments