Files
clang-p2996/mlir/test/lib/Dialect/TestIRDLToCpp/TestIRDLToCppDialect.cpp
Ivan Ho c76e2800e3 [MLIR][IRDL] Added IRDL to C++ Translation (#141248)
This PR introduces a new tool, mlir-irdl-to-cpp, that converts IRDL to
C++ definitions.

The C++ definitions allow use of the IRDL-defined dialect in MLIR C++
infrastructure, enabling the use of conversion patterns with IRDL
dialects for example. This PR also adds CMake utilities to easily
integrate the IRDL dialects into MLIR projects.

Note that most IRDL features are not supported. In general, we are only
able to define simple types and operations.

- The only type constraint supported is irdl.any.
- Variadic operands and results are not supported.
- Verifiers for the IRDL constraints are not generated.
- Attributes are not supported.

---------

Co-authored-by: Théo Degioanni <theo.degioanni.llvm.deluge062@simplelogin.fr>
Co-authored-by: Fehr Mathieu <mathieu.fehr@gmail.com>
2025-05-25 16:42:30 +02:00

89 lines
3.1 KiB
C++

//===- TestIRDLToCppDialect.cpp - MLIR Test Dialect Types ---------------*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file includes TestIRDLToCpp dialect.
//
//===----------------------------------------------------------------------===//
// #include "mlir/IR/Dialect.h"
#include "mlir/IR/Region.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/DialectImplementation.h"
#include "mlir/Interfaces/InferTypeOpInterface.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h"
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
#include "mlir/Target/LLVMIR/LLVMTranslationInterface.h"
#include "mlir/Target/LLVMIR/ModuleTranslation.h"
#include "mlir/Tools/mlir-translate/Translation.h"
#include "mlir/Transforms/DialectConversion.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/TypeSwitch.h"
#include "TestIRDLToCppDialect.h"
#define GEN_DIALECT_DEF
#include "test_irdl_to_cpp.irdl.mlir.cpp.inc"
namespace test {
using namespace mlir;
struct TestOpConversion : public OpConversionPattern<test_irdl_to_cpp::BeefOp> {
using OpConversionPattern::OpConversionPattern;
LogicalResult
matchAndRewrite(mlir::test_irdl_to_cpp::BeefOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
assert(adaptor.getStructuredOperands(0).size() == 1);
assert(adaptor.getStructuredOperands(1).size() == 1);
auto bar = rewriter.replaceOpWithNewOp<test_irdl_to_cpp::BarOp>(
op, op->getResultTypes().front());
rewriter.setInsertionPointAfter(bar);
rewriter.create<test_irdl_to_cpp::HashOp>(
bar.getLoc(), rewriter.getIntegerType(32), adaptor.getLhs(),
adaptor.getRhs());
return success();
}
};
struct ConvertTestDialectToSomethingPass
: PassWrapper<ConvertTestDialectToSomethingPass, OperationPass<ModuleOp>> {
void runOnOperation() override {
MLIRContext *ctx = &getContext();
RewritePatternSet patterns(ctx);
patterns.add<TestOpConversion>(ctx);
ConversionTarget target(getContext());
target.addIllegalOp<test_irdl_to_cpp::BeefOp>();
target.addLegalOp<test_irdl_to_cpp::BarOp>();
target.addLegalOp<test_irdl_to_cpp::HashOp>();
if (failed(applyPartialConversion(getOperation(), target,
std::move(patterns))))
signalPassFailure();
}
StringRef getArgument() const final { return "test-irdl-conversion-check"; }
StringRef getDescription() const final {
return "Checks the convertability of an irdl dialect";
}
};
void registerIrdlTestDialect(mlir::DialectRegistry &registry) {
registry.insert<mlir::test_irdl_to_cpp::TestIrdlToCppDialect>();
}
} // namespace test
namespace mlir::test {
void registerTestIrdlTestDialectConversionPass() {
PassRegistration<::test::ConvertTestDialectToSomethingPass>();
}
} // namespace mlir::test