Skip to content

Commit

Permalink
Auto-generate comments for dialect ops.
Browse files Browse the repository at this point in the history
Currently, the summary and description fields for dialect ops remain
unused. Add those as comment above the class declaration.
  • Loading branch information
Thomas Symalla committed Feb 17, 2025
1 parent d3ef239 commit 2ec7f1c
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 1 deletion.
16 changes: 16 additions & 0 deletions example/ExampleDialect.td
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,19 @@ def StringAttrOp : Op<ExampleDialect, "string.attr.op", [WillReturn]> {
The argument should not have a setter method
}];
}

def NoSummaryOp : Op<ExampleDialect, "no.summary.op", [WillReturn]> {
let results = (outs);
let arguments = (ins);

let description = [{
Some description
}];
}

def NoDescriptionOp : Op<ExampleDialect, "no.description.op", [WillReturn]> {
let results = (outs);
let arguments = (ins);

let summary = "Some summary";
}
5 changes: 5 additions & 0 deletions include/llvm-dialects/TableGen/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "llvm/Support/raw_ostream.h"
#include "llvm/TableGen/Record.h"
#include <string>

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

bool shouldEmitComments();

/// Prefix an incoming multi-line string with a single-line comment string line
/// by line.
std::string createCommentFromString(const std::string &input);

} // namespace llvm_dialects
2 changes: 2 additions & 0 deletions include/llvm-dialects/TableGen/Operations.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ class Operation : public OperationBase {
public:
std::string name;
std::string mnemonic;
std::string summary;
std::string description;
std::vector<Trait *> traits;

std::vector<NamedValue> results;
Expand Down
17 changes: 17 additions & 0 deletions lib/TableGen/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/

#include "llvm-dialects/TableGen/Common.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"

#include "llvm/Support/CommandLine.h"

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

bool llvm_dialects::shouldEmitComments() { return g_emitComments; }

std::string llvm_dialects::createCommentFromString(const std::string &input) {
StringRef inRef{input};
if (inRef.trim().empty())
return input;

SmallVector<StringRef> lines;
inRef.split(lines, '\n');

std::string outStr;
for (auto line : lines)
outStr += "/// " + line.str() + '\n';

return outStr;
}
12 changes: 11 additions & 1 deletion lib/TableGen/GenDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,16 @@ class Builder;
fmt.withOp(op.name);
fmt.addSubst("mnemonic", op.mnemonic);

std::string description = "/// " + op.name + '\n';

if (!op.summary.empty())
description += createCommentFromString(op.summary);

if (!op.description.empty())
description += createCommentFromString(op.description);

out << tgfmt(R"(
$2
class $_op : public $0 {
static const ::llvm::StringLiteral s_name; //{"$dialect.$mnemonic"};
Expand All @@ -188,7 +197,8 @@ class Builder;
&fmt,
op.superclass() ? op.superclass()->name : "::llvm::CallInst",
!op.haveResultOverloads() ? "isSimpleOperation"
: "isOverloadedOperation");
: "isOverloadedOperation",
description);

for (const auto &builder : op.builders())
builder.emitDeclaration(out, fmt);
Expand Down
5 changes: 5 additions & 0 deletions lib/TableGen/Operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,11 @@ bool Operation::parse(raw_ostream &errs, GenDialectsContext *context,

op->name = record->getName();
op->mnemonic = record->getValueAsString("mnemonic");
if (!record->isValueUnset("summary"))
op->summary = record->getValueAsString("summary");

if (!record->isValueUnset("description"))
op->description = record->getValueAsString("description");
for (RecordTy *traitRec : record->getValueAsListOfDefs("traits"))
op->traits.push_back(context->getTrait(traitRec));

Expand Down
Loading

0 comments on commit 2ec7f1c

Please sign in to comment.