[Mlir-commits] [mlir] [mlir] [memref] Elevate `AllocOp`s to `GlobalOp`s pass (PR #211141)
ioana ghiban
llvmlistbot at llvm.org
Mon Jul 27 05:20:43 PDT 2026
================
@@ -0,0 +1,126 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Dialect/MemRef/IR/MemRef.h"
+#include "mlir/Dialect/MemRef/Transforms/Passes.h"
+#include "mlir/Dialect/MemRef/Transforms/Transforms.h"
+#include "mlir/IR/Builders.h"
+#include "mlir/IR/BuiltinAttributes.h"
+#include "mlir/IR/BuiltinOps.h"
+#include "mlir/IR/PatternMatch.h"
+#include "mlir/IR/SymbolTable.h"
+#include "mlir/Interfaces/LoopLikeInterface.h"
+#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
+#include "llvm/Support/Casting.h"
+#include "llvm/Support/LogicalResult.h"
+
+namespace mlir {
+namespace memref {
+#define GEN_PASS_DEF_ELEVATEALLOCSTOGLOBALSPASS
+#include "mlir/Dialect/MemRef/Transforms/Passes.h.inc"
+} // namespace memref
+} // namespace mlir
+
+using namespace mlir;
+
+namespace {
+
+/// Returns true if `op` is contained inside any branching, region, or looping
+/// structure (such as scf.for, scf.if, or repetitive regions)
+static bool isInsideControlFlow(Operation *op) {
----------------
ioghiban wrote:
`isInsideControlFlow` only checks for enclosing operations implementing `RegionBranchOpInterface`, so it detects structured control flow (`scf. ...`) but not CFG-based control flow. For example, an allocation in a non-entry block reached via `cf.cond_br` has no enclosing `RegionBranchOpInterface`, so it is still considered eligible.
Is this intentional? If so, could the comment/documentation clarify that only _structured_ control flow is excluded?
If the intention is to reject allocations that are conditionally reached through the CFG as well, a conservative approximation would be to reject allocations outside the region entry block, e.g.
```cpp
!op->getBlock()->isEntryBlock();
```
although this is still only an approximation of “executes exactly once” and doesn’t address repeated function invocations.
https://github.com/llvm/llvm-project/pull/211141
More information about the Mlir-commits
mailing list