1 //===- TestPDLByteCode.cpp - Test PDLL 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/PDL/IR/PDL.h"
10 #include "mlir/Dialect/PDLInterp/IR/PDLInterp.h"
11 #include "mlir/Parser/Parser.h"
12 #include "mlir/Pass/Pass.h"
13 #include "mlir/Pass/PassManager.h"
14 #include "mlir/Transforms/GreedyPatternRewriteDriver.h"
15 
16 using namespace mlir;
17 
18 #include "TestPDLLPatterns.h.inc"
19 
20 namespace {
21 struct TestPDLLPass : public PassWrapper<TestPDLLPass, OperationPass<>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID__anon1273ab6f0111::TestPDLLPass22   MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestPDLLPass)
23 
24   StringRef getArgument() const final { return "test-pdll-pass"; }
getDescription__anon1273ab6f0111::TestPDLLPass25   StringRef getDescription() const final { return "Test PDLL functionality"; }
getDependentDialects__anon1273ab6f0111::TestPDLLPass26   void getDependentDialects(DialectRegistry &registry) const override {
27     registry.insert<pdl::PDLDialect, pdl_interp::PDLInterpDialect>();
28   }
initialize__anon1273ab6f0111::TestPDLLPass29   LogicalResult initialize(MLIRContext *ctx) override {
30     // Build the pattern set within the `initialize` to avoid recompiling PDL
31     // patterns during each `runOnOperation` invocation.
32     RewritePatternSet patternList(ctx);
33     populateGeneratedPDLLPatterns(patternList);
34     patterns = std::move(patternList);
35     return success();
36   }
37 
runOnOperation__anon1273ab6f0111::TestPDLLPass38   void runOnOperation() final {
39     // Invoke the pattern driver with the provided patterns.
40     (void)applyPatternsAndFoldGreedily(getOperation(), patterns);
41   }
42 
43   FrozenRewritePatternSet patterns;
44 };
45 } // namespace
46 
47 namespace mlir {
48 namespace test {
registerTestPDLLPasses()49 void registerTestPDLLPasses() { PassRegistration<TestPDLLPass>(); }
50 } // namespace test
51 } // namespace mlir
52