[Mlir-commits] [mlir] [mlir][mem2reg] Promote memory slots through transparent view operations (PR #196924)
Tobias Gysi
llvmlistbot at llvm.org
Mon May 11 22:23:32 PDT 2026
================
@@ -8,5 +8,114 @@
#include "mlir/Interfaces/MemorySlotInterfaces.h"
+#include "llvm/ADT/SmallPtrSet.h"
+#include "llvm/ADT/SmallVector.h"
+
#include "mlir/Interfaces/MemorySlotOpInterfaces.cpp.inc"
#include "mlir/Interfaces/MemorySlotTypeInterfaces.cpp.inc"
+
+using namespace mlir;
+
+namespace {
+/// One step in a view chain, leaf-first. `inputElemType` is the elemType
+/// of the slot one step closer to root; `outputElemType` is the elemType
+/// this step exposes.
+struct ViewStep {
+ PromotableOpInterface view;
+ Type inputElemType;
+ Type outputElemType;
+};
+} // namespace
+
+/// Walks back from `value` to `rootSlot.ptr` along
+/// `getPromotableSlotView` chains. On success, populates `chainOut` with
+/// the view ops leaf-to-root and writes the type at which `value` aliases
+/// the underlying slot to `*outViewElemType`.
+static bool walkPromotableSlotViewChain(Value value, const MemorySlot &rootSlot,
+ SmallVectorImpl<ViewStep> &chainOut,
+ Type *outViewElemType) {
+ if (value == rootSlot.ptr) {
+ if (outViewElemType)
+ *outViewElemType = rootSlot.elemType;
+ return true;
+ }
+
+ Value current = value;
+ Type aliasElemType{};
+ llvm::SmallPtrSet<Value, 4> seen;
+ while (current != rootSlot.ptr) {
+ if (!seen.insert(current).second)
+ return false;
+ auto promotable =
+ dyn_cast_or_null<PromotableOpInterface>(current.getDefiningOp());
+ if (!promotable)
+ return false;
+ std::optional<PromotableSlotView> info = promotable.getPromotableSlotView();
+ if (!info || info->view.ptr != current)
+ return false;
+ if (!aliasElemType)
+ aliasElemType = info->view.elemType;
+ chainOut.push_back(ViewStep{promotable, /*inputElemType=*/Type{},
+ /*outputElemType=*/info->view.elemType});
+ current = info->slotPointerOperand;
+ }
----------------
gysit wrote:
PS if we would like to store the subviews somewhere then `MemorySlot` could be the right place.
https://github.com/llvm/llvm-project/pull/196924
More information about the Mlir-commits
mailing list