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>
50 lines
1.9 KiB
C++
50 lines
1.9 KiB
C++
//===- TranslationRegistration.cpp - Register translation -----------------===//
|
|
//
|
|
// 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "mlir/Target/IRDLToCpp/TranslationRegistration.h"
|
|
#include "mlir/Dialect/IRDL/IR/IRDL.h"
|
|
#include "mlir/IR/BuiltinOps.h"
|
|
#include "mlir/Target/IRDLToCpp/IRDLToCpp.h"
|
|
#include "mlir/Tools/mlir-translate/Translation.h"
|
|
#include "llvm/ADT/TypeSwitch.h"
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
using namespace mlir;
|
|
|
|
namespace mlir {
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// Translation registration
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
void registerIRDLToCppTranslation() {
|
|
TranslateFromMLIRRegistration reg(
|
|
"irdl-to-cpp", "translate IRDL dialect definitions to C++ definitions",
|
|
[](Operation *op, raw_ostream &output) {
|
|
return TypeSwitch<Operation *, LogicalResult>(op)
|
|
.Case<irdl::DialectOp>([&](irdl::DialectOp dialectOp) {
|
|
return irdl::translateIRDLDialectToCpp(dialectOp, output);
|
|
})
|
|
.Case<ModuleOp>([&](ModuleOp moduleOp) {
|
|
for (Operation &op : moduleOp.getBody()->getOperations())
|
|
if (auto dialectOp = llvm::dyn_cast<irdl::DialectOp>(op))
|
|
if (failed(
|
|
irdl::translateIRDLDialectToCpp(dialectOp, output)))
|
|
return failure();
|
|
return success();
|
|
})
|
|
.Default([](Operation *op) {
|
|
return op->emitError(
|
|
"unsupported operation for IRDL to C++ translation");
|
|
});
|
|
},
|
|
[](DialectRegistry ®istry) { registry.insert<irdl::IRDLDialect>(); });
|
|
}
|
|
|
|
} // namespace mlir
|