[llvm] [mlir] [MLIR][IRDL] Added IRDL to C++ Translation (PR #133982)
Fehr Mathieu via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 2 08:57:49 PDT 2025
=?utf-8?q?Théo?= Degioanni,=?utf-8?q?Théo?= Degioanni,
=?utf-8?q?Théo?= Degioanni,=?utf-8?q?Théo?= Degioanni,Ivan Ho
<ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan
Ho <ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,--set <--set>,Ivan Ho
<ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan
Ho <ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan
Ho <ihkh2 at cam.ac.uk>,=?utf-8?q?Théo?= Degioanni,
=?utf-8?q?Théo?= Degioanni,Ivan Ho <ihkh2 at cam.ac.uk>,
=?utf-8?q?Théo?= Degioanni,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan Ho
<ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan
Ho <ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan Ho
<38537881+hhkit at users.noreply.github.com>,=?utf-8?q?Théo?= Degioanni,Ivan
Ho <ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan
Ho <ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan
Ho <ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan
Ho <ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan
Ho <ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan
Ho <38537881+hhkit at users.noreply.github.com>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan
Ho <ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan
Ho <ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan
Ho <ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan
Ho <ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan
Ho <ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan
Ho <ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan
Ho <ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>,
=?utf-8?q?Théo?= Degioanni,=?utf-8?q?Théo?= Degioanni,Ivan Ho
<ihkh2 at cam.ac.uk>,Ivan Ho <ihkh2 at cam.ac.uk>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/133982 at github.com>
================
@@ -0,0 +1,570 @@
+//===- IRDLToCpp.cpp - Converts IRDL definitions to C++ -------------------===//
+//
+// 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/IRDLToCpp.h"
+#include "mlir/Dialect/IRDL/IR/IRDL.h"
+#include "mlir/Support/LLVM.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/TypeSwitch.h"
+#include "llvm/Support/FormatVariadic.h"
+#include "llvm/Support/raw_ostream.h"
+
+#include "TemplatingUtils.h"
+
+using namespace mlir;
+
+constexpr char headerTemplateText[] =
+#include "Templates/Header.txt"
+ ;
+
+constexpr char declarationMacroFlag[] = "GEN_DIALECT_DECL_HEADER";
+constexpr char definitionMacroFlag[] = "GEN_DIALECT_DEF";
+
+namespace {
+
+struct DialectStrings {
+ StringRef dialectName;
+ StringRef dialectCppName;
+ StringRef dialectCppShortName;
+ StringRef dialectBaseTypeName;
+
+ StringRef namespaceOpen;
+ StringRef namespaceClose;
+ StringRef namespacePath;
+};
+
+struct TypeStrings {
+ StringRef typeName;
+ std::string typeCppName;
+};
+
+struct OpStrings {
+ StringRef opName;
+ std::string opCppName;
+ SmallVector<std::string> opResultNames;
+ SmallVector<std::string> opOperandNames;
+};
+
+static std::string joinNameList(llvm::ArrayRef<std::string> names) {
+ std::string nameArray;
+ llvm::raw_string_ostream nameArrayStream(nameArray);
+ nameArrayStream << "{\"" << llvm::join(names, "\", \"") << "\"}";
+
+ return nameArray;
+}
+
+static std::string typeToCppName(irdl::TypeOp type) {
+ return llvm::formatv("{0}Type",
+ convertToCamelFromSnakeCase(type.getSymName(), true));
+}
+
+static std::string opToCppName(irdl::OperationOp op) {
+ return llvm::formatv("{0}Op",
+ convertToCamelFromSnakeCase(op.getSymName(), true));
+}
+
+static TypeStrings getStrings(irdl::TypeOp type) {
+ TypeStrings strings;
+ strings.typeName = type.getSymName();
+ strings.typeCppName = typeToCppName(type);
+ return strings;
+}
+
+static OpStrings getStrings(irdl::OperationOp op) {
+ auto operands = op.getOps<irdl::OperandsOp>();
+ auto operandOp =
+ operands.empty() ? std::optional<irdl::OperandsOp>{} : *operands.begin();
+
+ auto results = op.getOps<irdl::ResultsOp>();
+ auto resultOp =
+ results.empty() ? std::optional<irdl::ResultsOp>{} : *results.begin();
+
+ OpStrings strings;
+ strings.opName = op.getSymName();
+ strings.opCppName = opToCppName(op);
+
+ if (operandOp) {
+ strings.opOperandNames = SmallVector<std::string>(
+ llvm::map_range(operandOp->getNames(), [](Attribute attr) {
+ return llvm::formatv("{0}", cast<StringAttr>(attr));
+ }));
+ }
+
+ if (resultOp) {
+ strings.opResultNames = SmallVector<std::string>(
+ llvm::map_range(resultOp->getNames(), [](Attribute attr) {
+ return llvm::formatv("{0}", cast<StringAttr>(attr));
+ }));
+ }
+
+ return strings;
+}
+
+static void fillDict(irdl::detail::dictionary &dict,
+ const TypeStrings &strings) {
+ dict["TYPE_NAME"] = strings.typeName;
+ dict["TYPE_CPP_NAME"] = strings.typeCppName;
+}
+
+static void fillDict(irdl::detail::dictionary &dict, const OpStrings &strings) {
+ const auto operandCount = strings.opOperandNames.size();
+ const auto resultCount = strings.opResultNames.size();
+
+ dict["OP_NAME"] = strings.opName;
+ dict["OP_CPP_NAME"] = strings.opCppName;
+ dict["OP_OPERAND_COUNT"] = std::to_string(strings.opOperandNames.size());
+ dict["OP_RESULT_COUNT"] = std::to_string(strings.opResultNames.size());
+ dict["OP_OPERAND_INITIALIZER_LIST"] =
+ operandCount ? joinNameList(strings.opOperandNames) : "{\"\"}";
+ dict["OP_RESULT_INITIALIZER_LIST"] =
+ resultCount ? joinNameList(strings.opResultNames) : "{\"\"}";
+}
+
+static void fillDict(irdl::detail::dictionary &dict,
+ const DialectStrings &strings) {
+ dict["DIALECT_NAME"] = strings.dialectName;
+ dict["DIALECT_BASE_TYPE_NAME"] = strings.dialectBaseTypeName;
+ dict["DIALECT_CPP_NAME"] = strings.dialectCppName;
+ dict["DIALECT_CPP_SHORT_NAME"] = strings.dialectCppShortName;
+ dict["NAMESPACE_OPEN"] = strings.namespaceOpen;
+ dict["NAMESPACE_CLOSE"] = strings.namespaceClose;
+ dict["NAMESPACE_PATH"] = strings.namespacePath;
+}
+
+static LogicalResult generateTypedefList(irdl::DialectOp &dialect,
+ SmallVector<std::string> &typeNames) {
+ auto typeOps = dialect.getOps<irdl::TypeOp>();
+ auto range = llvm::map_range(typeOps, typeToCppName);
+ typeNames = SmallVector<std::string>(range);
+ return success();
+}
+
+static LogicalResult generateOpList(irdl::DialectOp &dialect,
+ SmallVector<std::string> &opNames) {
+ auto operationOps = dialect.getOps<irdl::OperationOp>();
+ auto range = llvm::map_range(operationOps, opToCppName);
+ opNames = SmallVector<std::string>(range);
+ return success();
+}
+
+} // namespace
+
+static LogicalResult generateTypeInclude(irdl::TypeOp type, raw_ostream &output,
+ irdl::detail::dictionary &dict) {
+ static const auto typeDeclTemplate = irdl::detail::Template(
+#include "Templates/TypeDecl.txt"
+ );
+
+ fillDict(dict, getStrings(type));
+ typeDeclTemplate.render(output, dict);
+
+ return success();
+}
+
+static void generateOpGetterDeclarations(irdl::detail::dictionary &dict,
+ const OpStrings &opStrings) {
+ auto opGetters = std::string{};
+ auto resGetters = std::string{};
+
+ for (size_t i = 0; i < opStrings.opOperandNames.size(); ++i) {
+ const auto op =
+ llvm::convertToCamelFromSnakeCase(opStrings.opOperandNames[i], true);
+ opGetters += llvm::formatv("::mlir::Value get{0}() { return "
+ "getStructuredOperands({1}).front(); }\n ",
+ op, i);
+ }
+ for (size_t i = 0; i < opStrings.opResultNames.size(); ++i) {
----------------
math-fehr wrote:
```suggestion
for (size_t i = 0, end = opStrings.opResultNames.size(); i < end; ++i) {
```
https://github.com/llvm/llvm-project/pull/133982
More information about the llvm-commits
mailing list