|
13 | 13 |
|
14 | 14 | #include "OpGenHelpers.h" |
15 | 15 |
|
| 16 | +#include "mlir/Support/IndentedOstream.h" |
16 | 17 | #include "mlir/TableGen/GenInfo.h" |
17 | 18 | #include "mlir/TableGen/Operator.h" |
18 | 19 | #include "llvm/ADT/StringSet.h" |
19 | 20 | #include "llvm/Support/CommandLine.h" |
20 | 21 | #include "llvm/Support/FormatVariadic.h" |
21 | 22 | #include "llvm/TableGen/Error.h" |
22 | 23 | #include "llvm/TableGen/Record.h" |
| 24 | +#include <regex> |
23 | 25 |
|
24 | 26 | using namespace mlir; |
25 | 27 | using namespace mlir::tblgen; |
@@ -61,10 +63,11 @@ from ._{0}_ops_gen import _Dialect |
61 | 63 |
|
62 | 64 | /// Template for operation class: |
63 | 65 | /// {0} is the Python class name; |
64 | | -/// {1} is the operation name. |
| 66 | +/// {1} is the operation name; |
| 67 | +/// {2} is the docstring for this operation. |
65 | 68 | constexpr const char *opClassTemplate = R"Py( |
66 | 69 | @_ods_cext.register_operation(_Dialect) |
67 | | -class {0}(_ods_ir.OpView): |
| 70 | +class {0}(_ods_ir.OpView):{2} |
68 | 71 | OPERATION_NAME = "{1}" |
69 | 72 | )Py"; |
70 | 73 |
|
@@ -1031,9 +1034,31 @@ static void emitValueBuilder(const Operator &op, |
1031 | 1034 | } |
1032 | 1035 | } |
1033 | 1036 |
|
| 1037 | +/// Retrieve the description of the given op and generate a docstring for it. |
| 1038 | +static std::string makeDocStringForOp(const Operator &op) { |
| 1039 | + if (!op.hasDescription()) |
| 1040 | + return ""; |
| 1041 | + |
| 1042 | + auto desc = op.getDescription().rtrim(" \t").str(); |
| 1043 | + // Replace all """ with \"\"\" to avoid early termination of the literal. |
| 1044 | + desc = std::regex_replace(desc, std::regex(R"(""")"), R"(\"\"\")"); |
| 1045 | + |
| 1046 | + std::string docString = "\n"; |
| 1047 | + llvm::raw_string_ostream os(docString); |
| 1048 | + raw_indented_ostream identedOs(os); |
| 1049 | + os << R"( r""")" << "\n"; |
| 1050 | + identedOs.printReindented(desc, " "); |
| 1051 | + if (!StringRef(desc).ends_with("\n")) |
| 1052 | + os << "\n"; |
| 1053 | + os << R"( """)" << "\n"; |
| 1054 | + |
| 1055 | + return docString; |
| 1056 | +} |
| 1057 | + |
1034 | 1058 | /// Emits bindings for a specific Op to the given output stream. |
1035 | 1059 | static void emitOpBindings(const Operator &op, raw_ostream &os) { |
1036 | | - os << formatv(opClassTemplate, op.getCppClassName(), op.getOperationName()); |
| 1060 | + os << formatv(opClassTemplate, op.getCppClassName(), op.getOperationName(), |
| 1061 | + makeDocStringForOp(op)); |
1037 | 1062 |
|
1038 | 1063 | // Sized segments. |
1039 | 1064 | if (op.getTrait(attrSizedTraitForKind("operand")) != nullptr) { |
|
0 commit comments