-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[mlir] Add a utility method to move operation dependencies. #129975
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MaheshRavishankar
merged 6 commits into
llvm:main
from
MaheshRavishankar:users/MaheshRavishankar/moveOperationDeps
Mar 11, 2025
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f364860
[mlir] Add a utility method to move operation dependencies.
MaheshRavishankar d13fc99
Address comments (round 1).
MaheshRavishankar 1697e33
Modify `TransformRewriter` listener to get the match failure remark a…
MaheshRavishankar b0ddc6c
Simplify slice computation.
MaheshRavishankar 59ce8d0
Fix Windows failure
MaheshRavishankar ac9e0d1
Fix code formatter errors.
MaheshRavishankar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| // RUN: mlir-opt --allow-unregistered-dialect --transform-interpreter --split-input-file %s | FileCheck %s | ||
|
|
||
| // Check simple move of dependent operation before insertion. | ||
| func.func @simple_move() -> f32 { | ||
| %0 = "before"() : () -> (f32) | ||
| %1 = "moved_op"() : () -> (f32) | ||
| %2 = "foo"(%1) : (f32) -> (f32) | ||
| return %2 : f32 | ||
| } | ||
| // CHECK-LABEL: func @simple_move() | ||
| // CHECK: %[[MOVED:.+]] = "moved_op" | ||
| // CHECK: %[[BEFORE:.+]] = "before" | ||
| // CHECK: %[[FOO:.+]] = "foo"(%[[MOVED]]) | ||
| // CHECK: return %[[FOO]] | ||
|
|
||
| module attributes {transform.with_named_sequence} { | ||
| transform.named_sequence @__transform_main(%arg0 : !transform.any_op {transform.readonly}) { | ||
| %op1 = transform.structured.match ops{["foo"]} in %arg0 | ||
| : (!transform.any_op) -> !transform.any_op | ||
| %op2 = transform.structured.match ops{["before"]} in %arg0 | ||
| : (!transform.any_op) -> !transform.any_op | ||
| transform.test.move_operand_deps %op1 before %op2 | ||
| : !transform.any_op, !transform.any_op | ||
| transform.yield | ||
| } | ||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| // Move operands that are implicitly captured by the op | ||
| func.func @move_region_dependencies() -> f32 { | ||
| %0 = "before"() : () -> (f32) | ||
| %1 = "moved_op"() : () -> (f32) | ||
| %2 = "foo"() ({ | ||
| %3 = "inner_op"(%1) : (f32) -> (f32) | ||
| "yield"(%3) : (f32) -> () | ||
| }) : () -> (f32) | ||
| return %2 : f32 | ||
| } | ||
| // CHECK-LABEL: func @move_region_dependencies() | ||
| // CHECK: %[[MOVED:.+]] = "moved_op" | ||
| // CHECK: %[[BEFORE:.+]] = "before" | ||
| // CHECK: %[[FOO:.+]] = "foo" | ||
| // CHECK: return %[[FOO]] | ||
|
|
||
| module attributes {transform.with_named_sequence} { | ||
| transform.named_sequence @__transform_main(%arg0 : !transform.any_op {transform.readonly}) { | ||
| %op1 = transform.structured.match ops{["foo"]} in %arg0 | ||
| : (!transform.any_op) -> !transform.any_op | ||
| %op2 = transform.structured.match ops{["before"]} in %arg0 | ||
| : (!transform.any_op) -> !transform.any_op | ||
| transform.test.move_operand_deps %op1 before %op2 | ||
| : !transform.any_op, !transform.any_op | ||
| transform.yield | ||
| } | ||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| // Move operations in toplogical sort order | ||
| func.func @move_in_topological_sort_order() -> f32 { | ||
| %0 = "before"() : () -> (f32) | ||
| %1 = "moved_op_1"() : () -> (f32) | ||
| %2 = "moved_op_2"() : () -> (f32) | ||
| %3 = "moved_op_3"(%1) : (f32) -> (f32) | ||
| %4 = "moved_op_4"(%1, %3) : (f32, f32) -> (f32) | ||
| %5 = "moved_op_5"(%2) : (f32) -> (f32) | ||
| %6 = "foo"(%4, %5) : (f32, f32) -> (f32) | ||
| return %6 : f32 | ||
| } | ||
| // CHECK-LABEL: func @move_in_topological_sort_order() | ||
| // CHECK: %[[MOVED_1:.+]] = "moved_op_1" | ||
| // CHECK-DAG: %[[MOVED_2:.+]] = "moved_op_3"(%[[MOVED_1]]) | ||
| // CHECK-DAG: %[[MOVED_3:.+]] = "moved_op_4"(%[[MOVED_1]], %[[MOVED_2]]) | ||
| // CHECK-DAG: %[[MOVED_4:.+]] = "moved_op_2" | ||
| // CHECK-DAG: %[[MOVED_5:.+]] = "moved_op_5"(%[[MOVED_4]]) | ||
| // CHECK: %[[BEFORE:.+]] = "before" | ||
| // CHECK: %[[FOO:.+]] = "foo"(%[[MOVED_3]], %[[MOVED_5]]) | ||
| // CHECK: return %[[FOO]] | ||
|
|
||
| module attributes {transform.with_named_sequence} { | ||
| transform.named_sequence @__transform_main(%arg0 : !transform.any_op {transform.readonly}) { | ||
| %op1 = transform.structured.match ops{["foo"]} in %arg0 | ||
| : (!transform.any_op) -> !transform.any_op | ||
| %op2 = transform.structured.match ops{["before"]} in %arg0 | ||
| : (!transform.any_op) -> !transform.any_op | ||
| transform.test.move_operand_deps %op1 before %op2 | ||
| : !transform.any_op, !transform.any_op | ||
| transform.yield | ||
| } | ||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| // Fail when the "before" operation is part of the operation slice. | ||
| func.func @do_not_move_slice() -> f32 { | ||
| %0 = "before"() : () -> (f32) | ||
| %1 = "moved_op"(%0) : (f32) -> (f32) | ||
| %2 = "foo"(%1) : (f32) -> (f32) | ||
| return %2 : f32 | ||
| } | ||
|
|
||
| module attributes {transform.with_named_sequence} { | ||
| transform.named_sequence @__transform_main(%arg0 : !transform.any_op {transform.readonly}) { | ||
| %op1 = transform.structured.match ops{["foo"]} in %arg0 | ||
| : (!transform.any_op) -> !transform.any_op | ||
| %op2 = transform.structured.match ops{["before"]} in %arg0 | ||
| : (!transform.any_op) -> !transform.any_op | ||
| transform.test.move_operand_deps %op1 before %op2 | ||
| : !transform.any_op, !transform.any_op | ||
| transform.yield | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| //===- TestTransformsOps.cpp - Test Transforms ----------------------------===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This file defines transform dialect operations for testing MLIR | ||
| // transformations | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "mlir/Dialect/Transform/IR/TransformAttrs.h" | ||
| #include "mlir/Dialect/Transform/IR/TransformDialect.h" | ||
| #include "mlir/Dialect/Transform/Interfaces/TransformInterfaces.h" | ||
| #include "mlir/Transforms/RegionUtils.h" | ||
|
|
||
| #define GET_OP_CLASSES | ||
| #include "TestTransformsOps.h.inc" | ||
|
|
||
| using namespace mlir; | ||
| using namespace mlir::transform; | ||
|
|
||
| #define GET_OP_CLASSES | ||
| #include "TestTransformsOps.cpp.inc" | ||
|
|
||
| DiagnosedSilenceableFailure | ||
| transform::TestMoveOperandDeps::apply(TransformRewriter &rewriter, | ||
| TransformResults &TransformResults, | ||
| TransformState &state) { | ||
| Operation *op = *state.getPayloadOps(getOp()).begin(); | ||
| Operation *moveBefore = *state.getPayloadOps(getInsertionPoint()).begin(); | ||
| if (failed(moveOperationDependencies(rewriter, op, moveBefore))) { | ||
| auto listener = cast<ErrorCheckingTrackingListener>(rewriter.getListener()); | ||
| std::string errorMsg = listener->checkAndResetError().getMessage(); | ||
| return emitSilenceableFailure(op, errorMsg); | ||
| } | ||
MaheshRavishankar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return DiagnosedSilenceableFailure::success(); | ||
| } | ||
|
|
||
| namespace { | ||
|
|
||
| class TestTransformsDialectExtension | ||
| : public transform::TransformDialectExtension< | ||
| TestTransformsDialectExtension> { | ||
| public: | ||
| MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestTransformsDialectExtension) | ||
|
|
||
| using Base::Base; | ||
|
|
||
| void init() { | ||
| registerTransformOps< | ||
| #define GET_OP_LIST | ||
| #include "TestTransformsOps.cpp.inc" | ||
| >(); | ||
| } | ||
| }; | ||
| } // namespace | ||
|
|
||
| namespace test { | ||
| void registerTestTransformsTransformDialectExtension( | ||
| DialectRegistry ®istry) { | ||
| registry.addExtensions<TestTransformsDialectExtension>(); | ||
| } | ||
| } // namespace test | ||
MaheshRavishankar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| //===- TestTransformOps.td ---------------------------------*- tablegen -*-===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef TEST_TRANSFORM_OPS | ||
| #define TEST_TRANSFORM_OPS | ||
|
|
||
| include "mlir/Dialect/Transform/IR/TransformDialect.td" | ||
| include "mlir/Dialect/Transform/Interfaces/TransformInterfaces.td" | ||
| include "mlir/Dialect/Transform/IR/TransformTypes.td" | ||
| include "mlir/Interfaces/SideEffectInterfaces.td" | ||
| include "mlir/IR/OpBase.td" | ||
|
|
||
| /// Transform dialect perations for testing transformations in MLIR | ||
MaheshRavishankar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def TestMoveOperandDeps : | ||
| Op<Transform_Dialect, "test.move_operand_deps", | ||
| [FunctionalStyleTransformOpTrait, MemoryEffectsOpInterface, | ||
| DeclareOpInterfaceMethods<TransformOpInterface>, | ||
| ReportTrackingListenerFailuresOpTrait]> { | ||
| let description = [{ | ||
| Moves all dependencies of on operation before another operation. | ||
| }]; | ||
|
|
||
| let arguments = | ||
| (ins TransformHandleTypeInterface:$op, | ||
| TransformHandleTypeInterface:$insertion_point); | ||
|
|
||
| let results = (outs); | ||
|
|
||
| let assemblyFormat = [{ | ||
| $op `before` $insertion_point attr-dict | ||
| `:` type($op) `,` type($insertion_point) | ||
| }]; | ||
| } | ||
|
|
||
| #endif // TEST_TRANSFORM_OPS | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.