Skip to content

Commit b7ee542

Browse files
authored
Add argument index enum per op (GPUOpen-Drivers#113)
Add an enum that helps identifying specific argument slots by providing access to the indices used in `getArgOperand`.
1 parent 50e4ca3 commit b7ee542

File tree

2 files changed

+81
-18
lines changed

2 files changed

+81
-18
lines changed

lib/TableGen/Operations.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,20 @@ unsigned OperationBase::getNumFullArguments() const {
159159

160160
void OperationBase::emitArgumentAccessorDeclarations(llvm::raw_ostream &out,
161161
FmtContext &fmt) const {
162+
llvm::SmallVector<std::string> argNames;
163+
164+
unsigned numSuperclassArgs = 0;
165+
if (m_superclass)
166+
numSuperclassArgs = m_superclass->getNumFullArguments();
167+
162168
for (const auto &arg : m_arguments) {
169+
const std::string capitalizedArgName =
170+
convertToCamelFromSnakeCase(arg.name, true);
171+
163172
const bool isVarArg = arg.type->isVarArgList();
173+
174+
argNames.push_back(capitalizedArgName + (isVarArg ? "Start" : ""));
175+
164176
std::string defaultDeclaration = "$0 get$1() $2;";
165177

166178
if (!arg.type->isImmutable()) {
@@ -178,7 +190,14 @@ void OperationBase::emitArgumentAccessorDeclarations(llvm::raw_ostream &out,
178190
}
179191

180192
out << tgfmt(defaultDeclaration, &fmt, arg.type->getGetterCppType(),
181-
convertToCamelFromSnakeCase(arg.name, true), !isVarArg ? "const" : "", arg.name);
193+
capitalizedArgName, !isVarArg ? "const" : "", arg.name);
194+
}
195+
196+
if (!argNames.empty()) {
197+
out << "enum class ArgumentIndex: uint32_t {\n";
198+
for (const auto &[index, argName] : llvm::enumerate(argNames))
199+
out << tgfmt("$0 = $1,\n", &fmt, argName, numSuperclassArgs + index);
200+
out << "};";
182201
}
183202
}
184203

test/example/generated/ExampleDialect.h.inc

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@ uint32_t getNumElements() const;
104104
void setCount(::llvm::Value * count);
105105
::llvm::Value * getInitial() const;
106106
void setInitial(::llvm::Value * initial);
107-
107+
enum class ArgumentIndex: uint32_t {
108+
Ptr = 0,
109+
Count = 1,
110+
Initial = 2,
111+
};
108112
};
109113

110114
class Add32Op : public ::llvm::CallInst {
@@ -128,7 +132,11 @@ bool verifier(::llvm::raw_ostream &errs);
128132
void setRhs(::llvm::Value * rhs);
129133
uint32_t getExtra() const;
130134
void setExtra(uint32_t extra);
131-
135+
enum class ArgumentIndex: uint32_t {
136+
Lhs = 0,
137+
Rhs = 1,
138+
Extra = 2,
139+
};
132140
::llvm::Value * getResult();
133141

134142

@@ -153,7 +161,10 @@ bool verifier(::llvm::raw_ostream &errs);
153161
void setLhs(::llvm::Value * lhs);
154162
::llvm::Value * getRhs() const;
155163
void setRhs(::llvm::Value * rhs);
156-
164+
enum class ArgumentIndex: uint32_t {
165+
Lhs = 0,
166+
Rhs = 1,
167+
};
157168
::llvm::Value * getResult();
158169

159170

@@ -178,7 +189,10 @@ bool verifier(::llvm::raw_ostream &errs);
178189
void setVector(::llvm::Value * vector);
179190
::llvm::Value * getIndex() const;
180191
void setIndex(::llvm::Value * index);
181-
192+
enum class ArgumentIndex: uint32_t {
193+
Vector = 0,
194+
Index = 1,
195+
};
182196
::llvm::Value * getResult();
183197

184198

@@ -201,7 +215,9 @@ bool verifier(::llvm::raw_ostream &errs);
201215

202216
::llvm::Value * getSource() const;
203217
void setSource(::llvm::Value * source);
204-
218+
enum class ArgumentIndex: uint32_t {
219+
Source = 0,
220+
};
205221
::llvm::Value * getResult();
206222

207223

@@ -245,7 +261,9 @@ bool verifier(::llvm::raw_ostream &errs);
245261

246262
::llvm::Value * getSource() const;
247263
void setSource(::llvm::Value * source);
248-
264+
enum class ArgumentIndex: uint32_t {
265+
Source = 0,
266+
};
249267
::llvm::Value * getResult();
250268

251269

@@ -268,7 +286,9 @@ bool verifier(::llvm::raw_ostream &errs);
268286

269287
::llvm::Value * getSource() const;
270288
void setSource(::llvm::Value * source);
271-
289+
enum class ArgumentIndex: uint32_t {
290+
Source = 0,
291+
};
272292
::llvm::Value * getResult();
273293

274294

@@ -289,7 +309,9 @@ bool verifier(::llvm::raw_ostream &errs);
289309

290310
bool verifier(::llvm::raw_ostream &errs);
291311

292-
bool getVal() const;
312+
bool getVal() const;enum class ArgumentIndex: uint32_t {
313+
Val = 0,
314+
};
293315

294316

295317
};
@@ -315,7 +337,11 @@ bool verifier(::llvm::raw_ostream &errs);
315337
void setValue(::llvm::Value * value);
316338
::llvm::Value * getIndex() const;
317339
void setIndex(::llvm::Value * index);
318-
340+
enum class ArgumentIndex: uint32_t {
341+
Vector = 0,
342+
Value = 1,
343+
Index = 2,
344+
};
319345
::llvm::Value * getResult();
320346

321347

@@ -340,7 +366,10 @@ bool verifier(::llvm::raw_ostream &errs);
340366
void setInstName(::llvm::Value * instName);
341367
::llvm::Value * getInstName_0() const;
342368
void setInstName_0(::llvm::Value * instName_0);
343-
369+
enum class ArgumentIndex: uint32_t {
370+
InstName = 0,
371+
InstName_0 = 1,
372+
};
344373
::llvm::Value * getResult();
345374

346375

@@ -363,7 +392,9 @@ bool verifier(::llvm::raw_ostream &errs);
363392

364393
::llvm::Value * getInstName() const;
365394
void setInstName(::llvm::Value * instName);
366-
395+
enum class ArgumentIndex: uint32_t {
396+
InstName = 0,
397+
};
367398
::llvm::Value * getResult();
368399

369400

@@ -388,7 +419,9 @@ bool verifier(::llvm::raw_ostream &errs);
388419
/// Returns a new op with the same arguments and a new tail argument list.
389420
/// The object on which this is called will be replaced and erased.
390421
InstNameConflictVarargsOp *replaceInstName_0(::llvm::ArrayRef<Value *>);
391-
422+
enum class ArgumentIndex: uint32_t {
423+
InstName_0Start = 0,
424+
};
392425
::llvm::Value * getResult();
393426

394427

@@ -453,7 +486,9 @@ bool verifier(::llvm::raw_ostream &errs);
453486

454487
::llvm::Value * getData() const;
455488
void setData(::llvm::Value * data);
456-
489+
enum class ArgumentIndex: uint32_t {
490+
Data = 0,
491+
};
457492

458493

459494
};
@@ -475,7 +510,9 @@ bool verifier(::llvm::raw_ostream &errs);
475510

