Skip to content

Commit 2ec7f1c

Browse files
author
Thomas Symalla
committed
Auto-generate comments for dialect ops.
Currently, the summary and description fields for dialect ops remain unused. Add those as comment above the class declaration.
1 parent d3ef239 commit 2ec7f1c

File tree

7 files changed

+242
-1
lines changed

7 files changed

+242
-1
lines changed

example/ExampleDialect.td

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,19 @@ def StringAttrOp : Op<ExampleDialect, "string.attr.op", [WillReturn]> {
320320
The argument should not have a setter method
321321
}];
322322
}
323+
324+
def NoSummaryOp : Op<ExampleDialect, "no.summary.op", [WillReturn]> {
325+
let results = (outs);
326+
let arguments = (ins);
327+
328+
let description = [{
329+
Some description
330+
}];
331+
}
332+
333+
def NoDescriptionOp : Op<ExampleDialect, "no.description.op", [WillReturn]> {
334+
let results = (outs);
335+
let arguments = (ins);
336+
337+
let summary = "Some summary";
338+
}

include/llvm-dialects/TableGen/Common.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "llvm/Support/raw_ostream.h"
2222
#include "llvm/TableGen/Record.h"
23+
#include <string>
2324

2425
#if !defined(LLVM_MAIN_REVISION) || LLVM_MAIN_REVISION >= 513628
2526
using RecordKeeperTy = const llvm::RecordKeeper;
@@ -35,4 +36,8 @@ void emitHeader(llvm::raw_ostream& out);
3536

3637
bool shouldEmitComments();
3738

39+
/// Prefix an incoming multi-line string with a single-line comment string line
40+
/// by line.
41+
std::string createCommentFromString(const std::string &input);
42+
3843
} // namespace llvm_dialects

include/llvm-dialects/TableGen/Operations.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ class Operation : public OperationBase {
103103
public:
104104
std::string name;
105105
std::string mnemonic;
106+
std::string summary;
107+
std::string description;
106108
std::vector<Trait *> traits;
107109

108110
std::vector<NamedValue> results;

lib/TableGen/Common.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818

1919
#include "llvm-dialects/TableGen/Common.h"
20+
#include "llvm/ADT/SmallVector.h"
21+
#include "llvm/ADT/StringRef.h"
2022

2123
#include "llvm/Support/CommandLine.h"
2224

@@ -33,3 +35,18 @@ void llvm_dialects::emitHeader(raw_ostream& out) {
3335
}
3436

3537
bool llvm_dialects::shouldEmitComments() { return g_emitComments; }
38+
39+
std::string llvm_dialects::createCommentFromString(const std::string &input) {
40+
StringRef inRef{input};
41+
if (inRef.trim().empty())
42+
return input;
43+
44+
SmallVector<StringRef> lines;
45+
inRef.split(lines, '\n');
46+
47+
std::string outStr;
48+
for (auto line : lines)
49+
outStr += "/// " + line.str() + '\n';
50+
51+
return outStr;
52+
}

lib/TableGen/GenDialect.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,16 @@ class Builder;
172172
fmt.withOp(op.name);
173173
fmt.addSubst("mnemonic", op.mnemonic);
174174

175+
std::string description = "/// " + op.name + '\n';
176+
177+
if (!op.summary.empty())
178+
description += createCommentFromString(op.summary);
179+
180+
if (!op.description.empty())
181+
description += createCommentFromString(op.description);
182+
175183
out << tgfmt(R"(
184+
$2
176185
class $_op : public $0 {
177186
static const ::llvm::StringLiteral s_name; //{"$dialect.$mnemonic"};
178187
@@ -188,7 +197,8 @@ class Builder;
188197
&fmt,
189198
op.superclass() ? op.superclass()->name : "::llvm::CallInst",
190199
!op.haveResultOverloads() ? "isSimpleOperation"
191-
: "isOverloadedOperation");
200+
: "isOverloadedOperation",
201+
description);
192202

193203
for (const auto &builder : op.builders())
194204
builder.emitDeclaration(out, fmt);

lib/TableGen/Operations.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,11 @@ bool Operation::parse(raw_ostream &errs, GenDialectsContext *context,
360360

361361
op->name = record->getName();
362362
op->mnemonic = record->getValueAsString("mnemonic");
363+
if (!record->isValueUnset("summary"))
364+
op->summary = record->getValueAsString("summary");
365+
366+
if (!record->isValueUnset("description"))
367+
op->description = record->getValueAsString("description");
363368
for (RecordTy *traitRec : record->getValueAsListOfDefs("traits"))
364369
op->traits.push_back(context->getTrait(traitRec));
365370

0 commit comments

Comments
 (0)