Skip to content

Commit 726df05

Browse files
authored
Add comments about arguments to op declarations (GPUOpen-Drivers#119)
* Add comments about arguments to op declarations
1 parent 50260f8 commit 726df05

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

include/llvm-dialects/TableGen/Common.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ void emitHeader(llvm::raw_ostream& out);
3636

3737
bool shouldEmitComments();
3838

39+
/// Replace all occurrences of needle in haystack with replacement and return
40+
/// the new string.
41+
std::string replaceSubstring(const std::string &haystack,
42+
const std::string &needle,
43+
const std::string &replacement = "");
44+
3945
/// Prefix an incoming multi-line string with a single-line comment string line
4046
/// by line.
4147
std::string createCommentFromString(const std::string &input);

lib/TableGen/Common.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222

2323
#include "llvm/Support/CommandLine.h"
2424

25+
#include <regex>
26+
#include <string>
27+
2528
using namespace llvm_dialects;
2629
using namespace llvm;
2730

@@ -36,8 +39,14 @@ void llvm_dialects::emitHeader(raw_ostream& out) {
3639

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

42+
std::string llvm_dialects::replaceSubstring(const std::string &haystack,
43+
const std::string &needle,
44+
const std::string &replacement) {
45+
return std::regex_replace(haystack, std::regex(needle), replacement);
46+
}
47+
3948
std::string llvm_dialects::createCommentFromString(const std::string &input) {
40-
StringRef inRef{input};
49+
const StringRef inRef{input};
4150
if (inRef.trim().empty())
4251
return input;
4352

lib/TableGen/GenDialect.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "llvm-dialects/TableGen/SymbolTable.h"
2727
#include "llvm-dialects/TableGen/Traits.h"
2828

29+
#include "llvm/ADT/STLExtras.h"
2930
#include "llvm/ADT/StringExtras.h"
3031
#include "llvm/Support/CommandLine.h"
3132
#include "llvm/Support/FormatVariadic.h"
@@ -180,6 +181,15 @@ class Builder;
180181
if (!op.description.empty())
181182
description += createCommentFromString(op.description);
182183

184+
if (op.getNumFullArguments() > 0) {
185+
description += "/// Arguments\n";
186+
for (const auto &[idx, arg] : llvm::enumerate(op.getFullArguments())) {
187+
const std::string sanitizedName =
188+
replaceSubstring(arg.type->getBuilderCppType().str(), "::llvm::");
189+
description += "/// * " + sanitizedName + " " + arg.name + "\n";
190+
}
191+
}
192+
183193
out << tgfmt(R"(
184194
$2
185195
class $_op : public $0 {

test/example/generated/ExampleDialect.h.inc

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ Initial = 2,
117117
/// For those times when you want a little extra, this operation adds two
118118
/// numbers and puts a constant on top.
119119
///
120+
/// Arguments
121+
/// * Value * lhs
122+
/// * Value * rhs
123+
/// * uint32_t extra
120124

121125
class Add32Op : public ::llvm::CallInst {
122126
static const ::llvm::StringLiteral s_name; //{"xd.ir.add32"};
@@ -154,6 +158,9 @@ Extra = 2,
154158
///
155159
/// Longer description of... well, you know by now how this goes.
156160
///
161+
/// Arguments
162+
/// * Value * lhs
163+
/// * Value * rhs
157164

158165
class CombineOp : public ::llvm::CallInst {
159166
static const ::llvm::StringLiteral s_name; //{"xd.ir.combine"};
@@ -188,6 +195,9 @@ Rhs = 1,
188195
///
189196
/// Return the element of `vector` with the given `index`.
190197
///
198+
/// Arguments
199+
/// * Value * vector
200+
/// * Value * index
191201

192202
class ExtractElementOp : public ::llvm::CallInst {
193203
static const ::llvm::StringLiteral s_name; //{"xd.ir.extractelement"};
@@ -222,6 +232,8 @@ Index = 1,
222232
///
223233
/// Demonstrate a more complex unification case.
224234
///
235+
/// Arguments
236+
/// * Value * source
225237

226238
class FromFixedVectorOp : public ::llvm::CallInst {
227239
static const ::llvm::StringLiteral s_name; //{"xd.ir.fromfixedvector"};
@@ -281,6 +293,8 @@ bool verifier(::llvm::raw_ostream &errs);
281293
/// Demonstrates the use of the same unevaluatable `le` predicate in a valued
282294
/// position.
283295
///
296+
/// Arguments
297+
/// * Value * source
284298

285299
class IExtOp : public ::llvm::CallInst {
286300
static const ::llvm::StringLiteral s_name; //{"xd.ir.iext"};
@@ -312,6 +326,8 @@ Source = 0,
312326
///
313327
/// Demonstrates the use of a predicate in an unvalued position.
314328
///
329+
/// Arguments
330+
/// * Value * source
315331

316332
class ITruncOp : public ::llvm::CallInst {
317333
static const ::llvm::StringLiteral s_name; //{"xd.ir.itrunc"};
@@ -343,6 +359,8 @@ Source = 0,
343359
///
344360
/// Make an argument immutable
345361
///
362+
/// Arguments
363+
/// * bool val
346364

347365
class ImmutableOp : public ::llvm::CallInst {
348366
static const ::llvm::StringLiteral s_name; //{"xd.ir.immutable.op"};
@@ -372,6 +390,10 @@ Val = 0,
372390
/// Insert the given `value` into the given `vector` at the given `index` and
373391
/// returns the result.
374392
///
393+
/// Arguments
394+
/// * Value * vector
395+
/// * Value * value
396+
/// * Value * index
375397

376398
class InsertElementOp : public ::llvm::CallInst {
377399
static const ::llvm::StringLiteral s_name; //{"xd.ir.insertelement"};
@@ -410,6 +432,9 @@ Index = 2,
410432
/// Like InstNameConflictOp but this has a second parameter named like the
411433
/// dialect compiler's first choice
412434
///
435+
/// Arguments
436+
/// * Value * instName
437+
/// * Value * instName_0
413438

414439
class InstNameConflictDoubleOp : public ::llvm::CallInst {
415440
static const ::llvm::StringLiteral s_name; //{"xd.ir.inst.name.conflict.double"};
@@ -446,6 +471,8 @@ InstName_0 = 1,
446471
/// value like IRBuilder methods. This op produces a conflict so the parameter
447472
/// will be renamed.
448473
///
474+
/// Arguments
475+
/// * Value * instName
449476

450477
class InstNameConflictOp : public ::llvm::CallInst {
451478
static const ::llvm::StringLiteral s_name; //{"xd.ir.inst.name.conflict"};
@@ -612,6 +639,8 @@ bool verifier(::llvm::raw_ostream &errs);
612639
///
613640
/// Longer description of how this operation writes pieces of data.
614641
///
642+
/// Arguments
643+
/// * Value * data
615644

616645
class SetWriteOp : public ::llvm::CallInst {
617646
static const ::llvm::StringLiteral s_name; //{"xd.ir.set.write"};
@@ -642,6 +671,8 @@ Data = 0,
642671
///
643672
/// Returns the store size of the given type in bytes.
644673
///
674+
/// Arguments
675+
/// * Type * sizeof_type
645676

646677
class SizeOfOp : public ::llvm::CallInst {
647678
static const ::llvm::StringLiteral s_name; //{"xd.ir.sizeof"};
@@ -673,6 +704,10 @@ SizeofType = 0,
673704
///
674705
/// Illustrate the use of the OpClass feature.
675706
///
707+
/// Arguments
708+
/// * Value * ptr
709+
/// * Value * count
710+
/// * Value * initial
676711

677712
class StreamAddOp : public StreamReduceOp {
678713
static const ::llvm::StringLiteral s_name; //{"xd.ir.stream.add"};
@@ -700,6 +735,10 @@ bool verifier(::llvm::raw_ostream &errs);
700735
///
701736
/// Illustrate the use of the OpClass feature.
702737
///
738+
/// Arguments
739+
/// * Value * ptr
740+
/// * Value * count
741+
/// * Value * initial
703742

704743
class StreamMaxOp : public StreamReduceOp {
705744
static const ::llvm::StringLiteral s_name; //{"xd.ir.stream.max"};
@@ -727,6 +766,10 @@ bool verifier(::llvm::raw_ostream &errs);
727766
///
728767
/// Illustrate the use of the OpClass feature.
729768
///
769+
/// Arguments
770+
/// * Value * ptr
771+
/// * Value * count
772+
/// * Value * initial
730773

731774
class StreamMinOp : public StreamReduceOp {
732775
static const ::llvm::StringLiteral s_name; //{"xd.ir.stream.min"};
@@ -754,6 +797,8 @@ bool verifier(::llvm::raw_ostream &errs);
754797
///
755798
/// The argument should not have a setter method
756799
///
800+
/// Arguments
801+
/// * StringRef val
757802

758803
class StringAttrOp : public ::llvm::CallInst {
759804
static const ::llvm::StringLiteral s_name; //{"xd.ir.string.attr.op"};
@@ -782,6 +827,8 @@ Val = 0,
782827
///
783828
/// Longer description of how this operation writes a piece of data.
784829
///
830+
/// Arguments
831+
/// * Value * data
785832

786833
class WriteOp : public ::llvm::CallInst {
787834
static const ::llvm::StringLiteral s_name; //{"xd.ir.write"};
@@ -812,6 +859,9 @@ Data = 0,
812859
///
813860
/// Longer description of how this operation writes pieces of data.
814861
///
862+
/// Arguments
863+
/// * Value * data
864+
/// * ArrayRef<Value *> args
815865

816866
class WriteVarArgOp : public ::llvm::CallInst {
817867
static const ::llvm::StringLiteral s_name; //{"xd.ir.write.vararg"};

0 commit comments

Comments
 (0)