476511
::llvm::Type * getSizeofType() const;
477512
void setSizeofType(::llvm::Type * sizeof_type);
478-
513+
enum class ArgumentIndex: uint32_t {
514+
SizeofType = 0,
515+
};
479516
::llvm::Value * getResult();
480517

481518

@@ -559,7 +596,9 @@ bool verifier(::llvm::raw_ostream &errs);
559596

560597
bool verifier(::llvm::raw_ostream &errs);
561598

562-
::llvm::StringRef getVal() const;
599+
::llvm::StringRef getVal() const;enum class ArgumentIndex: uint32_t {
600+
Val = 0,
601+
};
563602

564603

565604
};
@@ -581,7 +620,9 @@ bool verifier(::llvm::raw_ostream &errs);
581620

582621
::llvm::Value * getData() const;
583622
void setData(::llvm::Value * data);
584-
623+
enum class ArgumentIndex: uint32_t {
624+
Data = 0,
625+
};
585626

586627

587628
};
@@ -607,7 +648,10 @@ bool verifier(::llvm::raw_ostream &errs);
607648
/// Returns a new op with the same arguments and a new tail argument list.
608649
/// The object on which this is called will be replaced and erased.
609650
WriteVarArgOp *replaceArgs(::llvm::ArrayRef<Value *>);
610-
651+
enum class ArgumentIndex: uint32_t {
652+
Data = 0,
653+
ArgsStart = 1,
654+
};
611655

612656

613657
};

0 commit comments

Comments
 (0)