[Mlir-commits] [mlir] [MLIR][Transform] Prefer entry points in current module (PR #151323)
Jakub Kuderski
llvmlistbot at llvm.org
Thu Jul 31 07:17:57 PDT 2025
================
@@ -121,23 +121,94 @@ ModuleOp transform::detail::getPreloadedTransformModule(MLIRContext *context) {
->getLibraryModule();
}
+namespace {
+
+transform::TransformOpInterface
+findTransformEntryPointNonRecursive(Operation *op, StringRef entryPoint) {
+ for (Region ®ion : op->getRegions()) {
+ for (Block &block : region.getBlocks()) {
+ auto namedSequenceOps = block.getOps<transform::NamedSequenceOp>();
+ for (transform::NamedSequenceOp namedSequenceOp : namedSequenceOps) {
+ if (namedSequenceOp.getSymName() == entryPoint) {
+ return cast<transform::TransformOpInterface>(
+ namedSequenceOp.getOperation());
+ }
+ }
+ }
+ }
+ return nullptr;
+}
+
+transform::TransformOpInterface
+findTransformEntryPointRecursive(Operation *op, StringRef entryPoint) {
+ transform::TransformOpInterface transform = nullptr;
+ op->walk<WalkOrder::PreOrder>(
+ [&](transform::NamedSequenceOp namedSequenceOp) {
+ if (namedSequenceOp.getSymName() == entryPoint) {
+ transform = cast<transform::TransformOpInterface>(
+ namedSequenceOp.getOperation());
+ return WalkResult::interrupt();
+ }
+ return WalkResult::advance();
+ });
+ return transform;
+}
+
+// Will look for the transform's entry point favouring NamedSequenceOps
+// ops that exist within the operation without the need for nesting.
+// If no operation exists in the blocks owned by op, then it will recursively
+// walk the op in preorder and find the first NamedSequenceOp that matches
+// the entry point's name.
+//
+// This allows for the following two use cases:
+// 1. op is a module annotated with the transform.with_named_sequence attribute
+// that has an entry point in its block. E.g.,
+//
+// ```mlir
+// module {transform.with_named_sequence} {
+// transform.named_sequence @__transform_main(%arg0 : !transform.any_op) ->
+// () {
+// transform.yield
+// }
+// }
+// ```
+//
+// 2. op is a program which contains a nested module annotated with the
+// transform.with_named_sequence attribute. E.g.,
+//
+// ```mlir
+// module {
+// func.func @foo () {
+// }
+//
+// module {transform.with_named_sequence} {
+// transform.named_sequence @__transform_main(%arg0 : !transform.any_op)
+// -> () {
+// transform.yield
+// }
+// }
+// }
+// ```
+transform::TransformOpInterface
----------------
kuhar wrote:
```suggestion
static transform::TransformOpInterface
```
https://github.com/llvm/llvm-project/pull/151323
More information about the Mlir-commits
mailing list