Skip to content

Commit

Permalink
Add argument index enum per op
Browse files Browse the repository at this point in the history
Add an enum that helps identifying specific argument slots by providing
access to the indices used in `getArgOperand`.
  • Loading branch information
Thomas Symalla committed Nov 22, 2024
1 parent 50e4ca3 commit d838ec7
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 18 deletions.
24 changes: 22 additions & 2 deletions lib/TableGen/Operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "llvm-dialects/TableGen/Format.h"

#include "llvm/TableGen/Record.h"
#include <map>

using namespace llvm;
using namespace llvm_dialects;
Expand Down Expand Up @@ -159,8 +160,20 @@ unsigned OperationBase::getNumFullArguments() const {

void OperationBase::emitArgumentAccessorDeclarations(llvm::raw_ostream &out,
FmtContext &fmt) const {
for (const auto &arg : m_arguments) {
std::map<std::string, uint32_t> argIndexMap;
unsigned numSuperclassArgs = 0;
if (m_superclass)
numSuperclassArgs = m_superclass->getNumFullArguments();

for (const auto &[index, arg] : llvm::enumerate(m_arguments)) {
const std::string capitalizedArgName =
convertToCamelFromSnakeCase(arg.name, true);

const bool isVarArg = arg.type->isVarArgList();

if (!isVarArg)
argIndexMap[capitalizedArgName] = numSuperclassArgs + index;

std::string defaultDeclaration = "$0 get$1() $2;";

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

out << tgfmt(defaultDeclaration, &fmt, arg.type->getGetterCppType(),
convertToCamelFromSnakeCase(arg.name, true), !isVarArg ? "const" : "", arg.name);
capitalizedArgName, !isVarArg ? "const" : "", arg.name);
}

if (!argIndexMap.empty()) {
out << "enum class ArgumentIndex: uint32_t {\n";
for (auto &[argName, index] : argIndexMap)
out << tgfmt("$0 = $1,\n", &fmt, argName, index);
out << "};";
}
}

Expand Down
73 changes: 57 additions & 16 deletions test/example/generated/ExampleDialect.h.inc
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ uint32_t getNumElements() const;
void setCount(::llvm::Value * count);
::llvm::Value * getInitial() const;
void setInitial(::llvm::Value * initial);

enum class ArgumentIndex: uint32_t {
Count = 1,
Initial = 2,
Ptr = 0,
};
};

class Add32Op : public ::llvm::CallInst {
Expand All @@ -128,7 +132,11 @@ bool verifier(::llvm::raw_ostream &errs);
void setRhs(::llvm::Value * rhs);
uint32_t getExtra() const;
void setExtra(uint32_t extra);

enum class ArgumentIndex: uint32_t {
Extra = 2,
Lhs = 0,
Rhs = 1,
};
::llvm::Value * getResult();


Expand All @@ -153,7 +161,10 @@ bool verifier(::llvm::raw_ostream &errs);
void setLhs(::llvm::Value * lhs);
::llvm::Value * getRhs() const;
void setRhs(::llvm::Value * rhs);

enum class ArgumentIndex: uint32_t {
Lhs = 0,
Rhs = 1,
};
::llvm::Value * getResult();


Expand All @@ -178,7 +189,10 @@ bool verifier(::llvm::raw_ostream &errs);
void setVector(::llvm::Value * vector);
::llvm::Value * getIndex() const;
void setIndex(::llvm::Value * index);

enum class ArgumentIndex: uint32_t {
Index = 1,
Vector = 0,
};
::llvm::Value * getResult();


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

::llvm::Value * getSource() const;
void setSource(::llvm::Value * source);

enum class ArgumentIndex: uint32_t {
Source = 0,
};
::llvm::Value * getResult();


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

::llvm::Value * getSource() const;
void setSource(::llvm::Value * source);

enum class ArgumentIndex: uint32_t {
Source = 0,
};
::llvm::Value * getResult();


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

::llvm::Value * getSource() const;
void setSource(::llvm::Value * source);

enum class ArgumentIndex: uint32_t {
Source = 0,
};
::llvm::Value * getResult();


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

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

bool getVal() const;
bool getVal() const;enum class ArgumentIndex: uint32_t {
Val = 0,
};


};
Expand All @@ -315,7 +337,11 @@ bool verifier(::llvm::raw_ostream &errs);
void setValue(::llvm::Value * value);
::llvm::Value * getIndex() const;
void setIndex(::llvm::Value * index);

enum class ArgumentIndex: uint32_t {
Index = 2,
Value = 1,
Vector = 0,
};
::llvm::Value * getResult();


Expand All @@ -340,7 +366,10 @@ bool verifier(::llvm::raw_ostream &errs);
void setInstName(::llvm::Value * instName);
::llvm::Value * getInstName_0() const;
void setInstName_0(::llvm::Value * instName_0);

enum class ArgumentIndex: uint32_t {
InstName = 0,
InstName_0 = 1,
};
::llvm::Value * getResult();


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

::llvm::Value * getInstName() const;
void setInstName(::llvm::Value * instName);

enum class ArgumentIndex: uint32_t {
InstName = 0,
};
::llvm::Value * getResult();


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

::llvm::Value * getData() const;
void setData(::llvm::Value * data);

enum class ArgumentIndex: uint32_t {
Data = 0,
};


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

::llvm::Type * getSizeofType() const;
void setSizeofType(::llvm::Type * sizeof_type);

enum class ArgumentIndex: uint32_t {
SizeofType = 0,
};
::llvm::Value * getResult();


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

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

::llvm::StringRef getVal() const;
::llvm::StringRef getVal() const;enum class ArgumentIndex: uint32_t {
Val = 0,
};


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

::llvm::Value * getData() const;
void setData(::llvm::Value * data);

enum class ArgumentIndex: uint32_t {
Data = 0,
};


};
Expand All @@ -607,7 +646,9 @@ bool verifier(::llvm::raw_ostream &errs);
/// Returns a new op with the same arguments and a new tail argument list.
/// The object on which this is called will be replaced and erased.
WriteVarArgOp *replaceArgs(::llvm::ArrayRef<Value *>);

enum class ArgumentIndex: uint32_t {
Data = 0,
};


};
Expand Down

0 comments on commit d838ec7

Please sign in to comment.