[Mlir-commits] [mlir] [MLIR][Python] Impl XOpInterface(s) from Python, with X=Transform and X=MemoryEffects (PR #176920)

Rolf Morel llvmlistbot at llvm.org
Thu Feb 12 04:59:50 PST 2026


================
@@ -167,3 +167,77 @@ MlirLogicalResult mlirInferShapedTypeOpInterfaceInferReturnTypes(
   }
   return mlirLogicalResultSuccess();
 }
+
+//===---------------------------------------------------------------------===//
+// MemoryEffectOpInterface
+//===---------------------------------------------------------------------===//
+
+MlirTypeID mlirMemoryEffectsOpInterfaceTypeID() {
+  return wrap(MemoryEffectOpInterface::getInterfaceID());
+}
+
+/// Fallback model for the MemoryEffectsOpInterface that uses C API callbacks.
+class MemoryEffectOpInterfaceFallbackModel
+    : public mlir::MemoryEffectOpInterface::FallbackModel<
+          MemoryEffectOpInterfaceFallbackModel> {
+public:
+  /// Sets the callbacks that this FallbackModel will use.
+  /// NB: the callbacks can only be set through this method as the
+  /// RegisteredOperationName::attachInterface mechanism default-constructs
+  /// the FallbackModel without being able to provide arguments.
+  void setCallbacks(MlirMemoryEffectsOpInterfaceCallbacks callbacks) {
+    this->callbacks = callbacks;
+  }
+
+  ~MemoryEffectOpInterfaceFallbackModel() {
+    if (callbacks.destruct)
+      callbacks.destruct(callbacks.userData);
+  }
+
+  static TypeID getInterfaceID() {
+    return MemoryEffectOpInterface::getInterfaceID();
+  }
+
+  static bool classof(const mlir::MemoryEffectOpInterface::Concept *op) {
+    // Enable casting back to the FallbackModel from the Interface. This is
+    // necessary as attachInterface(...) default-constructs the FallbackModel
+    // without being able to pass in the callbacks and returns just the Concept.
+    return true;
+  }
+
+  void
+  getEffects(Operation *op,
+             SmallVectorImpl<MemoryEffects::EffectInstance> &effects) const {
+    assert(callbacks.getEffects && "getEffects callback not set");
+    MlirMemoryEffectInstancesList cEffects = wrap(&effects);
+    callbacks.getEffects(wrap(op), cEffects, callbacks.userData);
+  }
+
+private:
+  MlirMemoryEffectsOpInterfaceCallbacks callbacks;
+};
+
+/// Attach a MemoryEffectsOpInterface FallbackModel to the given named op.
+/// The FallbackModel uses the provided callbacks to implement the interface.
+void mlirMemoryEffectsOpInterfaceAttachFallbackModel(
+    MlirContext ctx, MlirStringRef opName,
+    MlirMemoryEffectsOpInterfaceCallbacks callbacks) {
+  // Look up the operation definition in the context
+  std::optional<RegisteredOperationName> opInfo =
+      RegisteredOperationName::lookup(unwrap(opName), unwrap(ctx));
+
+  if (!opInfo.has_value()) {
+    llvm::errs() << "Operation '" << unwrap(opName)
+                 << "' not found in context\n";
+    return;
+  }
----------------
rolfmorel wrote:

Done.

https://github.com/llvm/llvm-project/pull/176920


More information about the Mlir-commits mailing list