From aac36c29d6cc459c9bb9e7079e7b3aa2388fc456 Mon Sep 17 00:00:00 2001 From: Dalia Shaaban Date: Mon, 5 Feb 2024 23:05:46 +0000 Subject: [PATCH 01/12] donezo --- xdsl/transforms/individual_rewrite.py | 37 +++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 xdsl/transforms/individual_rewrite.py diff --git a/xdsl/transforms/individual_rewrite.py b/xdsl/transforms/individual_rewrite.py new file mode 100644 index 0000000000..ad792d6306 --- /dev/null +++ b/xdsl/transforms/individual_rewrite.py @@ -0,0 +1,37 @@ +from dataclasses import dataclass + +from xdsl.dialects.builtin import ModuleOp +from xdsl.ir import MLContext +from xdsl.passes import ModulePass +from xdsl.pattern_rewriter import PatternRewriter, RewritePattern +from xdsl.tools.command_line_tool import get_all_dialects +from xdsl.traits import HasCanonicalisationPatternsTrait + +REWRITE_BY_NAMES: dict[str, dict[str, RewritePattern]] = { + op.name: { + pattern.__class__.__name__: pattern + for pattern in trait.get_canonicalization_patterns() + } + for dialect in get_all_dialects().values() + for op in dialect().operations + if (trait := op.get_trait(HasCanonicalisationPatternsTrait)) is not None +} + + +@dataclass +class IndividualRewrite(ModulePass): + name = "individual-rewrite" + + matched_operation_index: int | None = None + operation_name: str | None = None + pattern_name: str | None = None + + def apply(self, ctx: MLContext, op: ModuleOp) -> None: + assert self.matched_operation_index is not None + assert self.operation_name is not None + assert self.pattern_name is not None + + matched_operation = list(op.walk())[self.matched_operation_index] + rewriter = PatternRewriter(matched_operation) + pattern = REWRITE_BY_NAMES[self.operation_name][self.pattern_name] + pattern.match_and_rewrite(matched_operation, rewriter) From ff84ab817ccc48460c32f915dd3fa75fdbff83af Mon Sep 17 00:00:00 2001 From: Dalia Shaaban Date: Mon, 5 Feb 2024 23:07:07 +0000 Subject: [PATCH 02/12] hiii --- xdsl/dialects/arith.py | 13 +++++++++++-- .../canonicalization_patterns/arith.py | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 xdsl/transforms/canonicalization_patterns/arith.py diff --git a/xdsl/dialects/arith.py b/xdsl/dialects/arith.py index 15425ef26a..3407aac5b5 100644 --- a/xdsl/dialects/arith.py +++ b/xdsl/dialects/arith.py @@ -34,8 +34,9 @@ result_def, ) from xdsl.parser import Parser +from xdsl.pattern_rewriter import RewritePattern from xdsl.printer import Printer -from xdsl.traits import ConstantLike, Pure +from xdsl.traits import ConstantLike, HasCanonicalisationPatternsTrait, Pure from xdsl.utils.deprecation import deprecated from xdsl.utils.exceptions import VerifyException from xdsl.utils.hints import isa @@ -263,11 +264,19 @@ def print(self, printer: Printer): IntegerBinaryOp = BinaryOperation[IntegerType] +class AddiOpHasCanonicalizationPatternsTrait(HasCanonicalisationPatternsTrait): + @classmethod + def get_canonicalization_patterns(cls) -> tuple[RewritePattern, ...]: + from xdsl.transforms.canonicalization_patterns.arith import AddImmediateZero + + return (AddImmediateZero(),) + + @irdl_op_definition class Addi(SignlessIntegerBinaryOp): name = "arith.addi" - traits = frozenset([Pure()]) + traits = frozenset([Pure(), AddiOpHasCanonicalizationPatternsTrait()]) @irdl_op_definition diff --git a/xdsl/transforms/canonicalization_patterns/arith.py b/xdsl/transforms/canonicalization_patterns/arith.py new file mode 100644 index 0000000000..13f816b935 --- /dev/null +++ b/xdsl/transforms/canonicalization_patterns/arith.py @@ -0,0 +1,18 @@ +from xdsl.dialects import arith +from xdsl.dialects.builtin import IntegerAttr +from xdsl.pattern_rewriter import ( + PatternRewriter, + RewritePattern, + op_type_rewrite_pattern, +) + + +class AddImmediateZero(RewritePattern): + @op_type_rewrite_pattern + def match_and_rewrite(self, op: arith.Addi, rewriter: PatternRewriter) -> None: + if ( + isinstance(op.lhs.owner, arith.Constant) + and isinstance(value := op.lhs.owner.value, IntegerAttr) + and value.value.data == 0 + ): + rewriter.replace_matched_op([], [op.rhs]) From 4dc68d5939e7d34caca434c0258daf168b4e1747 Mon Sep 17 00:00:00 2001 From: Dalia Shaaban Date: Mon, 5 Feb 2024 23:08:32 +0000 Subject: [PATCH 03/12] hii --- xdsl/tools/command_line_tool.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/xdsl/tools/command_line_tool.py b/xdsl/tools/command_line_tool.py index b339a67e0d..6ba185a85e 100644 --- a/xdsl/tools/command_line_tool.py +++ b/xdsl/tools/command_line_tool.py @@ -333,6 +333,11 @@ def get_lower_halo_to_mpi(): return stencil_global_to_local.LowerHaloToMPI + def get_individual_rewrite(): + from xdsl.transforms.individual_rewrite import IndividualRewrite + + return IndividualRewrite + def get_lower_affine(): from xdsl.transforms import lower_affine @@ -489,6 +494,7 @@ def get_test_lower_linalg_to_snitch(): "frontend-desymrefy": get_desymrefy, "gpu-map-parallel-loops": get_gpu_map_parallel_loops, "hls-convert-stencil-to-ll-mlir": get_hls_convert_stencil_to_ll_mlir, + "individual-rewrite": get_individual_rewrite, "lower-affine": get_lower_affine, "lower-hls": get_lower_hls, "lower-mpi": get_lower_mpi, From 951fc8b2521d83b2f2cf8be4bda01ff2f9bc934c Mon Sep 17 00:00:00 2001 From: Dalia Shaaban Date: Mon, 5 Feb 2024 23:19:26 +0000 Subject: [PATCH 04/12] lil fix --- tests/interactive/test_app.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/interactive/test_app.py b/tests/interactive/test_app.py index 3ea455c6d8..2e35ce4f12 100644 --- a/tests/interactive/test_app.py +++ b/tests/interactive/test_app.py @@ -17,6 +17,7 @@ from xdsl.interactive.app import InputApp from xdsl.ir import Block, Region from xdsl.transforms import ( + individual_rewrite, mlir_opt, printf_to_llvm, scf_parallel_loop_tiling, @@ -270,6 +271,7 @@ async def test_buttons(): convert_func_to_riscv_func.ConvertFuncToRiscvFuncPass, stencil_global_to_local.DistributeStencilPass, hls_convert_stencil_to_ll_mlir.HLSConvertStencilToLLMLIRPass, + individual_rewrite.IndividualRewrite, mlir_opt.MLIROptPass, printf_to_llvm.PrintfToLLVM, scf_parallel_loop_tiling.ScfParallelLoopTilingPass, From 3eee9ac1a19fc765af0cae58b15bd376eb7f847b Mon Sep 17 00:00:00 2001 From: Dalia Shaaban Date: Tue, 6 Feb 2024 15:46:13 +0000 Subject: [PATCH 05/12] sasha + matty b comments --- tests/interactive/test_app.py | 2 +- xdsl/tools/command_line_tool.py | 2 +- xdsl/transforms/individual_rewrite.py | 29 +++++++++++++++++++++++++-- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/tests/interactive/test_app.py b/tests/interactive/test_app.py index 2e35ce4f12..a28c82d0d4 100644 --- a/tests/interactive/test_app.py +++ b/tests/interactive/test_app.py @@ -267,11 +267,11 @@ async def test_buttons(): condensed_list = tuple( ( + individual_rewrite.IndividualRewrite, convert_arith_to_riscv.ConvertArithToRiscvPass, convert_func_to_riscv_func.ConvertFuncToRiscvFuncPass, stencil_global_to_local.DistributeStencilPass, hls_convert_stencil_to_ll_mlir.HLSConvertStencilToLLMLIRPass, - individual_rewrite.IndividualRewrite, mlir_opt.MLIROptPass, printf_to_llvm.PrintfToLLVM, scf_parallel_loop_tiling.ScfParallelLoopTilingPass, diff --git a/xdsl/tools/command_line_tool.py b/xdsl/tools/command_line_tool.py index 6ba185a85e..85b2dcb121 100644 --- a/xdsl/tools/command_line_tool.py +++ b/xdsl/tools/command_line_tool.py @@ -494,7 +494,7 @@ def get_test_lower_linalg_to_snitch(): "frontend-desymrefy": get_desymrefy, "gpu-map-parallel-loops": get_gpu_map_parallel_loops, "hls-convert-stencil-to-ll-mlir": get_hls_convert_stencil_to_ll_mlir, - "individual-rewrite": get_individual_rewrite, + "apply-individual-rewrite": get_individual_rewrite, "lower-affine": get_lower_affine, "lower-hls": get_lower_hls, "lower-mpi": get_lower_mpi, diff --git a/xdsl/transforms/individual_rewrite.py b/xdsl/transforms/individual_rewrite.py index ad792d6306..5303aa9367 100644 --- a/xdsl/transforms/individual_rewrite.py +++ b/xdsl/transforms/individual_rewrite.py @@ -16,11 +16,24 @@ for op in dialect().operations if (trait := op.get_trait(HasCanonicalisationPatternsTrait)) is not None } +""" +Returns a dictionary representing all possible rewrites. Keys are operation names, and +values are dictionaries. In the inner dictionary, the keys are names of patterns +associated with each operation, and the values are the corresponding RewritePattern +instances. +""" @dataclass class IndividualRewrite(ModulePass): - name = "individual-rewrite" + """ + Module pass representing the application of an individual rewrite pattern to a module. + + Matches the operation at the provided index within the module and applies the rewrite + pattern specified by the operation and pattern names. + """ + + name = "apply-individual-rewrite" matched_operation_index: int | None = None operation_name: str | None = None @@ -31,7 +44,19 @@ def apply(self, ctx: MLContext, op: ModuleOp) -> None: assert self.operation_name is not None assert self.pattern_name is not None + matched_operation_list = list(op.walk()) + if self.matched_operation_index >= len(matched_operation_list): + raise ValueError("Matched operation index out of range.") + matched_operation = list(op.walk())[self.matched_operation_index] rewriter = PatternRewriter(matched_operation) - pattern = REWRITE_BY_NAMES[self.operation_name][self.pattern_name] + + rewrite_dictionary = REWRITE_BY_NAMES.get(self.operation_name) + if rewrite_dictionary is None: + raise ValueError("Operation name not found in the rewrite dictionary.") + + pattern = rewrite_dictionary.get(self.pattern_name) + if pattern is None: + raise ValueError("Pattern name not found for the provided operation name.") + pattern.match_and_rewrite(matched_operation, rewriter) From 07d7570ad9f786f2b53354afc9c371a326c2959a Mon Sep 17 00:00:00 2001 From: Dalia Shaaban Date: Tue, 6 Feb 2024 17:07:00 +0000 Subject: [PATCH 06/12] wip --- .../transforms/individual_rewrite.mlir | 19 +++++++++++++++++++ xdsl/transforms/individual_rewrite.py | 4 +++- 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 tests/filecheck/transforms/individual_rewrite.mlir diff --git a/tests/filecheck/transforms/individual_rewrite.mlir b/tests/filecheck/transforms/individual_rewrite.mlir new file mode 100644 index 0000000000..e04f29f42d --- /dev/null +++ b/tests/filecheck/transforms/individual_rewrite.mlir @@ -0,0 +1,19 @@ +// RUN: xdsl-opt -p 'apply-individual-rewrite{matched_operation_index=4 operation_name="arith.addi" pattern_name="AddImmediateZero"}'| filecheck %s + +func.func @hello(%n : i32) -> i32 { + %two = arith.constant 0 : i32 + %three = arith.constant 0 : i32 + %res = arith.addi %two, %n : i32 + %res2 = arith.addi %three, %res : i32 + func.return %res : i32 +} + + +//CHECK: builtin.module { +// CHECK-NEXT: func.func @hello(%n : i32) -> i32 { +// CHECK-NEXT: %two = arith.constant 0 : i32 +// CHECK-NEXT: %three = arith.constant 0 : i32 +// CHECK-NEXT: %res2 = arith.addi %three, %n : i32 +// CHECK-NEXT: func.return %res2 : i32 +// CHECK-NEXT: } +// CHECK-NEXT: } diff --git a/xdsl/transforms/individual_rewrite.py b/xdsl/transforms/individual_rewrite.py index 5303aa9367..342ca401ff 100644 --- a/xdsl/transforms/individual_rewrite.py +++ b/xdsl/transforms/individual_rewrite.py @@ -53,7 +53,9 @@ def apply(self, ctx: MLContext, op: ModuleOp) -> None: rewrite_dictionary = REWRITE_BY_NAMES.get(self.operation_name) if rewrite_dictionary is None: - raise ValueError("Operation name not found in the rewrite dictionary.") + raise ValueError( + f"Operation name {self.operation_name} not found in the rewrite dictionary." + ) pattern = rewrite_dictionary.get(self.pattern_name) if pattern is None: From 045f7f766cdcb6e7aa37986604c66465cd535d19 Mon Sep 17 00:00:00 2001 From: Dalia Shaaban Date: Tue, 6 Feb 2024 17:32:15 +0000 Subject: [PATCH 07/12] hi --- xdsl/transforms/individual_rewrite.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/xdsl/transforms/individual_rewrite.py b/xdsl/transforms/individual_rewrite.py index 342ca401ff..fbcf06a57e 100644 --- a/xdsl/transforms/individual_rewrite.py +++ b/xdsl/transforms/individual_rewrite.py @@ -45,6 +45,7 @@ def apply(self, ctx: MLContext, op: ModuleOp) -> None: assert self.pattern_name is not None matched_operation_list = list(op.walk()) + print(len(matched_operation_list)) if self.matched_operation_index >= len(matched_operation_list): raise ValueError("Matched operation index out of range.") @@ -59,6 +60,10 @@ def apply(self, ctx: MLContext, op: ModuleOp) -> None: pattern = rewrite_dictionary.get(self.pattern_name) if pattern is None: - raise ValueError("Pattern name not found for the provided operation name.") + raise ValueError( + f"Pattern name {self.pattern_name} not found for the provided operation name." + ) pattern.match_and_rewrite(matched_operation, rewriter) + if not rewriter.has_done_action: + raise ValueError("Invalid rewrite at current location.") From bbb867c98c62e967226986dc005086d67d6b12e9 Mon Sep 17 00:00:00 2001 From: Dalia Shaaban Date: Tue, 6 Feb 2024 17:35:00 +0000 Subject: [PATCH 08/12] hi --- xdsl/transforms/individual_rewrite.py | 1 - 1 file changed, 1 deletion(-) diff --git a/xdsl/transforms/individual_rewrite.py b/xdsl/transforms/individual_rewrite.py index fbcf06a57e..2bf30dd32b 100644 --- a/xdsl/transforms/individual_rewrite.py +++ b/xdsl/transforms/individual_rewrite.py @@ -45,7 +45,6 @@ def apply(self, ctx: MLContext, op: ModuleOp) -> None: assert self.pattern_name is not None matched_operation_list = list(op.walk()) - print(len(matched_operation_list)) if self.matched_operation_index >= len(matched_operation_list): raise ValueError("Matched operation index out of range.") From b07dd6a05135ac4f068baa537dd1bce5bcaf9bd4 Mon Sep 17 00:00:00 2001 From: Dalia Shaaban Date: Tue, 6 Feb 2024 17:36:08 +0000 Subject: [PATCH 09/12] filecheck --- .../transforms/arith-add-immediate-zero.mlir | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 tests/filecheck/transforms/arith-add-immediate-zero.mlir diff --git a/tests/filecheck/transforms/arith-add-immediate-zero.mlir b/tests/filecheck/transforms/arith-add-immediate-zero.mlir new file mode 100644 index 0000000000..ce6f4fbb7b --- /dev/null +++ b/tests/filecheck/transforms/arith-add-immediate-zero.mlir @@ -0,0 +1,21 @@ +// RUN: xdsl-opt %s -p 'apply-individual-rewrite{matched_operation_index=5 operation_name="arith.addi" pattern_name="AddImmediateZero"}'| filecheck %s + +func.func @hello(%n : i32) -> i32 { + %two = arith.constant 0 : i32 + %three = arith.constant 0 : i32 + %res = arith.addi %two, %n : i32 + %res2 = arith.addi %three, %res : i32 + func.return %res : i32 +} + + +//CHECK: builtin.module { +// CHECK-NEXT: func.func @hello(%n : i32) -> i32 { +// CHECK-NEXT: %two = arith.constant 0 : i32 +// CHECK-NEXT: %three = arith.constant 0 : i32 +// CHECK-NEXT: %res = arith.addi %two, %n : i32 +// CHECK-NEXT: func.return %res : i32 +// CHECK-NEXT: } +// CHECK-NEXT: } + + From f4ee3bd6b37faac8dbca64c918a1cc5802628249 Mon Sep 17 00:00:00 2001 From: Dalia Shaaban Date: Tue, 6 Feb 2024 17:45:35 +0000 Subject: [PATCH 10/12] add file check --- .../transforms/individual_rewrite.mlir | 70 +++++++++++++++---- 1 file changed, 56 insertions(+), 14 deletions(-) diff --git a/tests/filecheck/transforms/individual_rewrite.mlir b/tests/filecheck/transforms/individual_rewrite.mlir index e04f29f42d..2fd471a719 100644 --- a/tests/filecheck/transforms/individual_rewrite.mlir +++ b/tests/filecheck/transforms/individual_rewrite.mlir @@ -1,19 +1,61 @@ -// RUN: xdsl-opt -p 'apply-individual-rewrite{matched_operation_index=4 operation_name="arith.addi" pattern_name="AddImmediateZero"}'| filecheck %s +// RUN:xdsl-opt %s -p 'apply-individual-rewrite{matched_operation_index=8 operation_name="riscv.add" pattern_name="AddImmediates"}'| filecheck %s -func.func @hello(%n : i32) -> i32 { - %two = arith.constant 0 : i32 - %three = arith.constant 0 : i32 - %res = arith.addi %two, %n : i32 - %res2 = arith.addi %three, %res : i32 - func.return %res : i32 +riscv_func.func @at_least_once() { + %one = riscv.li 1 : () -> !riscv.reg<> + %three = riscv.li 3 : () -> !riscv.reg<> + %0 = riscv.mv %one : (!riscv.reg<>) -> !riscv.reg<> + riscv_cf.bge %0 : !riscv.reg<>, %three : !riscv.reg<>, ^1(%0 : !riscv.reg<>), ^0(%0 : !riscv.reg<>) +^0(%i : !riscv.reg<>): + riscv.label "scf_body_0_for" + "test.op"(%i) : (!riscv.reg<>) -> () + %1 = riscv.add %i, %one : (!riscv.reg<>, !riscv.reg<>) -> !riscv.reg<> + riscv_cf.blt %1 : !riscv.reg<>, %three : !riscv.reg<>, ^0(%1 : !riscv.reg<>), ^1(%1 : !riscv.reg<>) +^1(%2 : !riscv.reg<>): + riscv.label "scf_body_end_0_for" + riscv_func.return +} +riscv_func.func @never() { + %one = riscv.li 1 : () -> !riscv.reg<> + %three = riscv.li 3 : () -> !riscv.reg<> + %0 = riscv.mv %one : (!riscv.reg<>) -> !riscv.reg<> + riscv_cf.bge %three : !riscv.reg<>, %one : !riscv.reg<>, ^1(%0 : !riscv.reg<>), ^0(%0 : !riscv.reg<>) +^0(%i : !riscv.reg<>): + riscv.label "scf_body_0_for" + "test.op"(%i) : (!riscv.reg<>) -> () + %1 = riscv.add %i, %one : (!riscv.reg<>, !riscv.reg<>) -> !riscv.reg<> + riscv_cf.blt %1 : !riscv.reg<>, %three : !riscv.reg<>, ^0(%1 : !riscv.reg<>), ^1(%1 : !riscv.reg<>) +^1(%2 : !riscv.reg<>): + riscv.label "scf_body_end_0_for" + riscv_func.return } - //CHECK: builtin.module { -// CHECK-NEXT: func.func @hello(%n : i32) -> i32 { -// CHECK-NEXT: %two = arith.constant 0 : i32 -// CHECK-NEXT: %three = arith.constant 0 : i32 -// CHECK-NEXT: %res2 = arith.addi %three, %n : i32 -// CHECK-NEXT: func.return %res2 : i32 +// CHECK-NEXT: riscv_func.func @at_least_once() { +// CHECK-NEXT: %one = riscv.li 1 : () -> !riscv.reg<> +// CHECK-NEXT: %three = riscv.li 3 : () -> !riscv.reg<> +// CHECK-NEXT: %0 = riscv.mv %one : (!riscv.reg<>) -> !riscv.reg<> +// CHECK-NEXT: riscv_cf.bge %0 : !riscv.reg<>, %three : !riscv.reg<>, ^0(%0 : !riscv.reg<>), ^1(%0 : !riscv.reg<>) +// CHECK-NEXT: ^1(%i : !riscv.reg<>): +// CHECK-NEXT: riscv.label "scf_body_0_for" +// CHECK-NEXT: "test.op"(%i) : (!riscv.reg<>) -> () +// CHECK-NEXT: %1 = riscv.addi %i, 1 : (!riscv.reg<>) -> !riscv.reg<> +// CHECK-NEXT: riscv_cf.blt %1 : !riscv.reg<>, %three : !riscv.reg<>, ^1(%1 : !riscv.reg<>), ^0(%1 : !riscv.reg<>) +// CHECK-NEXT: ^0(%2 : !riscv.reg<>): +// CHECK-NEXT: riscv.label "scf_body_end_0_for" +// CHECK-NEXT: riscv_func.return +// CHECK-NEXT: } +// CHECK-NEXT: riscv_func.func @never() { +// CHECK-NEXT: %one_1 = riscv.li 1 : () -> !riscv.reg<> +// CHECK-NEXT: %three_1 = riscv.li 3 : () -> !riscv.reg<> +// CHECK-NEXT: %3 = riscv.mv %one_1 : (!riscv.reg<>) -> !riscv.reg<> +// CHECK-NEXT: riscv_cf.bge %three_1 : !riscv.reg<>, %one_1 : !riscv.reg<>, ^2(%3 : !riscv.reg<>), ^3(%3 : !riscv.reg<>) +// CHECK-NEXT: ^3(%i_1 : !riscv.reg<>): +// CHECK-NEXT: riscv.label "scf_body_0_for" +// CHECK-NEXT: "test.op"(%i_1) : (!riscv.reg<>) -> () +// CHECK-NEXT: %4 = riscv.add %i_1, %one_1 : (!riscv.reg<>, !riscv.reg<>) -> !riscv.reg<> +// CHECK-NEXT: riscv_cf.blt %4 : !riscv.reg<>, %three_1 : !riscv.reg<>, ^3(%4 : !riscv.reg<>), ^2(%4 : !riscv.reg<>) +// CHECK-NEXT: ^2(%5 : !riscv.reg<>): +// CHECK-NEXT: riscv.label "scf_body_end_0_for" +// CHECK-NEXT: riscv_func.return +// CHECK-NEXT: } // CHECK-NEXT: } -// CHECK-NEXT: } From b0893b9a0f6c11f108ea5e1587db51b0d903d461 Mon Sep 17 00:00:00 2001 From: Dalia Shaaban Date: Tue, 6 Feb 2024 18:08:13 +0000 Subject: [PATCH 11/12] mathieu comment --- tests/filecheck/transforms/arith-add-immediate-zero.mlir | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/filecheck/transforms/arith-add-immediate-zero.mlir b/tests/filecheck/transforms/arith-add-immediate-zero.mlir index ce6f4fbb7b..603e84c7a5 100644 --- a/tests/filecheck/transforms/arith-add-immediate-zero.mlir +++ b/tests/filecheck/transforms/arith-add-immediate-zero.mlir @@ -1,4 +1,4 @@ -// RUN: xdsl-opt %s -p 'apply-individual-rewrite{matched_operation_index=5 operation_name="arith.addi" pattern_name="AddImmediateZero"}'| filecheck %s +// RUN: xdsl-opt %s -p canonicalize | filecheck %s func.func @hello(%n : i32) -> i32 { %two = arith.constant 0 : i32 @@ -13,8 +13,7 @@ func.func @hello(%n : i32) -> i32 { // CHECK-NEXT: func.func @hello(%n : i32) -> i32 { // CHECK-NEXT: %two = arith.constant 0 : i32 // CHECK-NEXT: %three = arith.constant 0 : i32 -// CHECK-NEXT: %res = arith.addi %two, %n : i32 -// CHECK-NEXT: func.return %res : i32 +// CHECK-NEXT: func.return %n : i32 // CHECK-NEXT: } // CHECK-NEXT: } From b9b208c6f873d604c758d88d69631f28344f2354 Mon Sep 17 00:00:00 2001 From: Dalia Shaaban Date: Tue, 6 Feb 2024 19:58:30 +0000 Subject: [PATCH 12/12] sashaaaaa --- .../transforms/individual_rewrite.mlir | 69 ++++--------------- 1 file changed, 12 insertions(+), 57 deletions(-) diff --git a/tests/filecheck/transforms/individual_rewrite.mlir b/tests/filecheck/transforms/individual_rewrite.mlir index 2fd471a719..8f36475eec 100644 --- a/tests/filecheck/transforms/individual_rewrite.mlir +++ b/tests/filecheck/transforms/individual_rewrite.mlir @@ -1,61 +1,16 @@ -// RUN:xdsl-opt %s -p 'apply-individual-rewrite{matched_operation_index=8 operation_name="riscv.add" pattern_name="AddImmediates"}'| filecheck %s +// RUN:xdsl-opt %s -p 'apply-individual-rewrite{matched_operation_index=4 operation_name="riscv.add" pattern_name="AddImmediates"}'| filecheck %s + +%a = riscv.li 1 : () -> !riscv.reg<> +%b = riscv.li 2 : () -> !riscv.reg<> +%c = riscv.li 3 : () -> !riscv.reg<> +%d = riscv.add %a, %b : (!riscv.reg<>, !riscv.reg<>) -> !riscv.reg<> +%e = riscv.add %b, %c : (!riscv.reg<>, !riscv.reg<>) -> !riscv.reg<> -riscv_func.func @at_least_once() { - %one = riscv.li 1 : () -> !riscv.reg<> - %three = riscv.li 3 : () -> !riscv.reg<> - %0 = riscv.mv %one : (!riscv.reg<>) -> !riscv.reg<> - riscv_cf.bge %0 : !riscv.reg<>, %three : !riscv.reg<>, ^1(%0 : !riscv.reg<>), ^0(%0 : !riscv.reg<>) -^0(%i : !riscv.reg<>): - riscv.label "scf_body_0_for" - "test.op"(%i) : (!riscv.reg<>) -> () - %1 = riscv.add %i, %one : (!riscv.reg<>, !riscv.reg<>) -> !riscv.reg<> - riscv_cf.blt %1 : !riscv.reg<>, %three : !riscv.reg<>, ^0(%1 : !riscv.reg<>), ^1(%1 : !riscv.reg<>) -^1(%2 : !riscv.reg<>): - riscv.label "scf_body_end_0_for" - riscv_func.return -} -riscv_func.func @never() { - %one = riscv.li 1 : () -> !riscv.reg<> - %three = riscv.li 3 : () -> !riscv.reg<> - %0 = riscv.mv %one : (!riscv.reg<>) -> !riscv.reg<> - riscv_cf.bge %three : !riscv.reg<>, %one : !riscv.reg<>, ^1(%0 : !riscv.reg<>), ^0(%0 : !riscv.reg<>) -^0(%i : !riscv.reg<>): - riscv.label "scf_body_0_for" - "test.op"(%i) : (!riscv.reg<>) -> () - %1 = riscv.add %i, %one : (!riscv.reg<>, !riscv.reg<>) -> !riscv.reg<> - riscv_cf.blt %1 : !riscv.reg<>, %three : !riscv.reg<>, ^0(%1 : !riscv.reg<>), ^1(%1 : !riscv.reg<>) -^1(%2 : !riscv.reg<>): - riscv.label "scf_body_end_0_for" - riscv_func.return -} //CHECK: builtin.module { -// CHECK-NEXT: riscv_func.func @at_least_once() { -// CHECK-NEXT: %one = riscv.li 1 : () -> !riscv.reg<> -// CHECK-NEXT: %three = riscv.li 3 : () -> !riscv.reg<> -// CHECK-NEXT: %0 = riscv.mv %one : (!riscv.reg<>) -> !riscv.reg<> -// CHECK-NEXT: riscv_cf.bge %0 : !riscv.reg<>, %three : !riscv.reg<>, ^0(%0 : !riscv.reg<>), ^1(%0 : !riscv.reg<>) -// CHECK-NEXT: ^1(%i : !riscv.reg<>): -// CHECK-NEXT: riscv.label "scf_body_0_for" -// CHECK-NEXT: "test.op"(%i) : (!riscv.reg<>) -> () -// CHECK-NEXT: %1 = riscv.addi %i, 1 : (!riscv.reg<>) -> !riscv.reg<> -// CHECK-NEXT: riscv_cf.blt %1 : !riscv.reg<>, %three : !riscv.reg<>, ^1(%1 : !riscv.reg<>), ^0(%1 : !riscv.reg<>) -// CHECK-NEXT: ^0(%2 : !riscv.reg<>): -// CHECK-NEXT: riscv.label "scf_body_end_0_for" -// CHECK-NEXT: riscv_func.return -// CHECK-NEXT: } -// CHECK-NEXT: riscv_func.func @never() { -// CHECK-NEXT: %one_1 = riscv.li 1 : () -> !riscv.reg<> -// CHECK-NEXT: %three_1 = riscv.li 3 : () -> !riscv.reg<> -// CHECK-NEXT: %3 = riscv.mv %one_1 : (!riscv.reg<>) -> !riscv.reg<> -// CHECK-NEXT: riscv_cf.bge %three_1 : !riscv.reg<>, %one_1 : !riscv.reg<>, ^2(%3 : !riscv.reg<>), ^3(%3 : !riscv.reg<>) -// CHECK-NEXT: ^3(%i_1 : !riscv.reg<>): -// CHECK-NEXT: riscv.label "scf_body_0_for" -// CHECK-NEXT: "test.op"(%i_1) : (!riscv.reg<>) -> () -// CHECK-NEXT: %4 = riscv.add %i_1, %one_1 : (!riscv.reg<>, !riscv.reg<>) -> !riscv.reg<> -// CHECK-NEXT: riscv_cf.blt %4 : !riscv.reg<>, %three_1 : !riscv.reg<>, ^3(%4 : !riscv.reg<>), ^2(%4 : !riscv.reg<>) -// CHECK-NEXT: ^2(%5 : !riscv.reg<>): -// CHECK-NEXT: riscv.label "scf_body_end_0_for" -// CHECK-NEXT: riscv_func.return -// CHECK-NEXT: } +// CHECK-NEXT: %a = riscv.li 1 : () -> !riscv.reg<> +// CHECK-NEXT: %b = riscv.li 2 : () -> !riscv.reg<> +// CHECK-NEXT: %c = riscv.li 3 : () -> !riscv.reg<> +// CHECK-NEXT: %d = riscv.li 3 : () -> !riscv.reg<> +// CHECK-NEXT: %e = riscv.add %b, %c : (!riscv.reg<>, !riscv.reg<>) -> !riscv.reg<> // CHECK-NEXT: }