1//===- TestIRDLToCppDialect.cpp - MLIR Test Dialect Types ---------------*-===//
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// This file includes TestIRDLToCpp dialect.
10//
11//===----------------------------------------------------------------------===//
12
13// #include "mlir/IR/Dialect.h"
14#include "mlir/IR/Region.h"
15
16#include "mlir/IR/BuiltinTypes.h"
17#include "mlir/IR/DialectImplementation.h"
18#include "mlir/Interfaces/InferTypeOpInterface.h"
19#include "mlir/Pass/Pass.h"
20#include "mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h"
21#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
22#include "mlir/Target/LLVMIR/LLVMTranslationInterface.h"
23#include "mlir/Target/LLVMIR/ModuleTranslation.h"
24#include "mlir/Tools/mlir-translate/Translation.h"
25#include "mlir/Transforms/DialectConversion.h"
26#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
27#include "llvm/ADT/DenseSet.h"
28#include "llvm/ADT/TypeSwitch.h"
29
30#include "TestIRDLToCppDialect.h"
31
32#define GEN_DIALECT_DEF
33#include "test_irdl_to_cpp.irdl.mlir.cpp.inc"
34
35namespace test {
36using namespace mlir;
37struct TestOpConversion : public OpConversionPattern<test_irdl_to_cpp::BeefOp> {
38 using OpConversionPattern::OpConversionPattern;
39
40 LogicalResult
41 matchAndRewrite(mlir::test_irdl_to_cpp::BeefOp op, OpAdaptor adaptor,
42 ConversionPatternRewriter &rewriter) const override {
43 assert(adaptor.getStructuredOperands(0).size() == 1);
44 assert(adaptor.getStructuredOperands(1).size() == 1);
45
46 auto bar = rewriter.replaceOpWithNewOp<test_irdl_to_cpp::BarOp>(
47 op, op->getResultTypes().front());
48 rewriter.setInsertionPointAfter(bar);
49
50 rewriter.create<test_irdl_to_cpp::HashOp>(
51 bar.getLoc(), rewriter.getIntegerType(32), adaptor.getLhs(),
52 adaptor.getRhs());
53 return success();
54 }
55};
56
57struct ConvertTestDialectToSomethingPass
58 : PassWrapper<ConvertTestDialectToSomethingPass, OperationPass<ModuleOp>> {
59 void runOnOperation() override {
60 MLIRContext *ctx = &getContext();
61 RewritePatternSet patterns(ctx);
62 patterns.add<TestOpConversion>(arg&: ctx);
63 ConversionTarget target(getContext());
64 target.addIllegalOp<test_irdl_to_cpp::BeefOp>();
65 target.addLegalOp<test_irdl_to_cpp::BarOp>();
66 target.addLegalOp<test_irdl_to_cpp::HashOp>();
67 if (failed(applyPartialConversion(getOperation(), target,
68 std::move(patterns))))
69 signalPassFailure();
70 }
71
72 StringRef getArgument() const final { return "test-irdl-conversion-check"; }
73 StringRef getDescription() const final {
74 return "Checks the convertability of an irdl dialect";
75 }
76};
77
78void registerIrdlTestDialect(mlir::DialectRegistry &registry) {
79 registry.insert<mlir::test_irdl_to_cpp::TestIrdlToCppDialect>();
80}
81
82} // namespace test
83
84namespace mlir::test {
85void registerTestIrdlTestDialectConversionPass() {
86 PassRegistration<::test::ConvertTestDialectToSomethingPass>();
87}
88} // namespace mlir::test
89

source code of mlir/test/lib/Dialect/TestIRDLToCpp/TestIRDLToCppDialect.cpp