brintos

brintos / llvm-project-archived public Read only

0
0
Text · 53.9 KiB · 8221f00 Raw
1733 lines · plain
1// RUN: mlir-opt %s -test-pdl-bytecode-pass -split-input-file | FileCheck %s2 3// Note: Tests here are written using the PDL Interpreter dialect to avoid4// unnecessarily testing unnecessary aspects of the pattern compilation5// pipeline. These tests are written such that we can focus solely on the6// lowering/execution of the bytecode itself.7 8//===----------------------------------------------------------------------===//9// pdl_interp::ApplyConstraintOp10//===----------------------------------------------------------------------===//11 12module @patterns {13  pdl_interp.func @matcher(%root : !pdl.operation) {14    pdl_interp.apply_constraint "multi_entity_constraint"(%root, %root : !pdl.operation, !pdl.operation) -> ^pat, ^end15 16  ^pat:17    pdl_interp.apply_constraint "single_entity_constraint"(%root : !pdl.operation) -> ^pat2, ^end18 19  ^pat2:20    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end21 22  ^end:23    pdl_interp.finalize24  }25 26  module @rewriters {27    pdl_interp.func @success(%root : !pdl.operation) {28      %op = pdl_interp.create_operation "test.replaced_by_pattern"29      pdl_interp.erase %root30      pdl_interp.finalize31    }32  }33}34 35// CHECK-LABEL: test.apply_constraint_136// CHECK: "test.replaced_by_pattern"37module @ir attributes { test.apply_constraint_1 } {38  "test.op"() { test_attr } : () -> ()39}40 41// -----42 43module @patterns {44  pdl_interp.func @matcher(%root : !pdl.operation) {45    %results = pdl_interp.get_results of %root : !pdl.range<value>46    %types = pdl_interp.get_value_type of %results : !pdl.range<type>47    pdl_interp.apply_constraint "multi_entity_var_constraint"(%results, %types : !pdl.range<value>, !pdl.range<type>) -> ^pat, ^end48 49  ^pat:50    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end51 52  ^end:53    pdl_interp.finalize54  }55 56  module @rewriters {57    pdl_interp.func @success(%root : !pdl.operation) {58      %op = pdl_interp.create_operation "test.replaced_by_pattern"59      pdl_interp.erase %root60      pdl_interp.finalize61    }62  }63}64 65// CHECK-LABEL: test.apply_constraint_266// CHECK-NOT: "test.replaced_by_pattern"67// CHECK: "test.replaced_by_pattern"68module @ir attributes { test.apply_constraint_2 } {69  "test.failure_op"() { test_attr } : () -> ()70  "test.success_op"() : () -> (i32, i64)71}72 73// -----74 75// Test support for negated constraints.76module @patterns {77  pdl_interp.func @matcher(%root : !pdl.operation) {78    %test_attr = pdl_interp.create_attribute unit79    %attr = pdl_interp.get_attribute "test_attr" of %root80    pdl_interp.are_equal %test_attr, %attr : !pdl.attribute -> ^pat, ^end81 82  ^pat:83    pdl_interp.apply_constraint "single_entity_constraint"(%root : !pdl.operation) {isNegated = true} -> ^pat1, ^end84 85  ^pat1:86    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end87 88  ^end:89    pdl_interp.finalize90  }91 92  module @rewriters {93    pdl_interp.func @success(%root : !pdl.operation) {94      %op = pdl_interp.create_operation "test.replaced_by_pattern"95      pdl_interp.erase %root96      pdl_interp.finalize97    }98  }99}100 101// CHECK-LABEL: test.apply_constraint_3102// CHECK-NEXT: "test.replaced_by_pattern"103// CHECK-NOT: "test.replaced_by_pattern"104 105module @ir attributes { test.apply_constraint_3 } {106  "test.foo"() { test_attr } : () -> ()107  "test.op"() { test_attr } : () -> ()108}109 110// -----111 112// Test returning a type from a native constraint.113module @patterns {114  pdl_interp.func @matcher(%root : !pdl.operation) {115    pdl_interp.check_operation_name of %root is "test.success_op" -> ^pat, ^end116 117  ^pat:118    %new_type = pdl_interp.apply_constraint "op_constr_return_type"(%root : !pdl.operation) : !pdl.type -> ^pat2, ^end119 120  ^pat2:121    pdl_interp.record_match @rewriters::@success(%root, %new_type : !pdl.operation, !pdl.type) : benefit(1), loc([%root]) -> ^end122 123  ^end:124    pdl_interp.finalize125  }126 127  module @rewriters {128    pdl_interp.func @success(%root : !pdl.operation, %new_type : !pdl.type) {129      %op = pdl_interp.create_operation "test.replaced_by_pattern" -> (%new_type : !pdl.type)130      pdl_interp.erase %root131      pdl_interp.finalize132    }133  }134}135 136// CHECK-LABEL: test.apply_constraint_4137// CHECK-NOT: "test.replaced_by_pattern"138// CHECK: "test.replaced_by_pattern"() : () -> f32139module @ir attributes { test.apply_constraint_4 } {140  "test.failure_op"() : () -> ()141  "test.success_op"() : () -> ()142}143 144// -----145 146// Test returning a type from a native constraint.147module @patterns {148  pdl_interp.func @matcher(%root : !pdl.operation) {149    %new_type:2 = pdl_interp.apply_constraint "op_multiple_returns_failure"(%root : !pdl.operation) : !pdl.type, !pdl.type -> ^pat2, ^end150 151  ^pat2:152    pdl_interp.record_match @rewriters::@success(%root, %new_type#0 : !pdl.operation, !pdl.type) : benefit(1), loc([%root]) -> ^end153 154  ^end:155    pdl_interp.finalize156  }157 158  module @rewriters {159    pdl_interp.func @success(%root : !pdl.operation, %new_type : !pdl.type) {160      %op = pdl_interp.create_operation "test.replaced_by_pattern" -> (%new_type : !pdl.type)161      pdl_interp.erase %root162      pdl_interp.finalize163    }164  }165}166 167// CHECK-LABEL: test.apply_constraint_multi_result_failure168// CHECK-NOT: "test.replaced_by_pattern"169// CHECK: "test.success_op"170module @ir attributes { test.apply_constraint_multi_result_failure } {171  "test.success_op"() : () -> ()172}173 174// -----175 176// Test success and failure cases of native constraints with pdl.range results.177module @patterns {178  pdl_interp.func @matcher(%root : !pdl.operation) {179    pdl_interp.check_operation_name of %root is "test.success_op" -> ^pat, ^end180  181  ^pat:182    %num_results = pdl_interp.create_attribute 2 : i32183    %types = pdl_interp.apply_constraint "op_constr_return_type_range"(%root, %num_results : !pdl.operation, !pdl.attribute) : !pdl.range<type> -> ^pat1, ^end184 185  ^pat1:186    pdl_interp.record_match @rewriters::@success(%root, %types : !pdl.operation, !pdl.range<type>) : benefit(1), loc([%root]) -> ^end187 188  ^end:189    pdl_interp.finalize190  }191 192  module @rewriters {193    pdl_interp.func @success(%root : !pdl.operation, %types : !pdl.range<type>) {194      %op = pdl_interp.create_operation "test.replaced_by_pattern" -> (%types : !pdl.range<type>)195      pdl_interp.erase %root196      pdl_interp.finalize197    }198  }199}200 201// CHECK-LABEL: test.apply_constraint_5202// CHECK-NOT: "test.replaced_by_pattern"203// CHECK: "test.replaced_by_pattern"() : () -> (f32, f32)204module @ir attributes { test.apply_constraint_5 } {205  "test.failure_op"() : () -> ()206  "test.success_op"() : () -> ()207}208 209// -----210 211//===----------------------------------------------------------------------===//212// pdl_interp::ApplyRewriteOp213//===----------------------------------------------------------------------===//214 215module @patterns {216  pdl_interp.func @matcher(%root : !pdl.operation) {217    pdl_interp.check_operation_name of %root is "test.op" -> ^pat, ^end218 219  ^pat:220    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end221 222  ^end:223    pdl_interp.finalize224  }225 226  module @rewriters {227    pdl_interp.func @success(%root : !pdl.operation) {228      %operand = pdl_interp.get_operand 0 of %root229      pdl_interp.apply_rewrite "rewriter"(%root, %operand : !pdl.operation, !pdl.value)230      pdl_interp.finalize231    }232  }233}234 235// CHECK-LABEL: test.apply_rewrite_1236// CHECK: %[[INPUT:.*]] = "test.op_input"237// CHECK-NOT: "test.op"238// CHECK: "test.success"(%[[INPUT]])239module @ir attributes { test.apply_rewrite_1 } {240  %input = "test.op_input"() : () -> i32241  "test.op"(%input) : (i32) -> ()242}243 244// -----245 246module @patterns {247  pdl_interp.func @matcher(%root : !pdl.operation) {248    pdl_interp.check_operation_name of %root is "test.op" -> ^pat, ^end249 250  ^pat:251    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end252 253  ^end:254    pdl_interp.finalize255  }256 257  module @rewriters {258    pdl_interp.func @success(%root : !pdl.operation) {259      %op = pdl_interp.apply_rewrite "creator"(%root : !pdl.operation) : !pdl.operation260      pdl_interp.erase %root261      pdl_interp.finalize262    }263  }264}265 266// CHECK-LABEL: test.apply_rewrite_2267// CHECK: "test.success"268module @ir attributes { test.apply_rewrite_2 } {269  "test.op"() : () -> ()270}271 272// -----273 274module @patterns {275  pdl_interp.func @matcher(%root : !pdl.operation) {276    pdl_interp.check_operation_name of %root is "test.op" -> ^pat, ^end277 278  ^pat:279    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end280 281  ^end:282    pdl_interp.finalize283  }284 285  module @rewriters {286    pdl_interp.func @success(%root : !pdl.operation) {287      %operands, %types = pdl_interp.apply_rewrite "var_creator"(%root : !pdl.operation) : !pdl.range<value>, !pdl.range<type>288      %op = pdl_interp.create_operation "test.success"(%operands : !pdl.range<value>) -> (%types : !pdl.range<type>)289      pdl_interp.replace %root with (%operands : !pdl.range<value>)290      pdl_interp.finalize291    }292  }293}294 295// CHECK-LABEL: test.apply_rewrite_3296// CHECK: %[[OPERAND:.*]] = "test.producer"297// CHECK: "test.success"(%[[OPERAND]]) : (i32) -> i32298// CHECK: "test.consumer"(%[[OPERAND]])299module @ir attributes { test.apply_rewrite_3 } {300  %first_operand = "test.producer"() : () -> (i32)301  %operand = "test.op"(%first_operand) : (i32) -> (i32)302  "test.consumer"(%operand) : (i32) -> ()303}304 305// -----306 307module @patterns {308  pdl_interp.func @matcher(%root : !pdl.operation) {309    pdl_interp.check_operation_name of %root is "test.op" -> ^pat, ^end310 311  ^pat:312    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end313 314  ^end:315    pdl_interp.finalize316  }317 318  module @rewriters {319    pdl_interp.func @success(%root : !pdl.operation) {320      %attr = pdl_interp.apply_rewrite "str_creator" : !pdl.attribute321      %type = pdl_interp.apply_rewrite "type_creator" : !pdl.type322      %newOp = pdl_interp.create_operation "test.success" {"attr" = %attr} -> (%type : !pdl.type)323      pdl_interp.erase %root324      pdl_interp.finalize325    }326  }327}328 329// CHECK-LABEL: test.apply_rewrite_4330// CHECK: "test.success"() {attr = "test.str"} : () -> f32331module @ir attributes { test.apply_rewrite_4 } {332  "test.op"() : () -> ()333}334 335// -----336 337//===----------------------------------------------------------------------===//338// pdl_interp::AreEqualOp339//===----------------------------------------------------------------------===//340 341module @patterns {342  pdl_interp.func @matcher(%root : !pdl.operation) {343    %test_attr = pdl_interp.create_attribute unit344    %attr = pdl_interp.get_attribute "test_attr" of %root345    pdl_interp.are_equal %test_attr, %attr : !pdl.attribute -> ^pat, ^end346 347  ^pat:348    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end349 350  ^end:351    pdl_interp.finalize352  }353 354  module @rewriters {355    pdl_interp.func @success(%root : !pdl.operation) {356      %op = pdl_interp.create_operation "test.success"357      pdl_interp.erase %root358      pdl_interp.finalize359    }360  }361}362 363// CHECK-LABEL: test.are_equal_1364// CHECK: "test.success"365module @ir attributes { test.are_equal_1 } {366  "test.op"() { test_attr } : () -> ()367}368 369// -----370 371module @patterns {372  pdl_interp.func @matcher(%root : !pdl.operation) {373    %const_types = pdl_interp.create_types [i32, i64]374    %results = pdl_interp.get_results of %root : !pdl.range<value>375    %result_types = pdl_interp.get_value_type of %results : !pdl.range<type>376    pdl_interp.are_equal %result_types, %const_types : !pdl.range<type> -> ^pat, ^end377 378  ^pat:379    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end380 381  ^end:382    pdl_interp.finalize383  }384 385  module @rewriters {386    pdl_interp.func @success(%root : !pdl.operation) {387      %op = pdl_interp.create_operation "test.success"388      pdl_interp.erase %root389      pdl_interp.finalize390    }391  }392}393 394// CHECK-LABEL: test.are_equal_2395// CHECK: "test.not_equal"396// CHECK: "test.success"397// CHECK-NOT: "test.op"398module @ir attributes { test.are_equal_2 } {399  "test.not_equal"() : () -> (i32)400  "test.op"() : () -> (i32, i64)401}402 403// -----404 405//===----------------------------------------------------------------------===//406// pdl_interp::BranchOp407//===----------------------------------------------------------------------===//408 409module @patterns {410  pdl_interp.func @matcher(%root : !pdl.operation) {411    pdl_interp.check_operation_name of %root is "test.op" -> ^pat1, ^end412 413  ^pat1:414    pdl_interp.branch ^pat2415 416  ^pat2:417    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(2), loc([%root]) -> ^end418 419  ^end:420    pdl_interp.finalize421  }422 423  module @rewriters {424    pdl_interp.func @success(%root : !pdl.operation) {425      %op = pdl_interp.create_operation "test.success"426      pdl_interp.erase %root427      pdl_interp.finalize428    }429  }430}431 432// CHECK-LABEL: test.branch_1433// CHECK: "test.success"434module @ir attributes { test.branch_1 } {435  "test.op"() : () -> ()436}437 438// -----439 440//===----------------------------------------------------------------------===//441// pdl_interp::CheckAttributeOp442//===----------------------------------------------------------------------===//443 444module @patterns {445  pdl_interp.func @matcher(%root : !pdl.operation) {446    %attr = pdl_interp.get_attribute "test_attr" of %root447    pdl_interp.check_attribute %attr is unit -> ^pat, ^end448 449  ^pat:450    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end451 452  ^end:453    pdl_interp.finalize454  }455 456  module @rewriters {457    pdl_interp.func @success(%root : !pdl.operation) {458      %op = pdl_interp.create_operation "test.success"459      pdl_interp.erase %root460      pdl_interp.finalize461    }462  }463}464 465// CHECK-LABEL: test.check_attribute_1466// CHECK: "test.success"467module @ir attributes { test.check_attribute_1 } {468  "test.op"() { test_attr } : () -> ()469}470 471// -----472 473//===----------------------------------------------------------------------===//474// pdl_interp::CheckOperandCountOp475//===----------------------------------------------------------------------===//476 477module @patterns {478  pdl_interp.func @matcher(%root : !pdl.operation) {479    pdl_interp.check_operand_count of %root is at_least 1 -> ^exact_check, ^end480 481  ^exact_check:482    pdl_interp.check_operand_count of %root is 2 -> ^pat, ^end483 484  ^pat:485    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end486 487  ^end:488    pdl_interp.finalize489  }490 491  module @rewriters {492    pdl_interp.func @success(%root : !pdl.operation) {493      %op = pdl_interp.create_operation "test.success"494      pdl_interp.erase %root495      pdl_interp.finalize496    }497  }498}499 500// CHECK-LABEL: test.check_operand_count_1501// CHECK: "test.op"() : () -> i32502// CHECK: "test.success"503module @ir attributes { test.check_operand_count_1 } {504  %operand = "test.op"() : () -> i32505  "test.op"(%operand, %operand) : (i32, i32) -> ()506}507 508// -----509 510//===----------------------------------------------------------------------===//511// pdl_interp::CheckOperationNameOp512//===----------------------------------------------------------------------===//513 514module @patterns {515  pdl_interp.func @matcher(%root : !pdl.operation) {516    pdl_interp.check_operation_name of %root is "test.op" -> ^pat, ^end517 518  ^pat:519    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end520 521  ^end:522    pdl_interp.finalize523  }524 525  module @rewriters {526    pdl_interp.func @success(%root : !pdl.operation) {527      %op = pdl_interp.create_operation "test.success"528      pdl_interp.erase %root529      pdl_interp.finalize530    }531  }532}533 534// CHECK-LABEL: test.check_operation_name_1535// CHECK: "test.success"536module @ir attributes { test.check_operation_name_1 } {537  "test.op"() : () -> ()538}539 540// -----541 542//===----------------------------------------------------------------------===//543// pdl_interp::CheckResultCountOp544//===----------------------------------------------------------------------===//545 546module @patterns {547  pdl_interp.func @matcher(%root : !pdl.operation) {548    pdl_interp.check_result_count of %root is at_least 1 -> ^exact_check, ^end549 550  ^exact_check:551    pdl_interp.check_result_count of %root is 2 -> ^pat, ^end552 553  ^pat:554    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end555 556  ^end:557    pdl_interp.finalize558  }559 560  module @rewriters {561    pdl_interp.func @success(%root : !pdl.operation) {562      %op = pdl_interp.create_operation "test.success"563      pdl_interp.erase %root564      pdl_interp.finalize565    }566  }567}568 569// CHECK-LABEL: test.check_result_count_1570// CHECK: "test.op"() : () -> i32571// CHECK: "test.success"() : () -> ()572// CHECK-NOT: "test.op"() : () -> (i32, i32)573module @ir attributes { test.check_result_count_1 } {574  "test.op"() : () -> i32575  "test.op"() : () -> (i32, i32)576}577 578// -----579 580//===----------------------------------------------------------------------===//581// pdl_interp::CheckTypeOp582//===----------------------------------------------------------------------===//583 584module @patterns {585  pdl_interp.func @matcher(%root : !pdl.operation) {586    %attr = pdl_interp.get_attribute "test_attr" of %root587    pdl_interp.is_not_null %attr : !pdl.attribute -> ^pat1, ^end588 589  ^pat1:590    %type = pdl_interp.get_attribute_type of %attr591    pdl_interp.check_type %type is i32 -> ^pat2, ^end592 593  ^pat2:594    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end595 596  ^end:597    pdl_interp.finalize598  }599 600  module @rewriters {601    pdl_interp.func @success(%root : !pdl.operation) {602      %op = pdl_interp.create_operation "test.success"603      pdl_interp.erase %root604      pdl_interp.finalize605    }606  }607}608 609// CHECK-LABEL: test.check_type_1610// CHECK: "test.success"611module @ir attributes { test.check_type_1 } {612  "test.op"() { test_attr = 10 : i32 } : () -> ()613}614 615// -----616 617//===----------------------------------------------------------------------===//618// pdl_interp::CheckTypesOp619//===----------------------------------------------------------------------===//620 621module @patterns {622  pdl_interp.func @matcher(%root : !pdl.operation) {623    %results = pdl_interp.get_results of %root : !pdl.range<value>624    %result_types = pdl_interp.get_value_type of %results : !pdl.range<type>625    pdl_interp.check_types %result_types are [i32] -> ^pat2, ^end626 627  ^pat2:628    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end629 630  ^end:631    pdl_interp.finalize632  }633 634  module @rewriters {635    pdl_interp.func @success(%root : !pdl.operation) {636      %op = pdl_interp.create_operation "test.success"637      pdl_interp.erase %root638      pdl_interp.finalize639    }640  }641}642 643// CHECK-LABEL: test.check_types_1644// CHECK: "test.op"() : () -> (i32, i64)645// CHECK: "test.success"646// CHECK-NOT: "test.op"() : () -> i32647module @ir attributes { test.check_types_1 } {648  "test.op"() : () -> (i32, i64)649  "test.op"() : () -> i32650}651 652// -----653 654//===----------------------------------------------------------------------===//655// pdl_interp::ContinueOp656//===----------------------------------------------------------------------===//657 658// Fully tested within the tests for other operations.659 660//===----------------------------------------------------------------------===//661// pdl_interp::CreateAttributeOp662//===----------------------------------------------------------------------===//663 664// Fully tested within the tests for other operations.665 666//===----------------------------------------------------------------------===//667// pdl_interp::CreateOperationOp668//===----------------------------------------------------------------------===//669 670// Unused operation to force loading the `arithmetic` dialect for the671// test of type inferrence.672arith.constant 10673 674// Test support for inferring the types of an operation.675module @patterns {676  pdl_interp.func @matcher(%root : !pdl.operation) {677    pdl_interp.check_operation_name of %root is "test.op" -> ^pat, ^end678 679  ^pat:680    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end681 682  ^end:683    pdl_interp.finalize684  }685 686  module @rewriters {687    pdl_interp.func @success(%root : !pdl.operation) {688      %attr = pdl_interp.create_attribute true689      %cst = pdl_interp.create_operation "arith.constant" {"value" = %attr} -> <inferred>690      %cstResults = pdl_interp.get_results of %cst : !pdl.range<value>691      %op = pdl_interp.create_operation "test.success"(%cstResults : !pdl.range<value>)692      pdl_interp.erase %root693      pdl_interp.finalize694    }695  }696}697 698// CHECK-LABEL: test.create_op_infer_results699// CHECK: %[[CST:.*]] = arith.constant true700// CHECK: "test.success"(%[[CST]])701module @ir attributes { test.create_op_infer_results } {702  %results:2 = "test.op"() : () -> (i64, i64)703}704 705// -----706 707//===----------------------------------------------------------------------===//708// pdl_interp::CreateRangeOp709//===----------------------------------------------------------------------===//710 711module @patterns {712  pdl_interp.func @matcher(%root : !pdl.operation) {713    pdl_interp.check_operand_count of %root is 2 -> ^pat1, ^end714 715  ^pat1:716    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end717 718  ^end:719    pdl_interp.finalize720  }721 722  module @rewriters {723    pdl_interp.func @success(%root: !pdl.operation) {724      %rootOperand = pdl_interp.get_operand 0 of %root725      %rootOperands = pdl_interp.get_operands of %root : !pdl.range<value>726      %operandRange = pdl_interp.create_range %rootOperand, %rootOperands : !pdl.value, !pdl.range<value>727 728      %operandType = pdl_interp.get_value_type of %rootOperand : !pdl.type729      %operandTypes = pdl_interp.get_value_type of %rootOperands : !pdl.range<type>730      %typeRange = pdl_interp.create_range %operandType, %operandTypes : !pdl.type, !pdl.range<type>731 732      %op = pdl_interp.create_operation "test.success"(%operandRange : !pdl.range<value>) -> (%typeRange : !pdl.range<type>)733      pdl_interp.erase %root734      pdl_interp.finalize735    }736  }737}738 739// CHECK-LABEL: test.create_range_1740// CHECK: %[[INPUTS:.*]]:2 = "test.input"()741// CHECK: "test.success"(%[[INPUTS]]#0, %[[INPUTS]]#0, %[[INPUTS]]#1) : (i32, i32, i32) -> (i32, i32, i32)742module @ir attributes { test.create_range_1 } {743  %values:2 = "test.input"() : () -> (i32, i32)744  "test.op"(%values#0, %values#1) : (i32, i32) -> ()745}746 747// -----748 749//===----------------------------------------------------------------------===//750// pdl_interp::CreateTypeOp751//===----------------------------------------------------------------------===//752 753module @patterns {754  pdl_interp.func @matcher(%root : !pdl.operation) {755    %attr = pdl_interp.get_attribute "test_attr" of %root756    pdl_interp.is_not_null %attr : !pdl.attribute -> ^pat1, ^end757 758  ^pat1:759    %test_type = pdl_interp.create_type i32760    %type = pdl_interp.get_attribute_type of %attr761    pdl_interp.are_equal %type, %test_type : !pdl.type -> ^pat2, ^end762 763  ^pat2:764    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end765 766  ^end:767    pdl_interp.finalize768  }769 770  module @rewriters {771    pdl_interp.func @success(%root : !pdl.operation) {772      %op = pdl_interp.create_operation "test.success"773      pdl_interp.erase %root774      pdl_interp.finalize775    }776  }777}778 779// CHECK-LABEL: test.create_type_1780// CHECK: "test.success"781module @ir attributes { test.create_type_1 } {782  "test.op"() { test_attr = 0 : i32 } : () -> ()783}784 785// -----786 787//===----------------------------------------------------------------------===//788// pdl_interp::CreateTypesOp789//===----------------------------------------------------------------------===//790 791// Fully tested within the tests for other operations.792 793//===----------------------------------------------------------------------===//794// pdl_interp::EraseOp795//===----------------------------------------------------------------------===//796 797// Fully tested within the tests for other operations.798 799//===----------------------------------------------------------------------===//800// pdl_interp::ExtractOp801//===----------------------------------------------------------------------===//802 803module @patterns {804  pdl_interp.func @matcher(%root : !pdl.operation) {805    %val = pdl_interp.get_result 0 of %root806    %ops = pdl_interp.get_users of %val : !pdl.value807    %op1 = pdl_interp.extract 1 of %ops : !pdl.operation808    pdl_interp.is_not_null %op1 : !pdl.operation -> ^success, ^end809  ^success:810    pdl_interp.record_match @rewriters::@success(%op1 : !pdl.operation) : benefit(1), loc([%root]) -> ^end811  ^end:812    pdl_interp.finalize813  }814 815  module @rewriters {816    pdl_interp.func @success(%matched : !pdl.operation) {817      %op = pdl_interp.create_operation "test.success"818      pdl_interp.erase %matched819      pdl_interp.finalize820    }821  }822}823 824// CHECK-LABEL: test.extract_op825// CHECK: "test.success"826// CHECK: %[[OPERAND:.*]] = "test.op"827// CHECK: "test.op"(%[[OPERAND]])828module @ir attributes { test.extract_op } {829  %operand = "test.op"() : () -> i32830  "test.op"(%operand) : (i32) -> (i32)831  "test.op"(%operand, %operand) : (i32, i32) -> (i32)832}833 834// -----835 836module @patterns {837  pdl_interp.func @matcher(%root : !pdl.operation) {838    %vals = pdl_interp.get_results of %root : !pdl.range<value>839    %types = pdl_interp.get_value_type of %vals : !pdl.range<type>840    %type1 = pdl_interp.extract 1 of %types : !pdl.type841    pdl_interp.is_not_null %type1 : !pdl.type -> ^success, ^end842  ^success:843    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end844  ^end:845    pdl_interp.finalize846  }847 848  module @rewriters {849    pdl_interp.func @success(%matched : !pdl.operation) {850      %op = pdl_interp.create_operation "test.success"851      pdl_interp.erase %matched852      pdl_interp.finalize853    }854  }855}856 857// CHECK-LABEL: test.extract_type858// CHECK: %[[OPERAND:.*]] = "test.op"859// CHECK: "test.success"860// CHECK: "test.op"(%[[OPERAND]])861module @ir attributes { test.extract_type } {862  %operand = "test.op"() : () -> i32863  "test.op"(%operand) : (i32) -> (i32, i32)864  "test.op"(%operand) : (i32) -> (i32)865}866 867// -----868 869module @patterns {870  pdl_interp.func @matcher(%root : !pdl.operation) {871    %vals = pdl_interp.get_results of %root : !pdl.range<value>872    %val1 = pdl_interp.extract 1 of %vals : !pdl.value873    pdl_interp.is_not_null %val1 : !pdl.value -> ^success, ^end874  ^success:875    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end876  ^end:877    pdl_interp.finalize878  }879 880  module @rewriters {881    pdl_interp.func @success(%matched : !pdl.operation) {882      %op = pdl_interp.create_operation "test.success"883      pdl_interp.erase %matched884      pdl_interp.finalize885    }886  }887}888 889// CHECK-LABEL: test.extract_value890// CHECK: %[[OPERAND:.*]] = "test.op"891// CHECK: "test.success"892// CHECK: "test.op"(%[[OPERAND]])893module @ir attributes { test.extract_value } {894  %operand = "test.op"() : () -> i32895  "test.op"(%operand) : (i32) -> (i32, i32)896  "test.op"(%operand) : (i32) -> (i32)897}898 899// -----900 901//===----------------------------------------------------------------------===//902// pdl_interp::FinalizeOp903//===----------------------------------------------------------------------===//904 905// Fully tested within the tests for other operations.906 907//===----------------------------------------------------------------------===//908// pdl_interp::ForEachOp909//===----------------------------------------------------------------------===//910 911module @patterns {912  pdl_interp.func @matcher(%root : !pdl.operation) {913    %val1 = pdl_interp.get_result 0 of %root914    %ops1 = pdl_interp.get_users of %val1 : !pdl.value915    pdl_interp.foreach %op1 : !pdl.operation in %ops1 {916      %val2 = pdl_interp.get_result 0 of %op1917      %ops2 = pdl_interp.get_users of %val2 : !pdl.value918      pdl_interp.foreach %op2 : !pdl.operation in %ops2 {919        pdl_interp.record_match @rewriters::@success(%op2 : !pdl.operation) : benefit(1), loc([%root]) -> ^cont920      ^cont:921        pdl_interp.continue922      } -> ^cont923    ^cont:924      pdl_interp.continue925    } -> ^end926  ^end:927    pdl_interp.finalize928  }929 930  module @rewriters {931    pdl_interp.func @success(%matched : !pdl.operation) {932      %op = pdl_interp.create_operation "test.success"933      pdl_interp.erase %matched934      pdl_interp.finalize935    }936  }937}938 939// CHECK-LABEL: test.foreach940// CHECK: "test.success"941// CHECK: "test.success"942// CHECK: "test.success"943// CHECK: "test.success"944// CHECK: %[[ROOT:.*]] = "test.op"945// CHECK: %[[VALA:.*]] = "test.op"(%[[ROOT]])946// CHECK: %[[VALB:.*]] = "test.op"(%[[ROOT]])947module @ir attributes { test.foreach } {948  %root = "test.op"() : () -> i32949  %valA = "test.op"(%root) : (i32) -> (i32)950  "test.op"(%valA) : (i32) -> (i32)951  "test.op"(%valA) : (i32) -> (i32)952  %valB = "test.op"(%root) : (i32) -> (i32)953  "test.op"(%valB) : (i32) -> (i32)954  "test.op"(%valB) : (i32) -> (i32)955}956 957// -----958 959//===----------------------------------------------------------------------===//960// pdl_interp::GetUsersOp961//===----------------------------------------------------------------------===//962 963module @patterns {964  pdl_interp.func @matcher(%root : !pdl.operation) {965    %val = pdl_interp.get_result 0 of %root966    %ops = pdl_interp.get_users of %val : !pdl.value967    pdl_interp.foreach %op : !pdl.operation in %ops {968      pdl_interp.record_match @rewriters::@success(%op : !pdl.operation) : benefit(1), loc([%root]) -> ^cont969    ^cont:970      pdl_interp.continue971    } -> ^end972  ^end:973    pdl_interp.finalize974  }975 976  module @rewriters {977    pdl_interp.func @success(%matched : !pdl.operation) {978      %op = pdl_interp.create_operation "test.success"979      pdl_interp.erase %matched980      pdl_interp.finalize981    }982  }983}984 985// CHECK-LABEL: test.get_users_of_value986// CHECK: "test.success"987// CHECK: "test.success"988// CHECK: %[[OPERAND:.*]] = "test.op"989module @ir attributes { test.get_users_of_value } {990  %operand = "test.op"() : () -> i32991  "test.op"(%operand) : (i32) -> (i32)992  "test.op"(%operand, %operand) : (i32, i32) -> (i32)993}994 995// -----996 997module @patterns {998  pdl_interp.func @matcher(%root : !pdl.operation) {999    pdl_interp.check_result_count of %root is at_least 2 -> ^next, ^end1000  ^next:1001    %vals = pdl_interp.get_results of %root : !pdl.range<value>1002    %ops = pdl_interp.get_users of %vals : !pdl.range<value>1003    pdl_interp.foreach %op : !pdl.operation in %ops {1004      pdl_interp.record_match @rewriters::@success(%op : !pdl.operation) : benefit(1), loc([%root]) -> ^cont1005    ^cont:1006      pdl_interp.continue1007    } -> ^end1008  ^end:1009    pdl_interp.finalize1010  }1011 1012  module @rewriters {1013    pdl_interp.func @success(%matched : !pdl.operation) {1014      %op = pdl_interp.create_operation "test.success"1015      pdl_interp.erase %matched1016      pdl_interp.finalize1017    }1018  }1019}1020 1021// CHECK-LABEL: test.get_all_users_of_range1022// CHECK: "test.success"1023// CHECK: "test.success"1024// CHECK: %[[OPERANDS:.*]]:2 = "test.op"1025module @ir attributes { test.get_all_users_of_range } {1026  %operands:2 = "test.op"() : () -> (i32, i32)1027  "test.op"(%operands#0) : (i32) -> (i32)1028  "test.op"(%operands#1) : (i32) -> (i32)1029}1030 1031// -----1032 1033module @patterns {1034  pdl_interp.func @matcher(%root : !pdl.operation) {1035    pdl_interp.check_result_count of %root is at_least 2 -> ^next, ^end1036  ^next:1037    %vals = pdl_interp.get_results of %root : !pdl.range<value>1038    %val = pdl_interp.extract 0 of %vals : !pdl.value1039    %ops = pdl_interp.get_users of %val : !pdl.value1040    pdl_interp.foreach %op : !pdl.operation in %ops {1041      pdl_interp.record_match @rewriters::@success(%op : !pdl.operation) : benefit(1), loc([%root]) -> ^cont1042    ^cont:1043      pdl_interp.continue1044    } -> ^end1045  ^end:1046    pdl_interp.finalize1047  }1048 1049  module @rewriters {1050    pdl_interp.func @success(%matched : !pdl.operation) {1051      %op = pdl_interp.create_operation "test.success"1052      pdl_interp.erase %matched1053      pdl_interp.finalize1054    }1055  }1056}1057 1058// CHECK-LABEL: test.get_first_users_of_range1059// CHECK: "test.success"1060// CHECK: %[[OPERANDS:.*]]:2 = "test.op"1061// CHECK: "test.op"1062module @ir attributes { test.get_first_users_of_range } {1063  %operands:2 = "test.op"() : () -> (i32, i32)1064  "test.op"(%operands#0) : (i32) -> (i32)1065  "test.op"(%operands#1) : (i32) -> (i32)1066}1067 1068// -----1069 1070//===----------------------------------------------------------------------===//1071// pdl_interp::GetAttributeOp1072//===----------------------------------------------------------------------===//1073 1074// Fully tested within the tests for other operations.1075 1076//===----------------------------------------------------------------------===//1077// pdl_interp::GetAttributeTypeOp1078//===----------------------------------------------------------------------===//1079 1080// Fully tested within the tests for other operations.1081 1082//===----------------------------------------------------------------------===//1083// pdl_interp::GetDefiningOpOp1084//===----------------------------------------------------------------------===//1085 1086module @patterns {1087  pdl_interp.func @matcher(%root : !pdl.operation) {1088    pdl_interp.check_operand_count of %root is 5 -> ^pat1, ^end1089 1090  ^pat1:1091    %operand0 = pdl_interp.get_operand 0 of %root1092    %operand4 = pdl_interp.get_operand 4 of %root1093    %defOp0 = pdl_interp.get_defining_op of %operand0 : !pdl.value1094    %defOp4 = pdl_interp.get_defining_op of %operand4 : !pdl.value1095    pdl_interp.are_equal %defOp0, %defOp4 : !pdl.operation -> ^pat2, ^end1096 1097  ^pat2:1098    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end1099 1100  ^end:1101    pdl_interp.finalize1102  }1103 1104  module @rewriters {1105    pdl_interp.func @success(%root : !pdl.operation) {1106      %op = pdl_interp.create_operation "test.success"1107      pdl_interp.erase %root1108      pdl_interp.finalize1109    }1110  }1111}1112 1113// CHECK-LABEL: test.get_defining_op_11114// CHECK: %[[OPERAND0:.*]] = "test.op"1115// CHECK: %[[OPERAND1:.*]] = "test.op"1116// CHECK: "test.success"1117// CHECK: "test.op"(%[[OPERAND0]], %[[OPERAND0]], %[[OPERAND0]], %[[OPERAND0]], %[[OPERAND1]])1118module @ir attributes { test.get_defining_op_1 } {1119  %operand = "test.op"() : () -> i321120  %other_operand = "test.op"() : () -> i321121  "test.op"(%operand, %operand, %operand, %operand, %operand) : (i32, i32, i32, i32, i32) -> ()1122  "test.op"(%operand, %operand, %operand, %operand, %other_operand) : (i32, i32, i32, i32, i32) -> ()1123}1124 1125// -----1126 1127//===----------------------------------------------------------------------===//1128// pdl_interp::GetOperandOp1129//===----------------------------------------------------------------------===//1130 1131// Fully tested within the tests for other operations.1132 1133//===----------------------------------------------------------------------===//1134// pdl_interp::GetOperandsOp1135//===----------------------------------------------------------------------===//1136 1137module @patterns {1138  pdl_interp.func @matcher(%root : !pdl.operation) {1139    pdl_interp.check_operand_count of %root is 2 -> ^pat1, ^end1140 1141  ^pat1:1142    %operands = pdl_interp.get_operands 0 of %root : !pdl.range<value>1143    %full_operands = pdl_interp.get_operands of %root : !pdl.range<value>1144    pdl_interp.are_equal %operands, %full_operands : !pdl.range<value> -> ^pat2, ^end1145 1146  ^pat2:1147    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end1148 1149  ^end:1150    pdl_interp.finalize1151  }1152 1153  module @rewriters {1154    pdl_interp.func @success(%root : !pdl.operation) {1155      %op = pdl_interp.create_operation "test.success"1156      pdl_interp.erase %root1157      pdl_interp.finalize1158    }1159  }1160}1161 1162// CHECK-LABEL: test.get_operands_11163// CHECK: "test.success"1164module @ir attributes { test.get_operands_1 } {1165  %inputs:2 = "test.producer"() : () -> (i32, i32)1166  "test.op"(%inputs#0, %inputs#1) : (i32, i32) -> ()1167}1168 1169// -----1170 1171// Test all of the various combinations related to `AttrSizedOperandSegments`.1172module @patterns {1173  pdl_interp.func @matcher(%root : !pdl.operation) {1174    pdl_interp.check_operation_name of %root is "test.attr_sized_operands" -> ^pat1, ^end1175 1176  ^pat1:1177    %operands_0 = pdl_interp.get_operands 0 of %root : !pdl.range<value>1178    pdl_interp.is_not_null %operands_0 : !pdl.range<value> -> ^pat2, ^end1179 1180  ^pat2:1181    %operands_0_single = pdl_interp.get_operands 0 of %root : !pdl.value1182    pdl_interp.is_not_null %operands_0_single : !pdl.value -> ^end, ^pat31183 1184  ^pat3:1185    %operands_1 = pdl_interp.get_operands 1 of %root : !pdl.range<value>1186    pdl_interp.is_not_null %operands_1 : !pdl.range<value> -> ^pat4, ^end1187 1188  ^pat4:1189    %operands_1_single = pdl_interp.get_operands 1 of %root : !pdl.value1190    pdl_interp.is_not_null %operands_1_single : !pdl.value -> ^end, ^pat51191 1192  ^pat5:1193    %operands_2 = pdl_interp.get_operands 2 of %root : !pdl.range<value>1194    pdl_interp.is_not_null %operands_2 : !pdl.range<value> -> ^pat6, ^end1195 1196  ^pat6:1197    %operands_2_single = pdl_interp.get_operands 2 of %root : !pdl.value1198    pdl_interp.is_not_null %operands_2_single : !pdl.value -> ^pat7, ^end1199 1200  ^pat7:1201    %invalid_operands = pdl_interp.get_operands 50 of %root : !pdl.value1202    pdl_interp.is_not_null %invalid_operands : !pdl.value -> ^end, ^pat81203 1204  ^pat8:1205    pdl_interp.record_match @rewriters::@success(%root, %operands_0, %operands_1, %operands_2, %operands_2_single : !pdl.operation, !pdl.range<value>, !pdl.range<value>, !pdl.range<value>, !pdl.value) : benefit(1), loc([%root]) -> ^end1206 1207 1208  ^end:1209    pdl_interp.finalize1210  }1211 1212  module @rewriters {1213    pdl_interp.func @success(%root: !pdl.operation, %operands_0: !pdl.range<value>, %operands_1: !pdl.range<value>, %operands_2: !pdl.range<value>, %operands_2_single: !pdl.value) {1214      %op0 = pdl_interp.create_operation "test.success"(%operands_0 : !pdl.range<value>)1215      %op1 = pdl_interp.create_operation "test.success"(%operands_1 : !pdl.range<value>)1216      %op2 = pdl_interp.create_operation "test.success"(%operands_2 : !pdl.range<value>)1217      %op3 = pdl_interp.create_operation "test.success"(%operands_2_single : !pdl.value)1218      pdl_interp.erase %root1219      pdl_interp.finalize1220    }1221  }1222}1223 1224// CHECK-LABEL: test.get_operands_21225// CHECK-NEXT:  %[[INPUTS:.*]]:5 = "test.producer"() : () -> (i32, i32, i32, i32, i32)1226// CHECK-NEXT:  "test.success"() : () -> ()1227// CHECK-NEXT:  "test.success"(%[[INPUTS]]#0, %[[INPUTS]]#1, %[[INPUTS]]#2, %[[INPUTS]]#3) : (i32, i32, i32, i32) -> ()1228// CHECK-NEXT:  "test.success"(%[[INPUTS]]#4) : (i32) -> ()1229// CHECK-NEXT:  "test.success"(%[[INPUTS]]#4) : (i32) -> ()1230module @ir attributes { test.get_operands_2 } {1231  %inputs:5 = "test.producer"() : () -> (i32, i32, i32, i32, i32)1232  "test.attr_sized_operands"(%inputs#0, %inputs#1, %inputs#2, %inputs#3, %inputs#4) {operandSegmentSizes = array<i32: 0, 4, 1, 0>} : (i32, i32, i32, i32, i32) -> ()1233}1234 1235// -----1236 1237//===----------------------------------------------------------------------===//1238// pdl_interp::GetResultOp1239//===----------------------------------------------------------------------===//1240 1241module @patterns {1242  pdl_interp.func @matcher(%root : !pdl.operation) {1243    pdl_interp.check_result_count of %root is 5 -> ^pat1, ^end1244 1245  ^pat1:1246    %result0 = pdl_interp.get_result 0 of %root1247    %result4 = pdl_interp.get_result 4 of %root1248    %result0_type = pdl_interp.get_value_type of %result0 : !pdl.type1249    %result4_type = pdl_interp.get_value_type of %result4 : !pdl.type1250    pdl_interp.are_equal %result0_type, %result4_type : !pdl.type -> ^pat2, ^end1251 1252  ^pat2:1253    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end1254 1255  ^end:1256    pdl_interp.finalize1257  }1258 1259  module @rewriters {1260    pdl_interp.func @success(%root : !pdl.operation) {1261      %op = pdl_interp.create_operation "test.success"1262      pdl_interp.erase %root1263      pdl_interp.finalize1264    }1265  }1266}1267 1268// CHECK-LABEL: test.get_result_11269// CHECK: "test.success"1270// CHECK: "test.op"() : () -> (i32, i32, i32, i32, i64)1271module @ir attributes { test.get_result_1 } {1272  %a:5 = "test.op"() : () -> (i32, i32, i32, i32, i32)1273  %b:5 = "test.op"() : () -> (i32, i32, i32, i32, i64)1274}1275 1276// -----1277 1278//===----------------------------------------------------------------------===//1279// pdl_interp::GetResultsOp1280//===----------------------------------------------------------------------===//1281 1282module @patterns {1283  pdl_interp.func @matcher(%root : !pdl.operation) {1284    pdl_interp.check_result_count of %root is 5 -> ^pat1, ^end1285 1286  ^pat1:1287    %results = pdl_interp.get_results 0 of %root : !pdl.range<value>1288    %full_results = pdl_interp.get_results of %root : !pdl.range<value>1289    pdl_interp.are_equal %results, %full_results : !pdl.range<value> -> ^pat2, ^end1290 1291  ^pat2:1292    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end1293 1294  ^end:1295    pdl_interp.finalize1296  }1297 1298  module @rewriters {1299    pdl_interp.func @success(%root : !pdl.operation) {1300      %op = pdl_interp.create_operation "test.success"1301      pdl_interp.erase %root1302      pdl_interp.finalize1303    }1304  }1305}1306 1307// CHECK-LABEL: test.get_results_11308// CHECK: "test.success"1309module @ir attributes { test.get_results_1 } {1310  %a:5 = "test.producer"() : () -> (i32, i32, i32, i32, i32)1311}1312 1313// -----1314 1315// Test all of the various combinations related to `AttrSizedResultSegments`.1316module @patterns {1317  pdl_interp.func @matcher(%root : !pdl.operation) {1318    pdl_interp.check_operation_name of %root is "test.attr_sized_results" -> ^pat1, ^end1319 1320  ^pat1:1321    %results_0 = pdl_interp.get_results 0 of %root : !pdl.range<value>1322    pdl_interp.is_not_null %results_0 : !pdl.range<value> -> ^pat2, ^end1323 1324  ^pat2:1325    %results_0_single = pdl_interp.get_results 0 of %root : !pdl.value1326    pdl_interp.is_not_null %results_0_single : !pdl.value -> ^end, ^pat31327 1328  ^pat3:1329    %results_1 = pdl_interp.get_results 1 of %root : !pdl.range<value>1330    pdl_interp.is_not_null %results_1 : !pdl.range<value> -> ^pat4, ^end1331 1332  ^pat4:1333    %results_1_single = pdl_interp.get_results 1 of %root : !pdl.value1334    pdl_interp.is_not_null %results_1_single : !pdl.value -> ^end, ^pat51335 1336  ^pat5:1337    %results_2 = pdl_interp.get_results 2 of %root : !pdl.range<value>1338    pdl_interp.is_not_null %results_2 : !pdl.range<value> -> ^pat6, ^end1339 1340  ^pat6:1341    %results_2_single = pdl_interp.get_results 2 of %root : !pdl.value1342    pdl_interp.is_not_null %results_2_single : !pdl.value -> ^pat7, ^end1343 1344  ^pat7:1345    %invalid_results = pdl_interp.get_results 50 of %root : !pdl.value1346    pdl_interp.is_not_null %invalid_results : !pdl.value -> ^end, ^pat81347 1348  ^pat8:1349    pdl_interp.record_match @rewriters::@success(%root, %results_0, %results_1, %results_2, %results_2_single : !pdl.operation, !pdl.range<value>, !pdl.range<value>, !pdl.range<value>, !pdl.value) : benefit(1), loc([%root]) -> ^end1350 1351 1352  ^end:1353    pdl_interp.finalize1354  }1355 1356  module @rewriters {1357    pdl_interp.func @success(%root: !pdl.operation, %results_0: !pdl.range<value>, %results_1: !pdl.range<value>, %results_2: !pdl.range<value>, %results_2_single: !pdl.value) {1358      %results_0_types = pdl_interp.get_value_type of %results_0 : !pdl.range<type>1359      %results_1_types = pdl_interp.get_value_type of %results_1 : !pdl.range<type>1360      %results_2_types = pdl_interp.get_value_type of %results_2 : !pdl.range<type>1361      %results_2_single_types = pdl_interp.get_value_type of %results_2_single : !pdl.type1362 1363      %op0 = pdl_interp.create_operation "test.success" -> (%results_0_types : !pdl.range<type>)1364      %op1 = pdl_interp.create_operation "test.success" -> (%results_1_types : !pdl.range<type>)1365      %op2 = pdl_interp.create_operation "test.success" -> (%results_2_types : !pdl.range<type>)1366      %op3 = pdl_interp.create_operation "test.success" -> (%results_2_single_types : !pdl.type)1367 1368      %new_results_0 = pdl_interp.get_results of %op0 : !pdl.range<value>1369      %new_results_1 = pdl_interp.get_results of %op1 : !pdl.range<value>1370      %new_results_2 = pdl_interp.get_results of %op2 : !pdl.range<value>1371 1372      pdl_interp.replace %root with (%new_results_0, %new_results_1, %new_results_2 : !pdl.range<value>, !pdl.range<value>, !pdl.range<value>)1373      pdl_interp.finalize1374    }1375  }1376}1377 1378// CHECK-LABEL: test.get_results_21379// CHECK: "test.success"() : () -> ()1380// CHECK: %[[RESULTS_1:.*]]:4 = "test.success"() : () -> (i32, i32, i32, i32)1381// CHECK: %[[RESULTS_2:.*]] = "test.success"() : () -> i321382// CHECK: %[[RESULTS_2_SINGLE:.*]] = "test.success"() : () -> i321383// CHECK: "test.consumer"(%[[RESULTS_1]]#0, %[[RESULTS_1]]#1, %[[RESULTS_1]]#2, %[[RESULTS_1]]#3, %[[RESULTS_2]]) : (i32, i32, i32, i32, i32) -> ()1384module @ir attributes { test.get_results_2 } {1385  %results:5 = "test.attr_sized_results"() {resultSegmentSizes = array<i32: 0, 4, 1, 0>} : () -> (i32, i32, i32, i32, i32)1386  "test.consumer"(%results#0, %results#1, %results#2, %results#3, %results#4) : (i32, i32, i32, i32, i32) -> ()1387}1388 1389// -----1390 1391//===----------------------------------------------------------------------===//1392// pdl_interp::GetValueTypeOp1393//===----------------------------------------------------------------------===//1394 1395// Fully tested within the tests for other operations.1396 1397//===----------------------------------------------------------------------===//1398// pdl_interp::IsNotNullOp1399//===----------------------------------------------------------------------===//1400 1401// Fully tested within the tests for other operations.1402 1403//===----------------------------------------------------------------------===//1404// pdl_interp::RecordMatchOp1405//===----------------------------------------------------------------------===//1406 1407// Check that the highest benefit pattern is selected.1408module @patterns {1409  pdl_interp.func @matcher(%root : !pdl.operation) {1410    pdl_interp.check_operation_name of %root is "test.op" -> ^pat1, ^end1411 1412  ^pat1:1413    pdl_interp.record_match @rewriters::@failure(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^pat21414 1415  ^pat2:1416    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(2), loc([%root]) -> ^end1417 1418  ^end:1419    pdl_interp.finalize1420  }1421 1422  module @rewriters {1423    pdl_interp.func @failure(%root : !pdl.operation) {1424      pdl_interp.erase %root1425      pdl_interp.finalize1426    }1427    pdl_interp.func @success(%root : !pdl.operation) {1428      %op = pdl_interp.create_operation "test.success"1429      pdl_interp.erase %root1430      pdl_interp.finalize1431    }1432  }1433}1434 1435// CHECK-LABEL: test.record_match_11436// CHECK: "test.success"1437module @ir attributes { test.record_match_1 } {1438  "test.op"() : () -> ()1439}1440 1441// -----1442 1443// Check that ranges are properly forwarded to the result.1444module @patterns {1445  pdl_interp.func @matcher(%root : !pdl.operation) {1446    pdl_interp.check_operation_name of %root is "test.op" -> ^pat1, ^end1447 1448  ^pat1:1449    %operands = pdl_interp.get_operands of %root : !pdl.range<value>1450    %results = pdl_interp.get_results of %root : !pdl.range<value>1451    %types = pdl_interp.get_value_type of %results : !pdl.range<type>1452    pdl_interp.record_match @rewriters::@success(%operands, %types, %root : !pdl.range<value>, !pdl.range<type>, !pdl.operation) : benefit(1), loc([%root]) -> ^end1453 1454  ^end:1455    pdl_interp.finalize1456  }1457 1458  module @rewriters {1459    pdl_interp.func @success(%operands: !pdl.range<value>, %types: !pdl.range<type>, %root: !pdl.operation) {1460      %op = pdl_interp.create_operation "test.success"(%operands : !pdl.range<value>) -> (%types : !pdl.range<type>)1461      %results = pdl_interp.get_results of %op : !pdl.range<value>1462      pdl_interp.replace %root with (%results : !pdl.range<value>)1463      pdl_interp.finalize1464    }1465  }1466}1467 1468// CHECK-LABEL: test.record_match_21469// CHECK: %[[OPERAND:.*]] = "test.producer"() : () -> i321470// CHECK: %[[RESULTS:.*]]:2 = "test.success"(%[[OPERAND]]) : (i32) -> (i64, i32)1471// CHECK: "test.consumer"(%[[RESULTS]]#0, %[[RESULTS]]#1) : (i64, i32) -> ()1472module @ir attributes { test.record_match_2 } {1473  %input = "test.producer"() : () -> i321474  %results:2 = "test.op"(%input) : (i32) -> (i64, i32)1475  "test.consumer"(%results#0, %results#1) : (i64, i32) -> ()1476}1477 1478// -----1479 1480//===----------------------------------------------------------------------===//1481// pdl_interp::ReplaceOp1482//===----------------------------------------------------------------------===//1483 1484module @patterns {1485  pdl_interp.func @matcher(%root : !pdl.operation) {1486    pdl_interp.check_operation_name of %root is "test.op" -> ^pat, ^end1487 1488  ^pat:1489    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end1490 1491  ^end:1492    pdl_interp.finalize1493  }1494 1495  module @rewriters {1496    pdl_interp.func @success(%root : !pdl.operation) {1497      %operand = pdl_interp.get_operand 0 of %root1498      pdl_interp.replace %root with (%operand : !pdl.value)1499      pdl_interp.finalize1500    }1501  }1502}1503 1504// CHECK-LABEL: test.replace_op_11505// CHECK: %[[INPUT:.*]] = "test.op_input"1506// CHECK-NOT: "test.op"1507// CHECK: "test.op_consumer"(%[[INPUT]])1508module @ir attributes { test.replace_op_1 } {1509  %input = "test.op_input"() : () -> i321510  %result = "test.op"(%input) : (i32) -> i321511  "test.op_consumer"(%result) : (i32) -> ()1512}1513 1514// -----1515 1516//===----------------------------------------------------------------------===//1517// pdl_interp::SwitchAttributeOp1518//===----------------------------------------------------------------------===//1519 1520module @patterns {1521  pdl_interp.func @matcher(%root : !pdl.operation) {1522    %attr = pdl_interp.get_attribute "test_attr" of %root1523    pdl_interp.switch_attribute %attr to [0, unit](^end, ^pat) -> ^end1524 1525  ^pat:1526    %attr_2 = pdl_interp.get_attribute "test_attr_2" of %root1527    pdl_interp.switch_attribute %attr_2 to [0, unit](^end, ^end) -> ^pat21528 1529  ^pat2:1530    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end1531 1532  ^end:1533    pdl_interp.finalize1534  }1535 1536  module @rewriters {1537    pdl_interp.func @success(%root : !pdl.operation) {1538      %op = pdl_interp.create_operation "test.success"1539      pdl_interp.erase %root1540      pdl_interp.finalize1541    }1542  }1543}1544 1545// CHECK-LABEL: test.switch_attribute_11546// CHECK: "test.success"1547module @ir attributes { test.switch_attribute_1 } {1548  "test.op"() { test_attr } : () -> ()1549}1550 1551// -----1552 1553//===----------------------------------------------------------------------===//1554// pdl_interp::SwitchOperandCountOp1555//===----------------------------------------------------------------------===//1556 1557module @patterns {1558  pdl_interp.func @matcher(%root : !pdl.operation) {1559    pdl_interp.switch_operand_count of %root to dense<[0, 1]> : vector<2xi32>(^end, ^pat) -> ^end1560 1561  ^pat:1562    pdl_interp.switch_operand_count of %root to dense<[0, 2]> : vector<2xi32>(^end, ^end) -> ^pat21563 1564  ^pat2:1565    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end1566 1567  ^end:1568    pdl_interp.finalize1569  }1570 1571  module @rewriters {1572    pdl_interp.func @success(%root : !pdl.operation) {1573      %op = pdl_interp.create_operation "test.success"1574      pdl_interp.erase %root1575      pdl_interp.finalize1576    }1577  }1578}1579 1580// CHECK-LABEL: test.switch_operand_11581// CHECK: "test.success"1582module @ir attributes { test.switch_operand_1 } {1583  %input = "test.op_input"() : () -> i321584  "test.op"(%input) : (i32) -> ()1585}1586 1587// -----1588 1589//===----------------------------------------------------------------------===//1590// pdl_interp::SwitchOperationNameOp1591//===----------------------------------------------------------------------===//1592 1593module @patterns {1594  pdl_interp.func @matcher(%root : !pdl.operation) {1595    pdl_interp.switch_operation_name of %root to ["foo.op", "test.op"](^end, ^pat1) -> ^end1596 1597  ^pat1:1598    pdl_interp.switch_operation_name of %root to ["foo.op", "bar.op"](^end, ^end) -> ^pat21599 1600  ^pat2:1601    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end1602 1603  ^end:1604    pdl_interp.finalize1605  }1606 1607  module @rewriters {1608    pdl_interp.func @success(%root : !pdl.operation) {1609      %op = pdl_interp.create_operation "test.success"1610      pdl_interp.erase %root1611      pdl_interp.finalize1612    }1613  }1614}1615 1616// CHECK-LABEL: test.switch_operation_name_11617// CHECK: "test.success"1618module @ir attributes { test.switch_operation_name_1 } {1619  "test.op"() : () -> ()1620}1621 1622// -----1623 1624//===----------------------------------------------------------------------===//1625// pdl_interp::SwitchResultCountOp1626//===----------------------------------------------------------------------===//1627 1628module @patterns {1629  pdl_interp.func @matcher(%root : !pdl.operation) {1630    pdl_interp.switch_result_count of %root to dense<[0, 1]> : vector<2xi32>(^end, ^pat) -> ^end1631 1632  ^pat:1633    pdl_interp.switch_result_count of %root to dense<[0, 2]> : vector<2xi32>(^end, ^end) -> ^pat21634 1635  ^pat2:1636    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end1637 1638  ^end:1639    pdl_interp.finalize1640  }1641 1642  module @rewriters {1643    pdl_interp.func @success(%root : !pdl.operation) {1644      %op = pdl_interp.create_operation "test.success"1645      pdl_interp.erase %root1646      pdl_interp.finalize1647    }1648  }1649}1650 1651// CHECK-LABEL: test.switch_result_11652// CHECK: "test.success"1653module @ir attributes { test.switch_result_1 } {1654  "test.op"() : () -> i321655}1656 1657// -----1658 1659//===----------------------------------------------------------------------===//1660// pdl_interp::SwitchTypeOp1661//===----------------------------------------------------------------------===//1662 1663module @patterns {1664  pdl_interp.func @matcher(%root : !pdl.operation) {1665    %attr = pdl_interp.get_attribute "test_attr" of %root1666    pdl_interp.is_not_null %attr : !pdl.attribute -> ^pat1, ^end1667 1668  ^pat1:1669    %type = pdl_interp.get_attribute_type of %attr1670    pdl_interp.switch_type %type to [i32, i64](^pat2, ^end) -> ^end1671 1672  ^pat2:1673    pdl_interp.switch_type %type to [i16, i64](^end, ^end) -> ^pat31674 1675  ^pat3:1676    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end1677 1678  ^end:1679    pdl_interp.finalize1680  }1681 1682  module @rewriters {1683    pdl_interp.func @success(%root : !pdl.operation) {1684      %op = pdl_interp.create_operation "test.success"1685      pdl_interp.erase %root1686      pdl_interp.finalize1687    }1688  }1689}1690 1691// CHECK-LABEL: test.switch_type_11692// CHECK: "test.success"1693module @ir attributes { test.switch_type_1 } {1694  "test.op"() { test_attr = 10 : i32 } : () -> ()1695}1696 1697// -----1698 1699//===----------------------------------------------------------------------===//1700// pdl_interp::SwitchTypesOp1701//===----------------------------------------------------------------------===//1702 1703module @patterns {1704  pdl_interp.func @matcher(%root : !pdl.operation) {1705    %results = pdl_interp.get_results of %root : !pdl.range<value>1706    %types = pdl_interp.get_value_type of %results : !pdl.range<type>1707    pdl_interp.switch_types %types to [[i64, i64], [i32]](^pat2, ^end) -> ^end1708 1709  ^pat2:1710    pdl_interp.switch_types %types to [[i32], [i64, i32]](^end, ^end) -> ^pat31711 1712  ^pat3:1713    pdl_interp.record_match @rewriters::@success(%root : !pdl.operation) : benefit(1), loc([%root]) -> ^end1714 1715  ^end:1716    pdl_interp.finalize1717  }1718 1719  module @rewriters {1720    pdl_interp.func @success(%root : !pdl.operation) {1721      %op = pdl_interp.create_operation "test.success"1722      pdl_interp.erase %root1723      pdl_interp.finalize1724    }1725  }1726}1727 1728// CHECK-LABEL: test.switch_types_11729// CHECK: "test.success"1730module @ir attributes { test.switch_types_1 } {1731  %results:2 = "test.op"() : () -> (i64, i64)1732}1733