1 //===- TestPDLByteCode.cpp - Test rewriter bytecode functionality ---------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "mlir/Dialect/PDLInterp/IR/PDLInterp.h" 10 #include "mlir/Pass/Pass.h" 11 #include "mlir/Pass/PassManager.h" 12 #include "mlir/Transforms/GreedyPatternRewriteDriver.h" 13 14 using namespace mlir; 15 16 /// Custom constraint invoked from PDL. 17 static LogicalResult customSingleEntityConstraint(PDLValue value, 18 PatternRewriter &rewriter) { 19 Operation *rootOp = value.cast<Operation *>(); 20 return success(rootOp->getName().getStringRef() == "test.op"); 21 } 22 static LogicalResult customMultiEntityConstraint(ArrayRef<PDLValue> values, 23 PatternRewriter &rewriter) { 24 return customSingleEntityConstraint(values[1], rewriter); 25 } 26 static LogicalResult 27 customMultiEntityVariadicConstraint(ArrayRef<PDLValue> values, 28 PatternRewriter &rewriter) { 29 if (llvm::any_of(values, [](const PDLValue &value) { return !value; })) 30 return failure(); 31 ValueRange operandValues = values[0].cast<ValueRange>(); 32 TypeRange typeValues = values[1].cast<TypeRange>(); 33 if (operandValues.size() != 2 || typeValues.size() != 2) 34 return failure(); 35 return success(); 36 } 37 38 // Custom creator invoked from PDL. 39 static void customCreate(ArrayRef<PDLValue> args, PatternRewriter &rewriter, 40 PDLResultList &results) { 41 results.push_back(rewriter.createOperation( 42 OperationState(args[0].cast<Operation *>()->getLoc(), "test.success"))); 43 } 44 static void customVariadicResultCreate(ArrayRef<PDLValue> args, 45 PatternRewriter &rewriter, 46 PDLResultList &results) { 47 Operation *root = args[0].cast<Operation *>(); 48 results.push_back(root->getOperands()); 49 results.push_back(root->getOperands().getTypes()); 50 } 51 static void customCreateType(ArrayRef<PDLValue> args, PatternRewriter &rewriter, 52 PDLResultList &results) { 53 results.push_back(rewriter.getF32Type()); 54 } 55 56 /// Custom rewriter invoked from PDL. 57 static void customRewriter(ArrayRef<PDLValue> args, PatternRewriter &rewriter, 58 PDLResultList &results) { 59 Operation *root = args[0].cast<Operation *>(); 60 OperationState successOpState(root->getLoc(), "test.success"); 61 successOpState.addOperands(args[1].cast<Value>()); 62 rewriter.createOperation(successOpState); 63 rewriter.eraseOp(root); 64 } 65 66 namespace { 67 struct TestPDLByteCodePass 68 : public PassWrapper<TestPDLByteCodePass, OperationPass<ModuleOp>> { 69 StringRef getArgument() const final { return "test-pdl-bytecode-pass"; } 70 StringRef getDescription() const final { 71 return "Test PDL ByteCode functionality"; 72 } 73 void getDependentDialects(DialectRegistry ®istry) const override { 74 // Mark the pdl_interp dialect as a dependent. This is needed, because we 75 // create ops from that dialect as a part of the PDL-to-PDLInterp lowering. 76 registry.insert<pdl_interp::PDLInterpDialect>(); 77 } 78 void runOnOperation() final { 79 ModuleOp module = getOperation(); 80 81 // The test cases are encompassed via two modules, one containing the 82 // patterns and one containing the operations to rewrite. 83 ModuleOp patternModule = module.lookupSymbol<ModuleOp>( 84 StringAttr::get(module->getContext(), "patterns")); 85 ModuleOp irModule = module.lookupSymbol<ModuleOp>( 86 StringAttr::get(module->getContext(), "ir")); 87 if (!patternModule || !irModule) 88 return; 89 90 RewritePatternSet patternList(module->getContext()); 91 92 // Register ahead of time to test when functions are registered without a 93 // pattern. 94 patternList.getPDLPatterns().registerConstraintFunction( 95 "multi_entity_constraint", customMultiEntityConstraint); 96 patternList.getPDLPatterns().registerConstraintFunction( 97 "single_entity_constraint", customSingleEntityConstraint); 98 99 // Process the pattern module. 100 patternModule.getOperation()->remove(); 101 PDLPatternModule pdlPattern(patternModule); 102 103 // Note: This constraint was already registered, but we re-register here to 104 // ensure that duplication registration is allowed (the duplicate mapping 105 // will be ignored). This tests that we support separating the registration 106 // of library functions from the construction of patterns, and also that we 107 // allow multiple patterns to depend on the same library functions (without 108 // asserting/crashing). 109 pdlPattern.registerConstraintFunction("multi_entity_constraint", 110 customMultiEntityConstraint); 111 pdlPattern.registerConstraintFunction("multi_entity_var_constraint", 112 customMultiEntityVariadicConstraint); 113 pdlPattern.registerRewriteFunction("creator", customCreate); 114 pdlPattern.registerRewriteFunction("var_creator", 115 customVariadicResultCreate); 116 pdlPattern.registerRewriteFunction("type_creator", customCreateType); 117 pdlPattern.registerRewriteFunction("rewriter", customRewriter); 118 patternList.add(std::move(pdlPattern)); 119 120 // Invoke the pattern driver with the provided patterns. 121 (void)applyPatternsAndFoldGreedily(irModule.getBodyRegion(), 122 std::move(patternList)); 123 } 124 }; 125 } // namespace 126 127 namespace mlir { 128 namespace test { 129 void registerTestPDLByteCodePass() { PassRegistration<TestPDLByteCodePass>(); } 130 } // namespace test 131 } // namespace mlir 132