From d838ec754b9599743f209ab492d01cf13b0b20a8 Mon Sep 17 00:00:00 2001 From: Thomas Symalla Date: Fri, 22 Nov 2024 10:54:28 +0100 Subject: [PATCH 1/4] Add argument index enum per op Add an enum that helps identifying specific argument slots by providing access to the indices used in `getArgOperand`. --- lib/TableGen/Operations.cpp | 24 ++++++- test/example/generated/ExampleDialect.h.inc | 73 ++++++++++++++++----- 2 files changed, 79 insertions(+), 18 deletions(-) diff --git a/lib/TableGen/Operations.cpp b/lib/TableGen/Operations.cpp index dcfd06b..f87a3af 100644 --- a/lib/TableGen/Operations.cpp +++ b/lib/TableGen/Operations.cpp @@ -21,6 +21,7 @@ #include "llvm-dialects/TableGen/Format.h" #include "llvm/TableGen/Record.h" +#include using namespace llvm; using namespace llvm_dialects; @@ -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 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()) { @@ -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 << "};"; } } diff --git a/test/example/generated/ExampleDialect.h.inc b/test/example/generated/ExampleDialect.h.inc index 0e3e59b..62ab6f3 100644 --- a/test/example/generated/ExampleDialect.h.inc +++ b/test/example/generated/ExampleDialect.h.inc @@ -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 { @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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, +}; }; @@ -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(); @@ -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(); @@ -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(); @@ -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, +}; }; @@ -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(); @@ -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, +}; }; @@ -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, +}; }; @@ -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); - + enum class ArgumentIndex: uint32_t { +Data = 0, +}; }; From deaefdbbdf59782bd1e44bcdc3c67c466fc6c9c0 Mon Sep 17 00:00:00 2001 From: Thomas Symalla Date: Tue, 3 Dec 2024 08:53:03 +0100 Subject: [PATCH 2/4] Use a simple `SmallVector` --- lib/TableGen/Operations.cpp | 12 ++++++------ test/example/generated/ExampleDialect.h.inc | 10 +++++----- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/TableGen/Operations.cpp b/lib/TableGen/Operations.cpp index f87a3af..157a9da 100644 --- a/lib/TableGen/Operations.cpp +++ b/lib/TableGen/Operations.cpp @@ -21,7 +21,6 @@ #include "llvm-dialects/TableGen/Format.h" #include "llvm/TableGen/Record.h" -#include using namespace llvm; using namespace llvm_dialects; @@ -160,7 +159,8 @@ unsigned OperationBase::getNumFullArguments() const { void OperationBase::emitArgumentAccessorDeclarations(llvm::raw_ostream &out, FmtContext &fmt) const { - std::map argIndexMap; + llvm::SmallVector argNames; + unsigned numSuperclassArgs = 0; if (m_superclass) numSuperclassArgs = m_superclass->getNumFullArguments(); @@ -172,7 +172,7 @@ void OperationBase::emitArgumentAccessorDeclarations(llvm::raw_ostream &out, const bool isVarArg = arg.type->isVarArgList(); if (!isVarArg) - argIndexMap[capitalizedArgName] = numSuperclassArgs + index; + argNames.push_back(capitalizedArgName); std::string defaultDeclaration = "$0 get$1() $2;"; @@ -194,10 +194,10 @@ void OperationBase::emitArgumentAccessorDeclarations(llvm::raw_ostream &out, capitalizedArgName, !isVarArg ? "const" : "", arg.name); } - if (!argIndexMap.empty()) { + if (!argNames.empty()) { out << "enum class ArgumentIndex: uint32_t {\n"; - for (auto &[argName, index] : argIndexMap) - out << tgfmt("$0 = $1,\n", &fmt, argName, index); + for (const auto &[index, argName] : llvm::enumerate(argNames)) + out << tgfmt("$0 = $1,\n", &fmt, argName, numSuperclassArgs + index); out << "};"; } } diff --git a/test/example/generated/ExampleDialect.h.inc b/test/example/generated/ExampleDialect.h.inc index 62ab6f3..e15512c 100644 --- a/test/example/generated/ExampleDialect.h.inc +++ b/test/example/generated/ExampleDialect.h.inc @@ -105,9 +105,9 @@ uint32_t getNumElements() const; ::llvm::Value * getInitial() const; void setInitial(::llvm::Value * initial); enum class ArgumentIndex: uint32_t { +Ptr = 0, Count = 1, Initial = 2, -Ptr = 0, }; }; @@ -133,9 +133,9 @@ bool verifier(::llvm::raw_ostream &errs); uint32_t getExtra() const; void setExtra(uint32_t extra); enum class ArgumentIndex: uint32_t { -Extra = 2, Lhs = 0, Rhs = 1, +Extra = 2, }; ::llvm::Value * getResult(); @@ -190,8 +190,8 @@ bool verifier(::llvm::raw_ostream &errs); ::llvm::Value * getIndex() const; void setIndex(::llvm::Value * index); enum class ArgumentIndex: uint32_t { -Index = 1, Vector = 0, +Index = 1, }; ::llvm::Value * getResult(); @@ -338,9 +338,9 @@ bool verifier(::llvm::raw_ostream &errs); ::llvm::Value * getIndex() const; void setIndex(::llvm::Value * index); enum class ArgumentIndex: uint32_t { -Index = 2, -Value = 1, Vector = 0, +Value = 1, +Index = 2, }; ::llvm::Value * getResult(); From 23b2983fe209baa43000d0f627704a3b94bb774f Mon Sep 17 00:00:00 2001 From: Thomas Symalla Date: Tue, 3 Dec 2024 11:45:52 +0100 Subject: [PATCH 3/4] Add VarArgs index --- lib/TableGen/Operations.cpp | 3 +-- test/example/generated/ExampleDialect.h.inc | 5 ++++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/TableGen/Operations.cpp b/lib/TableGen/Operations.cpp index 157a9da..acfc92c 100644 --- a/lib/TableGen/Operations.cpp +++ b/lib/TableGen/Operations.cpp @@ -171,8 +171,7 @@ void OperationBase::emitArgumentAccessorDeclarations(llvm::raw_ostream &out, const bool isVarArg = arg.type->isVarArgList(); - if (!isVarArg) - argNames.push_back(capitalizedArgName); + argNames.push_back(capitalizedArgName + (isVarArg ? "Start" : "")); std::string defaultDeclaration = "$0 get$1() $2;"; diff --git a/test/example/generated/ExampleDialect.h.inc b/test/example/generated/ExampleDialect.h.inc index e15512c..9e5794f 100644 --- a/test/example/generated/ExampleDialect.h.inc +++ b/test/example/generated/ExampleDialect.h.inc @@ -419,7 +419,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. InstNameConflictVarargsOp *replaceInstName_0(::llvm::ArrayRef); - + enum class ArgumentIndex: uint32_t { +InstName_0Start = 0, +}; ::llvm::Value * getResult(); @@ -648,6 +650,7 @@ bool verifier(::llvm::raw_ostream &errs); WriteVarArgOp *replaceArgs(::llvm::ArrayRef); enum class ArgumentIndex: uint32_t { Data = 0, +ArgsStart = 1, }; From 1d3111678c1deaed979c4c9b1eeeb1a2ece96d8b Mon Sep 17 00:00:00 2001 From: Thomas Symalla Date: Tue, 3 Dec 2024 13:56:04 +0100 Subject: [PATCH 4/4] Remove `llvm::enumerate` call --- lib/TableGen/Operations.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/TableGen/Operations.cpp b/lib/TableGen/Operations.cpp index acfc92c..d10f3b7 100644 --- a/lib/TableGen/Operations.cpp +++ b/lib/TableGen/Operations.cpp @@ -165,7 +165,7 @@ void OperationBase::emitArgumentAccessorDeclarations(llvm::raw_ostream &out, if (m_superclass) numSuperclassArgs = m_superclass->getNumFullArguments(); - for (const auto &[index, arg] : llvm::enumerate(m_arguments)) { + for (const auto &arg : m_arguments) { const std::string capitalizedArgName = convertToCamelFromSnakeCase(arg.name, true);