-
Notifications
You must be signed in to change notification settings - Fork 738
Redefine ApplyRegisteredPass in the Transform dialect #7956
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
mehrdad2m
merged 24 commits into
master
from
eochoa/2025-07-24/transform-dialect-update
Aug 15, 2025
Merged
Changes from 17 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
c602a54
Use the transform dialect from our repository instead of the xDSL one
erick-xanadu eb94a67
Redefine Transform
erick-xanadu ebade75
copies over ApplyRegisteredPassOp
erick-xanadu 966a678
Use DictionaryAttr instead of StringAttr
erick-xanadu c3905c6
Redefine ApplyRegisteredPassOp
erick-xanadu 2e21cb8
initial test
erick-xanadu 0d1f4fa
Apply suggestions from code review
erick-xanadu 9ecadb7
Apply suggestions from code review
erick-xanadu 8c62649
Fix imports
mehrdad2m 6b83c17
move test to separate file
mehrdad2m 25dd616
fix type hints
mehrdad2m 3397df1
Merge branch 'master' into eochoa/2025-07-24/transform-dialect-update
mehrdad2m 701f80b
isort after rebase
mehrdad2m c0fda72
fix type hint
mehrdad2m 6f3e1ad
Merge branch 'master' into eochoa/2025-07-24/transform-dialect-update
mehrdad2m 0b7b631
import original transform separately
mehrdad2m 58a4a0f
remove redundant code
mehrdad2m 9f51c7b
check full option dict
mehrdad2m b0a3120
Update transform_interpreter to utilize full options data for PassPip…
mehrdad2m 262b32b
Add more unit tests for ApplyRegisteredPassOp with various options
mehrdad2m 28c98e8
Merge branch 'master' into eochoa/2025-07-24/transform-dialect-update
mehrdad2m 8558383
Merge branch 'master' into eochoa/2025-07-24/transform-dialect-update
mehrdad2m 6e94592
remove wildcard import
mehrdad2m d3b9139
fix a mistake
mehrdad2m 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
103 changes: 103 additions & 0 deletions
103
pennylane/compiler/python_compiler/dialects/transform.py
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,103 @@ | ||
| # Copyright 2025 Xanadu Quantum Technologies Inc. | ||
|
|
||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
|
|
||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """ | ||
| This file contains an updated version of the transform dialect. | ||
| As of the time of writing, xDSL uses the MLIR released with LLVM's | ||
| version 20.1.7. However, https://github.com/PennyLaneAI/catalyst/pull/1916 | ||
| will be updating MLIR where the transform dialect has the | ||
| `apply_registered_pass` operation re-defined. | ||
|
|
||
| See the following changelog on the above PR | ||
|
|
||
| Things related to transform.apply_registered_pass op: | ||
|
|
||
| It now takes in a dynamic_options | ||
|
|
||
| [MLIR][Transform] Allow ApplyRegisteredPassOp to take options as | ||
| a param llvm/llvm-project#142683. We don't need to use this as all our pass options are static. | ||
| https://github.com/llvm/llvm-project/pull/142683 | ||
|
|
||
| The options it takes in are now dictionaries instead of strings | ||
| [MLIR][Transform] apply_registered_pass op's options as a dict llvm/llvm-project#143159 | ||
| https://github.com/llvm/llvm-project/pull/143159 | ||
|
|
||
| This file will re-define the apply_registered_pass operation in xDSL | ||
| and the transform dialect. | ||
|
|
||
| Once xDSL moves to a newer version of MLIR, these changes should | ||
| be contributed upstream. | ||
| """ | ||
|
|
||
| # pylint: disable=unused-wildcard-import,wildcard-import,undefined-variable,too-few-public-methods | ||
| from xdsl.dialects.transform import ApplyRegisteredPassOp as xApplyRegisteredPassOp | ||
| from xdsl.dialects.transform import Transform as xTransform | ||
| from xdsl.dialects.transform import * | ||
|
|
||
|
|
||
| @irdl_op_definition | ||
| # pylint: disable=function-redefined | ||
| class ApplyRegisteredPassOp(IRDLOperation): | ||
| """ | ||
| See external [documentation](https://mlir.llvm.org/docs/Dialects/Transform/#transformapply_registered_pass-transformapplyregisteredpassop). | ||
| """ | ||
|
|
||
| name = "transform.apply_registered_pass" | ||
|
|
||
| options = prop_def(DictionaryAttr, default_value=DictionaryAttr({})) | ||
| pass_name = prop_def(StringAttr) | ||
| target = operand_def(TransformHandleType) | ||
| result = result_def(TransformHandleType) | ||
| # While this assembly format doesn't match | ||
| # the one in upstream MLIR, | ||
| # this is because xDSL currently lacks CustomDirectives | ||
| # https://mlir.llvm.org/docs/DefiningDialects/Operations/#custom-directives | ||
| # https://github.com/xdslproject/xdsl/pull/4829 | ||
| # However, storing the property in the attribute should still work | ||
| # specially when parsing and printing in generic format. | ||
| # Which is how Catalyst and XDSL currently communicate at the moment. | ||
| # TODO: Add test. | ||
| assembly_format = "$pass_name `to` $target attr-dict `:` functional-type(operands, results)" | ||
| irdl_options = [ParsePropInAttrDict()] | ||
|
|
||
| def __init__( | ||
| self, | ||
| pass_name: str | StringAttr, | ||
| target: SSAValue, | ||
| options: dict[str | StringAttr, Attribute | str | bool | int] | None = None, | ||
paul0403 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ): | ||
| if isinstance(pass_name, str): | ||
mehrdad2m marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| pass_name = StringAttr(pass_name) | ||
|
|
||
| if isinstance(options, dict): | ||
mehrdad2m marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| options = DictionaryAttr(options) | ||
|
|
||
| super().__init__( | ||
| properties={ | ||
| "pass_name": pass_name, | ||
| "options": options, | ||
| }, | ||
| operands=[target], | ||
| result_types=[target.type], | ||
| ) | ||
|
|
||
|
|
||
| # Copied over from xDSL's sources | ||
| # the main difference will be the use | ||
| # of a different ApplyRegisteredPassOp | ||
| operations = list(xTransform.operations) | ||
| del operations[operations.index(xApplyRegisteredPassOp)] | ||
| operations.append(ApplyRegisteredPassOp) | ||
| attributes = list(xTransform.attributes) | ||
|
|
||
| Transform = Dialect("transform", operations, attributes) | ||
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,42 @@ | ||
| # Copyright 2025 Xanadu Quantum Technologies Inc. | ||
|
|
||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
|
|
||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Unit test module for pennylane/compiler/python_compiler/transform.py.""" | ||
|
|
||
| import pytest | ||
|
|
||
| # pylint: disable=wrong-import-position | ||
|
|
||
| xdsl = pytest.importorskip("xdsl") | ||
| filecheck = pytest.importorskip("filecheck") | ||
|
|
||
| pytestmark = pytest.mark.external | ||
|
|
||
|
|
||
| def test_transform_dialect_update(run_filecheck): | ||
| """Test that the transform dialect is updated correctly.""" | ||
|
|
||
| program = """ | ||
| "builtin.module"() ({ | ||
| "transform.named_sequence"() <{function_type = (!transform.any_op) -> (), sym_name = "__transform_main"}> ({ | ||
| ^bb0(%arg0: !transform.any_op): | ||
| %0 = "transform.structured.match"(%arg0) <{ops = ["func.func"]}> : (!transform.any_op) -> !transform.any_op | ||
| // CHECK: "invalid-option" | ||
paul0403 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| %1 = "transform.apply_registered_pass"(%0) <{options = {"invalid-option" = 1 : i64}, pass_name = "canonicalize"}> : (!transform.any_op) -> !transform.any_op | ||
| "transform.yield"() : () -> () | ||
| }) : () -> () | ||
| }) {transform.with_named_sequence} : () -> () | ||
| """ | ||
|
|
||
| run_filecheck(program, ()) | ||
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.