-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[MLIR][Transform] Allow stateInitializer and stateExporter for applyTransforms #101186
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
Merged
Changes from all commits
Commits
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
19 changes: 19 additions & 0 deletions
19
mlir/test/Dialect/Transform/transform-state-extension-initializer.mlir
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,19 @@ | ||
// RUN: mlir-opt %s -test-pass-state-extension-communication -verify-diagnostics | FileCheck %s | ||
|
||
// CHECK: Printing opCollection before processing transform ops, size: 1 | ||
// CHECK: PASS-TRANSFORMOP-PASS | ||
|
||
// CHECK: Printing opCollection after processing transform ops, size: 4 | ||
// CHECK: PASS-TRANSFORMOP-PASS transform.test_initializer_extension_A transform.test_initializer_extension_B transform.test_initializer_extension_C | ||
|
||
module attributes {transform.with_named_sequence} { | ||
transform.named_sequence @__transform_main(%arg0: !transform.any_op) { | ||
// expected-remark @below {{Number of currently registered op: 1}} | ||
transform.test_initializer_extension "A" | ||
// expected-remark @below {{Number of currently registered op: 2}} | ||
transform.test_initializer_extension "B" | ||
// expected-remark @below {{Number of currently registered op: 3}} | ||
transform.test_initializer_extension "C" | ||
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
101 changes: 101 additions & 0 deletions
101
mlir/test/lib/Dialect/Transform/TestPassStateExtensionCommunication.cpp
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,101 @@ | ||
//===- TestPassStateExtensionCommunication.cpp ----------------------------===// | ||
// | ||
// 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 a test pass that showcases how communication can be | ||
// conducted between a regular mlir pass and transform ops through the | ||
// transform state extension stateInitializer and stateExporter mechanism. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "TestTransformStateExtension.h" | ||
#include "mlir/Dialect/Transform/Interfaces/TransformInterfaces.h" | ||
#include "mlir/IR/BuiltinOps.h" | ||
#include "mlir/Pass/Pass.h" | ||
|
||
using namespace llvm; | ||
using namespace mlir; | ||
using namespace mlir::test; | ||
|
||
namespace { | ||
template <typename Derived> | ||
class OpPassWrapper : public PassWrapper<Derived, OperationPass<>> {}; | ||
|
||
struct TestPassStateExtensionCommunication | ||
: public PassWrapper<TestPassStateExtensionCommunication, | ||
OperationPass<ModuleOp>> { | ||
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID( | ||
TestPassStateExtensionCommunication) | ||
|
||
StringRef getArgument() const final { | ||
return "test-pass-state-extension-communication"; | ||
} | ||
|
||
StringRef getDescription() const final { | ||
return "test state communciation between a mlir pass and transform ops"; | ||
} | ||
|
||
static void printVector(const SmallVector<std::string> &opCollection, | ||
const std::string &extraMessage = {}) { | ||
outs() << "Printing opCollection" << extraMessage | ||
<< ", size: " << opCollection.size() << "\n"; | ||
for (const auto &subVector : opCollection) { | ||
outs() << subVector << " "; | ||
} | ||
outs() << "\n"; | ||
} | ||
|
||
void runOnOperation() override { | ||
ModuleOp module = getOperation(); | ||
|
||
// Create an opCollection vector. | ||
SmallVector<std::string> opCollection = {"PASS-TRANSFORMOP-PASS "}; | ||
printVector(opCollection, " before processing transform ops"); | ||
|
||
auto stateInitializer = | ||
[&opCollection](mlir::transform::TransformState &state) -> void { | ||
TransformStateInitializerExtension *ext = | ||
state.getExtension<TransformStateInitializerExtension>(); | ||
if (!ext) | ||
state.addExtension<TransformStateInitializerExtension>(0, opCollection); | ||
}; | ||
|
||
auto stateExporter = | ||
[&opCollection]( | ||
mlir::transform::TransformState &state) -> LogicalResult { | ||
TransformStateInitializerExtension *ext = | ||
state.getExtension<TransformStateInitializerExtension>(); | ||
if (!ext) { | ||
errs() << "Target transform state extension not found!\n"; | ||
return failure(); | ||
} | ||
opCollection.clear(); | ||
opCollection = ext->getRegisteredOps(); | ||
return success(); | ||
}; | ||
|
||
// Process transform ops with stateInitializer and stateExporter. | ||
for (auto op : module.getBody()->getOps<transform::TransformOpInterface>()) | ||
if (failed(transform::applyTransforms( | ||
module, op, {}, mlir::transform::TransformOptions(), false, | ||
stateInitializer, stateExporter))) | ||
return signalPassFailure(); | ||
|
||
// Print the opCollection vector after processing transform ops. | ||
printVector(opCollection, " after processing transform ops"); | ||
} | ||
}; | ||
} // namespace | ||
|
||
namespace mlir { | ||
namespace test { | ||
/// Registers the test pass here. | ||
void registerTestPassStateExtensionCommunication() { | ||
PassRegistration<TestPassStateExtensionCommunication> reg; | ||
} | ||
} // namespace test | ||
} // namespace mlir |
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
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.