Skip to content

Commit d838ec7

Browse files
author
Thomas Symalla
committed
Add argument index enum per op
Add an enum that helps identifying specific argument slots by providing access to the indices used in `getArgOperand`.
1 parent 50e4ca3 commit d838ec7

File tree

2 files changed

+79
-18
lines changed

2 files changed

+79
-18
lines changed

lib/TableGen/Operations.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "llvm-dialects/TableGen/Format.h"
2222

2323
#include "llvm/TableGen/Record.h"
24+
#include <map>
2425

2526
using namespace llvm;
2627
using namespace llvm_dialects;
@@ -159,8 +160,20 @@ unsigned OperationBase::getNumFullArguments() const {
159160

160161
void OperationBase::emitArgumentAccessorDeclarations(llvm::raw_ostream &out,
161162
FmtContext &fmt) const {
162-
for (const auto &arg : m_arguments) {
163+
std::map<std::string, uint32_t> argIndexMap;
164+
unsigned numSuperclassArgs = 0;
165+
if (m_superclass)
166+
numSuperclassArgs = m_superclass->getNumFullArguments();
167+
168+
for (const auto &[index, arg] : llvm::enumerate(m_arguments)) {
169+
const std::string capitalizedArgName =
170+
convertToCamelFromSnakeCase(arg.name, true);
171+
163172
const bool isVarArg = arg.type->isVarArgList();
173+
174+
if (!isVarArg)
175+
argIndexMap[capitalizedArgName] = numSuperclassArgs + index;
176+
164177
std::string defaultDeclaration = "$0 get$1() $2;";
165178

166179
if (!arg.type->isImmutable()) {
@@ -178,7 +191,14 @@ void OperationBase::emitArgumentAccessorDeclarations(llvm::raw_ostream &out,
178191
}
179192

180193
out << tgfmt(defaultDeclaration, &fmt, arg.type->getGetterCppType(),
181-
convertToCamelFromSnakeCase(arg.name, true), !isVarArg ? "const" : "", arg.name);
194+
capitalizedArgName, !isVarArg ? "const" : "", arg.name);
195+
}
196+
197+
if (!argIndexMap.empty()) {
198+
out << "enum class ArgumentIndex: uint32_t {\n";
199+
for (auto &[argName, index] : argIndexMap)
200+
out << tgfmt("$0 = $1,\n", &fmt, argName, index);
201+
out << "};";
182202
}
183203
}
184204

test/example/generated/ExampleDialect.h.inc

Lines changed: 57 additions & 16 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+
Count = 1,
109+
Initial = 2,
110+
Ptr = 0,
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+
Extra = 2,
137+
Lhs = 0,
138+
Rhs = 1,
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+
Index = 1,
194+
Vector = 0,
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+
Index = 2,
342+
Value = 1,
343+
Vector = 0,
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

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

454485
::llvm::Value * getData() const;
455486
void setData(::llvm::Value * data);
456-
487+
enum class ArgumentIndex: uint32_t {
488+
Data = 0,
489+
};
457490

458491

459492
};
@@ -475,7 +508,9 @@ bool verifier(::llvm::raw_ostream &errs);
475508

476509
::llvm::Type * getSizeofType() const;
477510
void setSizeofType(::llvm::Type * sizeof_type);
478-
511+
enum class ArgumentIndex: uint32_t {
512+
SizeofType = 0,
513+
};
479514
::llvm::Value * getResult();
480515

481516

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

560595
bool verifier(::llvm::raw_ostream &errs);
561596

562-
::llvm::StringRef getVal() const;
597+
::llvm::StringRef getVal() const;enum class ArgumentIndex: uint32_t {
598+
Val = 0,
599+
};
563600

564601

565602
};
@@ -581,7 +618,9 @@ bool verifier(::llvm::raw_ostream &errs);
581618

582619
::llvm::Value * getData() const;
583620
void setData(::llvm::Value * data);
584-
621+
enum class ArgumentIndex: uint32_t {
622+
Data = 0,
623+
};
585624

586625

587626
};
@@ -607,7 +646,9 @@ bool verifier(::llvm::raw_ostream &errs);
607646
/// Returns a new op with the same arguments and a new tail argument list.
608647
/// The object on which this is called will be replaced and erased.
609648
WriteVarArgOp *replaceArgs(::llvm::ArrayRef<Value *>);
610-
649+
enum class ArgumentIndex: uint32_t {
650+
Data = 0,
651+
};
611652

612653

613654
};

0 commit comments

Comments
 (0)